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