[237] | 1 | package net.sabi.pester;
|
---|
| 2 |
|
---|
| 3 | import danger.app.Application;
|
---|
| 4 | import danger.app.Event;
|
---|
| 5 | import danger.ui.ActiveListView;
|
---|
[238] | 6 | import danger.ui.Button;
|
---|
[237] | 7 | import danger.ui.DialogWindow;
|
---|
[239] | 8 | import danger.ui.RadioButton;
|
---|
[237] | 9 | import danger.ui.Rect;
|
---|
| 10 | import danger.ui.TypeAheadTextField;
|
---|
| 11 | import danger.util.ActiveList;
|
---|
| 12 | import danger.util.StdActiveList;
|
---|
| 13 | import danger.util.DEBUG;
|
---|
| 14 |
|
---|
| 15 | public class AlarmListView extends ActiveListView
|
---|
| 16 | implements Resources, Commands {
|
---|
| 17 | private static Pester sPester;
|
---|
| 18 | private static AlarmListWindow sAlarmListWindow;
|
---|
| 19 | // XXX some of these shouldn't conceptually be static, but who cares
|
---|
| 20 | private static StdActiveList sAlarmList;
|
---|
| 21 | private static Alarms sAlarms;
|
---|
| 22 |
|
---|
| 23 | private static DialogWindow sEditWindow;
|
---|
| 24 | private TypeAheadTextField sMessageField;
|
---|
[238] | 25 | private Button sDiscardAlarmButton;
|
---|
[239] | 26 | private RadioButton sInButton, sAtButton;
|
---|
[237] | 27 |
|
---|
| 28 | private static Alarm sFocusedAlarm, sEditingAlarm;
|
---|
| 29 |
|
---|
| 30 | private static final int EVENT_CANCEL_ALARM = -1; // XXX move this?
|
---|
| 31 |
|
---|
| 32 | public void onDecoded() {
|
---|
| 33 | sPester = (Pester)Application.getCurrentApp();
|
---|
| 34 | sAlarmListWindow = (AlarmListWindow)getWindow();
|
---|
| 35 | sAlarmList = new StdActiveList();
|
---|
| 36 | setList(sAlarmList);
|
---|
| 37 |
|
---|
| 38 | sEditWindow = sPester.getDialog(ID_ALARM_SET_DIALOG, this);
|
---|
| 39 | sEditWindow.setCancelButtonEvent(new Event(this, EVENT_CANCEL_ALARM));
|
---|
| 40 | sMessageField = (TypeAheadTextField)sEditWindow.getDescendantWithID(ID_MESSAGE_FIELD);
|
---|
| 41 | sEditWindow.disableBottomRightButtonOnEmptyField(sMessageField);
|
---|
[239] | 42 | sInButton = (RadioButton)sEditWindow.getDescendantWithID(ID_IN_BUTTON);
|
---|
| 43 | sAtButton = (RadioButton)sEditWindow.getDescendantWithID(ID_AT_BUTTON);
|
---|
[238] | 44 | sDiscardAlarmButton = (Button)sEditWindow.getDescendantWithID(ID_DISCARD_ALARM_BUTTON);
|
---|
[237] | 45 |
|
---|
| 46 | super.onDecoded();
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | public boolean eventWidgetUp(int widget, Event e) {
|
---|
| 50 | switch (widget) {
|
---|
| 51 | case Event.DEVICE_BUTTON_BACK:
|
---|
| 52 | case Event.DEVICE_BUTTON_CANCEL:
|
---|
| 53 | sPester.returnToLauncher();
|
---|
| 54 | return true;
|
---|
| 55 | }
|
---|
| 56 | return super.eventWidgetUp(widget, e);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[238] | 59 | void editAlarm(Alarm alarm, boolean asNew) {
|
---|
[237] | 60 | sEditingAlarm = alarm;
|
---|
| 61 | alarm.beginEditing();
|
---|
| 62 | sMessageField.setText(sEditingAlarm.getMessage());
|
---|
[238] | 63 | if (asNew) {
|
---|
| 64 | sDiscardAlarmButton.hide();
|
---|
| 65 | } else {
|
---|
| 66 | sDiscardAlarmButton.show();
|
---|
| 67 | }
|
---|
| 68 | boolean usesPeriod = sEditingAlarm.getUsesPeriod();
|
---|
| 69 |
|
---|
[237] | 70 | sEditWindow.show();
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | public boolean receiveEvent(Event e) {
|
---|
| 74 | switch (e.type) {
|
---|
| 75 | case EVENT_NEW_ALARM:
|
---|
[238] | 76 | editAlarm(new Alarm(), true);
|
---|
[237] | 77 | return true;
|
---|
| 78 | case EVENT_SET_ALARM:
|
---|
| 79 | if (sEditingAlarm != sFocusedAlarm) { // new alarm
|
---|
| 80 | sAlarmList.addItem(sEditingAlarm);
|
---|
| 81 | setFocusedItem(sEditingAlarm);
|
---|
| 82 | }
|
---|
| 83 | sEditingAlarm.setMessage(sMessageField.toString());
|
---|
| 84 | sEditingAlarm.update();
|
---|
| 85 | case EVENT_CANCEL_ALARM:
|
---|
| 86 | // XXX (re)schedule alarm
|
---|
| 87 | sEditingAlarm.endEditing();
|
---|
| 88 | return true;
|
---|
| 89 | case EVENT_DISCARD_ALARM:
|
---|
| 90 | // XXX confirmation
|
---|
| 91 | sAlarmList.removeItem(sFocusedAlarm);
|
---|
| 92 | return true;
|
---|
[239] | 93 | case EVENT_IN:
|
---|
| 94 | sAtButton.setValue(0);
|
---|
| 95 | return true;
|
---|
| 96 | case EVENT_AT:
|
---|
| 97 | sInButton.setValue(0);
|
---|
| 98 | return true;
|
---|
[237] | 99 | }
|
---|
| 100 | return super.receiveEvent(e);
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | protected void itemActivated(Object item) {
|
---|
| 104 | // XXX deschedule alarm
|
---|
[238] | 105 | editAlarm(sFocusedAlarm, false);
|
---|
[237] | 106 | }
|
---|
| 107 | protected void itemFocused(Object item) {
|
---|
| 108 | sFocusedAlarm = (Alarm)item;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | public int alarmsSet() {
|
---|
| 112 | return getListSize();
|
---|
| 113 | }
|
---|
| 114 | public void onItemAdded(ActiveList list, Object item, int index) {
|
---|
| 115 | sAlarmListWindow.updateAlarmCount();
|
---|
| 116 | super.onItemAdded(list, item, index);
|
---|
| 117 | }
|
---|
| 118 | public void onItemRemoved(ActiveList list, Object item, int index) {
|
---|
| 119 | sAlarmListWindow.updateAlarmCount();
|
---|
| 120 | super.onItemRemoved(list, item, index);
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | protected CharSequence getToolTipForItem(Object item) {
|
---|
| 124 | if (item == null) // this is dumb, why do we get asked for a tooltip?
|
---|
| 125 | return null;
|
---|
| 126 | // XXX only set a tooltip if the message is truncated
|
---|
| 127 | return ((Alarm)item).getMessage();
|
---|
| 128 | }
|
---|
| 129 | }
|
---|