1 11 package org.eclipse.ui.internal.progress; 12 13 import java.util.HashSet ; 14 import java.util.Iterator ; 15 import java.util.Set ; 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 27 public class ProgressViewerContentProvider extends ProgressContentProvider { 28 protected AbstractProgressViewer progressViewer; 29 30 private KeptJobsListener keptJobListener; 31 32 private Set keptJobs = new HashSet (); 33 34 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 60 private KeptJobsListener getKeptJobListener() { 61 keptJobListener = new KeptJobsListener() { 62 63 68 public void finished(JobTreeElement jte) { 69 keptJobs.add(jte); 70 final JobTreeElement element = jte; 71 Job updateJob = new WorkbenchJob("Refresh finished") { 75 public IStatus runInUIThread(IProgressMonitor monitor) { 76 refresh(new Object [] { element }); 77 return Status.OK_STATUS; 78 } 79 }; 80 updateJob.setSystem(true); 81 updateJob.schedule(); 82 83 } 84 85 90 public void removed(JobTreeElement jte) { 91 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") { 102 public IStatus runInUIThread(IProgressMonitor monitor) { 103 if(element == null) { 104 refresh(); 105 } else { 106 ProgressViewerContentProvider.this.remove(new Object [] { 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 125 public void refresh() { 126 progressViewer.refresh(true); 127 } 128 129 134 public void refresh(Object [] elements) { 135 Object [] refreshes = getRoots(elements, true); 136 for (int i = 0; i < refreshes.length; i++) { 137 progressViewer.refresh(refreshes[i], true); 138 } 139 } 140 141 146 public Object [] getElements(Object inputElement) { 147 Object [] 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 all = new HashSet (); 157 158 for (int i = 0; i < elements.length; i++) { 159 Object element = elements[i]; 160 all.add(element); 161 if(keptJobs.contains(element)) { 162 keptJobs.remove(element); 163 } 164 165 } 166 167 Iterator 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 189 private Object [] getRoots(Object [] elements, boolean subWithParent) { 190 if (elements.length == 0) { 191 return elements; 192 } 193 HashSet roots = new HashSet (); 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 [] elements) { 213 progressViewer.add(elements); 214 215 } 216 217 public void remove(Object [] elements) { 218 progressViewer.remove(elements); 219 220 } 221 222 227 public void dispose() { 228 super.dispose(); 229 if (keptJobListener != null) { 230 FinishedJobs.getInstance().removeListener(keptJobListener); 231 } 232 } 233 } 234 | Popular Tags |