1 11 package org.eclipse.ui.internal.progress; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.Comparator ; 16 import java.util.Date ; 17 import java.util.HashMap ; 18 import java.util.HashSet ; 19 20 import org.eclipse.core.commands.common.EventManager; 21 import org.eclipse.core.runtime.IStatus; 22 import org.eclipse.core.runtime.jobs.Job; 23 import org.eclipse.ui.actions.ActionFactory; 24 import org.eclipse.ui.progress.IProgressConstants; 25 26 30 public class FinishedJobs extends EventManager { 31 32 35 static interface KeptJobsListener { 36 37 42 void finished(JobTreeElement jte); 43 44 49 void removed(JobTreeElement jte); 50 } 51 52 private static FinishedJobs theInstance; 53 54 private IJobProgressManagerListener listener; 55 56 private HashSet keptjobinfos = new HashSet (); 57 58 private HashMap finishedTime = new HashMap (); 59 60 private static JobTreeElement[] EMPTY_INFOS; 61 62 public static synchronized FinishedJobs getInstance() { 63 if (theInstance == null) { 64 theInstance = new FinishedJobs(); 65 EMPTY_INFOS = new JobTreeElement[0]; 66 } 67 return theInstance; 68 } 69 70 private FinishedJobs() { 71 listener = new IJobProgressManagerListener() { 72 public void addJob(JobInfo info) { 73 checkForDuplicates(info); 74 } 75 76 public void addGroup(GroupInfo info) { 77 checkForDuplicates(info); 78 } 79 80 public void refreshJobInfo(JobInfo info) { 81 checkTasks(info); 82 } 83 84 public void refreshGroup(GroupInfo info) { 85 } 86 87 public void refreshAll() { 88 } 89 90 public void removeJob(JobInfo info) { 91 if (keep(info)) { 92 checkForDuplicates(info); 93 add(info); 94 } 95 } 96 97 public void removeGroup(GroupInfo group) { 98 } 99 100 public boolean showsDebug() { 101 return false; 102 } 103 }; 104 ProgressManager.getInstance().addListener(listener); 105 } 106 107 110 static boolean keep(JobInfo info) { 111 Job job = info.getJob(); 112 if (job != null) { 113 Object prop = job.getProperty(ProgressManagerUtil.KEEP_PROPERTY); 114 if (prop instanceof Boolean ) { 115 if (((Boolean ) prop).booleanValue()) { 116 return true; 117 } 118 } 119 120 prop = job.getProperty(ProgressManagerUtil.KEEPONE_PROPERTY); 121 if (prop instanceof Boolean ) { 122 if (((Boolean ) prop).booleanValue()) { 123 return true; 124 } 125 } 126 127 IStatus status = job.getResult(); 128 if (status != null && status.getSeverity() == IStatus.ERROR) { 129 return true; 130 } 131 } 132 return false; 133 } 134 135 138 void addListener(KeptJobsListener l) { 139 addListenerObject(l); 140 } 141 142 145 void removeListener(KeptJobsListener l) { 146 removeListenerObject(l); 147 } 148 149 private void checkForDuplicates(GroupInfo info) { 150 Object [] objects = info.getChildren(); 151 for (int i = 0; i < objects.length; i++) { 152 if (objects[i] instanceof JobInfo) { 153 checkForDuplicates((JobInfo) objects[i]); 154 } 155 } 156 } 157 158 private void checkForDuplicates(JobTreeElement info) { 159 JobTreeElement[] toBeRemoved = findJobsToRemove(info); 160 if (toBeRemoved != null) { 161 for (int i = 0; i < toBeRemoved.length; i++) { 162 remove(toBeRemoved[i]); 163 } 164 } 165 } 166 167 170 private void add(JobInfo info) { 171 boolean fire = false; 172 173 synchronized (keptjobinfos) { 174 if (!keptjobinfos.contains(info)) { 175 keptjobinfos.add(info); 176 177 long now = System.currentTimeMillis(); 178 finishedTime.put(info, new Long (now)); 179 180 Object parent = info.getParent(); 181 if (parent != null && !keptjobinfos.contains(parent)) { 182 keptjobinfos.add(parent); 183 finishedTime.put(parent, new Long (now)); 184 } 185 186 fire = true; 187 } 188 } 189 190 if (fire) { 191 Object l[] = getListeners(); 192 for (int i = 0; i < l.length; i++) { 193 KeptJobsListener jv = (KeptJobsListener) l[i]; 194 jv.finished(info); 195 } 196 } 197 } 198 199 static void disposeAction(JobTreeElement jte) { 200 if (jte.isJobInfo()) { 201 JobInfo ji = (JobInfo) jte; 202 Job job = ji.getJob(); 203 if (job != null) { 204 Object prop = job 205 .getProperty(IProgressConstants.ACTION_PROPERTY); 206 if (prop instanceof ActionFactory.IWorkbenchAction) { 207 ((ActionFactory.IWorkbenchAction) prop).dispose(); 208 } 209 } 210 } 211 } 212 213 private JobTreeElement[] findJobsToRemove(JobTreeElement info) { 214 215 if (info.isJobInfo()) { 216 Job myJob = ((JobInfo) info).getJob(); 217 218 if (myJob != null) { 219 220 Object prop = myJob 221 .getProperty(ProgressManagerUtil.KEEPONE_PROPERTY); 222 if (prop instanceof Boolean && ((Boolean ) prop).booleanValue()) { 223 ArrayList found = null; 224 JobTreeElement[] all; 225 synchronized (keptjobinfos) { 226 all = (JobTreeElement[]) keptjobinfos 227 .toArray(new JobTreeElement[keptjobinfos.size()]); 228 } 229 for (int i = 0; i < all.length; i++) { 230 JobTreeElement jte = all[i]; 231 if (jte != info && jte.isJobInfo()) { 232 Job job = ((JobInfo) jte).getJob(); 233 if (job != null && job != myJob 234 && job.belongsTo(myJob)) { 235 if (found == null) { 236 found = new ArrayList (); 237 } 238 found.add(jte); 239 } 240 } 241 } 242 if (found != null) { 243 return (JobTreeElement[]) found 244 .toArray(new JobTreeElement[found.size()]); 245 } 246 } 247 } 248 } 249 return null; 250 } 251 252 private void checkTasks(JobInfo info) { 253 if (keep(info)) { 254 TaskInfo tinfo = info.getTaskInfo(); 255 if (tinfo != null) { 256 JobTreeElement[] toBeRemoved = null; 257 boolean fire = false; 258 JobTreeElement element = (JobTreeElement) tinfo.getParent(); 259 synchronized (keptjobinfos) { 260 if (element == info && !keptjobinfos.contains(tinfo)) { 261 toBeRemoved = findJobsToRemove(element); 262 keptjobinfos.add(tinfo); 263 finishedTime.put(tinfo, new Long (System 264 .currentTimeMillis())); 265 } 266 } 267 268 if (toBeRemoved != null) { 269 for (int i = 0; i < toBeRemoved.length; i++) { 270 remove(toBeRemoved[i]); 271 } 272 } 273 274 if (fire) { 275 Object l[] = getListeners(); 276 for (int i = 0; i < l.length; i++) { 277 KeptJobsListener jv = (KeptJobsListener) l[i]; 278 jv.finished(info); 279 } 280 } 281 } 282 } 283 } 284 285 public void removeErrorJobs() { 286 JobTreeElement[] infos = getJobInfos(); 287 for (int i = 0; i < infos.length; i++) { 288 if (infos[i].isJobInfo()) { 289 JobInfo info1 = (JobInfo) infos[i]; 290 Job job = info1.getJob(); 291 if (job != null) { 292 IStatus status = job.getResult(); 293 if (status != null && status.getSeverity() == IStatus.ERROR) { 294 JobTreeElement topElement = (JobTreeElement) info1 295 .getParent(); 296 if (topElement == null) { 297 topElement = info1; 298 } 299 FinishedJobs.getInstance().remove(topElement); 300 } 301 } 302 } 303 } 304 } 305 306 boolean remove(JobTreeElement jte) { 307 boolean fire = false; 308 boolean removed = false; 309 310 synchronized (keptjobinfos) { 311 if (keptjobinfos.remove(jte)) { 312 removed = true; 313 finishedTime.remove(jte); 314 disposeAction(jte); 315 316 JobTreeElement jtes[] = (JobTreeElement[]) keptjobinfos 319 .toArray(new JobTreeElement[keptjobinfos.size()]); 320 for (int i = 0; i < jtes.length; i++) { 321 JobTreeElement parent = (JobTreeElement) jtes[i] 322 .getParent(); 323 if (parent != null) { 324 if (parent == jte || parent.getParent() == jte) { 325 if (keptjobinfos.remove(jtes[i])) { 326 disposeAction(jtes[i]); 327 } 328 finishedTime.remove(jtes[i]); 329 } 330 } 331 } 332 fire = true; 333 } 334 } 335 336 if (fire) { 337 Object l[] = getListeners(); 339 for (int i = 0; i < l.length; i++) { 340 KeptJobsListener jv = (KeptJobsListener) l[i]; 341 jv.removed(jte); 342 } 343 } 344 return removed; 345 } 346 347 350 JobTreeElement[] getJobInfos() { 351 JobTreeElement[] all; 352 if (keptjobinfos.isEmpty()) { 353 return EMPTY_INFOS; 354 } 355 356 synchronized (keptjobinfos) { 357 all = (JobTreeElement[]) keptjobinfos 358 .toArray(new JobTreeElement[keptjobinfos.size()]); 359 } 360 Arrays.sort(all, new Comparator () { 361 public int compare(Object o1, Object o2) { 362 long t1 = getFinishedDateAsLong((JobTreeElement) o1); 363 long t2 = getFinishedDateAsLong((JobTreeElement) o2); 364 if (t1 < t2) { 365 return -1; 366 } 367 if (t1 > t2) { 368 return 1; 369 } 370 return 0; 371 } 372 }); 373 return all; 374 } 375 376 private long getFinishedDateAsLong(JobTreeElement jte) { 377 Object o = finishedTime.get(jte); 378 if (o instanceof Long ) { 379 return ((Long ) o).longValue(); 380 } 381 return 0; 382 } 383 384 390 public Date getFinishDate(JobTreeElement jte) { 391 Object o = finishedTime.get(jte); 392 if (o instanceof Long ) { 393 return new Date (((Long ) o).longValue()); 394 } 395 return null; 396 } 397 398 404 public boolean isFinished(JobTreeElement element) { 405 return keptjobinfos.contains(element); 406 } 407 408 411 public void clearAll() { 412 synchronized (keptjobinfos) { 413 JobTreeElement[] all = (JobTreeElement[]) keptjobinfos 414 .toArray(new JobTreeElement[keptjobinfos.size()]); 415 for (int i = 0; i < all.length; i++) { 416 disposeAction(all[i]); 417 } 418 keptjobinfos.clear(); 419 finishedTime.clear(); 420 } 421 422 Object l[] = getListeners(); 424 for (int i = 0; i < l.length; i++) { 425 KeptJobsListener jv = (KeptJobsListener) l[i]; 426 jv.removed(null); 427 } 428 } 429 } 430 | Popular Tags |