001: /*******************************************************************************
002: * Copyright (c) 2003, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: * Michael Fraenkel <fraenkel@us.ibm.com> - Fix for bug 60698 -
011: * [Progress] ConcurrentModificationException in NewProgressViewer.
012: *******************************************************************************/package org.eclipse.ui.internal.progress;
013:
014: import java.util.ArrayList;
015: import java.util.List;
016:
017: import org.eclipse.core.runtime.IProgressMonitor;
018: import org.eclipse.osgi.util.NLS;
019:
020: /**
021: * The GroupInfo is the object used to display group properties.
022: */
023:
024: class GroupInfo extends JobTreeElement implements IProgressMonitor {
025:
026: private List infos = new ArrayList();
027:
028: private Object lock = new Object();
029:
030: private String taskName;
031:
032: boolean isActive = false;
033:
034: double total = -1;
035:
036: double currentWork;
037:
038: /*
039: * (non-Javadoc)
040: *
041: * @see org.eclipse.ui.internal.progress.JobTreeElement#getParent()
042: */
043: Object getParent() {
044: return null;
045: }
046:
047: /*
048: * (non-Javadoc)
049: *
050: * @see org.eclipse.ui.internal.progress.JobTreeElement#hasChildren()
051: */
052: boolean hasChildren() {
053: synchronized (lock) {
054: return !infos.isEmpty();
055: }
056:
057: }
058:
059: /*
060: * (non-Javadoc)
061: *
062: * @see org.eclipse.ui.internal.progress.JobTreeElement#getChildren()
063: */
064: Object[] getChildren() {
065: synchronized (lock) {
066: return infos.toArray();
067: }
068: }
069:
070: /*
071: * (non-Javadoc)
072: *
073: * @see org.eclipse.ui.internal.progress.JobTreeElement#getDisplayString()
074: */
075: String getDisplayString() {
076: if (total < 0) {
077: return taskName;
078: }
079: String[] messageValues = new String[2];
080: messageValues[0] = taskName;
081: messageValues[1] = String.valueOf(getPercentDone());
082: return NLS.bind(ProgressMessages.JobInfo_NoTaskNameDoneMessage,
083: messageValues);
084:
085: }
086:
087: /**
088: * Return an integer representing the amount of work completed.
089: *
090: * @return int
091: */
092: int getPercentDone() {
093: return (int) (currentWork * 100 / total);
094: }
095:
096: /*
097: * (non-Javadoc)
098: *
099: * @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 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 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 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 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[] 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 getTaskName() {
266: return taskName;
267: }
268:
269: }
|