📄 progressmonitordialog.java
字号:
// exception because the cancel button was already disposed cancel.setEnabled(false); progressMonitor.setCanceled(true); super.cancelPressed(); } /* * (non-Javadoc) Method declared on Window. */ /** * The <code>ProgressMonitorDialog</code> implementation of this method * only closes the dialog if there are no currently running runnables. */ public boolean close() { if (getNestingDepth() <= 0) { clearCursors(); return super.close(); } return false; } /** * Clear the cursors in the dialog. * * @since 3.0 */ protected void clearCursors() { if (cancel != null && !cancel.isDisposed()) { cancel.setCursor(null); } Shell shell = getShell(); if (shell != null && !shell.isDisposed()) { shell.setCursor(null); } if (arrowCursor != null) { arrowCursor.dispose(); } if (waitCursor != null) { waitCursor.dispose(); } arrowCursor = null; waitCursor = null; } /* * (non-Javadoc) Method declared in Window. */ protected void configureShell(final Shell shell) { super.configureShell(shell); shell.setText(JFaceResources.getString("ProgressMonitorDialog.title")); //$NON-NLS-1$ if (waitCursor == null) { waitCursor = new Cursor(shell.getDisplay(), SWT.CURSOR_WAIT); } shell.setCursor(waitCursor); // Add a listener to set the message properly when the dialog becomes visible shell.addListener(SWT.Show, new Listener() { public void handleEvent(Event event) { setMessage(message); } }); } /* * (non-Javadoc) Method declared on Dialog. */ protected void createButtonsForButtonBar(Composite parent) { // cancel button createCancelButton(parent); } /** * Creates the cancel button. * * @param parent * the parent composite * @since 3.0 */ protected void createCancelButton(Composite parent) { cancel = createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, true); if (arrowCursor == null) { arrowCursor = new Cursor(cancel.getDisplay(), SWT.CURSOR_ARROW); } cancel.setCursor(arrowCursor); setOperationCancelButtonEnabled(enableCancelButton); } /* * (non-Javadoc) Method declared on Dialog. */ protected Control createDialogArea(Composite parent) { setMessage(DEFAULT_TASKNAME); createMessageArea(parent); //Only set for backwards compatibility taskLabel = messageLabel; // progress indicator progressIndicator = new ProgressIndicator(parent); GridData gd = new GridData(); gd.heightHint = convertVerticalDLUsToPixels(BAR_DLUS); gd.horizontalAlignment = GridData.FILL; gd.grabExcessHorizontalSpace = true; gd.horizontalSpan = 2; progressIndicator.setLayoutData(gd); // label showing current task subTaskLabel = new Label(parent, SWT.LEFT | SWT.WRAP); gd = new GridData(GridData.FILL_HORIZONTAL); gd.heightHint = convertVerticalDLUsToPixels(LABEL_DLUS); gd.horizontalSpan = 2; subTaskLabel.setLayoutData(gd); subTaskLabel.setFont(parent.getFont()); return parent; } /* * (non-Javadoc) * @see org.eclipse.jface.window.Window#getInitialSize() */ protected Point getInitialSize() { Point calculatedSize = super.getInitialSize(); if (calculatedSize.x < 450) { calculatedSize.x = 450; } return calculatedSize; } /** * Returns the progress monitor to use for operations run in this progress * dialog. * * @return the progress monitor */ public IProgressMonitor getProgressMonitor() { return progressMonitor; } /** * This implementation of IRunnableContext#run(boolean, boolean, * IRunnableWithProgress) runs the given <code>IRunnableWithProgress</code> * using the progress monitor for this progress dialog and blocks until * the runnable has been run, regardless of the value of <code>fork</code>. * The dialog is opened before the runnable is run, and closed after * it completes. It is recommended that <code>fork</code> is set to * true in most cases. If <code>fork</code> is set to <code>false</code>, * the runnable will run in the UI thread and it is the runnable's * responsibility to call <code>Display.readAndDispatch()</code> * to ensure UI responsiveness. */ public void run(boolean fork, boolean cancelable, IRunnableWithProgress runnable) throws InvocationTargetException, InterruptedException { setCancelable(cancelable); try { aboutToRun(); //Let the progress monitor know if they need to update in UI Thread progressMonitor.forked = fork; ModalContext.run(runnable, fork, getProgressMonitor(), getShell() .getDisplay()); } finally { finishedRun(); } } /** * Returns whether the dialog should be opened before the operation is run. * Defaults to <code>true</code> * * @return <code>true</code> to open the dialog before run, * <code>false</code> to only create the dialog, but not open it * @since 3.0 */ public boolean getOpenOnRun() { return openOnRun; } /** * Sets whether the dialog should be opened before the operation is run. * NOTE: Setting this to false and not forking a process may starve any * asyncExec that tries to open the dialog later. * * @param openOnRun * <code>true</code> to open the dialog before run, * <code>false</code> to only create the dialog, but not open * it * @since 3.0 */ public void setOpenOnRun(boolean openOnRun) { this.openOnRun = openOnRun; } /** * Returns the nesting depth of running operations. * * @return the nesting depth of running operations * @since 3.0 */ protected int getNestingDepth() { return nestingDepth; } /** * Increments the nesting depth of running operations. * * @since 3.0 */ protected void incrementNestingDepth() { nestingDepth++; } /** * Decrements the nesting depth of running operations. * * @since 3.0 * */ protected void decrementNestingDepth() { nestingDepth--; } /** * Called just before the operation is run. Default behaviour is to open or * create the dialog, based on the setting of <code>getOpenOnRun</code>, * and increment the nesting depth. * * @since 3.0 */ protected void aboutToRun() { if (getOpenOnRun()) { open(); } else { create(); } incrementNestingDepth(); } /** * Called just after the operation is run. Default behaviour is to decrement * the nesting depth, and close the dialog. * * @since 3.0 */ protected void finishedRun() { decrementNestingDepth(); close(); } /** * Sets whether the progress dialog is cancelable or not. * * @param cancelable * <code>true</code> if the end user can cancel this progress * dialog, and <code>false</code> if it cannot be canceled */ public void setCancelable(boolean cancelable) { if (cancel == null) { enableCancelButton = cancelable; } else { asyncSetOperationCancelButtonEnabled(cancelable); } } /** * Helper to enable/disable Cancel button for this dialog. * * @param b * <code>true</code> to enable the cancel button, and * <code>false</code> to disable it * @since 3.0 */ protected void setOperationCancelButtonEnabled(boolean b) { operationCancelableState = b; cancel.setEnabled(b); } /* * (non-Javadoc) * @see org.eclipse.jface.dialogs.IconAndMessageDialog#getImage() */ protected Image getImage() { return getInfoImage(); } /** * Set the message in the message label. * @param messageString The string for the new message. */ private void setMessage(String messageString) { //must not set null text in a label message = messageString == null ? "" : messageString; //$NON-NLS-1$ if (messageLabel == null || messageLabel.isDisposed() || !messageLabel.isVisible()) { return; } messageLabel.setToolTipText(messageString); messageLabel.setText(shortenText(message,messageLabel)); } /** * Update the message label. Required if the monitor is forked. */ private void update() { if (messageLabel == null || messageLabel.isDisposed()) { return; } messageLabel.update(); } /* * (non-Javadoc) * @see org.eclipse.jface.window.Window#open() */ public int open() { //Check to be sure it is not already done. If it is just return OK. if (!getOpenOnRun()) { if (getNestingDepth() == 0) { return OK; } } return super.open(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -