1 11 package org.eclipse.jface.dialogs; 12 13 import java.lang.reflect.InvocationTargetException ; 14 15 import org.eclipse.core.runtime.IProgressMonitor; 16 import org.eclipse.core.runtime.IProgressMonitorWithBlocking; 17 import org.eclipse.core.runtime.IStatus; 18 import org.eclipse.jface.operation.IRunnableContext; 19 import org.eclipse.jface.operation.IRunnableWithProgress; 20 import org.eclipse.jface.operation.ModalContext; 21 import org.eclipse.jface.resource.JFaceResources; 22 import org.eclipse.swt.SWT; 23 import org.eclipse.swt.graphics.Cursor; 24 import org.eclipse.swt.graphics.Image; 25 import org.eclipse.swt.graphics.Point; 26 import org.eclipse.swt.layout.GridData; 27 import org.eclipse.swt.widgets.Button; 28 import org.eclipse.swt.widgets.Composite; 29 import org.eclipse.swt.widgets.Control; 30 import org.eclipse.swt.widgets.Event; 31 import org.eclipse.swt.widgets.Label; 32 import org.eclipse.swt.widgets.Listener; 33 import org.eclipse.swt.widgets.Shell; 34 35 74 public class ProgressMonitorDialog extends IconAndMessageDialog implements 75 IRunnableContext { 76 79 private static String DEFAULT_TASKNAME = JFaceResources 80 .getString("ProgressMonitorDialog.message"); 82 85 private static int LABEL_DLUS = 21; 86 87 private static int BAR_DLUS = 9; 88 89 92 protected ProgressIndicator progressIndicator; 93 94 97 protected Label taskLabel; 98 99 102 protected Label subTaskLabel; 103 104 107 protected Button cancel; 108 109 112 protected boolean operationCancelableState = false; 113 114 117 protected boolean enableCancelButton; 118 119 122 private ProgressMonitor progressMonitor = new ProgressMonitor(); 123 124 127 private String task; 128 129 132 private int nestingDepth; 133 134 137 protected Cursor arrowCursor; 138 139 142 private Cursor waitCursor; 143 144 147 private boolean openOnRun = true; 148 149 152 private class ProgressMonitor implements IProgressMonitorWithBlocking { 153 private String fSubTask = ""; 155 private boolean fIsCanceled; 156 157 160 protected boolean forked = false; 161 162 165 protected boolean locked = false; 166 167 public void beginTask(String name, int totalWork) { 168 if (progressIndicator.isDisposed()) { 169 return; 170 } 171 if (name == null) { 172 task = ""; } else { 174 task = name; 175 } 176 String s = task; 177 if (s.length() <= 0) { 178 s = DEFAULT_TASKNAME; 179 } 180 setMessage(s, false); 181 if (!forked) { 182 update(); 183 } 184 if (totalWork == UNKNOWN) { 185 progressIndicator.beginAnimatedTask(); 186 } else { 187 progressIndicator.beginTask(totalWork); 188 } 189 } 190 191 public void done() { 192 if (!progressIndicator.isDisposed()) { 193 progressIndicator.sendRemainingWork(); 194 progressIndicator.done(); 195 } 196 } 197 198 public void setTaskName(String name) { 199 if (name == null) { 200 task = ""; } else { 202 task = name; 203 } 204 String s = task; 205 if (s.length() <= 0) { 206 s = DEFAULT_TASKNAME; 207 } 208 setMessage(s, false); 209 if (!forked) { 210 update(); 211 } 212 } 213 214 public boolean isCanceled() { 215 return fIsCanceled; 216 } 217 218 public void setCanceled(boolean b) { 219 fIsCanceled = b; 220 if (locked) { 221 clearBlocked(); 222 } 223 } 224 225 public void subTask(String name) { 226 if (subTaskLabel.isDisposed()) { 227 return; 228 } 229 if (name == null) { 230 fSubTask = ""; } else { 232 fSubTask = name; 233 } 234 subTaskLabel.setText(shortenText(fSubTask, subTaskLabel)); 235 if (!forked) { 236 subTaskLabel.update(); 237 } 238 } 239 240 public void worked(int work) { 241 internalWorked(work); 242 } 243 244 public void internalWorked(double work) { 245 if (!progressIndicator.isDisposed()) { 246 progressIndicator.worked(work); 247 } 248 } 249 250 255 public void clearBlocked() { 256 locked = false; 257 updateForClearBlocked(); 258 } 259 260 265 public void setBlocked(IStatus reason) { 266 locked = true; 267 updateForSetBlocked(reason); 268 } 269 } 270 271 274 protected void updateForClearBlocked() { 275 setMessage(task, true); 276 imageLabel.setImage(getImage()); 277 } 278 279 285 protected void updateForSetBlocked(IStatus reason) { 286 setMessage(reason.getMessage(), true); 287 imageLabel.setImage(getImage()); 288 } 289 290 298 public ProgressMonitorDialog(Shell parent) { 299 super(parent); 300 setShellStyle(getDefaultOrientation() | SWT.BORDER | SWT.TITLE 301 | SWT.APPLICATION_MODAL); setBlockOnOpen(false); 305 } 306 307 313 private void asyncSetOperationCancelButtonEnabled(final boolean b) { 314 if (getShell() != null) { 315 getShell().getDisplay().asyncExec(new Runnable () { 316 public void run() { 317 setOperationCancelButtonEnabled(b); 318 } 319 }); 320 } 321 } 322 323 328 protected void cancelPressed() { 329 cancel.setEnabled(false); 334 progressMonitor.setCanceled(true); 335 super.cancelPressed(); 336 } 337 338 341 345 public boolean close() { 346 if (getNestingDepth() <= 0) { 347 clearCursors(); 348 return super.close(); 349 } 350 return false; 351 } 352 353 358 protected void clearCursors() { 359 if (cancel != null && !cancel.isDisposed()) { 360 cancel.setCursor(null); 361 } 362 Shell shell = getShell(); 363 if (shell != null && !shell.isDisposed()) { 364 shell.setCursor(null); 365 } 366 if (arrowCursor != null) { 367 arrowCursor.dispose(); 368 } 369 if (waitCursor != null) { 370 waitCursor.dispose(); 371 } 372 arrowCursor = null; 373 waitCursor = null; 374 } 375 376 379 protected void configureShell(final Shell shell) { 380 super.configureShell(shell); 381 shell.setText(JFaceResources.getString("ProgressMonitorDialog.title")); if (waitCursor == null) { 383 waitCursor = new Cursor(shell.getDisplay(), SWT.CURSOR_WAIT); 384 } 385 shell.setCursor(waitCursor); 386 shell.addListener(SWT.Show, new Listener() { 389 public void handleEvent(Event event) { 390 shell.getDisplay().asyncExec(new Runnable () { 393 public void run() { 394 setMessage(message, true); 395 } 396 }); 397 } 398 }); 399 } 400 401 404 protected void createButtonsForButtonBar(Composite parent) { 405 createCancelButton(parent); 407 } 408 409 416 protected void createCancelButton(Composite parent) { 417 cancel = createButton(parent, IDialogConstants.CANCEL_ID, 418 IDialogConstants.CANCEL_LABEL, true); 419 if (arrowCursor == null) { 420 arrowCursor = new Cursor(cancel.getDisplay(), SWT.CURSOR_ARROW); 421 } 422 cancel.setCursor(arrowCursor); 423 setOperationCancelButtonEnabled(enableCancelButton); 424 } 425 426 429 protected Control createDialogArea(Composite parent) { 430 setMessage(DEFAULT_TASKNAME, false); 431 createMessageArea(parent); 432 taskLabel = messageLabel; 434 progressIndicator = new ProgressIndicator(parent); 436 GridData gd = new GridData(); 437 gd.heightHint = convertVerticalDLUsToPixels(BAR_DLUS); 438 gd.horizontalAlignment = GridData.FILL; 439 gd.grabExcessHorizontalSpace = true; 440 gd.horizontalSpan = 2; 441 progressIndicator.setLayoutData(gd); 442 subTaskLabel = new Label(parent, SWT.LEFT | SWT.WRAP); 444 gd = new GridData(GridData.FILL_HORIZONTAL); 445 gd.heightHint = convertVerticalDLUsToPixels(LABEL_DLUS); 446 gd.horizontalSpan = 2; 447 subTaskLabel.setLayoutData(gd); 448 subTaskLabel.setFont(parent.getFont()); 449 return parent; 450 } 451 452 457 protected Point getInitialSize() { 458 Point calculatedSize = super.getInitialSize(); 459 if (calculatedSize.x < 450) { 460 calculatedSize.x = 450; 461 } 462 return calculatedSize; 463 } 464 465 471 public IProgressMonitor getProgressMonitor() { 472 return progressMonitor; 473 } 474 475 487 public void run(boolean fork, boolean cancelable, 488 IRunnableWithProgress runnable) throws InvocationTargetException , 489 InterruptedException { 490 setCancelable(cancelable); 491 try { 492 aboutToRun(); 493 progressMonitor.forked = fork; 495 ModalContext.run(runnable, fork, getProgressMonitor(), getShell() 496 .getDisplay()); 497 } finally { 498 finishedRun(); 499 } 500 } 501 502 510 public boolean getOpenOnRun() { 511 return openOnRun; 512 } 513 514 525 public void setOpenOnRun(boolean openOnRun) { 526 this.openOnRun = openOnRun; 527 } 528 529 535 protected int getNestingDepth() { 536 return nestingDepth; 537 } 538 539 544 protected void incrementNestingDepth() { 545 nestingDepth++; 546 } 547 548 554 protected void decrementNestingDepth() { 555 nestingDepth--; 556 } 557 558 565 protected void aboutToRun() { 566 if (getOpenOnRun()) { 567 open(); 568 } else { 569 create(); 570 } 571 incrementNestingDepth(); 572 } 573 574 580 protected void finishedRun() { 581 decrementNestingDepth(); 582 close(); 583 } 584 585 592 public void setCancelable(boolean cancelable) { 593 if (cancel == null) { 594 enableCancelButton = cancelable; 595 } else { 596 asyncSetOperationCancelButtonEnabled(cancelable); 597 } 598 } 599 600 608 protected void setOperationCancelButtonEnabled(boolean b) { 609 operationCancelableState = b; 610 cancel.setEnabled(b); 611 } 612 613 618 protected Image getImage() { 619 return getInfoImage(); 620 } 621 622 630 private void setMessage(String messageString, boolean force) { 631 message = messageString == null ? "" : messageString; if (messageLabel == null || messageLabel.isDisposed()) { 634 return; 635 } 636 if (force || messageLabel.isVisible()) { 637 messageLabel.setToolTipText(message); 638 messageLabel.setText(shortenText(message, messageLabel)); 639 } 640 } 641 642 645 private void update() { 646 if (messageLabel == null || messageLabel.isDisposed()) { 647 return; 648 } 649 messageLabel.update(); 650 } 651 652 657 public int open() { 658 if (!getOpenOnRun()) { 660 if (getNestingDepth() == 0) { 661 return OK; 662 } 663 } 664 int result = super.open(); 665 if (task == null || task.length() == 0) 667 setMessage(DEFAULT_TASKNAME, true); 668 else 669 setMessage(task, true); 670 return result; 671 } 672 } 673 | Popular Tags |