KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2004, 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.HashSet JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Status;
20 import org.eclipse.core.runtime.jobs.Job;
21 import org.eclipse.ui.internal.progress.FinishedJobs.KeptJobsListener;
22 import org.eclipse.ui.progress.WorkbenchJob;
23
24 /**
25  * The ProgressViewerContentProvider is the content provider progress viewers.
26  */

27 public class ProgressViewerContentProvider extends ProgressContentProvider {
28     protected AbstractProgressViewer progressViewer;
29
30     private KeptJobsListener keptJobListener;
31
32     private Set JavaDoc keptJobs = new HashSet JavaDoc();
33
34     /**
35      * Create a new instance of the receiver.
36      *
37      * @param structured
38      * The Viewer we are providing content for
39      * @param debug
40      * If true debug information will be shown
41      * if the debug flag in the ProgressManager is true.
42      * @param showFinished
43      * A boolean that indicates whether or not the finished jobs
44      * should be shown.
45      */

46     public ProgressViewerContentProvider(AbstractProgressViewer structured,
47             boolean debug, boolean showFinished) {
48         super(debug);
49         progressViewer = structured;
50         if (showFinished) {
51             FinishedJobs.getInstance().addListener(getKeptJobListener());
52         }
53     }
54
55     /**
56      * Return a listener for kept jobs.
57      *
58      * @return KeptJobsListener
59      */

60     private KeptJobsListener getKeptJobListener() {
61         keptJobListener = new KeptJobsListener() {
62
63             /*
64              * (non-Javadoc)
65              *
66              * @see org.eclipse.ui.internal.progress.FinishedJobs.KeptJobsListener#finished(org.eclipse.ui.internal.progress.JobTreeElement)
67              */

68             public void finished(JobTreeElement jte) {
69                 keptJobs.add(jte);
70                 final JobTreeElement element = jte;
71                 Job updateJob = new WorkbenchJob("Refresh finished") {//$NON-NLS-1$
72
/* (non-Javadoc)
73                      * @see org.eclipse.ui.progress.UIJob#runInUIThread(org.eclipse.core.runtime.IProgressMonitor)
74                      */

75                     public IStatus runInUIThread(IProgressMonitor monitor) {
76                         refresh(new Object JavaDoc[] { element });
77                         return Status.OK_STATUS;
78                     }
79                 };
80                 updateJob.setSystem(true);
81                 updateJob.schedule();
82
83             }
84
85             /*
86              * (non-Javadoc)
87              *
88              * @see org.eclipse.ui.internal.progress.FinishedJobs.KeptJobsListener#removed(org.eclipse.ui.internal.progress.JobTreeElement)
89              */

90             public void removed(JobTreeElement jte) {
91                 //null indicates they are all removed
92
if(jte == null) {
93                     keptJobs.clear();
94                 } else {
95                     keptJobs.remove(jte);
96                 }
97                 final JobTreeElement element = jte;
98                 Job updateJob = new WorkbenchJob("Remove finished") {//$NON-NLS-1$
99
/* (non-Javadoc)
100                      * @see org.eclipse.ui.progress.UIJob#runInUIThread(org.eclipse.core.runtime.IProgressMonitor)
101                      */

102                     public IStatus runInUIThread(IProgressMonitor monitor) {
103                         if(element == null) {
104                             refresh();
105                         } else {
106                             ProgressViewerContentProvider.this.remove(new Object JavaDoc[] { element });
107                         }
108                         return Status.OK_STATUS;
109                     }
110                 };
111                 updateJob.setSystem(true);
112                 updateJob.schedule();
113
114             }
115
116         };
117         return keptJobListener;
118     }
119
120     /*
121      * (non-Javadoc)
122      *
123      * @see org.eclipse.ui.internal.progress.IProgressUpdateCollector#refresh()
124      */

125     public void refresh() {
126         progressViewer.refresh(true);
127     }
128
129     /*
130      * (non-Javadoc)
131      *
132      * @see org.eclipse.ui.internal.progress.IProgressUpdateCollector#refresh(org.eclipse.ui.internal.progress.JobTreeElement[])
133      */

134     public void refresh(Object JavaDoc[] elements) {
135         Object JavaDoc[] refreshes = getRoots(elements, true);
136         for (int i = 0; i < refreshes.length; i++) {
137             progressViewer.refresh(refreshes[i], true);
138         }
139     }
140
141     /*
142      * (non-Javadoc)
143      *
144      * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
145      */

146     public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
147         Object JavaDoc[] elements = super.getElements(inputElement);
148
149         if (keptJobs.size() == 0) {
150             return elements;
151         }
152         if (elements.length == 0) {
153             return keptJobs.toArray();
154         }
155
156         Set JavaDoc all = new HashSet JavaDoc();
157         
158         for (int i = 0; i < elements.length; i++) {
159             Object JavaDoc element = elements[i];
160             all.add(element);
161             if(keptJobs.contains(element)) {
162                 keptJobs.remove(element);
163             }
164
165         }
166         
167         Iterator JavaDoc keptIterator = keptJobs.iterator();
168         while(keptIterator.hasNext()){
169             JobInfo next = (JobInfo) keptIterator.next();
170             GroupInfo group = next.getGroupInfo();
171             if(group == null)
172                 all.add(next);
173             else
174                 all.add(group);
175         }
176         return all.toArray();
177     }
178
179     /**
180      * Get the root elements of the passed elements as we only show roots.
181      * Replace the element with its parent if subWithParent is true
182      *
183      * @param elements
184      * the array of elements.
185      * @param subWithParent
186      * sub with parent flag.
187      * @return Object[]
188      */

189     private Object JavaDoc[] getRoots(Object JavaDoc[] elements, boolean subWithParent) {
190         if (elements.length == 0) {
191             return elements;
192         }
193         HashSet JavaDoc roots = new HashSet JavaDoc();
194         for (int i = 0; i < elements.length; i++) {
195             JobTreeElement element = (JobTreeElement) elements[i];
196             if (element.isJobInfo()) {
197                 GroupInfo group = ((JobInfo) element).getGroupInfo();
198                 if (group == null) {
199                     roots.add(element);
200                 } else {
201                     if (subWithParent) {
202                         roots.add(group);
203                     }
204                 }
205             } else {
206                 roots.add(element);
207             }
208         }
209         return roots.toArray();
210     }
211
212     public void add(Object JavaDoc[] elements) {
213         progressViewer.add(elements);
214
215     }
216
217     public void remove(Object JavaDoc[] elements) {
218         progressViewer.remove(elements);
219
220     }
221
222     /*
223      * (non-Javadoc)
224      *
225      * @see org.eclipse.jface.viewers.IContentProvider#dispose()
226      */

227     public void dispose() {
228         super.dispose();
229         if (keptJobListener != null) {
230             FinishedJobs.getInstance().removeListener(keptJobListener);
231         }
232     }
233 }
234
Popular Tags