📄 dialog.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.util.Arrays;import java.util.HashMap;import org.eclipse.core.runtime.IProgressMonitor;import org.eclipse.core.runtime.IStatus;import org.eclipse.core.runtime.Status;import org.eclipse.jface.resource.ImageDescriptor;import org.eclipse.jface.resource.ImageRegistry;import org.eclipse.jface.resource.JFaceResources;import org.eclipse.jface.util.Policy;import org.eclipse.jface.window.IShellProvider;import org.eclipse.jface.window.SameShellProvider;import org.eclipse.jface.window.Window;import org.eclipse.swt.SWT;import org.eclipse.swt.events.MouseAdapter;import org.eclipse.swt.events.MouseEvent;import org.eclipse.swt.events.MouseListener;import org.eclipse.swt.events.SelectionAdapter;import org.eclipse.swt.events.SelectionEvent;import org.eclipse.swt.graphics.Font;import org.eclipse.swt.graphics.FontData;import org.eclipse.swt.graphics.FontMetrics;import org.eclipse.swt.graphics.GC;import org.eclipse.swt.graphics.Image;import org.eclipse.swt.graphics.Point;import org.eclipse.swt.graphics.Rectangle;import org.eclipse.swt.layout.FormData;import org.eclipse.swt.layout.GridData;import org.eclipse.swt.layout.GridLayout;import org.eclipse.swt.widgets.Button;import org.eclipse.swt.widgets.Composite;import org.eclipse.swt.widgets.Control;import org.eclipse.swt.widgets.Shell;/** * A dialog is a specialized window used for narrow-focused communication with * the user. * <p> * Dialogs are usually modal. Consequently, it is generally bad practice to open * a dialog without a parent. A modal dialog without a parent is not prevented * from disappearing behind the application's other windows, making it very * confusing for the user. * </p> * <p> * If there is more than one modal dialog is open the second one should be * parented off of the shell of the first one otherwise it is possible that the * OS will give focus to the first dialog potentially blocking the UI. * </p> */public abstract class Dialog extends Window { /** * Image registry key for error image (value * <code>"dialog_error_image"</code>). * * @deprecated use * org.eclipse.swt.widgets.Display.getSystemImage(SWT.ICON_ERROR) */ public static final String DLG_IMG_ERROR = "dialog_error_image"; //$NON-NLS-1$ /** * Image registry key for info image (value <code>"dialog_info_image"</code>). * * @deprecated use * org.eclipse.swt.widgets.Display.getSystemImage(SWT.ICON_INFORMATION) */ public static final String DLG_IMG_INFO = "dialog_info_imageg"; //$NON-NLS-1$ /** * Image registry key for question image (value * <code>"dialog_question_image"</code>). * * @deprecated org.eclipse.swt.widgets.Display.getSystemImage(SWT.ICON_QUESTION) */ public static final String DLG_IMG_QUESTION = "dialog_question_image"; //$NON-NLS-1$ /** * Image registry key for warning image (value * <code>"dialog_warning_image"</code>). * * @deprecated use * org.eclipse.swt.widgets.Display.getSystemImage(SWT.ICON_WARNING) */ public static final String DLG_IMG_WARNING = "dialog_warning_image"; //$NON-NLS-1$ /** * Image registry key for info message image (value * <code>"dialog_messasge_info_image"</code>). * * @since 2.0 */ public static final String DLG_IMG_MESSAGE_INFO = "dialog_messasge_info_image"; //$NON-NLS-1$ /** * Image registry key for info message image (value * <code>"dialog_messasge_warning_image"</code>). * * @since 2.0 */ public static final String DLG_IMG_MESSAGE_WARNING = "dialog_messasge_warning_image"; //$NON-NLS-1$ /** * Image registry key for info message image (value * <code>"dialog_message_error_image"</code>). * * @since 2.0 */ public static final String DLG_IMG_MESSAGE_ERROR = "dialog_message_error_image"; //$NON-NLS-1$ /** * Image registry key for help image (value * <code>"dialog_help_image"</code>). * * @since 3.2 */ public static final String DLG_IMG_HELP = "dialog_help_image"; //$NON-NLS-1$ /** * The ellipsis is the string that is used to represent shortened text. * * @since 3.0 */ public static final String ELLIPSIS = "..."; //$NON-NLS-1$ /** * The dialog settings key name for stored dialog x location. * * @since 3.2 */ private static final String DIALOG_ORIGIN_X = "DIALOG_X_ORIGIN"; //$NON-NLS-1$ /** * The dialog settings key name for stored dialog y location. * * @since 3.2 */ private static final String DIALOG_ORIGIN_Y = "DIALOG_Y_ORIGIN"; //$NON-NLS-1$ /** * The dialog settings key name for stored dialog width. * * @since 3.2 */ private static final String DIALOG_WIDTH = "DIALOG_WIDTH"; //$NON-NLS-1$ /** * The dialog settings key name for stored dialog height. * * @since 3.2 */ private static final String DIALOG_HEIGHT = "DIALOG_HEIGHT"; //$NON-NLS-1$ /** * The dialog settings key name for the font used when the dialog * height and width was stored. * *@since 3.2 */ private static final String DIALOG_FONT_DATA = "DIALOG_FONT_NAME"; //$NON-NLS-1$ /** * A value that can be used for stored dialog width or height that * indicates that the default bounds should be used. * * @since 3.2 */ public static final int DIALOG_DEFAULT_BOUNDS = -1; /** * Constants that can be used for specifying the strategy for persisting * dialog bounds. These constants represent bit masks that can be used * together. * *@since 3.2 */ /** * Persist the last location of the dialog. * @since 3.2 */ public static final int DIALOG_PERSISTLOCATION = 0x0001; /** * Persist the last known size of the dialog. * @since 3.2 */ public static final int DIALOG_PERSISTSIZE = 0x0002; /** * NOTE: Dialog does not the following images in the registry DLG_IMG_ERROR * DLG_IMG_INFO DLG_IMG_QUESTION DLG_IMG_WARNING * * They are now coming directly from SWT see ImageRegistry. For backwards * compatibility they are still supported, however new code should use SWT * for these. * * @see Display#getSystemIcon(int ID) */ static { ImageRegistry reg = JFaceResources.getImageRegistry(); reg.put(DLG_IMG_MESSAGE_INFO, ImageDescriptor.createFromFile( Dialog.class, "images/message_info.gif")); //$NON-NLS-1$ reg.put(DLG_IMG_MESSAGE_WARNING, ImageDescriptor.createFromFile( Dialog.class, "images/message_warning.gif")); //$NON-NLS-1$ reg.put(DLG_IMG_MESSAGE_ERROR, ImageDescriptor.createFromFile( Dialog.class, "images/message_error.gif")); //$NON-NLS-1$ } /** * The dialog area; <code>null</code> until dialog is layed out. */ protected Control dialogArea; /** * The button bar; <code>null</code> until dialog is layed out. */ public Control buttonBar; /** * A mouse listener that can be used to restore the default size * of a dialog. * * @since 3.2 */ private MouseListener restoreSizeMouseListener = new MouseAdapter() { public void mouseDoubleClick(MouseEvent event) { restoreDialogToComputedSize(); } }; /** * Collection of buttons created by the <code>createButton</code> method. */ private HashMap buttons = new HashMap(); /** * Font metrics to use for determining pixel sizes. */ private FontMetrics fontMetrics; /** * Point used for storing initial computed size of the dialog so * that it may be restored. */ private Point computedSize; /** * Number of horizontal dialog units per character, value <code>4</code>. */ private static final int HORIZONTAL_DIALOG_UNIT_PER_CHAR = 4; /** * Number of vertical dialog units per character, value <code>8</code>. */ private static final int VERTICAL_DIALOG_UNITS_PER_CHAR = 8; /** * Returns the number of pixels corresponding to the height of the given * number of characters. * <p> * The required <code>FontMetrics</code> parameter may be created in the * following way: <code> * GC gc = new GC(control); * gc.setFont(control.getFont()); * fontMetrics = gc.getFontMetrics(); * gc.dispose(); * </code> * </p> * * @param fontMetrics * used in performing the conversion * @param chars * the number of characters * @return the number of pixels * @since 2.0 */ public static int convertHeightInCharsToPixels(FontMetrics fontMetrics, int chars) { return fontMetrics.getHeight() * chars; } /** * Returns the number of pixels corresponding to the given number of * horizontal dialog units. * <p> * The required <code>FontMetrics</code> parameter may be created in the * following way: <code> * GC gc = new GC(control); * gc.setFont(control.getFont()); * fontMetrics = gc.getFontMetrics(); * gc.dispose(); * </code> * </p> * * @param fontMetrics * used in performing the conversion * @param dlus * the number of horizontal dialog units * @return the number of pixels * @since 2.0 */ public static int convertHorizontalDLUsToPixels(FontMetrics fontMetrics, int dlus) { // round to the nearest pixel return (fontMetrics.getAverageCharWidth() * dlus + HORIZONTAL_DIALOG_UNIT_PER_CHAR / 2) / HORIZONTAL_DIALOG_UNIT_PER_CHAR; } /** * Returns the number of pixels corresponding to the given number of * vertical dialog units. * <p> * The required <code>FontMetrics</code> parameter may be created in the * following way: <code> * GC gc = new GC(control); * gc.setFont(control.getFont()); * fontMetrics = gc.getFontMetrics(); * gc.dispose(); * </code> * </p> * * @param fontMetrics * used in performing the conversion * @param dlus * the number of vertical dialog units * @return the number of pixels * @since 2.0 */ public static int convertVerticalDLUsToPixels(FontMetrics fontMetrics, int dlus) { // round to the nearest pixel return (fontMetrics.getHeight() * dlus + VERTICAL_DIALOG_UNITS_PER_CHAR / 2) / VERTICAL_DIALOG_UNITS_PER_CHAR; } /** * Returns the number of pixels corresponding to the width of the given * number of characters. * <p> * The required <code>FontMetrics</code> parameter may be created in the * following way: <code> * GC gc = new GC(control); * gc.setFont(control.getFont()); * fontMetrics = gc.getFontMetrics(); * gc.dispose(); * </code> * </p> * * @param fontMetrics * used in performing the conversion * @param chars * the number of characters * @return the number of pixels * @since 2.0 */ public static int convertWidthInCharsToPixels(FontMetrics fontMetrics, int chars) { return fontMetrics.getAverageCharWidth() * chars; } /** * Shortens the given text <code>textValue</code> so that its width in * pixels does not exceed the width of the given control. Overrides * characters in the center of the original string with an ellipsis ("...") * if necessary. If a <code>null</code> value is given, <code>null</code> * is returned. * * @param textValue * the original string or <code>null</code> * @param control * the control the string will be displayed on * @return the string to display, or <code>null</code> if null was passed * in * * @since 3.0 */ public static String shortenText(String textValue, Control control) { if (textValue == null) { return null; } GC gc = new GC(control); int maxWidth = control.getBounds().width - 5; if (gc.textExtent(textValue).x < maxWidth) { gc.dispose(); return textValue; } int length = textValue.length(); int pivot = length / 2; int start = pivot; int end = pivot + 1; while (start >= 0 && end < length) { String s1 = textValue.substring(0, start); String s2 = textValue.substring(end, length); String s = s1 + ELLIPSIS + s2; int l = gc.textExtent(s).x; if (l < maxWidth) { gc.dispose(); return s; } start--; end++; } gc.dispose(); return textValue; } /** * Create a default instance of the blocked handler which does not do * anything. */ public static IDialogBlockedHandler blockedHandler = new IDialogBlockedHandler() { /* * (non-Javadoc) * * @see org.eclipse.jface.dialogs.IDialogBlockedHandler#clearBlocked() */ public void clearBlocked() { // No default behaviour } /* * (non-Javadoc) * * @see org.eclipse.jface.dialogs.IDialogBlockedHandler#showBlocked(org.eclipse.core.runtime.IProgressMonitor, * org.eclipse.core.runtime.IStatus, java.lang.String) */ public void showBlocked(IProgressMonitor blocking, IStatus blockingStatus, String blockedName) { // No default behaviour } /* * (non-Javadoc) * * @see org.eclipse.jface.dialogs.IDialogBlockedHandler#showBlocked(org.eclipse.swt.widgets.Shell, * org.eclipse.core.runtime.IProgressMonitor, * org.eclipse.core.runtime.IStatus, java.lang.String) */ public void showBlocked(Shell parentShell, IProgressMonitor blocking, IStatus blockingStatus, String blockedName) { // No default behaviour } }; /** * Creates a dialog instance. Note that the window will have no visual * representation (no widgets) until it is told to open. By default, * <code>open</code> blocks for dialogs. * * @param parentShell * the parent shell, or <code>null</code> to create a top-level * shell */ protected Dialog(Shell parentShell) { this(new SameShellProvider(parentShell)); if (parentShell == null && Policy.DEBUG_DIALOG_NO_PARENT) { Policy.getLog().log( new Status(IStatus.INFO, Policy.JFACE, IStatus.INFO, this .getClass() + " created with no shell",//$NON-NLS-1$ new Exception())); } } /** * Creates a dialog with the given parent.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -