[919] | 1 | /**
|
---|
| 2 | * Reminders.java
|
---|
| 3 | * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
|
---|
| 4 | *
|
---|
| 5 | * This program is free software: you can redistribute it and/or modify
|
---|
| 6 | * it under the terms of the GNU General Public License as published by
|
---|
| 7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 8 | * (at your option) any later version.
|
---|
| 9 | *
|
---|
| 10 | * This program is distributed in the hope that it will be useful,
|
---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 13 | * GNU General Public License for more details.
|
---|
| 14 | *
|
---|
| 15 | * You should have received a copy of the GNU General Public License
|
---|
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 17 | */
|
---|
| 18 |
|
---|
[285] | 19 | package org.expeditee.gui;
|
---|
| 20 |
|
---|
| 21 | import java.util.Date;
|
---|
| 22 |
|
---|
[1102] | 23 | import org.expeditee.Util;
|
---|
[285] | 24 | import org.expeditee.actions.Misc;
|
---|
[1102] | 25 | import org.expeditee.core.Colour;
|
---|
[285] | 26 | import org.expeditee.items.Text;
|
---|
| 27 |
|
---|
| 28 | public class Reminders {
|
---|
| 29 |
|
---|
| 30 | public synchronized static void init(final Frame reminderFrame) {
|
---|
[376] | 31 | if(reminderFrame == null)
|
---|
| 32 | return;
|
---|
| 33 |
|
---|
[285] | 34 | for (Text text : reminderFrame.getBodyTextItems(false)) {
|
---|
| 35 | try {
|
---|
| 36 | final Text reminderItem = text;
|
---|
| 37 | final AttributeValuePair avp = new AttributeValuePair(text
|
---|
| 38 | .getText(), true);
|
---|
| 39 | final String dateString = avp.getAttributeOrValue();
|
---|
| 40 | final String reminderString;
|
---|
| 41 | if (avp.hasPair()) {
|
---|
| 42 | reminderString = avp.getValue() + " at "
|
---|
| 43 | + avp.getAttribute();
|
---|
| 44 | } else {
|
---|
| 45 | reminderString = dateString;
|
---|
| 46 | }
|
---|
| 47 |
|
---|
[1102] | 48 | Date date = Util.parseDate(dateString);
|
---|
[285] | 49 | long millisToWait = date.getTime() - new Date().getTime();
|
---|
| 50 | final long adjustedToWait;
|
---|
| 51 |
|
---|
| 52 | // Adjust the time to wait if their is no time left
|
---|
| 53 | if (millisToWait < 0) {
|
---|
| 54 | adjustedToWait = 0;
|
---|
| 55 | } else {
|
---|
| 56 | adjustedToWait = millisToWait;
|
---|
| 57 | }
|
---|
| 58 | // Now create the reminder
|
---|
| 59 | new Thread() {
|
---|
| 60 | public void run() {
|
---|
| 61 | try {
|
---|
| 62 | Thread.sleep(adjustedToWait);
|
---|
| 63 | synchronized (_alertsRunning) {
|
---|
| 64 | _alertsRunning++;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | do {
|
---|
| 68 | Misc.beep();
|
---|
| 69 | MessageBay.displayMessage(
|
---|
| 70 | "Reminder: " + reminderString, null,
|
---|
[1102] | 71 | Colour.RED, false, "StopReminder");
|
---|
[285] | 72 | Thread.sleep(5000);
|
---|
| 73 | // newMessage.setColor(new Color(100, 70, 70));
|
---|
| 74 | // Thread.sleep(100);
|
---|
| 75 | // newMessage.setColor(Color.red);
|
---|
| 76 | } while (!_bStop);
|
---|
| 77 | synchronized (_alertsRunning) {
|
---|
| 78 | assert (_alertsRunning > 0);
|
---|
| 79 | _alertsRunning--;
|
---|
| 80 | reminderItem.setText("@" + reminderItem.getText());
|
---|
[348] | 81 | FrameIO.ForceSaveFrame(reminderItem.getParent());
|
---|
[285] | 82 | if (_alertsRunning == 0) {
|
---|
| 83 | _bStop = false;
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 | } catch (Exception e) {
|
---|
| 87 | //e.printStackTrace();
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 | }.start();
|
---|
| 91 | } catch (Exception e) {
|
---|
| 92 | MessageBay.errorMessage("Invalid reminder format: "
|
---|
| 93 | + text.getText());
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | private static Boolean _bStop = false;
|
---|
| 99 |
|
---|
| 100 | private static Integer _alertsRunning = 0;
|
---|
| 101 |
|
---|
| 102 | public synchronized static void stop() {
|
---|
| 103 | if (_alertsRunning > 0)
|
---|
| 104 | _bStop = true;
|
---|
| 105 | }
|
---|
| 106 | }
|
---|