1 19 20 package org.netbeans.modules.tasklist.usertasks.actions; 21 22 import java.util.ArrayList ; 23 import java.util.Arrays ; 24 import java.util.Calendar ; 25 import java.util.Collections ; 26 import java.util.Comparator ; 27 import java.util.HashSet ; 28 import java.util.List ; 29 import java.util.Set ; 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 50 public final class ScheduleAction extends CallableSystemAction { 51 private static final long serialVersionUID = 1; 52 53 59 private static String loc(String key) { 60 return NbBundle.getMessage(ScheduleAction.class, key); 61 } 62 63 66 public void performAction() { 67 String currentUser = System.getProperty("user.name"); String message = NbBundle.getMessage(ScheduleAction.class, 70 "AreYouSureToSchedule", currentUser); String title = loc("Warning"); 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 <UserTask> tasks = new ArrayList <UserTask>(); 80 Set <String > users = new HashSet <String >(); 81 82 UserTaskList utl = view.getUserTaskList(); 83 users.addAll(Arrays.asList(utl.getOwners())); 84 85 UTListTreeAbstraction tree = new UTListTreeAbstraction(utl); 87 UnaryFunction f = new UnaryFunction() { 88 public Object compute(Object 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 ScheduleUtils.createPriorityListBackflow(tasks); 102 103 users.add(currentUser); 104 105 String [] users_ = users.toArray(new String [users.size()]); 106 Arrays.sort(users_); 107 long[] firstFreeTime = new long[users_.length]; 108 Calendar 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 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 start = Calendar.getInstance(); 126 Calendar due = Calendar.getInstance(); 127 alignTo15Min(cal); 128 add(cal, ut.getEffort(), start, due, wd); 129 ut.setStart(start.getTimeInMillis()); 130 firstFreeTime[index] = cal.getTimeInMillis(); 132 } 133 } 134 135 138 static final void add(Calendar cal, int minutes, Calendar start, 139 Calendar 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 163 private static int findNextWorkPeriod(Calendar cal, boolean[] wd) { 164 scrollToWorkingDay(cal, wd); 165 166 Calendar startWorkingDay = Calendar.getInstance(); 167 startWorkingDay.setTime(cal.getTime()); 168 setMinutes(startWorkingDay, 169 Settings.getDefault().getWorkingDayStart()); 170 171 Calendar startPause = Calendar.getInstance(); 172 startPause.setTime(cal.getTime()); 173 setMinutes(startPause, Settings.getDefault().getPauseStart()); 174 175 Calendar endPause = Calendar.getInstance(); 176 endPause.setTime(cal.getTime()); 177 setMinutes(endPause, Settings.getDefault().getPauseEnd()); 178 179 Calendar endWorkingDay = Calendar.getInstance(); 180 endWorkingDay.setTime(cal.getTime()); 181 setMinutes(endWorkingDay, Settings.getDefault().getWorkingDayEnd()); 182 183 Calendar 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 211 private static void alignTo15Min(Calendar 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 getName() { 225 return loc("Schedule"); } 227 228 public HelpCtx getHelpCtx() { 229 return HelpCtx.DEFAULT_HELP; 230 } 233 234 241 private static void setMinutes(Calendar 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 255 private static void scrollToWorkingDay(Calendar 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 |