⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 preferencedialog.java

📁 jfa2ce 源码帮助开发人员更好的理解运用
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	/**	 * Creates the inner page container.	 * 	 * @param parent	 * @return Composite	 */	protected Composite createPageContainer(Composite parent) {				//Create an outer composite for spacing		Composite outer = new Composite(parent, SWT.NONE);				GridData outerData = new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL				| GridData.GRAB_VERTICAL);						outer.setLayout(new GridLayout());		outer.setLayoutData(outerData);		Composite result = new Composite(outer, SWT.NONE);				GridData resultData = new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL				| GridData.GRAB_VERTICAL);						result.setLayout(getPageLayout());		result.setLayoutData(resultData);				return result;	}	/**	 * Return the layout for the composite that contains	 * the pages.	 * @return PageLayout	 * 	 * @since 3.1	 */	protected Layout getPageLayout() {		return new PageLayout();	}	/**	 * Creates the wizard's title area.	 * 	 * @param parent	 *            the SWT parent for the title area composite.	 * @return the created title area composite.	 */	protected Composite createTitleArea(Composite parent) {		// Create the title area which will contain		// a title, message, and image.		int margins = 2;		titleArea = new Composite(parent, SWT.NONE);		FormLayout layout = new FormLayout();		layout.marginHeight = 0;		layout.marginWidth = margins;		titleArea.setLayout(layout);				GridData layoutData = new GridData(GridData.FILL_HORIZONTAL);		layoutData.verticalAlignment = SWT.TOP;		titleArea.setLayoutData(layoutData);		// Message label		messageArea = new PreferenceMessageArea(this);		messageArea.createContents(titleArea);		titleArea.addControlListener(new ControlAdapter() {			/* (non-Javadoc)			 * @see org.eclipse.swt.events.ControlAdapter#controlResized(org.eclipse.swt.events.ControlEvent)			 */			public void controlResized(ControlEvent e) {				updateMessage();			}		});		final IPropertyChangeListener fontListener = new IPropertyChangeListener() {			public void propertyChange(PropertyChangeEvent event) {				if (JFaceResources.BANNER_FONT.equals(event.getProperty())) {					updateMessage();				}				if (JFaceResources.DIALOG_FONT.equals(event.getProperty())) {					updateMessage();					Font dialogFont = JFaceResources.getDialogFont();					updateTreeFont(dialogFont);					Control[] children = ((Composite) buttonBar).getChildren();					for (int i = 0; i < children.length; i++) {						children[i].setFont(dialogFont);					}				}			}		};		titleArea.addDisposeListener(new DisposeListener() {			public void widgetDisposed(DisposeEvent event) {				JFaceResources.getFontRegistry().removeListener(fontListener);			}		});		JFaceResources.getFontRegistry().addListener(fontListener);		messageArea.setTitleLayoutData(createMessageAreaData());		messageArea.setMessageLayoutData(createMessageAreaData());		return titleArea;	}	/**	 * Create the layout data for the message area.	 * 	 * @return FormData for the message area.	 */	private FormData createMessageAreaData() {		FormData messageData = new FormData();		messageData.top = new FormAttachment(0);		messageData.bottom = new FormAttachment(100);		messageData.right = new FormAttachment(100);		messageData.left = new FormAttachment(0);		return messageData;	}	/**	 * @param parent	 *            the SWT parent for the tree area controls.	 * @return the new <code>Control</code>.	 * @since 3.0	 */	protected Control createTreeAreaContents(Composite parent) {		// Build the tree an put it into the composite.		treeViewer = createTreeViewer(parent);		treeViewer.setInput(getPreferenceManager());		updateTreeFont(JFaceResources.getDialogFont());		layoutTreeAreaControl(treeViewer.getControl());		return treeViewer.getControl();	}	/**	 * Create a new <code>TreeViewer</code>.	 * 	 * @param parent	 *            the parent <code>Composite</code>.	 * @return the <code>TreeViewer</code>.	 * @since 3.0	 */	protected TreeViewer createTreeViewer(Composite parent) {		final TreeViewer viewer = new TreeViewer(parent, SWT.NONE);		addListeners(viewer);		viewer.setLabelProvider(new PreferenceLabelProvider());		viewer.setContentProvider(new PreferenceContentProvider());		return viewer;	}	/**	 * Add the listeners to the tree viewer.	 * @param viewer	 * 	 * @since 3.1	 */	protected void addListeners(final TreeViewer viewer) {		viewer.addPostSelectionChangedListener(new ISelectionChangedListener() {			private void handleError() {				try {					// remove the listener temporarily so that the events caused					// by the error handling dont further cause error handling					// to occur.					viewer.removePostSelectionChangedListener(this);					showPageFlippingAbortDialog();					selectCurrentPageAgain();					clearSelectedNode();				} finally {					viewer.addPostSelectionChangedListener(this);				}			}			public void selectionChanged(SelectionChangedEvent event) {				Object selection = getSingleSelection(event.getSelection());				if (selection instanceof IPreferenceNode) {					if (!isCurrentPageValid()) {						handleError();					} else if (!showPage((IPreferenceNode) selection)) {						// Page flipping wasn't successful						handleError();					} else {						// Everything went well						lastSuccessfulNode = (IPreferenceNode) selection;					}				}			}		});		((Tree) viewer.getControl()).addSelectionListener(new SelectionAdapter() {			public void widgetDefaultSelected(final SelectionEvent event) {				ISelection selection = viewer.getSelection();				if (selection.isEmpty()) {					return;				}				IPreferenceNode singleSelection = getSingleSelection(selection);				boolean expanded = viewer.getExpandedState(singleSelection);				viewer.setExpandedState(singleSelection, !expanded);			}		});		//Register help listener on the tree to use context sensitive help		viewer.getControl().addHelpListener(new HelpListener() {			public void helpRequested(HelpEvent event) {				// call perform help on the current page				if (currentPage != null) {					currentPage.performHelp();				}			}		});	}	/**	 * Find the <code>IPreferenceNode</code> that has data the same id as the	 * supplied value.	 * 	 * @param nodeId	 *            the id to search for.	 * @return <code>IPreferenceNode</code> or <code>null</code> if not	 *         found.	 */	protected IPreferenceNode findNodeMatching(String nodeId) {		List nodes = preferenceManager.getElements(PreferenceManager.POST_ORDER);		for (Iterator i = nodes.iterator(); i.hasNext();) {			IPreferenceNode node = (IPreferenceNode) i.next();			if (node.getId().equals(nodeId)) {				return node;			}		}		return null;	}	/**	 * Get the last known right side width.	 * 	 * @return the width.	 */	protected int getLastRightWidth() {		return lastTreeWidth;	}	/**	 * Returns the preference mananger used by this preference dialog.	 * 	 * @return the preference mananger	 */	public PreferenceManager getPreferenceManager() {		return preferenceManager;	}	/*	 * (non-Javadoc)	 * 	 * @see org.eclipse.jface.preference.IPreferencePageContainer#getPreferenceStore()	 */	public IPreferenceStore getPreferenceStore() {		return preferenceStore;	}	/**	 * Get the name of the selected item preference	 * 	 * @return String	 */	protected String getSelectedNodePreference() {		return lastPreferenceId;	}	/**	 * @param selection	 *            the <code>ISelection</code> to examine.	 * @return the first element, or null if empty.	 */	protected IPreferenceNode getSingleSelection(ISelection selection) {		if (!selection.isEmpty()) {			IStructuredSelection structured = (IStructuredSelection) selection;			if (structured.getFirstElement() instanceof IPreferenceNode) {				return (IPreferenceNode) structured.getFirstElement();			}		}		return null;	}	/**	 * @return the <code>TreeViewer</code> for this dialog.	 * @since 3.0	 */	protected TreeViewer getTreeViewer() {		return treeViewer;	}	/**	 * Save the values specified in the pages.	 * <p>	 * The default implementation of this framework method saves all pages of	 * type <code>PreferencePage</code> (if their store needs saving and is a	 * <code>PreferenceStore</code>).	 * </p>	 * <p>	 * Subclasses may override.	 * </p>	 */	protected void handleSave() {		Iterator nodes = preferenceManager.getElements(PreferenceManager.PRE_ORDER).iterator();		while (nodes.hasNext()) {			IPreferenceNode node = (IPreferenceNode) nodes.next();			IPreferencePage page = node.getPage();			if (page instanceof PreferencePage) {				// Save now in case tbe workbench does not shutdown cleanly				IPreferenceStore store = ((PreferencePage) page).getPreferenceStore();				if (store != null && store.needsSaving()						&& store instanceof IPersistentPreferenceStore) {					try {						((IPersistentPreferenceStore) store).save();					} catch (IOException e) {						MessageDialog								.openError(										getShell(),										JFaceResources.getString("PreferenceDialog.saveErrorTitle"), //$NON-NLS-1$										JFaceResources												.format(														"PreferenceDialog.saveErrorMessage", new Object[] { page.getTitle(), e.getMessage() })); //$NON-NLS-1$					}				}			}		}	}	/**	 * Notifies that the window's close button was pressed, the close menu was	 * selected, or the ESCAPE key pressed.	 * <p>	 * The default implementation of this framework method sets the window's	 * return code to <code>CANCEL</code> and closes the window using	 * <code>close</code>. Subclasses may extend or reimplement.	 * </p>	 */	protected void handleShellCloseEvent() {		// handle the same as pressing cancel		cancelPressed();	}	/**	 * Notifies of the pressing of the Help button.	 * <p>	 * The default implementation of this framework method calls	 * <code>performHelp</code> on the currently active page.	 * </p>	 */	protected void helpPressed() {		if (currentPage != null) {			currentPage.performHelp();		}	}	/**	 * Returns whether the current page is valid.	 * 	 * @return <code>false</code> if the current page is not valid, or or	 *         <code>true</code> if the current page is valid or there is no	 *         current page	 */	protected boolean isCurrentPageValid() {		if (currentPage == null) {			return true;		}		return currentPage.isValid();	}	/**	 * @param control	 *            the <code>Control</code> to lay out.	 * @since 3.0	 */	protected void layoutTreeAreaControl(Control control) {		GridData gd = new GridData(GridData.FILL_VERTICAL);		gd.widthHint = getLastRightWidth();		gd.verticalSpan = 1;		control.setLayoutData(gd);	}	/**	 * The preference dialog implementation of this <code>Dialog</code>	 * framework method sends <code>performOk</code> to all pages of the	 * preference dialog, then calls <code>handleSave</code> on this dialog to	 * save any state, and then calls <code>close</code> to close this dialog.	 */	protected void okPressed() {		SafeRunnable.run(new SafeRunnable() {			private boolean errorOccurred;			/*			 * (non-Javadoc)			 * 			 * @see org.eclipse.core.runtime.ISafeRunnable#run()			 */			public void run() {				getButton(IDialogConstants.OK_ID).setEnabled(false);				errorOccurred = false;				boolean hasFailedOK = false;				try {					// Notify all the pages and give them a chance to abort					Iterator nodes = preferenceManager.getElements(PreferenceManager.PRE_ORDER)							.iterator();					while (nodes.hasNext()) {						IPreferenceNode node = (IPreferenceNode) nodes.next();						IPreferencePage page = node.getPage();						if (page != null) {							if (!page.performOk()){								hasFailedOK = true;								return;							}						}					}				} catch (Exception e) {					handleException(e);				} finally {					//Don't bother closing if the OK failed					if(hasFailedOK){						setReturnCode(FAILED);						getButton(IDialogConstants.OK_ID).setEnabled(true);						return;					}										if (!errorOccurred) {						//Give subclasses the choice to save the state of the					    //preference pages.						handleSave();					}					setReturnCode(OK);					close();				}			}			/*			 * (non-Javadoc)			 * 			 * @see org.eclipse.core.runtime.ISafeRunnable#handleException(java.lang.Throwable)			 */			public void handleException(Throwable e) {				errorOccurred = true;								Policy.getLog().log(new Status(IStatus.ERROR, Policy.JFACE, 0, e.toString(), e));				clearSelectedNode();				String message = JFaceResources.getString("SafeRunnable.errorMessage"); //$NON-NLS-1$				MessageDialog.openError(getShell(), JFaceResources.getString("Error"), message); //$NON-NLS-1$			}		});	}	/**	 * Selects the page determined by <code>lastSuccessfulNode</code> in the	 * page hierarchy.	 */	void selectCurrentPageAgain() {		if (lastSuccessfulNode == null) {			return;		}		getTreeViewer().setSelection(new StructuredSelection(lastSuccessfulNode));		currentPage.setVisible(true);	}	/**	 * Selects the saved item in the tree of preference pages. If it cannot do	 * this it saves the first one.	 */	protected void selectSavedItem() {		IPreferenceNode node = findNodeMatching(getSelectedNodePreference());		if (node == null) {			IPreferenceNode[] nodes = preferenceManager.getRoot().getSubNodes();			if (nodes.length > 0) {				node = nodes[0];			}		}		if (node != null) {			getTreeViewer().setSelection(new StructuredSelection(node), true);			// Keep focus in tree. See bugs 2692, 2621, and 6775.			getTreeViewer().getControl().setFocus();		}	}	/**	 * Display the given error message. The currently displayed message is saved	 * and will be redisplayed when the error message is set to	 * <code>null</code>.	 * 	 * @param newErrorMessage	 *            the errorMessage to display or <code>null</code>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -