2005-01-20

Interface Builder bug

Interestingly, I came across a bug in Interface Builder tonight while working on a Cocoa-Java app.

I was working with a NSTableView.DataSource as a delegate object, and in the NSTableView I assigned an NSNumberFormatter to one column, and set it to display negative values as red. After I saved and tested my app I was surprised that negatives were still black! Hm. I repeated several times.

Eventually I gave up and wrote the following code to work around the situation... it isn't pretty... feel free to send me optimizations and replacement code!:


public void awakeFromNib() {
//workaround for bug in interface builder that loses the setting to make the text color red on the
//Margin column for negative values
NSMutableDictionary tmp = new NSMutableDictionary();
tmp.setObjectForKey( NSColor.redColor(), "NSColor");

//argh, and since column identifier isn't saving in interface builder either, must loop through columns
NSTableColumn marginCol = null;
NSArray cols = updatesTable.tableColumns();
for (int i=0; i<cols.count(); i++) {
if ( ((NSCell)((NSTableColumn)cols.objectAtIndex(i)).headerCell()).stringValue().compareTo("Margin") == 0 ) {
marginCol = (NSTableColumn)cols.objectAtIndex(i);
}
}

if ( marginCol != null ) {
NSNumberFormatter tmpFormat = new NSNumberFormatter();
tmpFormat.setFormat("$#,##0.00;-$#,##0.00");
tmpFormat.setTextAttributesForNegativeValues(tmp);
NSCell tmpCell = marginCol.dataCell();
tmpCell.setType(NSCell.TextCellType);
tmpCell.setFormatter(tmpFormat);
}
}


(yes, i look at that code again and it is ugly still... but I can fix/optimize later. I'm more concerned with getting a working app finished)

No comments: