advertisement

Article:
  Working with Tables: Writing an Address Book Application
Subject:   Java Version - Solution
Date:   2002-05-25 15:03:39
From:   jsumnertx
I've been doing these exercises in Java rather than Objective C.


The code is below. Some notes:


1) Similar to the the Color Meter example, your Controller inherits from java.lang.Object rather than NSObject
2) Enable the debugger using the executables tab and set the debugger to "Java Debugger"
3) The alert dialogs are in a different object heirarchy.
4) The row enumerator returns "java.lang.Integer" objects rather than NSNumber. This, unfortunately, doesn't seem to be documented anywhere in Apple's docs.
5) Some of the methods weren't named exactly the same but they were easy enough to find when perusing the documentation.
6) You can't use "awakeFromNib" to initialize your variables. Set them in your class initialization instead.


Enjoy!
/* Controller */


import com.apple.cocoa.foundation.*;
import com.apple.cocoa.application.*;


public class Controller{


NSTextField emailField; /* IBOutlet */
NSTextField firstNameField; /* IBOutlet */
NSTextField homePhoneField; /* IBOutlet */
NSTextField lastNameField; /* IBOutlet */
NSTableView tableView; /* IBOutlet */
NSMutableArray records = new NSMutableArray();;

public NSDictionary createRecord()
{
NSMutableDictionary record = new NSMutableDictionary();

record.setObjectForKey(firstNameField.stringValue(), "First Name");
record.setObjectForKey(lastNameField.stringValue(), "Last Name");
record.setObjectForKey(emailField.stringValue(), "Email");
record.setObjectForKey(homePhoneField.stringValue(), "Home Phone");

return record;
}


public void addRecord(Object sender) { /* IBAction */
records.addObject(createRecord());
tableView.reloadData();
}


public void deleteRecord(Object sender) { /* IBAction */

if (tableView.numberOfSelectedRows() == 0)
return;
NSApplication.beep();
int status = NSAlertPanel.runAlert("Warning!", "Are you sure you want to delete the selected record(s)?", "OK", "Cancel", null);


if ( status == NSAlertPanel.DefaultReturn ) {
NSMutableArray tempArray = new NSMutableArray();
NSEnumerator enumerator = tableView.selectedRowEnumerator();
Integer item;
item = (Integer)enumerator.nextElement();
while ( item != null ) {
tempArray.addObject(records.objectAtIndex(item.intValue()));
item = (Integer)enumerator.nextElement();
}
records.removeObjectsInArray(tempArray);
tableView.reloadData();
}
}


public void insertRecord(Object sender) { /* IBAction */
int index = tableView.selectedRow();
if (index >= 0)
records.insertObjectAtIndex(createRecord(), index);
else
records.addObject(createRecord());
tableView.reloadData();
}

public int numberOfRowsInTableView(NSTableView aTableView){
return records.count();
}

public Object tableViewObjectValueForLocation( NSTableView aTableView, NSTableColumn aTableColumn, int rowIndex){
NSDictionary theRecord;
Object theValue=null;

theRecord = (NSDictionary)records.objectAtIndex(rowIndex);
if (theRecord!=null)
theValue = theRecord.objectForKey(aTableColumn.identifier());
return theValue;
}
}


1 to 1 of 1
  1. Java Version - Solution? No, trouble
    2003-04-28 09:21:51  anonymous2 [View]

1 to 1 of 1