📄 progressmonitordialog.java
字号:
/******************************************************************************* * Copyright (c) 2000, 2006 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/package org.eclipse.jface.dialogs;import java.lang.reflect.InvocationTargetException;import org.eclipse.core.runtime.IProgressMonitor;import org.eclipse.core.runtime.IProgressMonitorWithBlocking;import org.eclipse.core.runtime.IStatus;import org.eclipse.jface.operation.IRunnableContext;import org.eclipse.jface.operation.IRunnableWithProgress;import org.eclipse.jface.operation.ModalContext;import org.eclipse.jface.resource.JFaceResources;import org.eclipse.swt.SWT;import org.eclipse.swt.graphics.Cursor;import org.eclipse.swt.graphics.Image;import org.eclipse.swt.graphics.Point;import org.eclipse.swt.layout.GridData;import org.eclipse.swt.widgets.Button;import org.eclipse.swt.widgets.Composite;import org.eclipse.swt.widgets.Control;import org.eclipse.swt.widgets.Event;import org.eclipse.swt.widgets.Label;import org.eclipse.swt.widgets.Listener;import org.eclipse.swt.widgets.Shell;/** * A modal dialog that displays progress during a long running operation. * <p> * This concrete dialog class can be instantiated as is, or further subclassed * as required. * </p> * <p> * Typical usage is: * * <pre> * * * try { * IRunnableWithProgress op = ...; * new ProgressMonitorDialog(activeShell).run(true, true, op); * } catch (InvocationTargetException e) { * // handle exception * } catch (InterruptedException e) { * // handle cancelation * } * * * </pre> * * </p> * <p> * Note that the ProgressMonitorDialog is not intended to be used with multiple * runnables - this dialog should be discarded after completion of one * IRunnableWithProgress and a new one instantiated for use by a second or * sebsequent IRunnableWithProgress to ensure proper initialization. * </p> * <p> * Note that not forking the process will result in it running in the UI which * may starve the UI. The most obvious symptom of this problem is non * responsiveness of the cancel button. If you are running within the UI Thread * you should do the bulk of your work in another Thread to prevent starvation. * It is recommended that fork is set to true in most cases. * </p> */public class ProgressMonitorDialog extends IconAndMessageDialog implements IRunnableContext { /** * Name to use for task when normal task name is empty string. */ private static String DEFAULT_TASKNAME = JFaceResources .getString("ProgressMonitorDialog.message"); //$NON-NLS-1$ /** * Constants for label and monitor size */ private static int LABEL_DLUS = 21; private static int BAR_DLUS = 9; /** * The progress indicator control. */ protected ProgressIndicator progressIndicator; /** * The label control for the task. Kept for backwards compatibility. */ protected Label taskLabel; /** * The label control for the subtask. */ protected Label subTaskLabel; /** * The Cancel button control. */ protected Button cancel; /** * Indicates whether the Cancel button is to be shown. */ protected boolean operationCancelableState = false; /** * Indicates whether the Cancel button is to be enabled. */ protected boolean enableCancelButton; /** * The progress monitor. */ private ProgressMonitor progressMonitor = new ProgressMonitor(); /** * The name of the current task (used by ProgressMonitor). */ private String task; /** * The nesting depth of currently running runnables. */ private int nestingDepth; /** * The cursor used in the cancel button; */ protected Cursor arrowCursor; /** * The cursor used in the shell; */ private Cursor waitCursor; /** * Flag indicating whether to open or merely create the dialog before run. */ private boolean openOnRun = true; /** * Internal progress monitor implementation. */ private class ProgressMonitor implements IProgressMonitorWithBlocking { private String fSubTask = "";//$NON-NLS-1$ private boolean fIsCanceled; protected boolean forked = false; protected boolean locked = false; public void beginTask(String name, int totalWork) { if (progressIndicator.isDisposed()) { return; } if (name == null) { task = "";//$NON-NLS-1$ } else { task = name; } String s = task; if (s.length() <= 0) { s = DEFAULT_TASKNAME; } setMessage(s); if (!forked) { update(); } if (totalWork == UNKNOWN) { progressIndicator.beginAnimatedTask(); } else { progressIndicator.beginTask(totalWork); } } public void done() { if (!progressIndicator.isDisposed()) { progressIndicator.sendRemainingWork(); progressIndicator.done(); } } public void setTaskName(String name) { if (name == null) { task = "";//$NON-NLS-1$ } else { task = name; } String s = task; if (s.length() <= 0) { s = DEFAULT_TASKNAME; } setMessage(s); if (!forked) { update(); } } public boolean isCanceled() { return fIsCanceled; } public void setCanceled(boolean b) { fIsCanceled = b; if (locked) { clearBlocked(); } } public void subTask(String name) { if (subTaskLabel.isDisposed()) { return; } if (name == null) { fSubTask = "";//$NON-NLS-1$ } else { fSubTask = name; } subTaskLabel.setText(shortenText(fSubTask,subTaskLabel)); if (!forked) { subTaskLabel.update(); } } public void worked(int work) { internalWorked(work); } public void internalWorked(double work) { if (!progressIndicator.isDisposed()) { progressIndicator.worked(work); } } /* * (non-Javadoc) * * @see org.eclipse.core.runtime.IProgressMonitorWithBlocking#clearBlocked() */ public void clearBlocked() { locked = false; updateForClearBlocked(); } /* * (non-Javadoc) * * @see org.eclipse.core.runtime.IProgressMonitorWithBlocking#setBlocked(org.eclipse.core.runtime.IStatus) */ public void setBlocked(IStatus reason) { locked = true; updateForSetBlocked(reason); } } /** * Clear blocked state from the receiver. */ protected void updateForClearBlocked() { setMessage(task); imageLabel.setImage(getImage()); } /** * Set blocked state from the receiver. * * @param reason * IStatus that gives the details */ protected void updateForSetBlocked(IStatus reason) { setMessage(reason.getMessage()); imageLabel.setImage(getImage()); } /** * Creates a progress monitor dialog under the given shell. The dialog has a * standard title and no image. <code>open</code> is non-blocking. * * @param parent * the parent shell, or <code>null</code> to create a top-level * shell */ public ProgressMonitorDialog(Shell parent) { super(parent); setShellStyle(getDefaultOrientation() | SWT.BORDER | SWT.TITLE | SWT.APPLICATION_MODAL); // no // close // button setBlockOnOpen(false); } /** * Enables the cancel button (asynchronously). * @param b The state to set the button to. */ private void asyncSetOperationCancelButtonEnabled(final boolean b) { if (getShell() != null) { getShell().getDisplay().asyncExec(new Runnable() { public void run() { setOperationCancelButtonEnabled(b); } }); } } /** * The cancel button has been pressed. * * @since 3.0 */ protected void cancelPressed() { // NOTE: this was previously done from a listener installed on the // cancel button. On GTK, the listener installed by // Dialog.createButton is called first and this was throwing an
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -