KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > usertasks > actions > ScheduleAction


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.actions;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Calendar JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.Comparator JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Set JavaDoc;
30 import org.netbeans.modules.tasklist.usertasks.options.Settings;
31 import org.netbeans.modules.tasklist.usertasks.UserTaskView;
32 import org.netbeans.modules.tasklist.usertasks.UserTaskViewRegistry;
33 import org.netbeans.modules.tasklist.usertasks.model.UserTask;
34 import org.netbeans.modules.tasklist.usertasks.model.UserTaskList;
35 import org.netbeans.modules.tasklist.usertasks.schedule.ScheduleUtils;
36 import org.netbeans.modules.tasklist.usertasks.util.UTListTreeAbstraction;
37 import org.netbeans.modules.tasklist.usertasks.util.UTUtils;
38 import org.netbeans.modules.tasklist.usertasks.util.UnaryFunction;
39 import org.openide.DialogDisplayer;
40 import org.openide.NotifyDescriptor;
41 import org.openide.util.HelpCtx;
42 import org.openide.util.NbBundle;
43 import org.openide.util.actions.CallableSystemAction;
44
45 /**
46  * Collapses all tasks containing subtasks in the tasklist
47  *
48  * @author tl
49  */

50 public final class ScheduleAction extends CallableSystemAction {
51     private static final long serialVersionUID = 1;
52     
53     /**
54      * Loads a message.
55      *
56      * @param key key in Bundle.properties
57      * @return the message
58      */

59     private static String JavaDoc loc(String JavaDoc key) {
60         return NbBundle.getMessage(ScheduleAction.class, key);
61     }
62     
63     /**
64      * Schedule tasks.
65      */

66     public void performAction() {
67         // warn the user
68
String JavaDoc currentUser = System.getProperty("user.name"); // NOI18N
69
String JavaDoc message = NbBundle.getMessage(ScheduleAction.class,
70                 "AreYouSureToSchedule", currentUser); // NOI18N
71
String JavaDoc title = loc("Warning"); // NOI18N
72
NotifyDescriptor desc = new NotifyDescriptor.Confirmation(
73                 message, title, NotifyDescriptor.YES_NO_OPTION);
74         if (!NotifyDescriptor.YES_OPTION.equals(
75                 DialogDisplayer.getDefault().notify(desc)))
76             return;
77         
78         UserTaskView view = UserTaskViewRegistry.getInstance().getCurrent();
79         final List JavaDoc<UserTask> tasks = new ArrayList JavaDoc<UserTask>();
80         Set JavaDoc<String JavaDoc> users = new HashSet JavaDoc<String JavaDoc>();
81
82         UserTaskList utl = view.getUserTaskList();
83         users.addAll(Arrays.asList(utl.getOwners()));
84
85         // find all tasks with not computed values
86
UTListTreeAbstraction tree = new UTListTreeAbstraction(utl);
87         UnaryFunction f = new UnaryFunction() {
88             public Object JavaDoc compute(Object JavaDoc obj) {
89                 if (obj instanceof UserTask) {
90                     UserTask ut = (UserTask) obj;
91                     if (!ut.isValuesComputed() && !ut.isDone()) {
92                         tasks.add(ut);
93                     }
94                 }
95                 return null;
96             }
97         };
98         UTUtils.processDepthFirst(tree, f);
99
100         // sort them on priority
101
ScheduleUtils.createPriorityListBackflow(tasks);
102         
103         users.add(currentUser);
104         
105         String JavaDoc[] users_ = users.toArray(new String JavaDoc[users.size()]);
106         Arrays.sort(users_);
107         long[] firstFreeTime = new long[users_.length];
108         Calendar JavaDoc time = Calendar.getInstance();
109         alignTo15Min(time);
110         Arrays.fill(firstFreeTime, time.getTimeInMillis());
111         
112         Settings s = Settings.getDefault();
113         int daysPerWeek = s.getDaysPerWeek();
114         boolean[] wd = s.getWorkingDays();
115         
116         Calendar JavaDoc cal = Calendar.getInstance();
117         
118         for (int i = 0; i < tasks.size(); i++) {
119             UserTask ut = tasks.get(i);
120             if (ut.getOwner().trim().length() == 0)
121                 ut.setOwner(currentUser);
122             int index = Arrays.binarySearch(users_, ut.getOwner());
123             
124             cal.setTimeInMillis(firstFreeTime[index]);
125             Calendar JavaDoc start = Calendar.getInstance();
126             Calendar JavaDoc due = Calendar.getInstance();
127             alignTo15Min(cal);
128             add(cal, ut.getEffort(), start, due, wd);
129             ut.setStart(start.getTimeInMillis());
130             // ut.setDueDate(cal.getTime());
131
firstFreeTime[index] = cal.getTimeInMillis();
132         }
133     }
134
135     /**
136      * @param wd[7] working days (marked with true). [0] - monday
137      */

138     static final void add(Calendar JavaDoc cal, int minutes, Calendar JavaDoc start,
139             Calendar JavaDoc due, boolean[] wd) {
140         boolean first = true;
141         while (minutes > 0) {
142             int d = findNextWorkPeriod(cal, wd);
143             if (d > minutes)
144                 d = minutes;
145             due.setTime(cal.getTime());
146             due.add(Calendar.MINUTE, d);
147             if (first) {
148                 start.setTime(cal.getTime());
149                 first = false;
150             }
151             cal.setTime(due.getTime());
152             minutes -= d;
153         }
154     }
155     
156     /**
157      * Find next work period.
158      *
159      * @param cal current time
160      * @param wd[7] working days (marked with true). [0] - monday
161      * @return period duration in minutes
162      */

163     private static int findNextWorkPeriod(Calendar JavaDoc cal, boolean[] wd) {
164         scrollToWorkingDay(cal, wd);
165         
166         Calendar JavaDoc startWorkingDay = Calendar.getInstance();
167         startWorkingDay.setTime(cal.getTime());
168         setMinutes(startWorkingDay,
169                 Settings.getDefault().getWorkingDayStart());
170         
171         Calendar JavaDoc startPause = Calendar.getInstance();
172         startPause.setTime(cal.getTime());
173         setMinutes(startPause, Settings.getDefault().getPauseStart());
174         
175         Calendar JavaDoc endPause = Calendar.getInstance();
176         endPause.setTime(cal.getTime());
177         setMinutes(endPause, Settings.getDefault().getPauseEnd());
178         
179         Calendar JavaDoc endWorkingDay = Calendar.getInstance();
180         endWorkingDay.setTime(cal.getTime());
181         setMinutes(endWorkingDay, Settings.getDefault().getWorkingDayEnd());
182
183         Calendar JavaDoc end;
184         if (cal.compareTo(startWorkingDay) <= 0) {
185             cal.setTimeInMillis(startWorkingDay.getTimeInMillis());
186             end = startPause;
187         } else if (cal.compareTo(startPause) < 0) {
188             end = startPause;
189         } else if (cal.compareTo(endPause) <= 0) {
190             cal.setTimeInMillis(endPause.getTimeInMillis());
191             end = endWorkingDay;
192         } else if (cal.compareTo(endWorkingDay) < 0) {
193             end = endWorkingDay;
194         } else {
195             startWorkingDay.add(Calendar.DAY_OF_YEAR, 1);
196             scrollToWorkingDay(startWorkingDay, wd);
197             startPause.setTime(startWorkingDay.getTime());
198             setMinutes(startPause, Settings.getDefault().getPauseStart());
199             cal.setTime(startWorkingDay.getTime());
200             end = startPause;
201         }
202         return (int) ((end.getTimeInMillis() -
203             cal.getTimeInMillis()) / (1000 * 60));
204     }
205     
206     /**
207      * Aligns time to a 15min-boundary.
208      *
209      * @param cal a time point
210      */

211     private static void alignTo15Min(Calendar JavaDoc cal) {
212         int min = cal.get(Calendar.MINUTE);
213         min = (min + 14) / 15 * 15;
214         if (min == 60) {
215             cal.add(Calendar.HOUR_OF_DAY, 1);
216             cal.set(Calendar.MINUTE, 0);
217         } else {
218             cal.set(Calendar.MINUTE, min);
219         }
220         cal.set(Calendar.SECOND, 0);
221         cal.set(Calendar.MILLISECOND, 0);
222     }
223     
224     public String JavaDoc getName() {
225         return loc("Schedule"); // NOI18N
226
}
227
228     public HelpCtx getHelpCtx() {
229         return HelpCtx.DEFAULT_HELP;
230         // If you will provide context help then use:
231
// return new HelpCtx (MyAction.class);
232
}
233
234     /**
235      * Changes time of a Calendar.
236      *
237      * @param cal a calendar
238      * @param min offset in minutes from 00:00. The offset cannot be bigger
239      * than 60 * 24
240      */

241     private static void setMinutes(Calendar JavaDoc cal, int min) {
242         assert min <= 60 * 24;
243         cal.set(Calendar.HOUR_OF_DAY, min / 60);
244         cal.set(Calendar.MINUTE, min % 60);
245         cal.set(Calendar.SECOND, 0);
246         cal.set(Calendar.MILLISECOND, 0);
247     }
248
249     /**
250      * Scrolls cal to a working day.
251      *
252      * @param cal current time (will be changed)
253      * @param wd working days (marked with true). [0] - monday
254      */

255     private static void scrollToWorkingDay(Calendar JavaDoc cal, boolean[] wd) {
256         while (true) {
257             int dow = cal.get(Calendar.DAY_OF_WEEK);
258             if (dow == Calendar.SUNDAY)
259                 dow = 6;
260             else
261                 dow -= 2;
262             if (wd[dow])
263                 break;
264             cal.add(Calendar.DAY_OF_YEAR, 1);
265             setMinutes(cal, 0);
266         }
267     }
268 }
269
Popular Tags