📄 popupdialog.java
字号:
/******************************************************************************* * Copyright (c) 2005, 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.util.ArrayList;import java.util.List;import org.eclipse.jface.action.Action;import org.eclipse.jface.action.GroupMarker;import org.eclipse.jface.action.IAction;import org.eclipse.jface.action.IMenuManager;import org.eclipse.jface.action.MenuManager;import org.eclipse.jface.action.Separator;import org.eclipse.jface.resource.ImageDescriptor;import org.eclipse.jface.resource.JFaceResources;import org.eclipse.jface.window.Window;import org.eclipse.swt.SWT;import org.eclipse.swt.events.DisposeEvent;import org.eclipse.swt.events.DisposeListener;import org.eclipse.swt.events.SelectionAdapter;import org.eclipse.swt.events.SelectionEvent;import org.eclipse.swt.graphics.Color;import org.eclipse.swt.graphics.Font;import org.eclipse.swt.graphics.FontData;import org.eclipse.swt.graphics.Image;import org.eclipse.swt.graphics.Point;import org.eclipse.swt.graphics.Rectangle;import org.eclipse.swt.layout.GridData;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.Label;import org.eclipse.swt.widgets.Listener;import org.eclipse.swt.widgets.Menu;import org.eclipse.swt.widgets.Shell;import org.eclipse.swt.widgets.ToolBar;import org.eclipse.swt.widgets.ToolItem;import org.eclipse.swt.widgets.Tracker;/** * A lightweight, transient dialog that is popped up to show contextual or * temporal information and is easily dismissed. Clients control whether the * dialog should be able to receive input focus. An optional title area at the * top and an optional info area at the bottom can be used to provide additional * information. * <p> * Because the dialog is short-lived, most of the configuration of the dialog is * done in the constructor. Set methods are only provided for those values that * are expected to be dynamically computed based on a particular instance's * internal state. * <p> * Clients are expected to override the creation of the main dialog area, and * may optionally override the creation of the title area and info area in order * to add content. In general, however, the creation of stylistic features, such * as the dialog menu, separator styles, and fonts, is kept private so that all * popup dialogs will have a similar appearance. * * @since 3.2 */public class PopupDialog extends Window { /** * The dialog settings key name for stored dialog x location. */ private static final String DIALOG_ORIGIN_X = "DIALOG_X_ORIGIN"; //$NON-NLS-1$ /** * The dialog settings key name for stored dialog y location. */ private static final String DIALOG_ORIGIN_Y = "DIALOG_Y_ORIGIN"; //$NON-NLS-1$ /** * The dialog settings key name for stored dialog width. */ private static final String DIALOG_WIDTH = "DIALOG_WIDTH"; //$NON-NLS-1$ /** * The dialog settings key name for stored dialog height. */ private static final String DIALOG_HEIGHT = "DIALOG_HEIGHT"; //$NON-NLS-1$ /** * The dialog settings key name for remembering if the persisted bounds * should be accessed. */ private static final String DIALOG_USE_PERSISTED_BOUNDS = "DIALOG_USE_PERSISTED_BOUNDS"; //$NON-NLS-1$ /** * Move action for the dialog. */ private class MoveAction extends Action { MoveAction() { super(JFaceResources.getString("PopupDialog.move"), //$NON-NLS-1$ IAction.AS_PUSH_BUTTON); } /* * (non-Javadoc) * * @see org.eclipse.jface.action.IAction#run() */ public void run() { performTrackerAction(SWT.NONE); } } /** * Resize action for the dialog. */ private class ResizeAction extends Action { ResizeAction() { super(JFaceResources.getString("PopupDialog.resize"), //$NON-NLS-1$ IAction.AS_PUSH_BUTTON); } /* * @see org.eclipse.jface.action.Action#run() */ public void run() { performTrackerAction(SWT.RESIZE); } } /** * * Remember bounds action for the dialog. */ private class PersistBoundsAction extends Action { PersistBoundsAction() { super(JFaceResources.getString("PopupDialog.persistBounds"), //$NON-NLS-1$ IAction.AS_CHECK_BOX); setChecked(persistBounds); } /* * (non-Javadoc) * * @see org.eclipse.jface.action.IAction#run() */ public void run() { persistBounds = isChecked(); } } /** * Shell style appropriate for a simple hover popup that cannot get focus. */ public final static int HOVER_SHELLSTYLE = SWT.NO_FOCUS | SWT.ON_TOP | SWT.NO_TRIM; /** * Shell style appropriate for an info popup that can get focus. */ public final static int INFOPOPUP_SHELLSTYLE = SWT.NO_TRIM; /** * Shell style appropriate for a resizable info popup that can get focus. */ public final static int INFOPOPUPRESIZE_SHELLSTYLE = SWT.RESIZE; /** * Margin width (in pixels) to be used in layouts inside popup dialogs * (value is 0). */ public final static int POPUP_MARGINWIDTH = 0; /** * Margin height (in pixels) to be used in layouts inside popup dialogs * (value is 0). */ public final static int POPUP_MARGINHEIGHT = 0; /** * Vertical spacing (in pixels) between cells in the layouts inside popup * dialogs (value is 1). */ public final static int POPUP_VERTICALSPACING = 1; /** * Vertical spacing (in pixels) between cells in the layouts inside popup * dialogs (value is 1). */ public final static int POPUP_HORIZONTALSPACING = 1; /** * Border thickness in pixels. */ private static final int BORDER_THICKNESS = 1; /** * The dialog's toolbar for the move and resize capabilities. */ private ToolBar toolBar = null; /** * The dialog's menu manager. */ private MenuManager menuManager = null; /** * The control representing the main dialog area. */ private Control dialogArea; /** * Labels that contain title and info text. Cached so they can be updated * dynamically if possible. */ private Label titleLabel, infoLabel; /** * Separator controls. Cached so they can be excluded from color changes. */ private Control titleSeparator, infoSeparator; /** * The images for the dialog menu. */ private Image menuImage, disabledMenuImage = null; /** * Font to be used for the info area text. Computed based on the dialog's * font. */ private Font infoFont; /** * Font to be used for the title area text. Computed based on the dialog's * font. */ private Font titleFont; /** * Flags indicating whether we are listening for shell deactivate events, * either those or our parent's. Used to prevent closure when a menu command * is chosen or a secondary popup is launched. */ private boolean listenToDeactivate; private boolean listenToParentDeactivate; private Listener parentDeactivateListener; /** * Flag indicating whether focus should be taken when the dialog is opened. */ private boolean takeFocusOnOpen = false; /** * Flag specifying whether a menu should be shown that allows the user to * move and resize. */ private boolean showDialogMenu = false; /** * Flag specifying whether a menu action allowing the user to choose whether * the dialog bounds should be persisted is to be shown. */ private boolean showPersistAction = false; /** * Flag specifying whether the bounds of the popup should be persisted. This * flag is updated by a menu if the menu is shown. */ private boolean persistBounds = false; /** * Text to be shown in an optional title area (on top). */ private String titleText; /** * Text to be shown in an optional info area (at the bottom). */ private String infoText; /** * Constructs a new instance of <code>PopupDialog</code>. * * @param parent * The parent shell. * @param shellStyle * The shell style. * @param takeFocusOnOpen * A boolean indicating whether focus should be taken by this * popup when it opens. * @param persistBounds * A boolean indicating whether the bounds should be persisted * upon close of the dialog. The bounds can only be persisted if * the dialog settings for persisting the bounds are also * specified. If a menu action will be provided that allows the * user to control this feature, then the last known value of the * user's setting will be used instead of this flag. * @param showDialogMenu * A boolean indicating whether a menu for moving and resizing * the popup should be provided. * @param showPersistAction * A boolean indicating whether an action allowing the user to * control the persisting of the dialog bounds should be shown in * the dialog menu. This parameter has no effect if * <code>showDialogMenu</code> is <code>false</code>. * @param titleText * Text to be shown in an upper title area, or <code>null</code> * if there is no title. * @param infoText * Text to be shown in a lower info area, or <code>null</code> * if there is no info area. * * @see PopupDialog#getDialogSettings() */ public PopupDialog(Shell parent, int shellStyle, boolean takeFocusOnOpen, boolean persistBounds, boolean showDialogMenu, boolean showPersistAction, String titleText, String infoText) { super(parent); setShellStyle(shellStyle); this.takeFocusOnOpen = takeFocusOnOpen; this.showDialogMenu = showDialogMenu; this.showPersistAction = showPersistAction; this.titleText = titleText; this.infoText = infoText; setBlockOnOpen(false); this.persistBounds = persistBounds; initializeWidgetState(); } /* * (non-Javadoc) * * @see org.eclipse.jface.window.Window#configureShell(Shell) */ protected void configureShell(Shell shell) { GridLayout layout; Display display = shell.getDisplay(); shell.setBackground(display.getSystemColor(SWT.COLOR_BLACK)); layout = new GridLayout(1, false); int border = ((getShellStyle() & SWT.NO_TRIM) == 0) ? 0 : BORDER_THICKNESS; layout.marginHeight = border; layout.marginWidth = border; shell.setLayout(layout); shell.addListener(SWT.Deactivate, new Listener() { public void handleEvent(Event event) { /* * Close if we are deactivating and have no child shells. If we * have child shells, we are deactivating due to their opening. * On X, we receive this when a menu child (such as the system * menu) of the shell opens, but I have not found a way to * distinguish that case here. Hence bug #113577 still exists. */ if (listenToDeactivate && event.widget == getShell() && getShell().getShells().length == 0) { close(); } else { /* We typically ignore deactivates to work around platform-specific * event ordering. Now that we've ignored whatever we were supposed to, * start listening to deactivates. Example issues can be found in * https://bugs.eclipse.org/bugs/show_bug.cgi?id=123392 */ listenToDeactivate = true; } } }); // Set this true whenever we activate. It may have been turned // off by a menu or secondary popup showing. shell.addListener(SWT.Activate, new Listener() { public void handleEvent(Event event) { // ignore this event if we have launched a child if (event.widget == getShell() && getShell().getShells().length == 0) { listenToDeactivate = true; // Typically we start listening for parent deactivate after // we are activated, except on the Mac, where the deactivate // is received after activate. // See https://bugs.eclipse.org/bugs/show_bug.cgi?id=100668 listenToParentDeactivate = !"carbon".equals(SWT.getPlatform()); //$NON-NLS-1$ } } }); if ((getShellStyle() & SWT.ON_TOP) != 0 && shell.getParent() != null) { parentDeactivateListener= new Listener() { public void handleEvent(Event event) { if (listenToParentDeactivate) { close(); } else { // Our first deactivate, now start listening on the Mac. listenToParentDeactivate = listenToDeactivate; } } }; shell.getParent().addListener(SWT.Deactivate, parentDeactivateListener); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -