Index: trunk/hiptop/pester/net/sabi/pester/AlarmListView.java
===================================================================
--- trunk/hiptop/pester/net/sabi/pester/AlarmListView.java	(revision 249)
+++ trunk/hiptop/pester/net/sabi/pester/AlarmListView.java	(revision 250)
@@ -3,78 +3,24 @@
 import danger.app.Application;
 import danger.app.Event;
-import danger.audio.ToneClass;
-import danger.audio.ToneRights;
-
-import danger.internal.Date;
 import danger.ui.ActiveListView;
-import danger.ui.Button;
-import danger.ui.CheckBox;
-import danger.ui.DateTimeEditor;
-import danger.ui.DatePicker;
-import danger.ui.DialogWindow;
-import danger.ui.MenuItem;
-import danger.ui.NumberEditor;
-import danger.ui.PopupMenu;
-import danger.ui.RadioButton;
-import danger.ui.Rect;
-import danger.ui.RingTonePicker;
-import danger.ui.TypeAheadTextField;
 import danger.util.ActiveList;
-import danger.util.StdActiveList;
-import danger.util.DEBUG;
 
 public class AlarmListView extends ActiveListView
     implements Resources, Commands {
-    private static Pester sPester;
+
+    // XXX some of these shouldn't conceptually be static
     private static AlarmListWindow sAlarmListWindow;
-    // XXX some of these shouldn't conceptually be static, but who cares
-    private static StdActiveList sAlarmList;
-    private static Alarms sAlarms;
+    private static AlarmSetDialog sAlarmSetDialog;
 
-    private static DialogWindow sEditWindow;
-    private TypeAheadTextField sMessageField;
-    private RadioButton sInButton, sAtButton;
-    private NumberEditor sPeriodField;
-    private PopupMenu sPeriodUnitsPopup;
-    // XXX don't allow <5 second repeating alarms
-    private CheckBox sRepeatCheckBox;
-    private DateTimeEditor sTimeEditor, sDateEditor;
-    private DatePicker sDatePicker;
-    private RingTonePicker sAlertPicker;
-    private Button sDiscardAlarmButton, sSetAlarmButton;
+    public void onDecoded() {
+	sAlarmListWindow = (AlarmListWindow)getWindow();
+	setList(Alarms.getList());
+	super.onDecoded();
+    }
 
-    private static Alarm sFocusedAlarm, sEditingAlarm;
-
-    private static final int EVENT_CANCEL_ALARM = -1; // XXX move this?
-    
-    public void onDecoded() {
-	sPester = (Pester)Application.getCurrentApp();
-	sAlarmListWindow = (AlarmListWindow)getWindow();
-	sAlarmList = new StdActiveList();
-	setList(sAlarmList);
-
-	sEditWindow = sPester.getDialog(ID_ALARM_SET_DIALOG, this);
-	sEditWindow.setCancelButtonEvent(new Event(this, EVENT_CANCEL_ALARM));
-	sMessageField = (TypeAheadTextField)sEditWindow.getDescendantWithID(ID_MESSAGE_FIELD);
-	sEditWindow.disableBottomRightButtonOnEmptyField(sMessageField);
-	sInButton = (RadioButton)sEditWindow.getDescendantWithID(ID_IN_BUTTON);
-	sAtButton = (RadioButton)sEditWindow.getDescendantWithID(ID_AT_BUTTON);
-	sPeriodField = (NumberEditor)sEditWindow.getDescendantWithID(ID_PERIOD_FIELD);
-	sPeriodUnitsPopup = (PopupMenu)sEditWindow.getDescendantWithID(ID_PERIOD_UNITS_POPUP);
-	sRepeatCheckBox = (CheckBox)sEditWindow.getDescendantWithID(ID_PERIOD_REPEAT_CHECKBOX);
-	sTimeEditor = (DateTimeEditor)sEditWindow.getDescendantWithID(ID_TIME_EDITOR);
-	sDateEditor = (DateTimeEditor)sEditWindow.getDescendantWithID(ID_DATE_EDITOR);
-	sDatePicker = (DatePicker)sEditWindow.getDescendantWithID(ID_DATE_PICKER);
-	sAlertPicker = (RingTonePicker)sEditWindow.getDescendantWithID(ID_ALERT_PICKER);
-	sSetAlarmButton = (Button)sEditWindow.getDescendantWithID(ID_SET_ALARM_BUTTON);
-	sDiscardAlarmButton = (Button)sEditWindow.getDescendantWithID(ID_DISCARD_ALARM_BUTTON);
-	sInButton.setValue(1);
-	sDateEditor.limitToDangerEpoch();
-	sAlertPicker.setGroupFilter(ToneClass.FOREGROUND | ToneClass.CUSTOM);
-	sAlertPicker.setRights(ToneRights.FULL);
-	// this doesn't work properly yet
-	// sAlertPicker.setCanRecord(true);
-
-	super.onDecoded();
+    protected AlarmSetDialog alarmSetDialog() {
+	if (sAlarmSetDialog == null)
+	    sAlarmSetDialog = AlarmSetDialog.getDialog();
+	return sAlarmSetDialog;
     }
 
@@ -83,5 +29,5 @@
 	case Event.DEVICE_BUTTON_BACK:
 	case Event.DEVICE_BUTTON_CANCEL:
-	    sPester.returnToLauncher();
+	    Application.getCurrentApp().returnToLauncher();
 	    return true;
 	}
@@ -89,123 +35,12 @@
     }
 
-    protected void editDateOrPeriod(Alarm alarm) {
-        boolean usesPeriod = alarm.getUsesPeriod();
-	if (usesPeriod) {
-	    int period = alarm.getPeriod();
-	    for (int i = sPeriodUnitsPopup.itemCount() - 1 ; i >= 0 ; --i) {
-		int unitsPerSec = sPeriodUnitsPopup.getItem(i).getEvent().data;
-		if (period % unitsPerSec == 0) {
-		    sPeriodUnitsPopup.setValue(i);
-		    sPeriodField.setValue(period / unitsPerSec);
-		    break;
-		}
-	    }
-	    sRepeatCheckBox.setValue(alarm.getRepeating() ? 1 : 0);
-	}
-
-	Date date = alarm.getDate();
-	sTimeEditor.setDate(date);
-	sDateEditor.setDate(date);
-	sDatePicker.setDate(date);
-
-	sInButton.setValue(usesPeriod ? 1 : 0);
-	sAtButton.setValue(usesPeriod ? 0 : 1);
-    }
-
-    protected void editAlarm(Alarm alarm, boolean asNew) {
-	sEditingAlarm = alarm;
-	alarm.beginEditing();
-	if (asNew) {
-	    sDiscardAlarmButton.hide();
-	} else {
-	    sMessageField.setText(alarm.getMessage());
-	    sDiscardAlarmButton.show();
-	    editDateOrPeriod(alarm);
-	    sAlertPicker.setRingTone(sEditingAlarm.getAlert());
-	}
-	constrainDate();
-	sEditWindow.show();
-    }
-
-    protected Date editingDate() {
-	Date date = sDateEditor.getDate();
-	date.setTime(sTimeEditor.getDate().getTime());
-	date.setSeconds(0);
-	return date;
-    }
-
-    // XXX make this into a more general validation method;
-    // XXX check before dismissing dialog that alarm is valid
-    // XXX and display alarm date
-    // XXX default to now somehow?  Would be useful.
-    protected void constrainDate() {
-	// XXX schedule every minute (second?) if in AT mode
-	// XXX could use onPeriodicPulse() perhaps
-	Date now = new Date();
-	sDateEditor.min(now);
-	sDatePicker.min(now);
-	sSetAlarmButton.setEnabled(sInButton.getValue() == 1 ||
-				   editingDate().compareTo(now) > 0);
-    }
-
     public boolean receiveEvent(Event e) {
 	switch (e.type) {
 	case EVENT_NEW_ALARM:
-	    editAlarm(new Alarm(), true);
-	    return true;
-	case EVENT_SET_ALARM:
-	    sEditingAlarm.setMessage(sMessageField.toString());
-	    if (sInButton.getValue() == 1) {
-		int itemIndex = sPeriodUnitsPopup.getValue();
-		MenuItem itemAtIndex = sPeriodUnitsPopup.getItem(itemIndex);
-		Event event = itemAtIndex.getEvent();
-		sEditingAlarm.setPeriod(sPeriodField.getValue() * event.data,
-					sRepeatCheckBox.getValue() == 1);
-	    } else {
-		sEditingAlarm.setDate(editingDate());
-	    }
-	    sEditingAlarm.setAlert(sAlertPicker.getRingTone());
-	    // schedule before adding so the tooltip is correct
-	    sEditingAlarm.schedule();
-	    if (sEditingAlarm != sFocusedAlarm) { // new alarm
-		sAlarmList.addItem(sEditingAlarm);
-		setFocusedItem(sEditingAlarm);
-		sFocusedAlarm = sEditingAlarm;
-	    }
-	    return true;
-	case EVENT_CANCEL_ALARM:
-	    if (sEditingAlarm == sFocusedAlarm) // existing alarm
-		sEditingAlarm.resume();
-	    sEditingAlarm = null;
+	    alarmSetDialog().editAlarm(new Alarm(), true);
 	    return true;
 	case EVENT_DISCARD_ALARM:
 	    // XXX confirmation
-	    sAlarmList.removeItem(sFocusedAlarm);
-	    return true;
-	case EVENT_IN:
-	    sAtButton.setValue(0);
-	    constrainDate();
-	    return true;
-	case EVENT_AT:
-	    sInButton.setValue(0);
-	    constrainDate();
-	    return true;
-	case EVENT_TIME_EDITOR:
-	    Date date = editingDate();
-	    // XXX do this on exit from the field, not on change
-	    if (date.compareTo(new Date()) <= 0) {
-		date.addDays(1);
-		sDatePicker.setDate(date);
-		sDateEditor.setDate(date);
-	    }
-	    constrainDate();
-	    return true;
-        case EVENT_DATE_EDITOR:
-	    sDatePicker.setDate(sDateEditor.getDate());
-	    constrainDate();
-	    return true;
-        case EVENT_DATE_PICKER:
-	    sDateEditor.setDate((Date)e.argument);
-	    constrainDate();
+	    Alarms.removeAlarm((Alarm)getFocusedItem());
 	    return true;
 	}
@@ -214,8 +49,7 @@
 
     protected void itemActivated(Object item) {
-	editAlarm(sFocusedAlarm, false);
-    }
-    protected void itemFocused(Object item) {
-	sFocusedAlarm = (Alarm)item;
+	if (item == null)
+	    return;
+	alarmSetDialog().editAlarm((Alarm)item, false);
     }
 
@@ -225,4 +59,5 @@
     public void onItemAdded(ActiveList list, Object item, int index) {
 	sAlarmListWindow.updateAlarmCount();
+	setFocus(index);
 	super.onItemAdded(list, item, index);
     }
