1 11 package org.eclipse.ui.internal.progress; 12 13 import org.eclipse.core.runtime.IStatus; 14 import org.eclipse.core.runtime.jobs.Job; 15 import org.eclipse.jface.action.IAction; 16 import org.eclipse.osgi.util.NLS; 17 import org.eclipse.swt.SWT; 18 import org.eclipse.swt.events.DisposeEvent; 19 import org.eclipse.swt.events.DisposeListener; 20 import org.eclipse.swt.events.MouseAdapter; 21 import org.eclipse.swt.events.MouseEvent; 22 import org.eclipse.swt.events.MouseListener; 23 import org.eclipse.swt.events.SelectionAdapter; 24 import org.eclipse.swt.events.SelectionEvent; 25 import org.eclipse.swt.graphics.Image; 26 import org.eclipse.swt.layout.GridData; 27 import org.eclipse.swt.layout.GridLayout; 28 import org.eclipse.swt.widgets.Composite; 29 import org.eclipse.swt.widgets.Control; 30 import org.eclipse.swt.widgets.Display; 31 import org.eclipse.swt.widgets.Label; 32 import org.eclipse.swt.widgets.ProgressBar; 33 import org.eclipse.swt.widgets.ToolBar; 34 import org.eclipse.swt.widgets.ToolItem; 35 import org.eclipse.ui.PlatformUI; 36 import org.eclipse.ui.internal.WorkbenchImages; 37 import org.eclipse.ui.progress.IProgressConstants; 38 import org.eclipse.ui.statushandlers.StatusAdapter; 39 import org.eclipse.ui.statushandlers.StatusManager; 40 41 45 public class ProgressAnimationItem extends AnimationItem implements 46 FinishedJobs.KeptJobsListener { 47 48 ProgressBar bar; 49 50 MouseListener mouseListener; 51 52 Composite top; 53 54 ToolBar toolbar; 55 56 ToolItem toolButton; 57 58 ProgressRegion progressRegion; 59 60 Image noneImage, okImage, errorImage; 61 62 boolean animationRunning; 63 64 JobInfo lastJobInfo; 65 66 private int flags; 68 69 75 ProgressAnimationItem(ProgressRegion region, int flags) { 76 super(region.workbenchWindow); 77 this.flags = flags; 78 FinishedJobs.getInstance().addListener(this); 79 80 progressRegion = region; 81 mouseListener = new MouseAdapter() { 82 public void mouseDoubleClick(MouseEvent e) { 83 doAction(); 84 } 85 }; 86 } 87 88 void doAction() { 89 90 JobTreeElement[] jobTreeElements = FinishedJobs.getInstance() 91 .getJobInfos(); 92 for (int i = jobTreeElements.length - 1; i >= 0; i--) { 94 if (jobTreeElements[i] instanceof JobInfo) { 95 JobInfo ji = (JobInfo) jobTreeElements[i]; 96 Job job = ji.getJob(); 97 if (job != null) { 98 99 IStatus status = job.getResult(); 100 if (status != null && status.getSeverity() == IStatus.ERROR) { 101 StatusAdapter statusAdapter = StatusAdapterHelper 102 .getInstance().getStatusAdapter(ji); 103 104 StatusManager.getManager().handle(statusAdapter, 105 StatusManager.SHOW); 106 107 JobTreeElement topElement = (JobTreeElement) ji 108 .getParent(); 109 if (topElement == null) { 110 topElement = ji; 111 } 112 FinishedJobs.getInstance().remove(topElement); 113 } 114 115 IAction action = null; 116 Object property = job 117 .getProperty(IProgressConstants.ACTION_PROPERTY); 118 if (property instanceof IAction) { 119 action = (IAction) property; 120 } 121 if (action != null && action.isEnabled()) { 122 action.run(); 123 JobTreeElement topElement = (JobTreeElement) ji 124 .getParent(); 125 if (topElement == null) { 126 topElement = ji; 127 } 128 FinishedJobs.getInstance().remove(topElement); 129 return; 130 } 131 } 132 } 133 } 134 135 progressRegion.processDoubleClick(); 136 refresh(); 137 } 138 139 private IAction getAction(Job job) { 140 Object property = job.getProperty(IProgressConstants.ACTION_PROPERTY); 141 if (property instanceof IAction) { 142 return (IAction) property; 143 } 144 return null; 145 } 146 147 private void refresh() { 148 149 if (!PlatformUI.isWorkbenchRunning()) { 151 return; 152 } 153 154 if (toolbar == null || toolbar.isDisposed()) { 155 return; 156 } 157 158 lastJobInfo = null; 159 160 JobTreeElement[] jobTreeElements = FinishedJobs.getInstance() 161 .getJobInfos(); 162 for (int i = jobTreeElements.length - 1; i >= 0; i--) { 164 if (jobTreeElements[i] instanceof JobInfo) { 165 JobInfo ji = (JobInfo) jobTreeElements[i]; 166 lastJobInfo = ji; 167 Job job = ji.getJob(); 168 if (job != null) { 169 IStatus status = job.getResult(); 170 if (status != null && status.getSeverity() == IStatus.ERROR) { 171 initButton( 173 errorImage, 174 NLS.bind(ProgressMessages.ProgressAnimationItem_error, job.getName())); 175 return; 176 } 177 IAction action = getAction(job); 178 if (action != null && action.isEnabled()) { 179 String tt = action.getToolTipText(); 181 if (tt == null || tt.trim().length() == 0) { 182 tt = NLS.bind(ProgressMessages.ProgressAnimationItem_ok, job.getName()); 183 } 184 initButton(okImage, tt); 185 return; 186 } 187 initButton(noneImage, ProgressMessages.ProgressAnimationItem_tasks); 189 return; 190 } 191 } 192 } 193 194 if (animationRunning) { 195 initButton(noneImage, ProgressMessages.ProgressAnimationItem_tasks); 196 return; 197 } 198 199 toolbar.setVisible(false); 201 } 202 203 private void initButton(Image im, String tt) { 204 toolButton.setImage(im); 205 toolButton.setToolTipText(tt); 206 toolbar.setVisible(true); 207 toolbar.getParent().layout(); } 209 210 215 protected Control createAnimationItem(Composite parent) { 216 217 if (okImage == null) { 218 Display display = parent.getDisplay(); 219 noneImage = WorkbenchImages.getWorkbenchImageDescriptor("progress/progress_none.gif").createImage(display); okImage = WorkbenchImages.getWorkbenchImageDescriptor("progress/progress_ok.gif").createImage(display); errorImage = WorkbenchImages.getWorkbenchImageDescriptor("progress/progress_error.gif").createImage(display); } 223 224 top = new Composite(parent, SWT.NULL); 225 top.addDisposeListener(new DisposeListener() { 226 public void widgetDisposed(DisposeEvent e) { 227 FinishedJobs.getInstance().removeListener( 228 ProgressAnimationItem.this); 229 noneImage.dispose(); 230 okImage.dispose(); 231 errorImage.dispose(); 232 } 233 }); 234 235 boolean isCarbon = "carbon".equals(SWT.getPlatform()); 237 GridLayout gl = new GridLayout(); 238 if (isHorizontal()) 239 gl.numColumns = isCarbon ? 3 : 2; 240 gl.marginHeight = 0; 241 gl.marginWidth = 0; 242 if (isHorizontal()) { 243 gl.horizontalSpacing = 2; 244 } else { 245 gl.verticalSpacing = 2; 246 } 247 top.setLayout(gl); 248 249 bar = new ProgressBar(top, flags | SWT.INDETERMINATE); 250 bar.setVisible(false); 251 bar.addMouseListener(mouseListener); 252 253 GridData gd; 254 int hh = 12; 255 if (isHorizontal()) { 256 gd = new GridData(SWT.BEGINNING, SWT.CENTER, true, false); 257 gd.heightHint = hh; 258 } else { 259 gd = new GridData(SWT.CENTER, SWT.BEGINNING, false, true); 260 gd.widthHint = hh; 261 } 262 263 bar.setLayoutData(gd); 264 265 toolbar = new ToolBar(top, SWT.FLAT); 266 toolbar.setVisible(false); 267 268 toolButton = new ToolItem(toolbar, SWT.NONE); 269 toolButton.addSelectionListener(new SelectionAdapter() { 270 public void widgetSelected(SelectionEvent e) { 271 doAction(); 272 } 273 }); 274 275 if (isCarbon) { 276 new Label(top, SWT.NONE).setLayoutData(new GridData(4, 4)); 277 } 278 279 refresh(); 280 281 return top; 282 } 283 284 287 private boolean isHorizontal() { 288 return (flags & SWT.HORIZONTAL) != 0; 289 } 290 291 296 public Control getControl() { 297 return top; 298 } 299 300 305 void animationDone() { 306 super.animationDone(); 307 animationRunning = false; 308 if (bar.isDisposed()) { 309 return; 310 } 311 bar.setVisible(false); 312 refresh(); 313 } 314 315 318 public boolean animationRunning() { 319 return animationRunning; 320 } 321 326 void animationStart() { 327 super.animationStart(); 328 animationRunning = true; 329 if (bar.isDisposed()) { 330 return; 331 } 332 bar.setVisible(true); 333 refresh(); 334 } 335 336 public void removed(JobTreeElement info) { 337 final Display display = Display.getDefault(); 338 display.asyncExec(new Runnable () { 339 public void run() { 340 refresh(); 341 } 342 }); 343 } 344 345 public void finished(final JobTreeElement jte) { 346 final Display display = Display.getDefault(); 347 display.asyncExec(new Runnable () { 348 public void run() { 349 refresh(); 350 } 351 }); 352 } 353 354 } | Popular Tags |