[237] | 1 | package net.sabi.pester;
|
---|
| 2 |
|
---|
| 3 | import danger.app.Application;
|
---|
| 4 | import danger.app.Event;
|
---|
[240] | 5 | import danger.internal.Date;
|
---|
[237] | 6 | import danger.ui.ActiveListView;
|
---|
[238] | 7 | import danger.ui.Button;
|
---|
[240] | 8 | import danger.ui.CheckBox;
|
---|
| 9 | import danger.ui.DateTimeEditor;
|
---|
| 10 | import danger.ui.DatePicker;
|
---|
[237] | 11 | import danger.ui.DialogWindow;
|
---|
[240] | 12 | import danger.ui.NumberEditor;
|
---|
| 13 | import danger.ui.PopupMenu;
|
---|
[239] | 14 | import danger.ui.RadioButton;
|
---|
[237] | 15 | import danger.ui.Rect;
|
---|
| 16 | import danger.ui.TypeAheadTextField;
|
---|
| 17 | import danger.util.ActiveList;
|
---|
| 18 | import danger.util.StdActiveList;
|
---|
| 19 | import danger.util.DEBUG;
|
---|
| 20 |
|
---|
| 21 | public class AlarmListView extends ActiveListView
|
---|
| 22 | implements Resources, Commands {
|
---|
| 23 | private static Pester sPester;
|
---|
| 24 | private static AlarmListWindow sAlarmListWindow;
|
---|
| 25 | // XXX some of these shouldn't conceptually be static, but who cares
|
---|
| 26 | private static StdActiveList sAlarmList;
|
---|
| 27 | private static Alarms sAlarms;
|
---|
| 28 |
|
---|
| 29 | private static DialogWindow sEditWindow;
|
---|
| 30 | private TypeAheadTextField sMessageField;
|
---|
[239] | 31 | private RadioButton sInButton, sAtButton;
|
---|
[240] | 32 | private NumberEditor sPeriodField;
|
---|
| 33 | private PopupMenu sPeriodUnitsPopup;
|
---|
| 34 | private CheckBox sRepeatCheckBox;
|
---|
| 35 | private DateTimeEditor sTimeEditor, sDateEditor;
|
---|
| 36 | private DatePicker sDatePicker;
|
---|
| 37 | private Button sDiscardAlarmButton, sSetAlarmButton;
|
---|
[237] | 38 |
|
---|
| 39 | private static Alarm sFocusedAlarm, sEditingAlarm;
|
---|
| 40 |
|
---|
| 41 | private static final int EVENT_CANCEL_ALARM = -1; // XXX move this?
|
---|
| 42 |
|
---|
| 43 | public void onDecoded() {
|
---|
| 44 | sPester = (Pester)Application.getCurrentApp();
|
---|
| 45 | sAlarmListWindow = (AlarmListWindow)getWindow();
|
---|
| 46 | sAlarmList = new StdActiveList();
|
---|
| 47 | setList(sAlarmList);
|
---|
| 48 |
|
---|
| 49 | sEditWindow = sPester.getDialog(ID_ALARM_SET_DIALOG, this);
|
---|
| 50 | sEditWindow.setCancelButtonEvent(new Event(this, EVENT_CANCEL_ALARM));
|
---|
| 51 | sMessageField = (TypeAheadTextField)sEditWindow.getDescendantWithID(ID_MESSAGE_FIELD);
|
---|
| 52 | sEditWindow.disableBottomRightButtonOnEmptyField(sMessageField);
|
---|
[239] | 53 | sInButton = (RadioButton)sEditWindow.getDescendantWithID(ID_IN_BUTTON);
|
---|
| 54 | sAtButton = (RadioButton)sEditWindow.getDescendantWithID(ID_AT_BUTTON);
|
---|
[240] | 55 | sPeriodField = (NumberEditor)sEditWindow.getDescendantWithID(ID_PERIOD_FIELD);
|
---|
| 56 | sPeriodUnitsPopup = (PopupMenu)sEditWindow.getDescendantWithID(ID_PERIOD_UNITS_POPUP);
|
---|
| 57 | sRepeatCheckBox = (CheckBox)sEditWindow.getDescendantWithID(ID_PERIOD_REPEAT_CHECKBOX);
|
---|
| 58 | sTimeEditor = (DateTimeEditor)sEditWindow.getDescendantWithID(ID_TIME_EDITOR);
|
---|
| 59 | sDateEditor = (DateTimeEditor)sEditWindow.getDescendantWithID(ID_DATE_EDITOR);
|
---|
| 60 | sDatePicker = (DatePicker)sEditWindow.getDescendantWithID(ID_DATE_PICKER);
|
---|
| 61 | sSetAlarmButton = (Button)sEditWindow.getDescendantWithID(ID_SET_ALARM_BUTTON);
|
---|
[238] | 62 | sDiscardAlarmButton = (Button)sEditWindow.getDescendantWithID(ID_DISCARD_ALARM_BUTTON);
|
---|
[240] | 63 | sInButton.setValue(1);
|
---|
| 64 | sDateEditor.limitToDangerEpoch();
|
---|
[237] | 65 |
|
---|
| 66 | super.onDecoded();
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | public boolean eventWidgetUp(int widget, Event e) {
|
---|
| 70 | switch (widget) {
|
---|
| 71 | case Event.DEVICE_BUTTON_BACK:
|
---|
| 72 | case Event.DEVICE_BUTTON_CANCEL:
|
---|
| 73 | sPester.returnToLauncher();
|
---|
| 74 | return true;
|
---|
| 75 | }
|
---|
| 76 | return super.eventWidgetUp(widget, e);
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[240] | 79 | protected void editDateOrPeriod(Alarm alarm) {
|
---|
| 80 | boolean usesPeriod = alarm.getUsesPeriod();
|
---|
| 81 | if (usesPeriod) {
|
---|
| 82 | int period = alarm.getPeriod();
|
---|
| 83 | int unitsPerSec = 1;
|
---|
| 84 | DEBUG.p("period is " + period);
|
---|
| 85 | for (int i = sPeriodUnitsPopup.itemCount() - 1 ; i >= 0 ; --i) {
|
---|
| 86 | unitsPerSec =
|
---|
| 87 | sPeriodUnitsPopup.getIDOfItem(sPeriodUnitsPopup.getItem(i));
|
---|
| 88 | DEBUG.p("unitsPerSec = " + unitsPerSec);
|
---|
| 89 | if (period % unitsPerSec == 0) break;
|
---|
| 90 | }
|
---|
| 91 | sPeriodField.setValue(period / unitsPerSec);
|
---|
| 92 | sPeriodUnitsPopup.setValueWithItemID(unitsPerSec);
|
---|
| 93 | sRepeatCheckBox.setValue(alarm.getRepeating() ? 1 : 0);
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | Date date = alarm.getDate();
|
---|
| 97 | sTimeEditor.setDate(date);
|
---|
| 98 | sDateEditor.setDate(date);
|
---|
| 99 | sDatePicker.setDate(date);
|
---|
| 100 |
|
---|
| 101 | sInButton.setValue(usesPeriod ? 1 : 0);
|
---|
| 102 | sAtButton.setValue(usesPeriod ? 0 : 1);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | protected void editAlarm(Alarm alarm, boolean asNew) {
|
---|
[237] | 106 | sEditingAlarm = alarm;
|
---|
| 107 | alarm.beginEditing();
|
---|
[240] | 108 | sMessageField.setText(alarm.getMessage());
|
---|
[238] | 109 | if (asNew) {
|
---|
| 110 | sDiscardAlarmButton.hide();
|
---|
| 111 | } else {
|
---|
| 112 | sDiscardAlarmButton.show();
|
---|
[240] | 113 | editDateOrPeriod(alarm);
|
---|
[238] | 114 | }
|
---|
[240] | 115 | constrainDate();
|
---|
[237] | 116 | sEditWindow.show();
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[240] | 119 | protected Date editingDate() {
|
---|
| 120 | Date date = sDateEditor.getDate();
|
---|
| 121 | date.setTime(sTimeEditor.getDate().getTime());
|
---|
| 122 | return date;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | protected void constrainDate() {
|
---|
| 126 | // XXX schedule every minute (second?) if in AT mode
|
---|
| 127 | Date now = new Date();
|
---|
| 128 | sDateEditor.min(now);
|
---|
| 129 | sDatePicker.min(now);
|
---|
| 130 | sSetAlarmButton.setEnabled(editingDate().compareTo(now) > 0);
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[237] | 133 | public boolean receiveEvent(Event e) {
|
---|
| 134 | switch (e.type) {
|
---|
| 135 | case EVENT_NEW_ALARM:
|
---|
[238] | 136 | editAlarm(new Alarm(), true);
|
---|
[237] | 137 | return true;
|
---|
| 138 | case EVENT_SET_ALARM:
|
---|
[240] | 139 | sEditingAlarm.setMessage(sMessageField.toString());
|
---|
| 140 | if (sInButton.getValue() == 1) {
|
---|
| 141 | DEBUG.p("period: " + sPeriodField.getValue());
|
---|
| 142 | DEBUG.p(" units: " + sPeriodUnitsPopup.getValueAsItemID());
|
---|
| 143 | sEditingAlarm.setPeriod(sPeriodField.getValue() *
|
---|
| 144 | sPeriodUnitsPopup.getValueAsItemID(),
|
---|
| 145 | sRepeatCheckBox.getValue() == 1);
|
---|
| 146 | } else {
|
---|
| 147 | sEditingAlarm.setDate(editingDate());
|
---|
| 148 | }
|
---|
[237] | 149 | if (sEditingAlarm != sFocusedAlarm) { // new alarm
|
---|
| 150 | sAlarmList.addItem(sEditingAlarm);
|
---|
| 151 | setFocusedItem(sEditingAlarm);
|
---|
[240] | 152 | } else {
|
---|
| 153 | sEditingAlarm.update();
|
---|
[237] | 154 | }
|
---|
| 155 | case EVENT_CANCEL_ALARM:
|
---|
| 156 | // XXX (re)schedule alarm
|
---|
| 157 | sEditingAlarm.endEditing();
|
---|
| 158 | return true;
|
---|
| 159 | case EVENT_DISCARD_ALARM:
|
---|
| 160 | // XXX confirmation
|
---|
| 161 | sAlarmList.removeItem(sFocusedAlarm);
|
---|
| 162 | return true;
|
---|
[239] | 163 | case EVENT_IN:
|
---|
| 164 | sAtButton.setValue(0);
|
---|
| 165 | return true;
|
---|
| 166 | case EVENT_AT:
|
---|
| 167 | sInButton.setValue(0);
|
---|
[240] | 168 | constrainDate();
|
---|
[239] | 169 | return true;
|
---|
[240] | 170 | case EVENT_TIME_EDITOR:
|
---|
| 171 | Date date = editingDate();
|
---|
| 172 | // XXX factor this out (it's used twice)
|
---|
| 173 | // XXX do this on exit from the field, not on change
|
---|
| 174 | if (date.compareTo(new Date()) <= 0) {
|
---|
| 175 | date.addDays(1);
|
---|
| 176 | sDatePicker.setDate(date);
|
---|
| 177 | sDateEditor.setDate(date);
|
---|
| 178 | }
|
---|
| 179 | constrainDate();
|
---|
| 180 | return true;
|
---|
| 181 | case EVENT_DATE_EDITOR:
|
---|
| 182 | sDatePicker.setDate(sDateEditor.getDate());
|
---|
| 183 | constrainDate();
|
---|
| 184 | return true;
|
---|
| 185 | case EVENT_DATE_PICKER:
|
---|
| 186 | sDateEditor.setDate((Date)e.argument);
|
---|
| 187 | constrainDate();
|
---|
| 188 | return true;
|
---|
[237] | 189 | }
|
---|
| 190 | return super.receiveEvent(e);
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | protected void itemActivated(Object item) {
|
---|
| 194 | // XXX deschedule alarm
|
---|
[238] | 195 | editAlarm(sFocusedAlarm, false);
|
---|
[237] | 196 | }
|
---|
| 197 | protected void itemFocused(Object item) {
|
---|
| 198 | sFocusedAlarm = (Alarm)item;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | public int alarmsSet() {
|
---|
| 202 | return getListSize();
|
---|
| 203 | }
|
---|
| 204 | public void onItemAdded(ActiveList list, Object item, int index) {
|
---|
| 205 | sAlarmListWindow.updateAlarmCount();
|
---|
| 206 | super.onItemAdded(list, item, index);
|
---|
| 207 | }
|
---|
| 208 | public void onItemRemoved(ActiveList list, Object item, int index) {
|
---|
| 209 | sAlarmListWindow.updateAlarmCount();
|
---|
| 210 | super.onItemRemoved(list, item, index);
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | protected CharSequence getToolTipForItem(Object item) {
|
---|
| 214 | if (item == null) // this is dumb, why do we get asked for a tooltip?
|
---|
| 215 | return null;
|
---|
| 216 | // XXX only set a tooltip if the message is truncated
|
---|
| 217 | return ((Alarm)item).getMessage();
|
---|
| 218 | }
|
---|
| 219 | }
|
---|