📄 modalcontext.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.operation;import java.lang.reflect.InvocationTargetException;import org.eclipse.core.runtime.IProgressMonitor;import org.eclipse.core.runtime.OperationCanceledException;import org.eclipse.core.runtime.ProgressMonitorWrapper;import org.eclipse.jface.util.Assert;import org.eclipse.swt.widgets.Display;/** * Utility class for supporting modal operations. * The runnable passed to the <code>run</code> method is executed in a * separate thread, depending on the value of the passed fork argument. * If the runnable is executed in a separate thread then the current thread * either waits until the new thread ends or, if the current thread is the * UI thread, it polls the SWT event queue and dispatches each event. * <p> * This class is not intended to be subclassed. * </p> */public class ModalContext { /** * Indicated whether ModalContext is in debug mode; * <code>false</code> by default. */ private static boolean debug = false; /** * The number of nested modal runs, or 0 if not inside a modal run. * This is global state. */ private static int modalLevel = 0; /** * Indicates whether operations should be run in a separate thread. * Defaults to true. * For internal debugging use, set to false to run operations in the calling thread. */ private static boolean runInSeparateThread = true; /** * Thread which runs the modal context. */ private static class ModalContextThread extends Thread { /** * The operation to be run. */ private IRunnableWithProgress runnable; /** * The exception thrown by the operation starter. */ private Throwable throwable; /** * The progress monitor used for progress and cancelation. */ private IProgressMonitor progressMonitor; /** * The display used for event dispatching. */ private Display display; /** * Indicates whether to continue event queue dispatching. */ private volatile boolean continueEventDispatching = true; /** * The thread that forked this modal context thread. * * @since 3.1 */ private Thread callingThread; /** * Creates a new modal context. * * @param operation the runnable to run * @param monitor the progress monitor to use to display progress and receive * requests for cancelation * @param display the display to be used to read and dispatch events */ private ModalContextThread(IRunnableWithProgress operation, IProgressMonitor monitor, Display display) { super("ModalContext"); //$NON-NLS-1$ Assert.isTrue(monitor != null && display != null); runnable = operation; progressMonitor = new AccumulatingProgressMonitor(monitor, display); this.display = display; this.callingThread = Thread.currentThread(); } /* (non-Javadoc) * Method declared on Thread. */ public void run() { try { if (runnable != null) { runnable.run(progressMonitor); } } catch (InvocationTargetException e) { throwable = e; } catch (InterruptedException e) { throwable = e; } catch (RuntimeException e) { throwable = e; } catch (ThreadDeath e) { // Make sure to propagate ThreadDeath, or threads will never fully terminate throw e; } catch (Error e) { throwable = e; } finally { //notify the operation of change of thread of control if (runnable instanceof IThreadListener) { ((IThreadListener)runnable).threadChange(callingThread); } // Make sure that all events in the asynchronous event queue // are dispatched. display.syncExec(new Runnable() { public void run() { // do nothing } }); // Stop event dispatching continueEventDispatching = false; // Force the event loop to return from sleep () so that // it stops event dispatching. display.asyncExec(null); } } /** * Processes events or waits until this modal context thread terminates. */ public void block() { if (display == Display.getCurrent()) { while (continueEventDispatching) { // Run the event loop. Handle any uncaught exceptions caused // by UI events. try { if (!display.readAndDispatch()) { display.sleep(); } } // ThreadDeath is a normal error when the thread is dying. We must // propagate it in order for it to properly terminate. catch (ThreadDeath e) { throw (e); } // For all other exceptions, log the problem. catch (Throwable e) { System.err.println("Unhandled event loop exception during blocked modal context."); //$NON-NLS-1$ e.printStackTrace(); } } } else { try { join(); } catch (InterruptedException e) { throwable = e; } } } } /** * Returns whether the first progress monitor is the same as, or * a wrapper around, the second progress monitor. * * @param monitor1 the first progress monitor * @param monitor2 the second progress monitor * @return <code>true</code> if the first is the same as, or * a wrapper around, the second * @see ProgressMonitorWrapper */ public static boolean canProgressMonitorBeUsed(IProgressMonitor monitor1, IProgressMonitor monitor2) { if (monitor1 == monitor2) { return true; } while (monitor1 instanceof ProgressMonitorWrapper) { monitor1 = ((ProgressMonitorWrapper) monitor1) .getWrappedProgressMonitor(); if (monitor1 == monitor2) { return true; } } return false;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -