advertisement

Article:
  Mac OS X's Preferences System (and More!)
Subject:   Java Version - Solution
Date:   2002-05-26 17:49:04
From:   jsumnertx
I've been following these examples and doing them in Java rather than Objective C. Here is the solution:


Notes:
1) This project came in two parts. The first involved writing the data to a file specified by the user via the initWithContentsOfFile method of NSArray. However, this method doesn't exist in the Java version of NSArray and the documentation didn't say anything about how to do it. I searched around and found some anecdotal information but no actual code. Therefore, I'll show you my final code for the first part since it was a real pain to get working....


Read Prefs:
String recordsFile;

Controller()
{
recordsFile = "~/Library/Preferences/AddressBookData.plist";
recordsFile = NSPathUtilities.stringByExpandingTildeInPath(recordsFile);

try{
java.io.File file = new java.io.File(recordsFile);
NSData data = new NSData(file);
NSArray loadedRecords = (NSArray)NSPropertyListSerialization.propertyListFromData(data);
records = new NSMutableArray(loadedRecords);
}
catch (Exception e)
{
records = new NSMutableArray();
}
}


private void saveData(NSArray records)
{
try
{
java.net.URL fileURL=NSPathUtilities.URLWithPath(recordsFile);
NSData data = NSPropertyListSerialization.dataFromPropertyList(records);
data.writeToURL(fileURL, true);
}
catch (Exception e)
{
// put up a dialog
int status = NSAlertPanel.runAlert("Warning!", "Unable to save address data", "OK", null, null);
}
}



Section 2: Completed program using NSUserDefaults...


/* 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;
NSUserDefaults prefs;

//String recordsFile;

Controller()
{

prefs = NSUserDefaults.standardUserDefaults();
records = new NSMutableArray(prefs.arrayForKey("Address"));
}

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();
saveData(records);
}


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();
saveData(records);
}
}


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

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;
}



private void saveData(NSArray records)
{
prefs.setObjectForKey(records, "Address");
}
}