Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2011 itemis AG (http://www.itemis.eu) 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 /
  
  package org.eclipse.xtext.ui.preferences;
  
 import java.util.Map;
 import java.util.Set;
 
 import  org.eclipse.core.resources.IProject;
 import  org.eclipse.core.resources.IResource;
 import  org.eclipse.core.resources.ResourcesPlugin;
 import  org.eclipse.core.runtime.IAdaptable;
 import  org.eclipse.core.runtime.IStatus;
 import  org.eclipse.jface.dialogs.ControlEnableState;
 import  org.eclipse.jface.dialogs.Dialog;
 import  org.eclipse.jface.dialogs.DialogPage;
 import  org.eclipse.jface.dialogs.IDialogSettings;
 import  org.eclipse.jface.dialogs.IMessageProvider;
 import  org.eclipse.jface.preference.IPreferencePageContainer;
 import  org.eclipse.jface.preference.PreferencePage;
 import  org.eclipse.jface.window.Window;
 import  org.eclipse.swt.SWT;
 import  org.eclipse.swt.events.SelectionEvent;
 import  org.eclipse.swt.events.SelectionListener;
 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.Label;
 import  org.eclipse.swt.widgets.Link;
 import  org.eclipse.ui.IWorkbench;
 import  org.eclipse.ui.IWorkbenchPreferencePage;
 import  org.eclipse.ui.IWorkbenchPropertyPage;
 import  org.eclipse.ui.dialogs.PreferencesUtil;
 import  org.eclipse.ui.preferences.IWorkbenchPreferenceContainer;
 import  org.eclipse.ui.preferences.IWorkingCopyManager;
 import  org.eclipse.ui.preferences.WorkingCopyManager;
 
Initially copied from Jdt.

Author(s):
Michael Clay
Since:
2.1
 
 public abstract class PropertyAndPreferencePage extends PreferencePage implements IWorkbenchPreferencePage,
 		IWorkbenchPropertyPage {
 	public static final String DATA_NO_LINK = "PropertyAndPreferencePage.nolink"//$NON-NLS-1$
 	private Control configurationBlockControl;
 	private ControlEnableState blockEnableState;
 	private Link changeWorkspaceSettings;
 	private IStatus blockStatus;
 	private Composite parentComposite;
 	private IProject project;
 	private Map<ObjectObjectpageData;
 	private IDialogSettings dialogSettings;
 
 		 = new StatusInfo();
 		 = null;
 		 = null;
 		 = null;
 	}
 
 	protected abstract Control createPreferenceContent(Composite composite,
 			IPreferencePageContainer preferencePageContainer);
 
 	protected abstract boolean hasProjectSpecificOptions(IProject project);
 
 	protected abstract String getPreferencePageID();
 
 	protected abstract String getPropertyPageID();
 
 	protected boolean supportsProjectSpecificOptions() {
 		return getPropertyPageID() != null;
 	}
 
 	protected boolean offerLink() {
 		return  == null || !..equals(.get());
 	}
 
 	protected Label createDescriptionLabel(Composite parent) {
 		 = parent;
			Composite composite = new Composite(parent, SWT.NONE);
			composite.setFont(parent.getFont());
			GridLayout layout = new GridLayout();
			layout.marginHeight = 0;
			layout.marginWidth = 0;
			layout.numColumns = 2;
			composite.setLayout(layout);
			composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, truefalse));
				public void dialogFieldChanged(DialogField field) {
					boolean enabled = ((SelectionButtonDialogFieldfield).isSelected();
					if (enabled && getData() != null) {
					}
				}
			};
			if (offerLink()) {
				.setLayoutData(new GridData(SWT.END, SWT.CENTER, falsefalse));
else {
			}
			Label horizontalLine = new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL);
			horizontalLine.setLayoutData(new GridData(GridData.FILL, GridData.FILL, truefalse, 2, 1));
			horizontalLine.setFont(composite.getFont());
else if (supportsProjectSpecificOptions() && offerLink()) {
			.setLayoutData(new GridData(SWT.END, SWT.CENTER, truefalse));
		}
		return super.createDescriptionLabel(parent);
	}
	protected Control createContents(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		composite.setLayout(layout);
		composite.setFont(parent.getFont());
		GridData data = new GridData(GridData.FILL, GridData.FILL, truetrue);
		.setLayoutData(data);
			boolean useProjectSettings = hasProjectSpecificOptions(getProject());
			enableProjectSpecificSettings(useProjectSettings);
		}
		Dialog.applyDialogFont(composite);
		return composite;
	}
	private Link createLink(Composite compositeString text) {
		Link link = new Link(composite, SWT.NONE);
		link.setFont(composite.getFont());
		link.setText("<A>" + text + "</A>"); //$NON-NLS-1$//$NON-NLS-2$
		link.addSelectionListener(new SelectionListener() {
			public void widgetSelected(SelectionEvent e) {
				doLinkActivated((Link) e.widget);
			}
			public void widgetDefaultSelected(SelectionEvent e) {
				doLinkActivated((Link) e.widget);
			}
		});
		return link;
	}
	protected boolean useProjectSettings() {
	}
	protected boolean isProjectPreferencePage() {
		return  != null;
	}
	protected IProject getProject() {
		return ;
	}
	final void doLinkActivated(Link link) {
		Map<ObjectObjectdata = getData();
		if (data == null) {
			data = new HashMap<ObjectObject>();
		}
else {
			Set<IProject> projectsWithSpecifics = Sets.newHashSet();
			IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
			for (IProject project : projects) {
				if (XtextProjectHelper.hasNature(project) && hasProjectSpecificOptions(project)) {
					projectsWithSpecifics.add(project);
				}
			}
			ProjectSelectionDialog dialog = new ProjectSelectionDialog(getShell(), projectsWithSpecifics,
			if (dialog.open() == Window.OK) {
				IProject project = (IProject) dialog.getFirstResult();
				openProjectProperties(projectdata);
			}
		}
	}
	protected final void openWorkspacePreferences(Object data) {
		PreferencesUtil.createPreferenceDialogOn(getShell(), idnew String[] { id }, data).open();
	}
	protected final void openProjectProperties(IProject projectObject data) {
		if (id != null) {
			PreferencesUtil.createPropertyDialogOn(getShell(), projectidnew String[] { id }, data).open();
		}
	}
	protected void enableProjectSpecificSettings(boolean useProjectSpecificSettings) {
		.setSelection(useProjectSpecificSettings);
		enablePreferenceContent(useProjectSpecificSettings);
	}
	private void updateLinkVisibility() {
		if ( == null || .isDisposed()) {
			return;
		}
		}
	}
	protected void setPreferenceContentStatus(IStatus status) {
		 = status;
	}
		return new IStatusChangeListener() {
			public void statusChanged(IStatus status) {
			}
		};
	}
	protected IStatus getPreferenceContentStatus() {
		return ;
	}
	protected void doStatusChanged() {
else {
		}
	}
	protected void enablePreferenceContent(boolean enable) {
		if (enable) {
			if ( != null) {
			}
else {
			if ( == null) {
				 = ControlEnableState.disable();
			}
		}
	}
	protected void performDefaults() {
		}
		super.performDefaults();
	}
	private void updateStatus(IStatus status) {
		setValid(!status.matches(IStatus.ERROR));
		applyToStatusLine(thisstatus);
	}
	public void init(IWorkbench workbench) {
	}
	public IAdaptable getElement() {
		return ;
	}
	public void setElement(IAdaptable element) {
		 = (IProject) element.getAdapter(IResource.class);
	}
	@SuppressWarnings("unchecked")
	public void applyData(Object data) {
		if (data instanceof Map) {
			 = (Map<ObjectObject>) data;
		}
		if ( != null) {
			if (!offerLink()) {
				.layout(truetrue);
			}
		}
	}
	protected Map<ObjectObjectgetData() {
		return ;
	}
	protected IWorkingCopyManager getWorkingCopyManager() {
		IWorkbenchPreferenceContainer container = (IWorkbenchPreferenceContainer) getContainer();
		IWorkingCopyManager manager;
		if (container == null) {
			manager = new WorkingCopyManager();
else {
			manager = container.getWorkingCopyManager();
		}
		return manager;
	}
	private static void applyToStatusLine(DialogPage page, IStatus status) {
		String message = status.getMessage();
		if (message != null && message.length() == 0) {
			message = null;
		}
		switch (status.getSeverity()) {
			case IStatus.OK:
				page.setMessage(message, IMessageProvider.NONE);
				page.setErrorMessage(null);
				break;
			case IStatus.WARNING:
				page.setMessage(message, IMessageProvider.WARNING);
				page.setErrorMessage(null);
				break;
			case IStatus.INFO:
				page.setMessage(message, IMessageProvider.INFORMATION);
				page.setErrorMessage(null);
				break;
			default:
				page.setMessage(null);
				page.setErrorMessage(message);
				break;
		}
	}
New to GrepCode? Check out our FAQ X