/** * This method will list all date entries found in persistent storage. The user may select one of * the entries and either change the selected entry or delete it or even quit without any change. * * @return Date object chosen or <code>null</code>. */ private static Date listDates() { Date result = null; Date[] list = new Date[4]; // the entry list (the size will be changed if necessary) // two items for every tlv entry (date and subject message) { int index = 0; Date current = Date.getNextDate(new Time(2000, 1, 1, 0, 0, 0, 0)); // start searching here while (current != null) { if (index >= list.length) { // if list array is too small Date[] newlist = new Date[list.length + 4]; for (int c = 0; c < list.length; c++) newlist[c] = list[c]; // enlarge the array list = newlist; } list[index++] = current; // the subject message Time next = current.time; current = Date.getNextDate( new Time(next.year, next.month, next.day, next.dow, next.hour, next.minute + 1, 0)); } if (index <= 0) { // if there are no entries, return immediately return null; } else { if (index < list.length) { // trim to size Date[] newlist = new Date[index]; for (int c = 0; c < index; c++) newlist[c] = list[c]; list = newlist; } } } // all entries were found and set into the list { // now, create a choose dialog and wait for user input lcd.clearRect(1, 20, 126, 43); // clear the main rectangle area { String[] image = new String[] {"\u040E\u1F0E\u0400"}; // the up and down arrow image // create menu items lcd.drawImage(image, 22, 57, 5, 3, 0, 0); // up lcd.drawImage(image, 7, 57, 5, 3, 0, 2); // down lcd.drawString("modify", 35, 55); // change lcd.drawString("delete", 65, 55); // delete lcd.drawString("back", 100, 55); // quit } lcd.setDrawMode(Display.XOR); char c = 0; int shift = 0; int select = 0; for (int i = 0; i < 4; i++) { // draw four entries on the screen (there isn't enough space for more than four // :-)) if (i + shift < list.length) { int pos = lcd.drawString(time2string(list[i + shift].time), 9, 21 + (i << 3)) + 14; if (!"$".equals(list[i + shift].text)) lcd.drawString(list[i + shift].text, pos, 21 + (i << 3), 116 - pos, 10, 0, 0); } } lcd.fillRect(6, 20, 120, 8); // draw a rectangle around the first entry as it is selected // select one of the menu items lcd.fillRect( select == 0 ? 2 : select == 1 ? 17 : select == 2 ? 30 : select == 3 ? 60 : 95, 55, select < 2 ? 15 : select == 3 ? 36 : 32, 7); mainloop: for (; ; ) { c = key.read(); // wait for keypress switch (c) { case 'R': // select the next menu item case 'U': case 'u': lcd.fillRect( select == 0 ? 2 : select == 1 ? 17 : select == 2 ? 30 : select == 3 ? 60 : 95, 55, select < 2 ? 15 : select == 3 ? 36 : 32, 7); select++; select %= 5; lcd.fillRect( select == 0 ? 2 : select == 1 ? 17 : select == 2 ? 30 : select == 3 ? 60 : 95, 55, select < 2 ? 15 : select == 3 ? 36 : 32, 7); break; case 'L': // select the previous menu item case 'D': case 'd': lcd.fillRect( select == 0 ? 2 : select == 1 ? 17 : select == 2 ? 30 : select == 3 ? 60 : 95, 55, select < 2 ? 15 : select == 3 ? 36 : 32, 7); select += 4; select %= 5; lcd.fillRect( select == 0 ? 2 : select == 1 ? 17 : select == 2 ? 30 : select == 3 ? 60 : 95, 55, select < 2 ? 15 : select == 3 ? 36 : 32, 7); break; case 'S': // button was pressed -> do something switch (select) { case 0: // down case 1: // up // scroll around for (int i = 0; i < 4; i++) { if (i + shift < list.length) { int pos = lcd.drawString(time2string(list[i + shift].time), 9, 21 + (i << 3)) + 14; if (!"$".equals(list[i + shift].text)) lcd.drawString(list[i + shift].text, pos, 21 + (i << 3), 116 - pos, 10, 0, 0); } } if (select == 1) shift = (shift <= 0 ? 0 : shift - 1); if (select == 0) shift = (shift >= list.length - 1 ? list.length - 1 : shift + 1); for (int i = 0; i < 4; i++) { if (i + shift < list.length) { int pos = lcd.drawString(time2string(list[i + shift].time), 9, 21 + (i << 3)) + 14; if (!"$".equals(list[i + shift].text)) lcd.drawString(list[i + shift].text, pos, 21 + (i << 3), 116 - pos, 10, 0, 0); } } break; case 2: // change result = list[shift]; case 3: // delete try { list[shift].remove(); updateAlarm(null); } catch (IOException e) { } case 4: break mainloop; // quit without change } break; } } lcd.setDrawMode(Display.NORMAL); return result; } }