Ignore:
Timestamp:
07/13/06 16:43:36 (18 years ago)
Author:
Nicholas Riley
Message:

Refactor editing, coalese Alarms and alarm list. Try (and fail) to recognize exit from alarm time editor.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/hiptop/pester/net/sabi/pester/AlarmListView.java

    r249 r250  
    33import danger.app.Application;
    44import danger.app.Event;
    5 import danger.audio.ToneClass;
    6 import danger.audio.ToneRights;
    7 
    8 import danger.internal.Date;
    95import danger.ui.ActiveListView;
    10 import danger.ui.Button;
    11 import danger.ui.CheckBox;
    12 import danger.ui.DateTimeEditor;
    13 import danger.ui.DatePicker;
    14 import danger.ui.DialogWindow;
    15 import danger.ui.MenuItem;
    16 import danger.ui.NumberEditor;
    17 import danger.ui.PopupMenu;
    18 import danger.ui.RadioButton;
    19 import danger.ui.Rect;
    20 import danger.ui.RingTonePicker;
    21 import danger.ui.TypeAheadTextField;
    226import danger.util.ActiveList;
    23 import danger.util.StdActiveList;
    24 import danger.util.DEBUG;
    257
    268public class AlarmListView extends ActiveListView
    279    implements Resources, Commands {
    28     private static Pester sPester;
     10
     11    // XXX some of these shouldn't conceptually be static
    2912    private static AlarmListWindow sAlarmListWindow;
    30     // XXX some of these shouldn't conceptually be static, but who cares
    31     private static StdActiveList sAlarmList;
    32     private static Alarms sAlarms;
     13    private static AlarmSetDialog sAlarmSetDialog;
    3314
    34     private static DialogWindow sEditWindow;
    35     private TypeAheadTextField sMessageField;
    36     private RadioButton sInButton, sAtButton;
    37     private NumberEditor sPeriodField;
    38     private PopupMenu sPeriodUnitsPopup;
    39     // XXX don't allow <5 second repeating alarms
    40     private CheckBox sRepeatCheckBox;
    41     private DateTimeEditor sTimeEditor, sDateEditor;
    42     private DatePicker sDatePicker;
    43     private RingTonePicker sAlertPicker;
    44     private Button sDiscardAlarmButton, sSetAlarmButton;
     15    public void onDecoded() {
     16        sAlarmListWindow = (AlarmListWindow)getWindow();
     17        setList(Alarms.getList());
     18        super.onDecoded();
     19    }
    4520
    46     private static Alarm sFocusedAlarm, sEditingAlarm;
    47 
    48     private static final int EVENT_CANCEL_ALARM = -1; // XXX move this?
    49    
    50     public void onDecoded() {
    51         sPester = (Pester)Application.getCurrentApp();
    52         sAlarmListWindow = (AlarmListWindow)getWindow();
    53         sAlarmList = new StdActiveList();
    54         setList(sAlarmList);
    55 
    56         sEditWindow = sPester.getDialog(ID_ALARM_SET_DIALOG, this);
    57         sEditWindow.setCancelButtonEvent(new Event(this, EVENT_CANCEL_ALARM));
    58         sMessageField = (TypeAheadTextField)sEditWindow.getDescendantWithID(ID_MESSAGE_FIELD);
    59         sEditWindow.disableBottomRightButtonOnEmptyField(sMessageField);
    60         sInButton = (RadioButton)sEditWindow.getDescendantWithID(ID_IN_BUTTON);
    61         sAtButton = (RadioButton)sEditWindow.getDescendantWithID(ID_AT_BUTTON);
    62         sPeriodField = (NumberEditor)sEditWindow.getDescendantWithID(ID_PERIOD_FIELD);
    63         sPeriodUnitsPopup = (PopupMenu)sEditWindow.getDescendantWithID(ID_PERIOD_UNITS_POPUP);
    64         sRepeatCheckBox = (CheckBox)sEditWindow.getDescendantWithID(ID_PERIOD_REPEAT_CHECKBOX);
    65         sTimeEditor = (DateTimeEditor)sEditWindow.getDescendantWithID(ID_TIME_EDITOR);
    66         sDateEditor = (DateTimeEditor)sEditWindow.getDescendantWithID(ID_DATE_EDITOR);
    67         sDatePicker = (DatePicker)sEditWindow.getDescendantWithID(ID_DATE_PICKER);
    68         sAlertPicker = (RingTonePicker)sEditWindow.getDescendantWithID(ID_ALERT_PICKER);
    69         sSetAlarmButton = (Button)sEditWindow.getDescendantWithID(ID_SET_ALARM_BUTTON);
    70         sDiscardAlarmButton = (Button)sEditWindow.getDescendantWithID(ID_DISCARD_ALARM_BUTTON);
    71         sInButton.setValue(1);
    72         sDateEditor.limitToDangerEpoch();
    73         sAlertPicker.setGroupFilter(ToneClass.FOREGROUND | ToneClass.CUSTOM);
    74         sAlertPicker.setRights(ToneRights.FULL);
    75         // this doesn't work properly yet
    76         // sAlertPicker.setCanRecord(true);
    77 
    78         super.onDecoded();
     21    protected AlarmSetDialog alarmSetDialog() {
     22        if (sAlarmSetDialog == null)
     23            sAlarmSetDialog = AlarmSetDialog.getDialog();
     24        return sAlarmSetDialog;
    7925    }
    8026
     
    8329        case Event.DEVICE_BUTTON_BACK:
    8430        case Event.DEVICE_BUTTON_CANCEL:
    85             sPester.returnToLauncher();
     31            Application.getCurrentApp().returnToLauncher();
    8632            return true;
    8733        }
     
    8935    }
    9036
    91     protected void editDateOrPeriod(Alarm alarm) {
    92         boolean usesPeriod = alarm.getUsesPeriod();
    93         if (usesPeriod) {
    94             int period = alarm.getPeriod();
    95             for (int i = sPeriodUnitsPopup.itemCount() - 1 ; i >= 0 ; --i) {
    96                 int unitsPerSec = sPeriodUnitsPopup.getItem(i).getEvent().data;
    97                 if (period % unitsPerSec == 0) {
    98                     sPeriodUnitsPopup.setValue(i);
    99                     sPeriodField.setValue(period / unitsPerSec);
    100                     break;
    101                 }
    102             }
    103             sRepeatCheckBox.setValue(alarm.getRepeating() ? 1 : 0);
    104         }
    105 
    106         Date date = alarm.getDate();
    107         sTimeEditor.setDate(date);
    108         sDateEditor.setDate(date);
    109         sDatePicker.setDate(date);
    110 
    111         sInButton.setValue(usesPeriod ? 1 : 0);
    112         sAtButton.setValue(usesPeriod ? 0 : 1);
    113     }
    114 
    115     protected void editAlarm(Alarm alarm, boolean asNew) {
    116         sEditingAlarm = alarm;
    117         alarm.beginEditing();
    118         if (asNew) {
    119             sDiscardAlarmButton.hide();
    120         } else {
    121             sMessageField.setText(alarm.getMessage());
    122             sDiscardAlarmButton.show();
    123             editDateOrPeriod(alarm);
    124             sAlertPicker.setRingTone(sEditingAlarm.getAlert());
    125         }
    126         constrainDate();
    127         sEditWindow.show();
    128     }
    129 
    130     protected Date editingDate() {
    131         Date date = sDateEditor.getDate();
    132         date.setTime(sTimeEditor.getDate().getTime());
    133         date.setSeconds(0);
    134         return date;
    135     }
    136 
    137     // XXX make this into a more general validation method;
    138     // XXX check before dismissing dialog that alarm is valid
    139     // XXX and display alarm date
    140     // XXX default to now somehow?  Would be useful.
    141     protected void constrainDate() {
    142         // XXX schedule every minute (second?) if in AT mode
    143         // XXX could use onPeriodicPulse() perhaps
    144         Date now = new Date();
    145         sDateEditor.min(now);
    146         sDatePicker.min(now);
    147         sSetAlarmButton.setEnabled(sInButton.getValue() == 1 ||
    148                                    editingDate().compareTo(now) > 0);
    149     }
    150 
    15137    public boolean receiveEvent(Event e) {
    15238        switch (e.type) {
    15339        case EVENT_NEW_ALARM:
    154             editAlarm(new Alarm(), true);
    155             return true;
    156         case EVENT_SET_ALARM:
    157             sEditingAlarm.setMessage(sMessageField.toString());
    158             if (sInButton.getValue() == 1) {
    159                 int itemIndex = sPeriodUnitsPopup.getValue();
    160                 MenuItem itemAtIndex = sPeriodUnitsPopup.getItem(itemIndex);
    161                 Event event = itemAtIndex.getEvent();
    162                 sEditingAlarm.setPeriod(sPeriodField.getValue() * event.data,
    163                                         sRepeatCheckBox.getValue() == 1);
    164             } else {
    165                 sEditingAlarm.setDate(editingDate());
    166             }
    167             sEditingAlarm.setAlert(sAlertPicker.getRingTone());
    168             // schedule before adding so the tooltip is correct
    169             sEditingAlarm.schedule();
    170             if (sEditingAlarm != sFocusedAlarm) { // new alarm
    171                 sAlarmList.addItem(sEditingAlarm);
    172                 setFocusedItem(sEditingAlarm);
    173                 sFocusedAlarm = sEditingAlarm;
    174             }
    175             return true;
    176         case EVENT_CANCEL_ALARM:
    177             if (sEditingAlarm == sFocusedAlarm) // existing alarm
    178                 sEditingAlarm.resume();
    179             sEditingAlarm = null;
     40            alarmSetDialog().editAlarm(new Alarm(), true);
    18041            return true;
    18142        case EVENT_DISCARD_ALARM:
    18243            // XXX confirmation
    183             sAlarmList.removeItem(sFocusedAlarm);
    184             return true;
    185         case EVENT_IN:
    186             sAtButton.setValue(0);
    187             constrainDate();
    188             return true;
    189         case EVENT_AT:
    190             sInButton.setValue(0);
    191             constrainDate();
    192             return true;
    193         case EVENT_TIME_EDITOR:
    194             Date date = editingDate();
    195             // XXX do this on exit from the field, not on change
    196             if (date.compareTo(new Date()) <= 0) {
    197                 date.addDays(1);
    198                 sDatePicker.setDate(date);
    199                 sDateEditor.setDate(date);
    200             }
    201             constrainDate();
    202             return true;
    203         case EVENT_DATE_EDITOR:
    204             sDatePicker.setDate(sDateEditor.getDate());
    205             constrainDate();
    206             return true;
    207         case EVENT_DATE_PICKER:
    208             sDateEditor.setDate((Date)e.argument);
    209             constrainDate();
     44            Alarms.removeAlarm((Alarm)getFocusedItem());
    21045            return true;
    21146        }
     
    21449
    21550    protected void itemActivated(Object item) {
    216         editAlarm(sFocusedAlarm, false);
    217     }
    218     protected void itemFocused(Object item) {
    219         sFocusedAlarm = (Alarm)item;
     51        if (item == null)
     52            return;
     53        alarmSetDialog().editAlarm((Alarm)item, false);
    22054    }
    22155
     
    22559    public void onItemAdded(ActiveList list, Object item, int index) {
    22660        sAlarmListWindow.updateAlarmCount();
     61        setFocus(index);
    22762        super.onItemAdded(list, item, index);
    22863    }
Note: See TracChangeset for help on using the changeset viewer.