Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.eclipse.xtext.ui.editor.preferences;

Copyright (c) 2008, 2012 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 Contributors: itemis AG - initial API and implementation Cloudsmith Inc - changes to store 'use project settings' in project /
 
 
 import java.util.List;
 
 import  org.eclipse.core.resources.IProject;
 import  org.eclipse.core.resources.ProjectScope;
 import  org.eclipse.core.runtime.IAdaptable;
 import  org.eclipse.core.runtime.QualifiedName;
 import  org.eclipse.core.runtime.preferences.ConfigurationScope;
 import  org.eclipse.core.runtime.preferences.IEclipsePreferences;
 import  org.eclipse.core.runtime.preferences.IScopeContext;
 import  org.eclipse.core.runtime.preferences.InstanceScope;
 import  org.eclipse.jface.preference.FieldEditor;
 import  org.eclipse.jface.preference.FieldEditorPreferencePage;
 import  org.eclipse.jface.preference.IPreferenceStore;
 import  org.eclipse.swt.SWT;
 import  org.eclipse.swt.events.SelectionAdapter;
 import  org.eclipse.swt.events.SelectionEvent;
 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.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;
 
This implementation works with a FixedScopedPreferenceStore

The following changes have been made:

  • 'useProjectSettings' is now stored in the project preference store as opposed to the project meta-data stored in the workspace. This is done so that import of a project automatically gets the intended 'useProjectSettings'.

Author(s):
Dennis H´┐Żbner - Initial contribution and API
Henrik Lindberg - changes to store 'use project settings' in project
See also:
PropertyAndPreferencePage
 
 public abstract class AbstractPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage,
 		IWorkbenchPropertyPage {
 
 	private static final Logger log = Logger.getLogger(AbstractPreferencePage.class);
 
 	private static final String USE_PROJECT_SETTINGS = "useProjectSettings"//$NON-NLS-1$
 
 	private IWorkbench workbench;
 	private IProject project;
 
 	private Button useProjectSettingsButton;
 	private final List<FieldEditor> editors = new ArrayList<FieldEditor>();
 
 	private Link link;
 
 	public void init(IWorkbench workbench) {
 		this. = workbench;
 	}
 
 	protected IWorkbench getWorkbench() {
 		return ;
 	}
 
 	public void setElement(IAdaptable element) {
 		this. = (IProject) element.getAdapter(IProject.class);
 	}
 
 	public IAdaptable getElement() {
 		return ;
 	}
 
 	public boolean isPropertyPage() {
 		return  != null;
	}
		super(GRID);
	}
	protected IPreferenceStore doGetPreferenceStore() {
		if (isPropertyPage()) {
		}
	}

Deprecated:
Use qualifiedName() instead
Returns:
the qualifer used to look up the preference node of the configured preferencesstore
	protected String getQualifier() {
		return qualifiedName();
	}
	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * org.eclipse.jface.preference.FieldEditorPreferencePage#createContents
	 * (org.eclipse.swt.widgets.Composite)
	 */
	protected Control createContents(Composite parent) {
		return super.createContents(parent);
	}
	private void createUseProjectSettingsControls(Composite parent) {
		Composite projectSettingsParent = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout(2, false);
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		projectSettingsParent.setLayout(layout);
		projectSettingsParent.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		// use project settings button
		 = new Button(projectSettingsParent, SWT.CHECK);
		.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				.debug("AbstractPreferencePage.widgetSelected()"); //$NON-NLS-1$
			}
		});
		// configure ws settings link
		 = new Link(projectSettingsParent, SWT.NONE);
		.setFont(projectSettingsParent.getFont());
		.setText("<A>" + . + "</A>"); //$NON-NLS-1$
		.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				PreferencesUtil.createPreferenceDialogOn(getShell(), idnew String[] { id }, null).open();
			}
		});
		.setLayoutData(new GridData(SWT.END, SWT.CENTER, truefalse));
		// separator line
		Label horizontalLine = new Label(projectSettingsParent, SWT.SEPARATOR | SWT.HORIZONTAL);
		horizontalLine.setLayoutData(new GridData(GridData.FILL, GridData.FILL, truefalse, 2, 1));
		horizontalLine.setFont(projectSettingsParent.getFont());
		//backward compatibility
	}
	private void restoreOldSettings() {
		if (isPropertyPage()) {
			QualifiedName oldKey = new QualifiedName(qualifiedName(), );
			try {
				String oldValue = currentProject().getPersistentProperty(oldKey);
				if (oldValue != null) {
					//remove old entry
					currentProject().setPersistentProperty(oldKeynull);
					//if were true - save copy into project settings
					if (Boolean.valueOf(oldValue)) {
					}
				}
catch (Exception e) {
				.debug("Failed to read persistent property"e);
			}
		}
	}
	private IProject currentProject() {
		if ( == null)
			throw new IllegalStateException("Not a property page case, but current project was requested."); //$NON-NLS-1$
		return ;
	}
	@Named("languageName")
	protected String getLanguageName() {
		return this.;
	}

Returns:
prefix for preference keys
	protected String qualifiedName() {
		return ;
	}
	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * org.eclipse.jface.preference.PreferencePage#createControl(org.eclipse
	 * .swt.widgets.Composite)
	 */
	public void createControl(Composite parent) {
		super.createControl(parent);
	}
	protected void addField(FieldEditor editor) {
		.add(editor);
		super.addField(editor);
	}

Gets the 'useProjectSettings' flag in the project preferences.

Returns:
true if the settings on this page are project specific
	}

Produces the preference key to use for the 'use project settings' flag when this preference page is is used as a properties page.

Returns:
the concatenation of qualifiedName(), "." and USE_PROJECT_SETTINGS.
Since:
2.8
		return qualifiedName() + "." + ;
	}

Switches the search scope of the preference store to use [Project, Instance, Configuration] if values are project specific, and [Instance, Configuration] otherwise. This implementation requires that the given preference store is based on the Project preference store when the page is used as a Properties page. (This is done in doGetPreferenceStore()).
	@SuppressWarnings("deprecation")
	private void handleUseProjectSettings() {
		// Note: uses the pre Eclipse 3.6 way of specifying search scopes (deprecated since 3.6)
		boolean isUseProjectSettings = .getSelection();
		.setEnabled(!isUseProjectSettings);
		if (!isUseProjectSettings) {
					new InstanceScope(), new ConfigurationScope() });
else {
					new ProjectScope(currentProject()), new InstanceScope(), new ConfigurationScope() });
		}
		updateFieldEditors(isUseProjectSettings);
	}

Loads values of all field editors using current search scopes in the preference store. Also updates fields enabled status. (The effect is that fields show project specific values when enabled, and instance scoped/default values when disabled).

Parameters:
enabled
	protected void updateFieldEditors(boolean enabled) {
		Composite parent = getFieldEditorParent();
		for (FieldEditor editor : ) {
			editor.load();
			editor.setEnabled(enabledparent);
		}
		getDefaultsButton().setEnabled(enabled);
	}
	public boolean performOk() {
		boolean retVal = super.performOk();
		if (retVal && isPropertyPage()) {
			try {
catch (Exception e) {
				.error("Error"e); //$NON-NLS-1$
				retVal = false;
			}
		}
		return retVal;
	}

Saves the 'use project settings' as a preference in the store using the useProjectSettingsPreferenceName() as the key. If not project specific, all affected keys are removed from the project preferences.

Throws:
IOException
	private void saveUseProjectSettings(boolean isProjectSpecificthrows IOException {
		if (!isProjectSpecific) {
			// remove all the keys (written by various field editors)
			IEclipsePreferences storePreferences = store.getStorePreferences();
			for (FieldEditor field : ) {
				storePreferences.remove(field.getPreferenceName());
			}
			// Also remove the master key (i.e. use default/default == false when later reading).
			storePreferences.remove(useProjectSettingsPreferenceName());
else {
			store.setValue(useProjectSettingsPreferenceName(), Boolean.toString(isProjectSpecific));
		}
		store.save();
	}

Copies all the project specific values (except master key) to the values of the instance preference store.
	private void setProjectSpecificValues() {
		IEclipsePreferences storePreferences = store.getStorePreferences();
		for (FieldEditor field : ) {
			String key = field.getPreferenceName();
			storePreferences.put(keystore.getString(key));
		}
	}
New to GrepCode? Check out our FAQ X