📄 titleareadialog.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 * Konstantin Scheglov <scheglov_ke@nlmk.ru > - Fix for bug 41172 * [Dialogs] Bug with Image in TitleAreaDialog * Sebastian Davids <sdavids@gmx.de> - Fix for bug 82064 * [Dialogs] TitleAreaDialog#setTitleImage cannot be called before open() *******************************************************************************/package org.eclipse.jface.dialogs;import org.eclipse.jface.resource.ImageDescriptor;import org.eclipse.jface.resource.ImageRegistry;import org.eclipse.jface.resource.JFaceColors;import org.eclipse.jface.resource.JFaceResources;import org.eclipse.jface.util.Policy;import org.eclipse.swt.SWT;import org.eclipse.swt.events.DisposeEvent;import org.eclipse.swt.events.DisposeListener;import org.eclipse.swt.graphics.Color;import org.eclipse.swt.graphics.Image;import org.eclipse.swt.graphics.Point;import org.eclipse.swt.graphics.RGB;import org.eclipse.swt.graphics.Rectangle;import org.eclipse.swt.layout.FormAttachment;import org.eclipse.swt.layout.FormData;import org.eclipse.swt.layout.FormLayout;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.Label;import org.eclipse.swt.widgets.Shell;import org.eclipse.swt.widgets.Text;/** * A dialog that has a title area for displaying a title and an image as well as * a common area for displaying a description, a message, or an error message. * <p> * This dialog class may be subclassed. */public class TitleAreaDialog extends TrayDialog { /** * Image registry key for error message image. */ public static final String DLG_IMG_TITLE_ERROR = DLG_IMG_MESSAGE_ERROR; /** * Image registry key for banner image (value * <code>"dialog_title_banner_image"</code>). */ public static final String DLG_IMG_TITLE_BANNER = "dialog_title_banner_image";//$NON-NLS-1$ /** * Message type constant used to display an info icon with the message. * * @since 2.0 * @deprecated */ public final static String INFO_MESSAGE = "INFO_MESSAGE"; //$NON-NLS-1$ /** * Message type constant used to display a warning icon with the message. * * @since 2.0 * @deprecated */ public final static String WARNING_MESSAGE = "WARNING_MESSAGE"; //$NON-NLS-1$ // Space between an image and a label private static final int H_GAP_IMAGE = 5; //Minimum dialog width (in dialog units) private static final int MIN_DIALOG_WIDTH = 350; //Minimum dialog height (in dialog units) private static final int MIN_DIALOG_HEIGHT = 150; static { ImageRegistry reg = JFaceResources.getImageRegistry(); reg.put(DLG_IMG_TITLE_BANNER, ImageDescriptor.createFromFile( TitleAreaDialog.class, "images/title_banner.gif"));//$NON-NLS-1$ } private Label titleLabel; private Label titleImageLabel; private Image titleImage; private Label leftFillerLabel; private RGB titleAreaRGB; Color titleAreaColor; private String message = ""; //$NON-NLS-1$ private String errorMessage; private Text messageLabel; private Composite workArea; private Composite titleArea; private Label messageImageLabel; private Image messageImage; private boolean showingError = false; private boolean showingWarning = false; private boolean titleImageLargest = true; private int messageLabelHeight; private ImageAndMessageArea messageArea; private String warningMessage; private ControlAnimator animator; /** * Instantiate a new title area dialog. * * @param parentShell * the parent SWT shell */ public TitleAreaDialog(Shell parentShell) { super(parentShell); } /* * @see Dialog.createContents(Composite) */ protected Control createContents(Composite parent) { // create the overall composite Composite contents = new Composite(parent, SWT.NONE); contents.setLayoutData(new GridData(GridData.FILL_BOTH)); // initialize the dialog units initializeDialogUnits(contents); FormLayout layout = new FormLayout(); contents.setLayout(layout); //Now create a work area for the rest of the dialog workArea = new Composite(contents, SWT.NONE); GridLayout childLayout = new GridLayout(); childLayout.marginHeight = 0; childLayout.marginWidth = 0; childLayout.verticalSpacing = 0; workArea.setLayout(childLayout); Control top = createTitleArea(contents); resetWorkAreaAttachments(top); workArea.setFont(JFaceResources.getDialogFont()); // initialize the dialog units initializeDialogUnits(workArea); // create the dialog area and button bar dialogArea = createDialogArea(workArea); buttonBar = createButtonBar(workArea); return contents; } /** * Creates and returns the contents of the upper part of this dialog (above * the button bar). * <p> * The <code>Dialog</code> implementation of this framework method creates * and returns a new <code>Composite</code> with no margins and spacing. * Subclasses should override. * </p> * * @param parent * The parent composite to contain the dialog area * @return the dialog area control */ protected Control createDialogArea(Composite parent) { // create the top level composite for the dialog area Composite composite = new Composite(parent, SWT.NONE); GridLayout layout = new GridLayout(); layout.marginHeight = 0; layout.marginWidth = 0; layout.verticalSpacing = 0; layout.horizontalSpacing = 0; composite.setLayout(layout); composite.setLayoutData(new GridData(GridData.FILL_BOTH)); composite.setFont(parent.getFont()); // Build the separator line Label titleBarSeparator = new Label(composite, SWT.HORIZONTAL | SWT.SEPARATOR); titleBarSeparator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); return composite; } /** * Creates the dialog's title area. * * @param parent * the SWT parent for the title area widgets * @return Control with the highest x axis value. */ private Control createTitleArea(Composite parent) { titleArea = new Composite(parent, SWT.NONE); initializeDialogUnits(titleArea); FormData titleAreaData = new FormData(); titleAreaData.top = new FormAttachment(0,0); titleAreaData.left = new FormAttachment(0,0); titleAreaData.right = new FormAttachment(100,0); titleArea.setLayoutData(titleAreaData); FormLayout layout = new FormLayout(); titleArea.setLayout(layout); // add a dispose listener titleArea.addDisposeListener(new DisposeListener() { public void widgetDisposed(DisposeEvent e) { if (titleAreaColor != null) { titleAreaColor.dispose(); } } }); // Determine the background color of the title bar Display display = titleArea.getDisplay(); Color background; Color foreground; if (titleAreaRGB != null) { titleAreaColor = new Color(display, titleAreaRGB); background = titleAreaColor; foreground = null; } else { background = JFaceColors.getBannerBackground(display); foreground = JFaceColors.getBannerForeground(display); } int verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING); int horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING); titleArea.setBackground(background); // Dialog image @ right titleImageLabel = new Label(titleArea, SWT.CENTER); titleImageLabel.setBackground(background); if (titleImage == null || titleImage.isDisposed()) { titleImageLabel.setImage(JFaceResources.getImage(DLG_IMG_TITLE_BANNER)); } else { titleImageLabel.setImage(titleImage); } FormData imageData = new FormData(); imageData.top = new FormAttachment(0, 0); // Note: do not use horizontalSpacing on the right as that would be a // regression from // the R2.x style where there was no margin on the right and images are // flush to the right // hand side. see reopened comments in 41172 imageData.right = new FormAttachment(100, 0); // horizontalSpacing titleImageLabel.setLayoutData(imageData); // Title label @ top, left titleLabel = new Label(titleArea, SWT.LEFT); JFaceColors.setColors(titleLabel, foreground, background); titleLabel.setFont(JFaceResources.getBannerFont()); titleLabel.setText(" ");//$NON-NLS-1$ FormData titleData = new FormData(); titleData.top = new FormAttachment(0, verticalSpacing); titleData.right = new FormAttachment(titleImageLabel); titleData.left = new FormAttachment(0, horizontalSpacing); titleLabel.setLayoutData(titleData); // Message image @ bottom, left messageImageLabel = new Label(titleArea, SWT.CENTER); messageImageLabel.setBackground(background); // Message label @ bottom, center messageLabel = new Text(titleArea, SWT.WRAP | SWT.READ_ONLY); JFaceColors.setColors(messageLabel, foreground, background); messageLabel.setText(" \n "); // two lines//$NON-NLS-1$ messageLabel.setFont(JFaceResources.getDialogFont()); messageLabelHeight = messageLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT).y; // Filler label leftFillerLabel = new Label(titleArea, SWT.CENTER); leftFillerLabel.setBackground(background); setLayoutsForNormalMessage(verticalSpacing, horizontalSpacing); determineTitleImageLargest(); return titleArea; } /** * Determine if the title image is larger than the title message and message * area. This is used for layout decisions. */ private void determineTitleImageLargest() { int titleY = titleImageLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT).y; int verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING); int labelY = titleLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT).y; labelY += verticalSpacing; labelY += messageLabelHeight; labelY += verticalSpacing; titleImageLargest = titleY > labelY; } /** * Set the layout values for the messageLabel, messageImageLabel and * fillerLabel for the case where there is a normal message. * * @param verticalSpacing * int The spacing between widgets on the vertical axis. * @param horizontalSpacing * int The spacing between widgets on the horizontal axis. */ private void setLayoutsForNormalMessage(int verticalSpacing, int horizontalSpacing) { FormData messageLabelData = new FormData(); messageLabelData.top = new FormAttachment(titleLabel, verticalSpacing); messageLabelData.right = new FormAttachment(titleImageLabel); messageLabelData.left = new FormAttachment(messageImageLabel,horizontalSpacing); messageLabelData.height = messageLabelHeight; if (titleImageLargest) { messageLabelData.bottom = new FormAttachment(titleImageLabel, 0, SWT.BOTTOM); } messageLabel.setLayoutData(messageLabelData); FormData imageLabelData = new FormData(); imageLabelData.top = new FormAttachment(titleLabel, verticalSpacing); imageLabelData.left = new FormAttachment(leftFillerLabel); imageLabelData.right = new FormAttachment(messageLabel); messageImageLabel.setLayoutData(imageLabelData); FormData data = new FormData(); data.top = new FormAttachment(titleLabel, 0, SWT.TOP); data.left = new FormAttachment(0,H_GAP_IMAGE); data.bottom = new FormAttachment(messageLabel, 0, SWT.BOTTOM); leftFillerLabel.setLayoutData(data); } /** * The <code>TitleAreaDialog</code> implementation of this * <code>Window</code> methods returns an initial size which is at least * some reasonable minimum. * * @return the initial size of the dialog */ protected Point getInitialSize() { Point shellSize = super.getInitialSize(); return new Point(Math.max( convertHorizontalDLUsToPixels(MIN_DIALOG_WIDTH), shellSize.x), Math.max(convertVerticalDLUsToPixels(MIN_DIALOG_HEIGHT), shellSize.y)); } /** * Retained for backward compatibility. * * Returns the title area composite. There is no composite in this * implementation so the shell is returned. * * @return Composite
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -