KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > usertasks > DueTasksNotifier


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tasklist.usertasks;
21
22 import javax.swing.SwingUtilities JavaDoc;
23 import javax.swing.event.ChangeEvent JavaDoc;
24 import javax.swing.event.ChangeListener JavaDoc;
25 import org.netbeans.modules.tasklist.usertasks.model.*;
26 import org.netbeans.modules.tasklist.usertasks.model.UserTaskList.UserTaskProcessor;
27 import org.openide.DialogDisplayer;
28 import org.openide.DialogDescriptor;
29 import org.openide.NotifyDescriptor;
30 import org.openide.util.NbBundle;
31
32 /**
33  * Notifies the user about due tasks.
34  *
35  * @author tl
36  */

37 public class DueTasksNotifier implements Timeout {
38     /**
39      * Callback for finding the next timeout
40      */

41     private static class FindNextTimeoutUserTaskProcessor implements
42     UserTaskProcessor {
43         private long nextTimeout = Long.MAX_VALUE;
44         
45         /** Task for the next timeout */
46         public UserTask ref = null;
47         
48         public void process(UserTask t) {
49             long n = t.getDueTime();
50             if (n != Long.MAX_VALUE && !t.isDueAlarmSent() && !t.isDone() &&
51                 n > System.currentTimeMillis() && n < nextTimeout) {
52                 nextTimeout = n;
53                 ref = t;
54             }
55         }
56     }
57     
58     private static class ShowExpiredUserTaskProcessor implements
59         UserTaskProcessor {
60         public void process(UserTask t) {
61             long n = t.getDueTime();
62             if (n != Long.MAX_VALUE && !t.isDueAlarmSent() &&
63                 !t.isDone() &&
64                 n <= System.currentTimeMillis()) {
65                 showExpiredTask(t);
66             }
67         }
68
69         /**
70          * Present the user with a dialog that shows information of the task that
71          * expired...
72          *
73          * @param task the task to show
74          */

75         private void showExpiredTask(UserTask task) {
76             task.setDueAlarmSent(true);
77
78             final UserTask t = task;
79             SwingUtilities.invokeLater(new Runnable JavaDoc() {
80                 public void run() {
81                     UserTaskDuePanel panel = new UserTaskDuePanel(t);
82
83                     String JavaDoc title = NbBundle.getMessage(DueTasksNotifier.class,
84                             "TaskDueLabel"); // NOI18N
85
DialogDescriptor d = new DialogDescriptor(panel, title);
86                     d.setModal(true);
87                     d.setMessageType(NotifyDescriptor.PLAIN_MESSAGE);
88                     d.setOptions(new Object JavaDoc[] {DialogDescriptor.OK_OPTION});
89                     java.awt.Dialog JavaDoc dlg = DialogDisplayer.getDefault().createDialog(d);
90                     dlg.pack();
91                     dlg.setVisible(true);
92                 }
93             });
94         }
95     }
96     
97     /** The current timeout */
98     private long currentTimeout;
99     
100     private UserTaskList utl;
101     
102     /**
103      * Creates a new instance of DueTasksNotifier
104      *
105      * @param utl a task list
106      */

107     public DueTasksNotifier(UserTaskList utl) {
108         this.utl = utl;
109         currentTimeout = Long.MAX_VALUE;
110         utl.addChangeListener(new ChangeListener JavaDoc() {
111             public void stateChanged(ChangeEvent JavaDoc e) {
112                 orderNextTimeout();
113             }
114         });
115     }
116
117     /**
118      * Order a timeout for the next due date
119      */

120     public void orderNextTimeout() {
121         ShowExpiredUserTaskProcessor se =
122             new ShowExpiredUserTaskProcessor();
123         utl.processDepthFirst(se, utl.getSubtasks());
124         
125         FindNextTimeoutUserTaskProcessor p =
126             new FindNextTimeoutUserTaskProcessor();
127         utl.processDepthFirst(p, utl.getSubtasks());
128         
129         if (p.ref != null && p.ref.getDueTime() != Long.MAX_VALUE &&
130             !p.ref.isDueAlarmSent() && !p.ref.isDone() &&
131             p.ref.getDueTime() != currentTimeout) {
132             // cancel the previous ordered timeout, and add the new one
133
if (currentTimeout != Long.MAX_VALUE) {
134                 TimeoutProvider.getInstance().cancel(this, null);
135             }
136             TimeoutProvider.getInstance().add(this, p.ref, p.ref.getDueTime());
137             currentTimeout = p.ref.getDueTime();
138         }
139     }
140     
141     /**
142      * Callback function for the TimeoutProvider to call when the timeout
143      * expired. This function will block the TimeoutProviders thread, so
144      * it should be used for a timeconsuming task (one should probably
145      * reschedule oneself with the SwingUtilities.invokeLater() ???)
146      * @param o the object provided as a user reference
147      */

148     public void timeoutExpired(Object JavaDoc o) {
149         // order the next timeout for this list
150
orderNextTimeout();
151     }
152
153 }
154
Popular Tags