KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2003, 2006 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.Collections JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.jobs.Job;
20 import org.eclipse.jface.resource.JFaceResources;
21 import org.eclipse.osgi.util.NLS;
22 import org.eclipse.swt.graphics.Image;
23
24 /**
25  * JobInfo is the class that keeps track of the tree structure for objects that
26  * display job status in a tree.
27  */

28 class JobInfo extends JobTreeElement {
29
30     private IStatus blockedStatus;
31
32     private boolean canceled = false;
33     private List JavaDoc children = Collections.synchronizedList(new ArrayList JavaDoc());
34
35     private Job job;
36
37     private GroupInfo parent;
38
39     private TaskInfo taskInfo;
40
41     //Default to no progress
42
private int ticks = -1;
43
44     /**
45      * Create a top level JobInfo.
46      *
47      * @param enclosingJob
48      */

49     JobInfo(Job enclosingJob) {
50         this.job = enclosingJob;
51     }
52
53     /**
54      * Add the subtask to the receiver.
55      *
56      * @param subTaskName
57      */

58     void addSubTask(String JavaDoc subTaskName) {
59         children.add(new SubTaskInfo(this, subTaskName));
60     }
61
62     /**
63      * Add the amount of work to the job info.
64      *
65      * @param workIncrement
66      */

67     void addWork(double workIncrement) {
68         if (taskInfo == null) {
69             return;
70         }
71         if (parent == null || ticks < 1) {
72             taskInfo.addWork(workIncrement);
73         } else {
74             taskInfo.addWork(workIncrement, parent, ticks);
75         }
76     }
77
78     /**
79      * Begin the task called taskName with the supplied work.
80      *
81      * @param taskName
82      * @param work
83      */

84     void beginTask(String JavaDoc taskName, int work) {
85         taskInfo = new TaskInfo(this, taskName, work);
86     }
87
88     /*
89      * (non-Javadoc)
90      *
91      * @see org.eclipse.ui.internal.progress.JobTreeElement#cancel()
92      */

93     public void cancel() {
94         this.canceled = true;
95         this.job.cancel();
96         //Call the refresh so that this is updated immediately
97
ProgressManager.getInstance().refreshJobInfo(this);
98     }
99
100     /**
101      * Clear the collection of subtasks an the task info.
102      */

103     void clearChildren() {
104         children.clear();
105     }
106
107     /*
108      * (non-Javadoc)
109      *
110      * @see org.eclipse.ui.internal.progress.JobTreeElement#isJobInfo()
111      */

112     void clearTaskInfo() {
113         taskInfo = null;
114     }
115
116     /**
117      * Compare the the job of the receiver to job2.
118      *
119      * @param jobInfo
120      * The info we are comparing to
121      * @return @see Comparable#compareTo(java.lang.Object)
122      */

123     private int compareJobs(JobInfo jobInfo) {
124
125         Job job2 = jobInfo.getJob();
126
127         //User jobs have top priority
128
if (job.isUser()) {
129             if (!job2.isUser()) {
130                 return -1;
131             }
132         } else {
133             if (job2.isUser()) {
134                 return 1;
135             }
136         }
137
138         //Show the blocked ones last
139
if (isBlocked()) {
140             if (!jobInfo.isBlocked()) {
141                 return 1;
142             }
143         } else {
144             if (jobInfo.isBlocked()) {
145                 return -1;
146             }
147         }
148
149         if (job.getPriority() == job2.getPriority()) {
150             return job.getName().compareTo(job2.getName());
151         }
152
153         if (job.getPriority() > job2.getPriority()) {
154             return -1;
155         }
156         return 1;
157     }
158
159     /*
160      * (non-Javadoc)
161      *
162      * @see java.lang.Comparable#compareTo(java.lang.Object)
163      */

164     public int compareTo(Object JavaDoc arg0) {
165
166         if (!(arg0 instanceof JobInfo)) {
167             return super.compareTo(arg0);
168         }
169         JobInfo element = (JobInfo) arg0;
170
171         //If the receiver is cancelled then it is lowest priority
172
if (isCanceled() && !element.isCanceled()) {
173             return 1;
174         }
175
176         if (element.getJob().getState() == getJob().getState()) {
177             return compareJobs(element);
178         }
179
180         if (getJob().getState() == Job.RUNNING) {
181             return -1;
182         }
183         return 1;
184
185     }
186
187     /**
188      * Dispose of the receiver.
189      */

190     void dispose() {
191         if (parent != null) {
192             parent.removeJobInfo(this);
193         }
194     }
195
196     /**
197      * Return the blocked status or <code>null</code> if there isn't one.
198      *
199      * @return Returns the blockedStatus.
200      */

201     public IStatus getBlockedStatus() {
202         return blockedStatus;
203     }
204
205     /*
206      * (non-Javadoc)
207      *
208      * @see org.eclipse.ui.internal.progress.JobTreeElement#getChildren()
209      */

210     Object JavaDoc[] getChildren() {
211         return children.toArray();
212     }
213
214     /*
215      * (non-Javadoc)
216      *
217      * @see org.eclipse.ui.internal.progress.JobTreeElement#getCondensedDisplayString()
218      */

219     String JavaDoc getCondensedDisplayString() {
220         TaskInfo info = getTaskInfo();
221         if (info != null) {
222             return info.getDisplayStringWithoutTask(true);
223         }
224         return getJob().getName();
225     }
226
227     /*
228      * (non-Javadoc)
229      *
230      * @see org.eclipse.ui.internal.progress.JobTreeElement#getDisplayImage()
231      */

232     public Image getDisplayImage() {
233         int done = getPercentDone();
234         if (done > 0) {
235             return super.getDisplayImage();
236         }
237         if (isBlocked()) {
238             return JFaceResources.getImage(ProgressManager.BLOCKED_JOB_KEY);
239         }
240         int state = getJob().getState();
241         if (state == Job.SLEEPING) {
242             return JFaceResources.getImage(ProgressManager.SLEEPING_JOB_KEY);
243         }
244         if (state == Job.WAITING) {
245             return JFaceResources.getImage(ProgressManager.WAITING_JOB_KEY);
246         }
247         //By default return the first progress image
248
return super.getDisplayImage();
249
250     }
251     /* (non-Javadoc)
252      * @see org.eclipse.ui.internal.progress.JobTreeElement#getDisplayString()
253      */

254     String JavaDoc getDisplayString() {
255         return getDisplayString(true);
256     }
257
258     /* (non-Javadoc)
259      * @see org.eclipse.ui.internal.progress.JobTreeElement#getDisplayString(boolean)
260      */

261     String JavaDoc getDisplayString(boolean showProgress) {
262         String JavaDoc name = getDisplayStringWithStatus(showProgress);
263         if (job.isSystem()) {
264             return NLS.bind(ProgressMessages.JobInfo_System, (new Object JavaDoc[] { name }));
265         }
266         return name;
267     }
268
269     /**
270      * Get the display string based on the current status and the name of the
271      * job.
272      * @param showProgress a boolean to indicate if we should
273      * show progress or not.
274      *
275      * @return String
276      */

277     private String JavaDoc getDisplayStringWithStatus(boolean showProgress) {
278         if (isCanceled()) {
279             return NLS.bind(ProgressMessages.JobInfo_Cancelled, (new Object JavaDoc[] { getJob().getName() }));
280         }
281         if (isBlocked()) {
282             return NLS.bind(ProgressMessages.JobInfo_Blocked, (new Object JavaDoc[] { getJob().getName(),
283             blockedStatus.getMessage() }));
284         }
285         if (getJob().getState() == Job.RUNNING) {
286             if (taskInfo == null) {
287                 return getJob().getName();
288             }
289             return taskInfo.getDisplayString(showProgress);
290         }
291         if (getJob().getState() == Job.SLEEPING) {
292             return NLS.bind(ProgressMessages.JobInfo_Sleeping, (new Object JavaDoc[] { getJob().getName() }));
293         }
294
295         return NLS.bind(ProgressMessages.JobInfo_Waiting, (new Object JavaDoc[] { getJob().getName() }));
296
297     }
298
299     /**
300      * Return the GroupInfo for the receiver if it' is active.
301      *
302      * @return GroupInfo or <code>null</code>.
303      */

304     GroupInfo getGroupInfo() {
305         if (parent != null && parent.isActive()) {
306             return parent;
307         }
308         return null;
309     }
310
311     /**
312      * Return the job that the receiver is collecting data on.
313      *
314      * @return Job
315      */

316     Job getJob() {
317         return job;
318     }
319
320     /*
321      * (non-Javadoc)
322      *
323      * @see org.eclipse.ui.internal.progress.JobTreeElement#getParent()
324      */

325     Object JavaDoc getParent() {
326         return parent;
327     }
328
329     /**
330      * Return the amount of progress we have had as a percentage. If there is no
331      * progress or it is indeterminate return IProgressMonitor.UNKNOWN.
332      *
333      * @return int
334      */

335     int getPercentDone() {
336         TaskInfo info = getTaskInfo();
337         if (info != null){
338             if(info.totalWork == IProgressMonitor.UNKNOWN) {
339                 return IProgressMonitor.UNKNOWN;
340             }
341             if(info.totalWork == 0) {
342                 return 0;
343             }
344             return (int) info.preWork * 100 / info.totalWork;
345         }
346         return IProgressMonitor.UNKNOWN;
347     }
348
349     /**
350      * @return Returns the taskInfo.
351      */

352     TaskInfo getTaskInfo() {
353         return taskInfo;
354     }
355
356     /*
357      * (non-Javadoc)
358      *
359      * @see org.eclipse.ui.internal.progress.JobTreeElement#hasChildren()
360      */

361     boolean hasChildren() {
362         return children.size() > 0;
363     }
364
365     /**
366      * Return whether or not there is a task.
367      *
368      * @return boolean
369      */

370     boolean hasTaskInfo() {
371         return taskInfo != null;
372     }
373
374     /*
375      * (non-Javadoc)
376      *
377      * @see org.eclipse.ui.internal.progress.JobTreeElement#isActive()
378      */

379     boolean isActive() {
380         return getJob().getState() != Job.NONE;
381     }
382
383     /**
384      * Return whether or not the receiver is blocked.
385      *
386      * @return boolean <code>true</code> if this is a currently
387      * blocked job.
388      */

389     public boolean isBlocked() {
390         return getBlockedStatus() != null;
391     }
392
393     /**
394      * Return whether or not the job was cancelled in the UI.
395      *
396      * @return boolean
397      */

398     public boolean isCanceled() {
399         return canceled;
400     }
401
402     /*
403      * (non-Javadoc)
404      *
405      * @see org.eclipse.ui.internal.progress.JobTreeElement#isCancellable()
406      */

407     public boolean isCancellable() {
408         return super.isCancellable();
409     }
410
411     /*
412      * (non-Javadoc)
413      *
414      * @see org.eclipse.ui.internal.progress.JobTreeElement#isJobInfo()
415      */

416     boolean isJobInfo() {
417         return true;
418     }
419
420     /**
421      * Set the description of the blocking status.
422      *
423      * @param blockedStatus
424      * The IStatus that describes the blockage or <code>null</code>
425      */

426     public void setBlockedStatus(IStatus blockedStatus) {
427         this.blockedStatus = blockedStatus;
428     }
429
430     /**
431      * Set the GroupInfo to be the group.
432      *
433      * @param group
434      */

435     void setGroupInfo(GroupInfo group) {
436         parent = group;
437     }
438
439     /**
440      * Set the name of the taskInfo.
441      *
442      * @param name
443      */

444     void setTaskName(String JavaDoc name) {
445         taskInfo.setTaskName(name);
446     }
447
448     /**
449      * Set the number of ticks this job represents. Default is indeterminate
450      * (-1).
451      *
452      * @param ticks
453      * The ticks to set.
454      */

455     public void setTicks(int ticks) {
456         this.ticks = ticks;
457     }
458
459 }
460
Popular Tags