📄 preferencedialog.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.preference;import java.io.IOException;import java.util.Iterator;import java.util.List;import org.eclipse.core.runtime.ISafeRunnable;import org.eclipse.core.runtime.IStatus;import org.eclipse.core.runtime.ListenerList;import org.eclipse.core.runtime.Status;import org.eclipse.jface.dialogs.DialogMessageArea;import org.eclipse.jface.dialogs.IDialogConstants;import org.eclipse.jface.dialogs.IMessageProvider;import org.eclipse.jface.dialogs.IPageChangeProvider;import org.eclipse.jface.dialogs.IPageChangedListener;import org.eclipse.jface.dialogs.MessageDialog;import org.eclipse.jface.dialogs.PageChangedEvent;import org.eclipse.jface.dialogs.TrayDialog;import org.eclipse.jface.resource.ImageDescriptor;import org.eclipse.jface.resource.ImageRegistry;import org.eclipse.jface.resource.JFaceResources;import org.eclipse.jface.util.Assert;import org.eclipse.jface.util.IPropertyChangeListener;import org.eclipse.jface.util.Policy;import org.eclipse.jface.util.PropertyChangeEvent;import org.eclipse.jface.util.SafeRunnable;import org.eclipse.jface.viewers.ISelection;import org.eclipse.jface.viewers.ISelectionChangedListener;import org.eclipse.jface.viewers.IStructuredSelection;import org.eclipse.jface.viewers.SelectionChangedEvent;import org.eclipse.jface.viewers.StructuredSelection;import org.eclipse.jface.viewers.TreeViewer;import org.eclipse.swt.SWT;import org.eclipse.swt.custom.BusyIndicator;import org.eclipse.swt.events.ControlAdapter;import org.eclipse.swt.events.ControlEvent;import org.eclipse.swt.events.DisposeEvent;import org.eclipse.swt.events.DisposeListener;import org.eclipse.swt.events.HelpEvent;import org.eclipse.swt.events.HelpListener;import org.eclipse.swt.events.SelectionAdapter;import org.eclipse.swt.events.SelectionEvent;import org.eclipse.swt.events.ShellAdapter;import org.eclipse.swt.events.ShellEvent;import org.eclipse.swt.graphics.Font;import org.eclipse.swt.graphics.Point;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.Button;import org.eclipse.swt.widgets.Composite;import org.eclipse.swt.widgets.Control;import org.eclipse.swt.widgets.Event;import org.eclipse.swt.widgets.Label;import org.eclipse.swt.widgets.Layout;import org.eclipse.swt.widgets.Listener;import org.eclipse.swt.widgets.Sash;import org.eclipse.swt.widgets.Shell;import org.eclipse.swt.widgets.Tree;/** * A preference dialog is a hierarchical presentation of preference pages. Each * page is represented by a node in the tree shown on the left hand side of the * dialog; when a node is selected, the corresponding page is shown on the right * hand side. */public class PreferenceDialog extends TrayDialog implements IPreferencePageContainer, IPageChangeProvider { /** * Layout for the page container. * */ private class PageLayout extends Layout { public Point computeSize(Composite composite, int wHint, int hHint, boolean force) { if (wHint != SWT.DEFAULT && hHint != SWT.DEFAULT) { return new Point(wHint, hHint); } int x = minimumPageSize.x; int y = minimumPageSize.y; Control[] children = composite.getChildren(); for (int i = 0; i < children.length; i++) { Point size = children[i].computeSize(SWT.DEFAULT, SWT.DEFAULT, force); x = Math.max(x, size.x); y = Math.max(y, size.y); } //As pages can implement thier own computeSize //take it into account if(currentPage != null){ Point size = currentPage.computeSize(); x = Math.max(x, size.x); y = Math.max(y, size.y); } if (wHint != SWT.DEFAULT) { x = wHint; } if (hHint != SWT.DEFAULT) { y = hHint; } return new Point(x, y); } public void layout(Composite composite, boolean force) { Rectangle rect = composite.getClientArea(); Control[] children = composite.getChildren(); for (int i = 0; i < children.length; i++) { children[i].setSize(rect.width, rect.height); } } } //The id of the last page that was selected private static String lastPreferenceId = null; //The last known tree width private static int lastTreeWidth = 150; /** * Indentifier for the error image */ public static final String PREF_DLG_IMG_TITLE_ERROR = DLG_IMG_MESSAGE_ERROR; /** * Title area fields */ public static final String PREF_DLG_TITLE_IMG = "preference_dialog_title_image"; //$NON-NLS-1$ protected static final int FAILED = 2; static { ImageRegistry reg = JFaceResources.getImageRegistry(); reg.put(PREF_DLG_TITLE_IMG, ImageDescriptor.createFromFile(PreferenceDialog.class, "images/pref_dialog_title.gif")); //$NON-NLS-1$ } /** * The current preference page, or <code>null</code> if there is none. */ private IPreferencePage currentPage; private DialogMessageArea messageArea; private Point lastShellSize; private IPreferenceNode lastSuccessfulNode; /** * The minimum page size; 400 by 400 by default. * * @see #setMinimumPageSize(Point) */ private Point minimumPageSize = new Point(400, 400); /** * The OK button. */ private Button okButton; /** * The Composite in which a page is shown. */ private Composite pageContainer; /** * The preference manager. */ private PreferenceManager preferenceManager; /** * Flag for the presence of the error message. */ private boolean showingError = false; /** * Preference store, initially <code>null</code> meaning none. * * @see #setPreferenceStore */ private IPreferenceStore preferenceStore; private Composite titleArea; /** * The tree viewer. */ private TreeViewer treeViewer; private ListenerList pageChangedListeners = new ListenerList(); /** * Composite with a FormLayout to contain the title area */ Composite formTitleComposite; /** * Creates a new preference dialog under the control of the given preference * manager. * * @param parentShell * the parent shell * @param manager * the preference manager */ public PreferenceDialog(Shell parentShell, PreferenceManager manager) { super(parentShell); setShellStyle(getShellStyle() | SWT.RESIZE | SWT.MAX); preferenceManager = manager; } /* * (non-Javadoc) * * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int) */ protected void buttonPressed(int buttonId) { switch (buttonId) { case IDialogConstants.OK_ID: { okPressed(); return; } case IDialogConstants.CANCEL_ID: { cancelPressed(); return; } case IDialogConstants.HELP_ID: { helpPressed(); return; } } } /* * (non-Javadoc) * * @see org.eclipse.jface.dialogs.Dialog#cancelPressed() */ protected void cancelPressed() { // Inform all pages that we are cancelling Iterator nodes = preferenceManager.getElements(PreferenceManager.PRE_ORDER).iterator(); while (nodes.hasNext()) { final IPreferenceNode node = (IPreferenceNode) nodes.next(); if (getPage(node) != null) { SafeRunnable.run(new SafeRunnable() { public void run() { if (!getPage(node).performCancel()) { return; } } }); } } setReturnCode(CANCEL); close(); } /** * Clear the last selected node. This is so that we not chache the last * selection in case of an error. */ void clearSelectedNode() { setSelectedNodePreference(null); } /* * (non-Javadoc) * * @see org.eclipse.jface.window.Window#close() */ public boolean close() { List nodes = preferenceManager.getElements(PreferenceManager.PRE_ORDER); for (int i = 0; i < nodes.size(); i++) { IPreferenceNode node = (IPreferenceNode) nodes.get(i); node.disposeResources(); } return super.close(); } /* * (non-Javadoc) * * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell) */ protected void configureShell(Shell newShell) { super.configureShell(newShell); newShell.setText(JFaceResources.getString("PreferenceDialog.title")); //$NON-NLS-1$ newShell.addShellListener(new ShellAdapter() { public void shellActivated(ShellEvent e) { if (lastShellSize == null) { lastShellSize = getShell().getSize(); } } }); } /* * (non-Javadoc) * * @see org.eclipse.jface.window.Window#constrainShellSize() */ protected void constrainShellSize() { super.constrainShellSize(); // record opening shell size if (lastShellSize == null) { lastShellSize = getShell().getSize(); } } /* * (non-Javadoc) * * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite) */ protected void createButtonsForButtonBar(Composite parent) { // create OK and Cancel buttons by default okButton = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true); getShell().setDefaultButton(okButton); createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false); } /* * (non-Javadoc) * * @see org.eclipse.jface.window.Window#createContents(org.eclipse.swt.widgets.Composite) */ protected Control createContents(final Composite parent) { final Control[] control = new Control[1]; BusyIndicator.showWhile(getShell().getDisplay(), new Runnable() { public void run() { control[0] = PreferenceDialog.super.createContents(parent); // Add the first page selectSavedItem(); } }); return control[0]; } /* * (non-Javadoc) * * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite) */ protected Control createDialogArea(Composite parent) { final Composite composite = (Composite) super.createDialogArea(parent); GridLayout parentLayout = ((GridLayout) composite.getLayout()); parentLayout.numColumns = 4; parentLayout.marginHeight = 0; parentLayout.marginWidth = 0; parentLayout.verticalSpacing = 0; parentLayout.horizontalSpacing = 0; composite.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND)); Control treeControl = createTreeAreaContents(composite); createSash(composite,treeControl); Label versep = new Label(composite, SWT.SEPARATOR | SWT.VERTICAL); GridData verGd = new GridData(GridData.FILL_VERTICAL | GridData.GRAB_VERTICAL); versep.setLayoutData(verGd); versep.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true)); Composite pageAreaComposite = new Composite(composite, SWT.NONE); pageAreaComposite.setLayoutData(new GridData(GridData.FILL_BOTH)); GridLayout layout = new GridLayout(1, true); layout.marginHeight = 0; layout.marginWidth = 0; layout.verticalSpacing = 0; pageAreaComposite.setLayout(layout); formTitleComposite = new Composite(pageAreaComposite, SWT.NONE); FormLayout titleLayout = new FormLayout(); titleLayout.marginWidth = 0; titleLayout.marginHeight = 0; formTitleComposite.setLayout(titleLayout); GridData titleGridData = new GridData(GridData.FILL_HORIZONTAL); titleGridData.horizontalIndent = IDialogConstants.HORIZONTAL_MARGIN; formTitleComposite.setLayoutData(titleGridData); // Build the title area and separator line Composite titleComposite = new Composite(formTitleComposite, SWT.NONE); layout = new GridLayout(); layout.marginBottom = 5; layout.marginHeight = 0; layout.marginWidth = 0; layout.horizontalSpacing = 0; titleComposite.setLayout(layout); FormData titleFormData = new FormData(); titleFormData.top = new FormAttachment(0,0); titleFormData.left = new FormAttachment(0,0); titleFormData.right = new FormAttachment(100,0); titleFormData.bottom = new FormAttachment(100,0); titleComposite.setLayoutData(titleFormData); createTitleArea(titleComposite); Label separator = new Label(pageAreaComposite, SWT.HORIZONTAL | SWT.SEPARATOR); separator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL)); // Build the Page container pageContainer = createPageContainer(pageAreaComposite); GridData pageContainerData = new GridData(GridData.FILL_BOTH); pageContainerData.horizontalIndent = IDialogConstants.HORIZONTAL_MARGIN; pageContainer.setLayoutData(pageContainerData); // Build the separator line Label bottomSeparator = new Label(parent, SWT.HORIZONTAL | SWT.SEPARATOR); bottomSeparator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL)); return composite; } /** * Create the sash with right control on the right. Note * that this method assumes GridData for the layout data * of the rightControl. * @param composite * @param rightControl * @return Sash * * @since 3.1 */ protected Sash createSash(final Composite composite, final Control rightControl) { final Sash sash = new Sash(composite, SWT.VERTICAL); sash.setLayoutData(new GridData(GridData.FILL_VERTICAL)); sash.setBackground(composite.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND)); // the following listener resizes the tree control based on sash deltas. // If necessary, it will also grow/shrink the dialog. sash.addListener(SWT.Selection, new Listener() { /* * (non-Javadoc) * * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event) */ public void handleEvent(Event event) { if (event.detail == SWT.DRAG) { return; } int shift = event.x - sash.getBounds().x; GridData data = (GridData) rightControl.getLayoutData(); int newWidthHint = data.widthHint + shift; if (newWidthHint < 20) { return; } Point computedSize = getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT); Point currentSize = getShell().getSize(); // if the dialog wasn't of a custom size we know we can shrink // it if necessary based on sash movement. boolean customSize = !computedSize.equals(currentSize); data.widthHint = newWidthHint; setLastTreeWidth(newWidthHint); composite.layout(true); // recompute based on new widget size computedSize = getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT); // if the dialog was of a custom size then increase it only if // necessary. if (customSize) { computedSize.x = Math.max(computedSize.x, currentSize.x); } computedSize.y = Math.max(computedSize.y, currentSize.y); if (computedSize.equals(currentSize)) { return; } setShellSize(computedSize.x, computedSize.y); lastShellSize = getShell().getSize(); } }); return sash; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -