001: /*******************************************************************************
002: * Copyright (c) 2004, 2006 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: *******************************************************************************/package org.eclipse.ui.internal.progress;
011:
012: import java.util.HashSet;
013: import java.util.Iterator;
014: import java.util.Set;
015:
016: import org.eclipse.core.runtime.IProgressMonitor;
017: import org.eclipse.core.runtime.IStatus;
018: import org.eclipse.core.runtime.Status;
019: import org.eclipse.core.runtime.jobs.Job;
020: import org.eclipse.ui.internal.progress.FinishedJobs.KeptJobsListener;
021: import org.eclipse.ui.progress.WorkbenchJob;
022:
023: /**
024: * The ProgressViewerContentProvider is the content provider progress viewers.
025: */
026: public class ProgressViewerContentProvider extends
027: ProgressContentProvider {
028: protected AbstractProgressViewer progressViewer;
029:
030: private KeptJobsListener keptJobListener;
031:
032: private Set keptJobs = new HashSet();
033:
034: /**
035: * Create a new instance of the receiver.
036: *
037: * @param structured
038: * The Viewer we are providing content for
039: * @param debug
040: * If true debug information will be shown
041: * if the debug flag in the ProgressManager is true.
042: * @param showFinished
043: * A boolean that indicates whether or not the finished jobs
044: * should be shown.
045: */
046: public ProgressViewerContentProvider(
047: AbstractProgressViewer structured, boolean debug,
048: boolean showFinished) {
049: super (debug);
050: progressViewer = structured;
051: if (showFinished) {
052: FinishedJobs.getInstance()
053: .addListener(getKeptJobListener());
054: }
055: }
056:
057: /**
058: * Return a listener for kept jobs.
059: *
060: * @return KeptJobsListener
061: */
062: private KeptJobsListener getKeptJobListener() {
063: keptJobListener = new KeptJobsListener() {
064:
065: /*
066: * (non-Javadoc)
067: *
068: * @see org.eclipse.ui.internal.progress.FinishedJobs.KeptJobsListener#finished(org.eclipse.ui.internal.progress.JobTreeElement)
069: */
070: public void finished(JobTreeElement jte) {
071: keptJobs.add(jte);
072: final JobTreeElement element = jte;
073: Job updateJob = new WorkbenchJob("Refresh finished") {//$NON-NLS-1$
074: /* (non-Javadoc)
075: * @see org.eclipse.ui.progress.UIJob#runInUIThread(org.eclipse.core.runtime.IProgressMonitor)
076: */
077: public IStatus runInUIThread(
078: IProgressMonitor monitor) {
079: refresh(new Object[] { element });
080: return Status.OK_STATUS;
081: }
082: };
083: updateJob.setSystem(true);
084: updateJob.schedule();
085:
086: }
087:
088: /*
089: * (non-Javadoc)
090: *
091: * @see org.eclipse.ui.internal.progress.FinishedJobs.KeptJobsListener#removed(org.eclipse.ui.internal.progress.JobTreeElement)
092: */
093: public void removed(JobTreeElement jte) {
094: //null indicates they are all removed
095: if (jte == null) {
096: keptJobs.clear();
097: } else {
098: keptJobs.remove(jte);
099: }
100: final JobTreeElement element = jte;
101: Job updateJob = new WorkbenchJob("Remove finished") {//$NON-NLS-1$
102: /* (non-Javadoc)
103: * @see org.eclipse.ui.progress.UIJob#runInUIThread(org.eclipse.core.runtime.IProgressMonitor)
104: */
105: public IStatus runInUIThread(
106: IProgressMonitor monitor) {
107: if (element == null) {
108: refresh();
109: } else {
110: ProgressViewerContentProvider.this
111: .remove(new Object[] { element });
112: }
113: return Status.OK_STATUS;
114: }
115: };
116: updateJob.setSystem(true);
117: updateJob.schedule();
118:
119: }
120:
121: };
122: return keptJobListener;
123: }
124:
125: /*
126: * (non-Javadoc)
127: *
128: * @see org.eclipse.ui.internal.progress.IProgressUpdateCollector#refresh()
129: */
130: public void refresh() {
131: progressViewer.refresh(true);
132: }
133:
134: /*
135: * (non-Javadoc)
136: *
137: * @see org.eclipse.ui.internal.progress.IProgressUpdateCollector#refresh(org.eclipse.ui.internal.progress.JobTreeElement[])
138: */
139: public void refresh(Object[] elements) {
140: Object[] refreshes = getRoots(elements, true);
141: for (int i = 0; i < refreshes.length; i++) {
142: progressViewer.refresh(refreshes[i], true);
143: }
144: }
145:
146: /*
147: * (non-Javadoc)
148: *
149: * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
150: */
151: public Object[] getElements(Object inputElement) {
152: Object[] elements = super .getElements(inputElement);
153:
154: if (keptJobs.size() == 0) {
155: return elements;
156: }
157: if (elements.length == 0) {
158: return keptJobs.toArray();
159: }
160:
161: Set all = new HashSet();
162:
163: for (int i = 0; i < elements.length; i++) {
164: Object element = elements[i];
165: all.add(element);
166: if (keptJobs.contains(element)) {
167: keptJobs.remove(element);
168: }
169:
170: }
171:
172: Iterator keptIterator = keptJobs.iterator();
173: while (keptIterator.hasNext()) {
174: JobInfo next = (JobInfo) keptIterator.next();
175: GroupInfo group = next.getGroupInfo();
176: if (group == null)
177: all.add(next);
178: else
179: all.add(group);
180: }
181: return all.toArray();
182: }
183:
184: /**
185: * Get the root elements of the passed elements as we only show roots.
186: * Replace the element with its parent if subWithParent is true
187: *
188: * @param elements
189: * the array of elements.
190: * @param subWithParent
191: * sub with parent flag.
192: * @return Object[]
193: */
194: private Object[] getRoots(Object[] elements, boolean subWithParent) {
195: if (elements.length == 0) {
196: return elements;
197: }
198: HashSet roots = new HashSet();
199: for (int i = 0; i < elements.length; i++) {
200: JobTreeElement element = (JobTreeElement) elements[i];
201: if (element.isJobInfo()) {
202: GroupInfo group = ((JobInfo) element).getGroupInfo();
203: if (group == null) {
204: roots.add(element);
205: } else {
206: if (subWithParent) {
207: roots.add(group);
208: }
209: }
210: } else {
211: roots.add(element);
212: }
213: }
214: return roots.toArray();
215: }
216:
217: public void add(Object[] elements) {
218: progressViewer.add(elements);
219:
220: }
221:
222: public void remove(Object[] elements) {
223: progressViewer.remove(elements);
224:
225: }
226:
227: /*
228: * (non-Javadoc)
229: *
230: * @see org.eclipse.jface.viewers.IContentProvider#dispose()
231: */
232: public void dispose() {
233: super.dispose();
234: if (keptJobListener != null) {
235: FinishedJobs.getInstance().removeListener(keptJobListener);
236: }
237: }
238: }
|