KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > progress > FinishedJobs


1 /*******************************************************************************
2  * Copyright (c) 2003, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.progress;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.Comparator JavaDoc;
16 import java.util.Date JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.HashSet JavaDoc;
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 /**
27  * This singleton remembers all JobTreeElements that should be preserved (e.g.
28  * because their associated Jobs have the "keep" property set).
29  */

30 public class FinishedJobs extends EventManager {
31
32     /*
33      * Interface for notify listeners.
34      */

35     static interface KeptJobsListener {
36
37         /**
38          * A job to be kept has finished
39          *
40          * @param jte
41          */

42         void finished(JobTreeElement jte);
43
44         /**
45          * A kept job has been removed.
46          *
47          * @param jte
48          */

49         void removed(JobTreeElement jte);
50     }
51
52     private static FinishedJobs theInstance;
53
54     private IJobProgressManagerListener listener;
55
56     private HashSet JavaDoc keptjobinfos = new HashSet JavaDoc();
57
58     private HashMap JavaDoc finishedTime = new HashMap JavaDoc();
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     /**
108      * Returns true if JobInfo indicates that it must be kept.
109      */

110     static boolean keep(JobInfo info) {
111         Job job = info.getJob();
112         if (job != null) {
113             Object JavaDoc prop = job.getProperty(ProgressManagerUtil.KEEP_PROPERTY);
114             if (prop instanceof Boolean JavaDoc) {
115                 if (((Boolean JavaDoc) prop).booleanValue()) {
116                     return true;
117                 }
118             }
119
120             prop = job.getProperty(ProgressManagerUtil.KEEPONE_PROPERTY);
121             if (prop instanceof Boolean JavaDoc) {
122                 if (((Boolean JavaDoc) 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     /**
136      * Register for notification.
137      */

138     void addListener(KeptJobsListener l) {
139         addListenerObject(l);
140     }
141
142     /**
143      * Deregister for notification.
144      */

145     void removeListener(KeptJobsListener l) {
146         removeListenerObject(l);
147     }
148
149     private void checkForDuplicates(GroupInfo info) {
150         Object JavaDoc[] 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     /**
168      * Add given Job to list of kept jobs.
169      */

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 JavaDoc(now));
179
180                 Object JavaDoc parent = info.getParent();
181                 if (parent != null && !keptjobinfos.contains(parent)) {
182                     keptjobinfos.add(parent);
183                     finishedTime.put(parent, new Long JavaDoc(now));
184                 }
185
186                 fire = true;
187             }
188         }
189
190         if (fire) {
191             Object JavaDoc 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 JavaDoc 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 JavaDoc prop = myJob
221                         .getProperty(ProgressManagerUtil.KEEPONE_PROPERTY);
222                 if (prop instanceof Boolean JavaDoc && ((Boolean JavaDoc) prop).booleanValue()) {
223                     ArrayList JavaDoc 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 JavaDoc();
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 JavaDoc(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 JavaDoc 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                 // delete all elements that have jte as their direct or indirect
317
// parent
318
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             // notify listeners
338
Object JavaDoc 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     /**
348      * Returns all kept elements sorted by finished date.
349      */

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 JavaDoc() {
361             public int compare(Object JavaDoc o1, Object JavaDoc 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 JavaDoc o = finishedTime.get(jte);
378         if (o instanceof Long JavaDoc) {
379             return ((Long JavaDoc) o).longValue();
380         }
381         return 0;
382     }
383
384     /**
385      * Get the date that indicates the finish time.
386      *
387      * @param jte
388      * @return Date
389      */

390     public Date JavaDoc getFinishDate(JobTreeElement jte) {
391         Object JavaDoc o = finishedTime.get(jte);
392         if (o instanceof Long JavaDoc) {
393             return new Date JavaDoc(((Long JavaDoc) o).longValue());
394         }
395         return null;
396     }
397
398     /**
399      * Return whether or not the kept infos have the element.
400      *
401      * @param element
402      * @return boolean
403      */

404     public boolean isFinished(JobTreeElement element) {
405         return keptjobinfos.contains(element);
406     }
407
408     /**
409      * Clear all kept jobs.
410      */

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         // notify listeners
423
Object JavaDoc 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