⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 modalcontext.java

📁 jfa2ce 源码帮助开发人员更好的理解运用
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    }    /**     * Checks with the given progress monitor and throws      * <code>InterruptedException</code> if it has been canceled.     * <p>     * Code in a long-running operation should call this method     * regularly so that a request to cancel will be honored.     * </p>     * <p>     * Convenience for:     * <pre>     * if (monitor.isCanceled())     *    throw new InterruptedException();     * </pre>     * </p>     *     * @param monitor the progress monitor     * @exception InterruptedException if cancelling the operation has been requested     * @see IProgressMonitor#isCanceled()     */    public static void checkCanceled(IProgressMonitor monitor)            throws InterruptedException {        if (monitor.isCanceled()) {			throw new InterruptedException();		}    }    /**     * Returns the currently active modal context thread, or null if no modal context is active.     */    private static ModalContextThread getCurrentModalContextThread() {        Thread t = Thread.currentThread();        if (t instanceof ModalContextThread) {			return (ModalContextThread) t;		}        return null;    }    /**     * Returns the modal nesting level.     * <p>     * The modal nesting level increases by one each time the     * <code>ModalContext.run</code> method is called within the     * dynamic scope of another call to <code>ModalContext.run</code>.     * </p>     *     * @return the modal nesting level, or <code>0</code> if      *  this method is called outside the dynamic scope of any     *  invocation of <code>ModalContext.run</code>     */    public static int getModalLevel() {        return modalLevel;    }    /**     * Returns whether the given thread is running a modal context.     *      * @param thread The thread to be checked     * @return <code>true</code> if the given thread is running a modal context, <code>false</code> if not     */    public static boolean isModalContextThread(Thread thread) {        return thread instanceof ModalContextThread;    }    /**     * Runs the given runnable in a modal context, passing it a progress monitor.     * <p>     * The modal nesting level is increased by one from the perspective     * of the given runnable.     * </p>     *<p>     * If the supplied operation implements <code>IThreadListener</code>, it     * will be notified of any thread changes required to execute the operation.     * Specifically, the operation will be notified of the thread that will call its      * <code>run</code> method before it is called, and will be notified of the     * change of control back to the thread calling this method when the operation     * completes.  These thread change notifications give the operation an      * opportunity to transfer any thread-local state to the execution thread before      * control is transferred to the new thread.     *</p>     * @param operation the runnable to run     * @param fork <code>true</code> if the runnable should run in a separate thread,     *   and <code>false</code> if in the same thread     * @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     * @exception InvocationTargetException if the run method must propagate a checked exception,     * 	it should wrap it inside an <code>InvocationTargetException</code>; runtime exceptions and errors are automatically     *  wrapped in an <code>InvocationTargetException</code> by this method     * @exception InterruptedException if the operation detects a request to cancel,      *  using <code>IProgressMonitor.isCanceled()</code>, it should exit by throwing      *  <code>InterruptedException</code>; this method propagates the exception     */    public static void run(IRunnableWithProgress operation, boolean fork,            IProgressMonitor monitor, Display display)            throws InvocationTargetException, InterruptedException {        Assert.isTrue(operation != null && monitor != null);        modalLevel++;        try {            if (monitor != null) {				monitor.setCanceled(false);			}            // Is the runnable supposed to be execute in the same thread.            if (!fork || !runInSeparateThread) {                runInCurrentThread(operation, monitor);            } else {                ModalContextThread t = getCurrentModalContextThread();                if (t != null) {                    Assert.isTrue(canProgressMonitorBeUsed(monitor,                            t.progressMonitor));                    runInCurrentThread(operation, monitor);                } else {                    t = new ModalContextThread(operation, monitor, display);                	if (operation instanceof IThreadListener) {						((IThreadListener)operation).threadChange(t);					}                    t.start();                    t.block();                    Throwable throwable = t.throwable;                    if (throwable != null) {                        if (debug                                && !(throwable instanceof InterruptedException)                                && !(throwable instanceof OperationCanceledException)) {                            System.err                                    .println("Exception in modal context operation:"); //$NON-NLS-1$                            throwable.printStackTrace();                            System.err.println("Called from:"); //$NON-NLS-1$                            // Don't create the InvocationTargetException on the throwable,                            // otherwise it will print its stack trace (from the other thread).                            new InvocationTargetException(null)                                    .printStackTrace();                        }                        if (throwable instanceof InvocationTargetException) {                            throw (InvocationTargetException) throwable;                        } else if (throwable instanceof InterruptedException) {                            throw (InterruptedException) throwable;                        } else if (throwable instanceof OperationCanceledException) {                            // See 1GAN3L5: ITPUI:WIN2000 - ModalContext converts OperationCancelException into InvocationTargetException                            throw new InterruptedException(throwable                                    .getMessage());                        } else {                            throw new InvocationTargetException(throwable);                        }                    }                }            }        } finally {            modalLevel--;        }    }    /**     * Run a runnable.  Convert all thrown exceptions to      * either InterruptedException or InvocationTargetException     */    private static void runInCurrentThread(IRunnableWithProgress runnable,            IProgressMonitor progressMonitor) throws InterruptedException,            InvocationTargetException {        try {            if (runnable != null) {				runnable.run(progressMonitor);			}        } catch (InvocationTargetException e) {            throw e;        } catch (InterruptedException e) {            throw e;        } catch (OperationCanceledException e) {            throw new InterruptedException();        } catch (ThreadDeath e) {            // Make sure to propagate ThreadDeath, or threads will never fully terminate            throw e;        } catch (RuntimeException e) {            throw new InvocationTargetException(e);        } catch (Error e) {            throw new InvocationTargetException(e);        }    }    /**     * Sets whether ModalContext is running in debug mode.     *     * @param debugMode <code>true</code> for debug mode,      *  and <code>false</code> for normal mode (the default)     */    public static void setDebugMode(boolean debugMode) {        debug = debugMode;    }	/**	 * Sets whether ModalContext may process events (by calling <code>Display.readAndDispatch()</code>)	 * while running operations. By default, ModalContext will process events while running operations.	 * Use this method to disallow event processing temporarily.	 * @param allowReadAndDispatch <code>true</code> (the default) if events may be processed while	 * running an operation, <code>false</code> if Display.readAndDispatch() should not be called	 * from ModalContext.	 * @since 3.2	 */	public static void setAllowReadAndDispatch(boolean allowReadAndDispatch) {		// use a separate thread if and only if it is OK to spin the event loop		runInSeparateThread = allowReadAndDispatch;	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -