📄 dialog.java
字号:
protected void initializeDialogUnits(Control control) { // Compute and store a font metric GC gc = new GC(control); gc.setFont(JFaceResources.getDialogFont()); fontMetrics = gc.getFontMetrics(); gc.dispose(); } /** * Notifies that the ok button of this dialog has been pressed. * <p> * The <code>Dialog</code> implementation of this framework method sets * this dialog's return code to <code>Window.OK</code> and closes the * dialog. Subclasses may override. * </p> */ protected void okPressed() { setReturnCode(OK); close(); } /** * Set the layout data of the button to a GridData with appropriate heights * and widths. * * @param button */ protected void setButtonLayoutData(Button button) { GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL); int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH); Point minSize = button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true); data.widthHint = Math.max(widthHint, minSize.x); button.setLayoutData(data); } /** * Set the layout data of the button to a FormData with appropriate heights * and widths. * * @param button */ protected void setButtonLayoutFormData(Button button) { FormData data = new FormData(); int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH); Point minSize = button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true); data.width = Math.max(widthHint, minSize.x); button.setLayoutData(data); } /** * @see org.eclipse.jface.window.Window#close() */ public boolean close() { if (getShell() != null && !getShell().isDisposed()) { saveDialogBounds(getShell()); } removeRestoreSizeMouseListeners(); boolean returnValue = super.close(); if (returnValue) { buttons = new HashMap(); buttonBar = null; dialogArea = null; } return returnValue; } /** * Applies the dialog font to all controls that currently have the default * font. * * @param control * the control to apply the font to. Font will also be applied to * its children. If the control is <code>null</code> nothing * happens. */ public static void applyDialogFont(Control control) { if (control == null || dialogFontIsDefault()) { return; } Font dialogFont = JFaceResources.getDialogFont(); applyDialogFont(control, dialogFont); } /** * Sets the dialog font on the control and any of its children if thier font * is not otherwise set. * * @param control * the control to apply the font to. Font will also be applied to * its children. * @param dialogFont * the dialog font to set */ private static void applyDialogFont(Control control, Font dialogFont) { if (hasDefaultFont(control)) { control.setFont(dialogFont); } if (control instanceof Composite) { Control[] children = ((Composite) control).getChildren(); for (int i = 0; i < children.length; i++) { applyDialogFont(children[i], dialogFont); } } } /** * Return whether or not this control has the same font as it's default. * * @param control * Control * @return boolean */ private static boolean hasDefaultFont(Control control) { FontData[] controlFontData = control.getFont().getFontData(); FontData[] defaultFontData = getDefaultFont(control).getFontData(); if (controlFontData.length == defaultFontData.length) { for (int i = 0; i < controlFontData.length; i++) { if (controlFontData[i].equals(defaultFontData[i])) { continue; } return false; } return true; } return false; } /** * Get the default font for this type of control. * * @param control * @return the default font */ private static Font getDefaultFont(Control control) { String fontName = "DEFAULT_FONT_" + control.getClass().getName(); //$NON-NLS-1$ if (JFaceResources.getFontRegistry().hasValueFor(fontName)) { return JFaceResources.getFontRegistry().get(fontName); } Font cached = control.getFont(); control.setFont(null); Font defaultFont = control.getFont(); control.setFont(cached); JFaceResources.getFontRegistry().put(fontName, defaultFont.getFontData()); return defaultFont; } /** * Return whether or not the dialog font is currently the same as the * default font. * * @return boolean if the two are the same */ protected static boolean dialogFontIsDefault() { FontData[] dialogFontData = JFaceResources.getFontRegistry() .getFontData(JFaceResources.DIALOG_FONT); FontData[] defaultFontData = JFaceResources.getFontRegistry() .getFontData(JFaceResources.DEFAULT_FONT); return Arrays.equals(dialogFontData, defaultFontData); } /* * (non-Javadoc) * * @see org.eclipse.jface.window.Window#create() */ public void create() { super.create(); applyDialogFont(buttonBar); // Register a mouse listener so that the user can restore this // size with a double-click. // See https://bugs.eclipse.org/bugs/show_bug.cgi?id=116906 addRestoreSizeMouseListeners(); } /** * Get the IDialogBlockedHandler to be used by WizardDialogs and * ModalContexts. * * @return Returns the blockedHandler. */ public static IDialogBlockedHandler getBlockedHandler() { return blockedHandler; } /** * Set the IDialogBlockedHandler to be used by WizardDialogs and * ModalContexts. * * @param blockedHandler * The blockedHandler for the dialogs. */ public static void setBlockedHandler(IDialogBlockedHandler blockedHandler) { Dialog.blockedHandler = blockedHandler; } /** * Gets the dialog settings that should be used for remembering the bounds of * of the dialog, according to the dialog bounds strategy. * * @return settings the dialog settings used to store the dialog's location * and/or size, or <code>null</code> if the dialog's bounds should * never be stored. * * @since 3.2 * @see Dialog#getDialogBoundsStrategy() */ protected IDialogSettings getDialogBoundsSettings() { return null; } /** * Get the integer constant that describes the strategy for persisting the * dialog bounds. This strategy is ignored if the implementer does not also * specify the dialog settings for storing the bounds in * Dialog.getDialogBoundsSettings(). * * @return the constant describing the strategy for persisting the dialog * bounds. * * @since 3.2 * @see Dialog#DIALOG_PERSISTLOCATION * @see Dialog#DIALOG_PERSISTSIZE * @see Dialog#getDialogBoundsSettings() */ protected int getDialogBoundsStrategy() { return DIALOG_PERSISTLOCATION | DIALOG_PERSISTSIZE; } /** * Saves the bounds of the shell in the appropriate dialog settings. The * bounds are recorded relative to the parent shell, if there is one, or * display coordinates if there is no parent shell. * * @param shell * The shell whose bounds are to be stored * * @since 3.2 */ private void saveDialogBounds(Shell shell) { IDialogSettings settings = getDialogBoundsSettings(); if (settings != null) { Point shellLocation = shell.getLocation(); Point shellSize = shell.getSize(); Shell parent = getParentShell(); if (parent != null) { Point parentLocation = parent.getLocation(); shellLocation.x -= parentLocation.x; shellLocation.y -= parentLocation.y; } int strategy = getDialogBoundsStrategy(); if ((strategy & DIALOG_PERSISTLOCATION) != 0) { settings.put(DIALOG_ORIGIN_X, shellLocation.x); settings.put(DIALOG_ORIGIN_Y, shellLocation.y); } if ((strategy & DIALOG_PERSISTSIZE) != 0) { settings.put(DIALOG_WIDTH, shellSize.x); settings.put(DIALOG_HEIGHT, shellSize.y); FontData [] fontDatas = JFaceResources.getDialogFont().getFontData(); if (fontDatas.length > 0) { settings.put(DIALOG_FONT_DATA, fontDatas[0].toString()); } } } } /** * Returns the initial size to use for the shell. Overridden * to check whether a size has been stored in dialog settings. * If a size has been stored, it is returned. * * @return the initial size of the shell * * @since 3.2 * @see #getDialogBoundsSettings() * @see #getDialogBoundsStrategy() */ protected Point getInitialSize() { Point result = super.getInitialSize(); // Check the dialog settings for a stored size. if ((getDialogBoundsStrategy() & DIALOG_PERSISTSIZE)!= 0) { IDialogSettings settings = getDialogBoundsSettings(); if (settings != null) { // Check that the dialog font matches the font used // when the bounds was stored. If the font has changed, // we do not honor the stored settings. // See https://bugs.eclipse.org/bugs/show_bug.cgi?id=132821 boolean useStoredBounds = true; String previousDialogFontData = settings.get(DIALOG_FONT_DATA); // There is a previously stored font, so we will check it. // Note that if we haven't stored the font before, then we will // use the stored bounds. This allows restoring of dialog bounds // that were stored before we started storing the fontdata. if (previousDialogFontData != null && previousDialogFontData.length() > 0) { FontData [] fontDatas = JFaceResources.getDialogFont().getFontData(); if (fontDatas.length > 0) { String currentDialogFontData = fontDatas[0].toString(); useStoredBounds = currentDialogFontData.equalsIgnoreCase(previousDialogFontData); } } if (useStoredBounds) { try { // Get the stored width and height. int width = settings.getInt(DIALOG_WIDTH); if (width != DIALOG_DEFAULT_BOUNDS) { result.x = width; } int height = settings.getInt(DIALOG_HEIGHT); if (height != DIALOG_DEFAULT_BOUNDS) { result.y = height; } } catch (NumberFormatException e) { } } } } // No attempt is made to constrain the bounds. The default // constraining behavior in Window will be used. return result; } /** * Returns the initial location to use for the shell. Overridden * to check whether the bounds of the dialog have been stored in * dialog settings. If a location has been stored, it is returned. * * @param initialSize * the initial size of the shell, as returned by * <code>getInitialSize</code>. * @return the initial location of the shell * * @since 3.2 * @see #getDialogBoundsSettings() * @see #getDialogBoundsStrategy() */ protected Point getInitialLocation(Point initialSize) { Point result = super.getInitialLocation(initialSize); if ((getDialogBoundsStrategy() & DIALOG_PERSISTLOCATION)!= 0) { IDialogSettings settings = getDialogBoundsSettings(); if (settings != null) { try { int x = settings.getInt(DIALOG_ORIGIN_X); int y = settings.getInt(DIALOG_ORIGIN_Y); result = new Point(x, y); // The coordinates were stored relative to the parent shell. // Convert to display coordinates. Shell parent = getParentShell(); if (parent != null) { Point parentLocation = parent.getLocation(); result.x += parentLocation.x; result.y += parentLocation.y; } } catch (NumberFormatException e) { } } } // No attempt is made to constrain the bounds. The default // constraining behavior in Window will be used. return result; } /** * Add mouse listeners as needed to provide dialog size restore * behavior. Double-clicking in unused areas will restore * the dialog size. * * @since 3.2 */ private void addRestoreSizeMouseListeners() { // Hook a double click event for restoring the dialog's computed // size. We hook onto the button bar and the contents, and any // nested composites in between, in order to accomodate different // layout and construction styles. Control dialogContents = getContents(); if (buttonBar != null) { // Hook onto the button bar composite and // any nested composites within the button bar. // See https://bugs.eclipse.org/bugs/show_bug.cgi?id=137315 addRestoreSizeMouseListenerToComposites(buttonBar); // Hook onto any nested composites between the button bar // and the contents. Control control = buttonBar.getParent(); while (control != dialogContents && control != null) { control.addMouseListener(restoreSizeMouseListener); control = control.getParent(); } } if (dialogContents != null) { dialogContents.addMouseListener(restoreSizeMouseListener); } } /** * Add mouse listeners to the specified control if it is a composite, * and any child composites. Called recursively. * * @since 3.2 */ private void addRestoreSizeMouseListenerToComposites(Control control) { if (control instanceof Composite) { control.addMouseListener(restoreSizeMouseListener); Control [] children = ((Composite)control).getChildren(); for (int i=0; i<children.length; i++) { addRestoreSizeMouseListenerToComposites(children[i]); } } } /** * Remove any mouse listeners that were registered. * * @since 3.2 */ private void removeRestoreSizeMouseListeners() { Control dialogContents = getContents(); if (buttonBar != null && !buttonBar.isDisposed()) { buttonBar.removeMouseListener(restoreSizeMouseListener); Control control = buttonBar.getParent(); while (control != dialogContents && control != null && !control.isDisposed()) { control.removeMouseListener(restoreSizeMouseListener); control = control.getParent(); } } if (dialogContents != null && !dialogContents.isDisposed()) { dialogContents.removeMouseListener(restoreSizeMouseListener); } } /** * Restore the dialog to its initially computed size, resetting * any bounds that may have been stored in dialog settings. * * @since 3.2 */ private void restoreDialogToComputedSize() { // The computed size was never stored. This should not typically // happen, but could if a client completely override the bounds initialization. if (computedSize == null) { return; } Shell shell = getShell(); Point shellSize = shell.getSize(); Point shellLocation = shell.getLocation(); // If the size has not changed, do nothing if (shellSize.equals(computedSize)) { return; } // Now reset the bounds shell.setBounds(getConstrainedShellBounds(new Rectangle(shellLocation.x, shellLocation.y, computedSize.x, computedSize.y))); // If we do store the bounds, update the value so default bounds // will be used. IDialogSettings settings = getDialogBoundsSettings(); if (settings != null) { int strategy = getDialogBoundsStrategy(); if ((strategy & DIALOG_PERSISTSIZE) != 0) { settings.put(DIALOG_WIDTH, DIALOG_DEFAULT_BOUNDS); settings.put(DIALOG_HEIGHT, DIALOG_DEFAULT_BOUNDS); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -