KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Michael Fraenkel <fraenkel@us.ibm.com> - Fix for bug 60698 -
11  * [Progress] ConcurrentModificationException in NewProgressViewer.
12  *******************************************************************************/

13 package org.eclipse.ui.internal.progress;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.osgi.util.NLS;
20
21 /**
22  * The GroupInfo is the object used to display group properties.
23  */

24
25 class GroupInfo extends JobTreeElement implements IProgressMonitor {
26
27     private List JavaDoc infos = new ArrayList JavaDoc();
28
29     private Object JavaDoc lock = new Object JavaDoc();
30
31     private String JavaDoc taskName;
32
33     boolean isActive = false;
34
35     double total = -1;
36
37     double currentWork;
38
39     /*
40      * (non-Javadoc)
41      *
42      * @see org.eclipse.ui.internal.progress.JobTreeElement#getParent()
43      */

44     Object JavaDoc getParent() {
45         return null;
46     }
47
48     /*
49      * (non-Javadoc)
50      *
51      * @see org.eclipse.ui.internal.progress.JobTreeElement#hasChildren()
52      */

53     boolean hasChildren() {
54         synchronized (lock) {
55             return !infos.isEmpty();
56         }
57
58     }
59
60     /*
61      * (non-Javadoc)
62      *
63      * @see org.eclipse.ui.internal.progress.JobTreeElement#getChildren()
64      */

65     Object JavaDoc[] getChildren() {
66         synchronized (lock) {
67             return infos.toArray();
68         }
69     }
70
71     /*
72      * (non-Javadoc)
73      *
74      * @see org.eclipse.ui.internal.progress.JobTreeElement#getDisplayString()
75      */

76     String JavaDoc getDisplayString() {
77         if (total < 0) {
78             return taskName;
79         }
80         String JavaDoc[] messageValues = new String JavaDoc[2];
81         messageValues[0] = taskName;
82         messageValues[1] = String.valueOf(getPercentDone());
83         return NLS.bind(ProgressMessages.JobInfo_NoTaskNameDoneMessage, messageValues);
84
85     }
86
87     /**
88      * Return an integer representing the amount of work completed.
89      *
90      * @return int
91      */

92     int getPercentDone() {
93         return (int) (currentWork * 100 / total);
94     }
95
96     /*
97      * (non-Javadoc)
98      *
99      * @see org.eclipse.ui.internal.progress.JobTreeElement#isJobInfo()
100      */

101     boolean isJobInfo() {
102         return false;
103     }
104
105     /*
106      * (non-Javadoc)
107      *
108      * @see java.lang.Comparable#compareTo(java.lang.Object)
109      */

110     public int compareTo(Object JavaDoc arg0) {
111         return getDisplayString().compareTo(
112                 ((JobTreeElement) arg0).getDisplayString());
113     }
114
115     /*
116      * (non-Javadoc)
117      *
118      * @see org.eclipse.core.runtime.IProgressMonitor#beginTask(java.lang.String,
119      * int)
120      */

121     public void beginTask(String JavaDoc name, int totalWork) {
122         taskName = name;
123         total = totalWork;
124         synchronized (lock) {
125             isActive = true;
126         }
127         ProgressManager.getInstance().refreshGroup(this);
128
129     }
130
131     /*
132      * (non-Javadoc)
133      *
134      * @see org.eclipse.core.runtime.IProgressMonitor#done()
135      */

136     public void done() {
137         synchronized (lock) {
138             isActive = false;
139         }
140         ProgressManager.getInstance().removeGroup(this);
141
142     }
143
144     /*
145      * (non-Javadoc)
146      *
147      * @see org.eclipse.core.runtime.IProgressMonitor#internalWorked(double)
148      */

149     public void internalWorked(double work) {
150         synchronized (lock) {
151             currentWork += work;
152         }
153
154     }
155
156     /*
157      * (non-Javadoc)
158      *
159      * @see org.eclipse.core.runtime.IProgressMonitor#isCanceled()
160      */

161     public boolean isCanceled() {
162         //Just a group so no cancel state
163
return false;
164     }
165
166     /*
167      * (non-Javadoc)
168      *
169      * @see org.eclipse.core.runtime.IProgressMonitor#setCanceled(boolean)
170      */

171     public void setCanceled(boolean value) {
172         cancel();
173     }
174
175     /*
176      * (non-Javadoc)
177      *
178      * @see org.eclipse.core.runtime.IProgressMonitor#setTaskName(java.lang.String)
179      */

180     public void setTaskName(String JavaDoc name) {
181         synchronized (this) {
182             isActive = true;
183         }
184         taskName = name;
185
186     }
187
188     /*
189      * (non-Javadoc)
190      *
191      * @see org.eclipse.core.runtime.IProgressMonitor#subTask(java.lang.String)
192      */

193     public void subTask(String JavaDoc name) {
194         //Not interesting for this monitor
195
}
196
197     /*
198      * (non-Javadoc)
199      *
200      * @see org.eclipse.core.runtime.IProgressMonitor#worked(int)
201      */

202     public void worked(int work) {
203         internalWorked(work);
204     }
205
206     /**
207      * Remove the job from the list of jobs.
208      *
209      * @param job
210      */

211     void removeJobInfo(final JobInfo job) {
212         synchronized (lock) {
213             infos.remove(job);
214             if (infos.isEmpty()) {
215                 done();
216             }
217         }
218     }
219
220     /**
221      * Remove the job from the list of jobs.
222      *
223      * @param job
224      */

225     void addJobInfo(final JobInfo job) {
226         synchronized (lock) {
227             infos.add(job);
228         }
229     }
230
231     /*
232      * (non-Javadoc)
233      *
234      * @see org.eclipse.ui.internal.progress.JobTreeElement#isActive()
235      */

236     boolean isActive() {
237         return isActive;
238     }
239
240     /*
241      * (non-Javadoc)
242      *
243      * @see org.eclipse.ui.internal.progress.JobTreeElement#cancel()
244      */

245     public void cancel() {
246         Object JavaDoc[] jobInfos = getChildren();
247         for (int i = 0; i < jobInfos.length; i++) {
248             ((JobInfo) jobInfos[i]).cancel();
249         }
250     }
251
252     /*
253      * (non-Javadoc)
254      *
255      * @see org.eclipse.ui.internal.progress.JobTreeElement#isCancellable()
256      */

257     public boolean isCancellable() {
258         return true;
259     }
260
261     /**
262      * Get the task name for the receiver.
263      * @return String
264      */

265     String JavaDoc getTaskName() {
266         return taskName;
267     }
268
269 }
270
Popular Tags