📄 window.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.window;import java.util.ArrayList;import org.eclipse.jface.resource.JFaceResources;import org.eclipse.jface.util.Assert;import org.eclipse.jface.util.Geometry;import org.eclipse.jface.util.IPropertyChangeListener;import org.eclipse.jface.util.PropertyChangeEvent;import org.eclipse.swt.SWT;import org.eclipse.swt.events.ShellAdapter;import org.eclipse.swt.events.ShellEvent;import org.eclipse.swt.events.ShellListener;import org.eclipse.swt.graphics.Image;import org.eclipse.swt.graphics.Point;import org.eclipse.swt.graphics.Rectangle;import org.eclipse.swt.layout.GridLayout;import org.eclipse.swt.widgets.Composite;import org.eclipse.swt.widgets.Control;import org.eclipse.swt.widgets.Display;import org.eclipse.swt.widgets.Event;import org.eclipse.swt.widgets.Layout;import org.eclipse.swt.widgets.Listener;import org.eclipse.swt.widgets.Monitor;import org.eclipse.swt.widgets.Shell;/** * A JFace window is an object that has no visual representation (no widgets) * until it is told to open. * <p> * Creating a window involves the following steps: * <ul> * <li>creating an instance of a concrete subclass of <code>Window</code> * </li> * <li>creating the window's shell and widget tree by calling * <code>create</code> (optional)</li> * <li>assigning the window to a window manager using * <code>WindowManager.add</code> (optional)</li> * <li>opening the window by calling <code>open</code></li> * </ul> * Opening the window will create its shell and widget tree if they have not * already been created. When the window is closed, the shell and widget tree * are disposed of and are no longer referenced, and the window is automatically * removed from its window manager. A window may be reopened. * </p> * <p> * The JFace window framework (this package) consists of this class, * <code>Window</code>, the abstract base of all windows, and one concrete * window classes (<code>ApplicationWindow</code>) which may also be * subclassed. Clients may define additional window subclasses as required. * </p> * <p> * The <code>Window</code> class provides methods that subclasses may * override to configure the window, including: * <ul> * <li><code>close</code>- extend to free other SWT resources</li> * <li><code>configureShell</code>- extend or reimplement to set shell * properties before window opens</li> * <li><code>createContents</code>- extend or reimplement to create controls * before window opens</li> * <li><code>getInitialSize</code>- reimplement to give the initial size for * the shell</li> * <li><code>getInitialLocation</code>- reimplement to give the initial * location for the shell</li> * <li><code>getShellListener</code>- extend or reimplement to receive shell * events</li> * <li><code>handleFontChange</code>- reimplement to respond to font changes * </li> * <li><code>handleShellCloseEvent</code>- extend or reimplement to handle * shell closings</li> * </ul> * </p> */public abstract class Window implements IShellProvider { /** * Standard return code constant (value 0) indicating that the window was * opened. * * @see #open */ public static final int OK = 0; /** * Standard return code constant (value 1) indicating that the window was * canceled. * * @see #open */ public static final int CANCEL = 1; /** * An array of images to be used for the window. It is expected that the * array will contain the same icon rendered at different resolutions. */ private static Image[] defaultImages; /** * This interface defines a Exception Handler which can be set as a global * handler and will be called if an exception happens in the event loop. */ public static interface IExceptionHandler { /** * Handle the exception. * * @param t * The exception that occured. */ public void handleException(Throwable t); } /** * Defines a default exception handler. */ private static class DefaultExceptionHandler implements IExceptionHandler { /* * (non-Javadoc) * * @see org.eclipse.jface.window.Window.IExceptionHandler#handleException(java.lang.Throwable) */ public void handleException(Throwable t) { if (t instanceof ThreadDeath) { // Don't catch ThreadDeath as this is a normal occurrence when // the thread dies throw (ThreadDeath) t; } // Try to keep running. t.printStackTrace(); } } /** * The exception handler for this application. */ private static IExceptionHandler exceptionHandler = new DefaultExceptionHandler(); /** * The default orientation of the window. By default * it is SWT#NONE but it can also be SWT#LEFT_TO_RIGHT * or SWT#RIGHT_TO_LEFT */ private static int orientation = SWT.NONE; /** * Object used to locate the default parent for modal shells */ private static IShellProvider defaultModalParent = new IShellProvider() { public Shell getShell() { Display d = Display.getCurrent(); if (d == null) { return null; } Shell parent = d.getActiveShell(); // Make sure we don't pick a parent that has a modal child (this can lock the app) if (parent == null) { // If this is a top-level window, then there must not be any open modal windows. parent = getModalChild(Display.getCurrent().getShells()); } else { // If we picked a parent with a modal child, use the modal child instead Shell modalChild = getModalChild(parent.getShells()); if (modalChild != null) { parent = modalChild; } } return parent; } }; /** * Object that returns the parent shell. */ private IShellProvider parentShell; /** * Shell style bits. * * @see #setShellStyle */ private int shellStyle = SWT.SHELL_TRIM; /** * Window manager, or <code>null</code> if none. * * @see #setWindowManager */ private WindowManager windowManager; /** * Window shell, or <code>null</code> if none. */ private Shell shell; /** * Top level SWT control, or <code>null</code> if none */ private Control contents; /** * Window return code; initially <code>OK</code>. * * @see #setReturnCode */ private int returnCode = OK; /** * <code>true</code> if the <code>open</code> method should not return * until the window closes, and <code>false</code> if the * <code>open</code> method should return immediately; initially * <code>false</code> (non-blocking). * * @see #setBlockOnOpen */ private boolean block = false; /** * Internal class for informing this window when fonts change. */ private class FontChangeListener implements IPropertyChangeListener { public void propertyChange(PropertyChangeEvent event) { handleFontChange(event); } } /** * Internal font change listener. */ private FontChangeListener fontChangeListener; /** * Internal fields to detect if shell size has been set */ private boolean resizeHasOccurred = false; private Listener resizeListener; /** * Creates a window instance, whose shell will be created under the given * parent shell. Note that the window will have no visual representation * until it is told to open. By default, <code>open</code> does not block. * * @param parentShell * the parent shell, or <code>null</code> to create a top-level * shell. Try passing "(Shell)null" to this method instead of "null" * if your compiler complains about an ambiguity error. * @see #setBlockOnOpen * @see #getDefaultOrientation() */ protected Window(Shell parentShell) { this(new SameShellProvider(parentShell)); if(parentShell == null) { setShellStyle(getShellStyle() | getDefaultOrientation()); } } /** * Creates a new window which will create its shell as a child of whatever * the given shellProvider returns. * * @param shellProvider object that will return the current parent shell. Not null. * * @since 3.1 */ protected Window(IShellProvider shellProvider) { Assert.isNotNull(shellProvider); this.parentShell = shellProvider; } /** * Determines if the window should handle the close event or do nothing. * <p> * The default implementation of this framework method returns * <code>true</code>, which will allow the * <code>handleShellCloseEvent</code> method to be called. Subclasses may * extend or reimplement. * </p> * * @return whether the window should handle the close event. */ protected boolean canHandleShellCloseEvent() { return true; } /** * Closes this window, disposes its shell, and removes this window from its * window manager (if it has one). * <p> * This framework method may be extended (<code>super.close</code> must * be called). * </p> * * @return <code>true</code> if the window is (or was already) closed, and * <code>false</code> if it is still open */ public boolean close() { // stop listening for font changes if (fontChangeListener != null) { JFaceResources.getFontRegistry().removeListener(fontChangeListener); fontChangeListener = null; } // remove this window from a window manager if it has one if (windowManager != null) { windowManager.remove(this); windowManager = null; } if (shell == null || shell.isDisposed()) { return true; } // If we "close" the shell recursion will occur. // Instead, we need to "dispose" the shell to remove it from the // display. shell.dispose(); shell = null; contents = null; return true; } /** * Configures the given shell in preparation for opening this window in it. * <p> * The default implementation of this framework method sets the shell's * image and gives it a grid layout. Subclasses may extend or reimplement. * </p> * * @param newShell * the shell */ protected void configureShell(Shell newShell) { // The single image version of this code had a comment related to bug // 46624, // and some code that did nothing if the stored image was already // disposed. // The equivalent in the multi-image version seems to be to remove the // disposed images from the array passed to the shell. if (defaultImages != null && defaultImages.length > 0) { ArrayList nonDisposedImages = new ArrayList(defaultImages.length); for (int i = 0; i < defaultImages.length; ++i) { if (defaultImages[i] != null && !defaultImages[i].isDisposed()) { nonDisposedImages.add(defaultImages[i]);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -