|
Here's the Java solution to this example. I've only included the code that has changed since the last exercise.
Notes:
1) Creating a selector is different in Java.
2) NSApp variable doesn't exist but it's easy enough to derive...
-----
public void deleteRecord(Object sender) { /* IBAction */
String title = "Warning";
String defaultButton = "Delete";
String alternateButton = "Don't Delete";
String otherButton = null;
String message = "Are you sure you want to delete the selected record(s)?";
// here's how to make a selector in Java
NSSelector sel = new NSSelector("sheetDidEnd", new Class[] {NSWindow.class, int.class, Object.class} );
if (tableView.numberOfSelectedRows() == 0)
return;
NSApplication.beep();
NSAlertPanel.beginAlertSheet(title, defaultButton , alternateButton, otherButton, mainWindow, this, sel, null, null, message);
}
public void sheetDidEnd(NSWindow sheet, int returnCode, Object contextInfo)
{
if ( returnCode == 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 openPrefsSheet(Object sender)
{
// NSApp variable doesn't appear to exist in Java. We can get it easily though
NSApplication nsapp=NSApplication.sharedApplication();
nsapp.beginSheet(prefsWindow, mainWindow, null, null, null);
nsapp.runModalForWindow(prefsWindow);
nsapp.endSheet(prefsWindow);
prefsWindow.orderOut(this);
}
public void closePrefsSheet(Object sender)
{
NSApplication.sharedApplication().stopModal();
}
|
You know, when you look at the .NET documentation, MS provides examples at least in VB.NET and C#, and sometimes J# and Managed C++ as well. Why can't Apple do the same?