diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CProject.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CProject.java index edb133e8101..13bf4d2ac48 100644 --- a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CProject.java +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CProject.java @@ -50,11 +50,15 @@ import org.eclipse.cdt.core.settings.model.ICSourceEntry; import org.eclipse.cdt.internal.core.settings.model.CProjectDescriptionManager; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.ProjectScope; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.Preferences; import org.eclipse.core.runtime.QualifiedName; +import org.eclipse.core.runtime.preferences.IEclipsePreferences; +import org.eclipse.core.runtime.preferences.IScopeContext; +import org.osgi.service.prefs.BackingStoreException; public class CProject extends Openable implements ICProject { @@ -242,15 +246,14 @@ public class CProject extends Openable implements ICProject { * @see org.eclipse.cdt.core.model.ICProject#getOption(String, boolean) */ public String getOption(String optionName, boolean inheritCCoreOptions) { - if (CModelManager.OptionNames.contains(optionName)) { - Preferences preferences = getPreferences(); - - if (preferences == null || preferences.isDefault(optionName)) { - return inheritCCoreOptions ? CCorePlugin.getOption(optionName) : null; + IEclipsePreferences preferences = getPreferences(); + final String cCoreDefault= inheritCCoreOptions ? CCorePlugin.getOption(optionName) : null; + if (preferences == null) { + return cCoreDefault; } - - return preferences.getString(optionName).trim(); + String value= preferences.get(optionName, cCoreDefault).trim(); + return value == null ? null : value.trim(); } return null; @@ -261,30 +264,25 @@ public class CProject extends Openable implements ICProject { */ public Map getOptions(boolean inheritCCoreOptions) { // initialize to the defaults from CCorePlugin options pool - Map options = inheritCCoreOptions ? CCorePlugin.getOptions() : new HashMap(5); + Map options= inheritCCoreOptions ? CCorePlugin.getOptions() : new HashMap(5); - Preferences preferences = getPreferences(); + IEclipsePreferences preferences = getPreferences(); if (preferences == null) return options; - HashSet optionNames = CModelManager.OptionNames; + HashSet optionNames= CModelManager.OptionNames; - // get preferences set to their default - if (inheritCCoreOptions) { - String[] defaultPropertyNames = preferences.defaultPropertyNames(); - for (int i = 0; i < defaultPropertyNames.length; i++) { - String propertyName = defaultPropertyNames[i]; - if (optionNames.contains(propertyName)) { - options.put(propertyName, preferences.getDefaultString(propertyName).trim()); + // create project options + try { + String[] propertyNames= preferences.keys(); + for (int i= 0; i < propertyNames.length; i++){ + String propertyName= propertyNames[i]; + String value= preferences.get(propertyName, null); + if (value != null && optionNames.contains(propertyName)){ + options.put(propertyName, value.trim()); } } - } - // get custom preferences not set to their default - String[] propertyNames = preferences.propertyNames(); - for (int i = 0; i < propertyNames.length; i++) { - String propertyName = propertyNames[i]; - if (optionNames.contains(propertyName)) { - options.put(propertyName, preferences.getString(propertyName).trim()); - } + } catch (BackingStoreException e) { + // ignore silently } return options; } @@ -296,11 +294,20 @@ public class CProject extends Openable implements ICProject { if (!CModelManager.OptionNames.contains(optionName)) return; // unrecognized option - Preferences preferences = getPreferences(); - preferences.setDefault(optionName, CUSTOM_DEFAULT_OPTION_VALUE); // empty string isn't the default (26251) - preferences.setValue(optionName, optionValue); - - savePreferences(preferences); + IEclipsePreferences projectPreferences= getPreferences(); + if (optionValue == null) { + // remove preference + projectPreferences.remove(optionName); + } else { + projectPreferences.put(optionName, optionValue); + } + + // Dump changes + try { + projectPreferences.flush(); + } catch (BackingStoreException e) { + // problem with pref store - quietly ignore + } } /** @@ -332,28 +339,15 @@ public class CProject extends Openable implements ICProject { /** * Returns the project custom preference pool. * Project preferences may include custom encoding. + * @return IEclipsePreferences or null if the project + * does not have a C nature. */ - private Preferences getPreferences() { + private IEclipsePreferences getPreferences() { if (!(isCProject())) { return null; } - Preferences preferences = new Preferences(); - Iterator iter = CModelManager.OptionNames.iterator(); - - while (iter.hasNext()) { - String qualifiedName = (String) iter.next(); - String dequalifiedName = qualifiedName.substring(CCorePlugin.PLUGIN_ID.length() + 1); - String value = null; - - try { - value = resource.getPersistentProperty(new QualifiedName(CCorePlugin.PLUGIN_ID, dequalifiedName)); - } catch (CoreException e) { - } - - if (value != null) - preferences.setValue(qualifiedName, value); - } - + IScopeContext context= new ProjectScope(getProject()); + final IEclipsePreferences preferences= context.getNode(CCorePlugin.PLUGIN_ID); return preferences; } diff --git a/core/org.eclipse.cdt.ui/plugin.xml b/core/org.eclipse.cdt.ui/plugin.xml index f4db8b8f54d..02a421b9bbf 100644 --- a/core/org.eclipse.cdt.ui/plugin.xml +++ b/core/org.eclipse.cdt.ui/plugin.xml @@ -1931,7 +1931,20 @@ + + + + + + + + + stores= new ArrayList(3); + + ICProject project= EditorUtility.getCProject(input); + if (project != null) { + stores.add(new EclipsePreferencesAdapter(new ProjectScope(project.getProject()), CCorePlugin.PLUGIN_ID)); + } + + stores.add(CUIPlugin.getDefault().getPreferenceStore()); + stores.add(new PreferencesAdapter(CCorePlugin.getDefault().getPluginPreferences())); + stores.add(EditorsUI.getPreferenceStore()); + + return new ChainedPreferenceStore(stores.toArray(new IPreferenceStore[stores.size()])); + } + } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CSourceViewer.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CSourceViewer.java index 006f736fc70..207550232e8 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CSourceViewer.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CSourceViewer.java @@ -169,6 +169,7 @@ public class CSourceViewer extends ProjectionViewer implements IPropertyChangeLi if (configuration instanceof CSourceViewerConfiguration) { CSourceViewerConfiguration cConfiguration= (CSourceViewerConfiguration)configuration; + cConfiguration.resetScanners(); fOutlinePresenter= cConfiguration.getOutlinePresenter(this); if (fOutlinePresenter != null) fOutlinePresenter.install(this); @@ -404,7 +405,7 @@ public class CSourceViewer extends ProjectionViewer implements IPropertyChangeLi Assert.isNotNull(listener); if (fTextPresentationListeners == null) - fTextPresentationListeners= new ArrayList(); + fTextPresentationListeners= new ArrayList(); fTextPresentationListeners.remove(listener); fTextPresentationListeners.add(0, listener); diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/EclipsePreferencesAdapter.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/EclipsePreferencesAdapter.java new file mode 100644 index 00000000000..3527c58a37b --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/EclipsePreferencesAdapter.java @@ -0,0 +1,316 @@ +/******************************************************************************* + * Copyright (c) 2005, 2008 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.cdt.internal.ui.editor; + +import org.eclipse.core.runtime.ListenerList; +import org.eclipse.core.runtime.preferences.IEclipsePreferences; +import org.eclipse.core.runtime.preferences.IScopeContext; +import org.eclipse.jface.preference.IPreferenceStore; +import org.eclipse.jface.util.IPropertyChangeListener; +import org.eclipse.jface.util.PropertyChangeEvent; +import org.eclipse.swt.widgets.Display; +import org.osgi.service.prefs.BackingStoreException; + +/** + * Adapts an options {@link IEclipsePreferences} to {@link org.eclipse.jface.preference.IPreferenceStore}. + *

+ * This preference store is read-only i.e. write access + * throws an {@link java.lang.UnsupportedOperationException}. + *

+ */ +class EclipsePreferencesAdapter implements IPreferenceStore { + + /** + * Preference change listener. Listens for events preferences + * fires a {@link org.eclipse.jface.util.PropertyChangeEvent} + * on this adapter with arguments from the received event. + */ + private class PreferenceChangeListener implements IEclipsePreferences.IPreferenceChangeListener { + + /** + * {@inheritDoc} + */ + public void preferenceChange(final IEclipsePreferences.PreferenceChangeEvent event) { + if (Display.getCurrent() == null) { + Display.getDefault().asyncExec(new Runnable() { + public void run() { + firePropertyChangeEvent(event.getKey(), event.getOldValue(), event.getNewValue()); + } + }); + } else { + firePropertyChangeEvent(event.getKey(), event.getOldValue(), event.getNewValue()); + } + } + } + + /** Listeners on on this adapter */ + private ListenerList fListeners= new ListenerList(ListenerList.IDENTITY); + + /** Listener on the node */ + private IEclipsePreferences.IPreferenceChangeListener fListener= new PreferenceChangeListener(); + + /** wrapped node */ + private final IScopeContext fContext; + private final String fQualifier; + + /** + * Initialize with the node to wrap + * + * @param context the context to access + * @param qualifier the qualifier + */ + public EclipsePreferencesAdapter(IScopeContext context, String qualifier) { + fContext= context; + fQualifier= qualifier; + } + + private IEclipsePreferences getNode() { + return fContext.getNode(fQualifier); + } + + /** + * {@inheritDoc} + */ + public void addPropertyChangeListener(IPropertyChangeListener listener) { + if (fListeners.size() == 0) + getNode().addPreferenceChangeListener(fListener); + fListeners.add(listener); + } + + /** + * {@inheritDoc} + */ + public void removePropertyChangeListener(IPropertyChangeListener listener) { + fListeners.remove(listener); + if (fListeners.size() == 0) { + getNode().removePreferenceChangeListener(fListener); + } + } + + /** + * {@inheritDoc} + */ + public boolean contains(String name) { + return getNode().get(name, null) != null; + } + + /** + * {@inheritDoc} + */ + public void firePropertyChangeEvent(String name, Object oldValue, Object newValue) { + PropertyChangeEvent event= new PropertyChangeEvent(this, name, oldValue, newValue); + Object[] listeners= fListeners.getListeners(); + for (int i= 0; i < listeners.length; i++) + ((IPropertyChangeListener) listeners[i]).propertyChange(event); + } + + /** + * {@inheritDoc} + */ + public boolean getBoolean(String name) { + return getNode().getBoolean(name, BOOLEAN_DEFAULT_DEFAULT); + } + + /** + * {@inheritDoc} + */ + public boolean getDefaultBoolean(String name) { + return BOOLEAN_DEFAULT_DEFAULT; + } + + /** + * {@inheritDoc} + */ + public double getDefaultDouble(String name) { + return DOUBLE_DEFAULT_DEFAULT; + } + + /** + * {@inheritDoc} + */ + public float getDefaultFloat(String name) { + return FLOAT_DEFAULT_DEFAULT; + } + + /** + * {@inheritDoc} + */ + public int getDefaultInt(String name) { + return INT_DEFAULT_DEFAULT; + } + + /** + * {@inheritDoc} + */ + public long getDefaultLong(String name) { + return LONG_DEFAULT_DEFAULT; + } + + /** + * {@inheritDoc} + */ + public String getDefaultString(String name) { + return STRING_DEFAULT_DEFAULT; + } + + /** + * {@inheritDoc} + */ + public double getDouble(String name) { + return getNode().getDouble(name, DOUBLE_DEFAULT_DEFAULT); + } + + /** + * {@inheritDoc} + */ + public float getFloat(String name) { + return getNode().getFloat(name, FLOAT_DEFAULT_DEFAULT); + } + + /** + * {@inheritDoc} + */ + public int getInt(String name) { + return getNode().getInt(name, INT_DEFAULT_DEFAULT); + } + + /** + * {@inheritDoc} + */ + public long getLong(String name) { + return getNode().getLong(name, LONG_DEFAULT_DEFAULT); + } + + /** + * {@inheritDoc} + */ + public String getString(String name) { + return getNode().get(name, STRING_DEFAULT_DEFAULT); + } + + /** + * {@inheritDoc} + */ + public boolean isDefault(String name) { + return false; + } + + /** + * {@inheritDoc} + */ + public boolean needsSaving() { + try { + return getNode().keys().length > 0; + } catch (BackingStoreException e) { + // ignore + } + return true; + } + + /** + * {@inheritDoc} + */ + public void putValue(String name, String value) { + throw new UnsupportedOperationException(); + } + + /** + * {@inheritDoc} + */ + public void setDefault(String name, double value) { + throw new UnsupportedOperationException(); + } + + /** + * {@inheritDoc} + */ + public void setDefault(String name, float value) { + throw new UnsupportedOperationException(); + } + + /** + * {@inheritDoc} + */ + public void setDefault(String name, int value) { + throw new UnsupportedOperationException(); + } + + /** + * {@inheritDoc} + */ + public void setDefault(String name, long value) { + throw new UnsupportedOperationException(); + } + + /** + * {@inheritDoc} + */ + public void setDefault(String name, String defaultObject) { + throw new UnsupportedOperationException(); + } + + /** + * {@inheritDoc} + */ + public void setDefault(String name, boolean value) { + throw new UnsupportedOperationException(); + } + + /** + * {@inheritDoc} + */ + public void setToDefault(String name) { + throw new UnsupportedOperationException(); + } + + /** + * {@inheritDoc} + */ + public void setValue(String name, double value) { + throw new UnsupportedOperationException(); + } + + /** + * {@inheritDoc} + */ + public void setValue(String name, float value) { + throw new UnsupportedOperationException(); + } + + /** + * {@inheritDoc} + */ + public void setValue(String name, int value) { + throw new UnsupportedOperationException(); + } + + /** + * {@inheritDoc} + */ + public void setValue(String name, long value) { + throw new UnsupportedOperationException(); + } + + /** + * {@inheritDoc} + */ + public void setValue(String name, String value) { + throw new UnsupportedOperationException(); + } + + /** + * {@inheritDoc} + */ + public void setValue(String name, boolean value) { + throw new UnsupportedOperationException(); + } + +} \ No newline at end of file diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/CodeFormatterPreferencePage.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/CodeFormatterPreferencePage.java index 959da136de9..43cc5d0dc25 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/CodeFormatterPreferencePage.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/CodeFormatterPreferencePage.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2006 QNX Software Systems and others. + * Copyright (c) 2000, 2008 QNX Software Systems 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 @@ -8,20 +8,17 @@ * Contributors: * QNX Software Systems - Initial API and implementation * Sergey Prigogin, Google + * Anton Leherbauer (Wind River Systems) *******************************************************************************/ package org.eclipse.cdt.internal.ui.preferences; -import org.eclipse.core.runtime.IAdaptable; - import org.eclipse.core.resources.IProject; - +import org.eclipse.core.runtime.IAdaptable; +import org.eclipse.jface.preference.IPreferencePageContainer; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; - -import org.eclipse.jface.preference.IPreferencePageContainer; - import org.eclipse.ui.PlatformUI; import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer; import org.eclipse.ui.preferences.IWorkingCopyManager; @@ -47,7 +44,7 @@ public class CodeFormatterPreferencePage extends PropertyAndPreferencePage { setTitle(PreferencesMessages.CodeFormatterPreferencePage_title); } - /* (non-Javadoc) + /* * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite) */ public void createControl(Composite parent) { @@ -65,21 +62,21 @@ public class CodeFormatterPreferencePage extends PropertyAndPreferencePage { PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), ICHelpContextIds.CODEFORMATTER_PREFERENCE_PAGE); } - /* (non-Javadoc) + /* * @see org.eclipse.cdt.internal.ui.preferences.PropertyAndPreferencePage#createPreferenceContent(org.eclipse.swt.widgets.Composite) */ protected Control createPreferenceContent(Composite composite) { return fConfigurationBlock.createContents(composite); } - /* (non-Javadoc) + /* * @see org.eclipse.cdt.internal.ui.preferences.PropertyAndPreferencePage#hasProjectSpecificOptions(org.eclipse.core.resources.IProject) */ protected boolean hasProjectSpecificOptions(IProject project) { return fConfigurationBlock.hasProjectSpecificOptions(project); } - /* (non-Javadoc) + /* * @see org.eclipse.cdt.internal.ui.preferences.PropertyAndPreferencePage#enableProjectSpecificSettings(boolean) */ protected void enableProjectSpecificSettings(boolean useProjectSpecificSettings) { @@ -89,23 +86,21 @@ public class CodeFormatterPreferencePage extends PropertyAndPreferencePage { } } - /* (non-Javadoc) + /* * @see org.eclipse.cdt.internal.ui.preferences.PropertyAndPreferencePage#getPreferencePageID() */ protected String getPreferencePageID() { return PREF_ID; } - /* (non-Javadoc) + /* * @see org.eclipse.cdt.internal.ui.preferences.PropertyAndPreferencePage#getPropertyPageID() */ protected String getPropertyPageID() { - return null; - // project specific settings unsupported for now -// return PROP_ID; + return PROP_ID; } - /* (non-Javadoc) + /* * @see org.eclipse.jface.dialogs.DialogPage#dispose() */ public void dispose() { @@ -115,7 +110,7 @@ public class CodeFormatterPreferencePage extends PropertyAndPreferencePage { super.dispose(); } - /* (non-Javadoc) + /* * @see org.eclipse.jface.preference.IPreferencePage#performDefaults() */ protected void performDefaults() { @@ -125,7 +120,7 @@ public class CodeFormatterPreferencePage extends PropertyAndPreferencePage { super.performDefaults(); } - /* (non-Javadoc) + /* * @see org.eclipse.jface.preference.IPreferencePage#performOk() */ public boolean performOk() { @@ -135,7 +130,7 @@ public class CodeFormatterPreferencePage extends PropertyAndPreferencePage { return super.performOk(); } - /* (non-Javadoc) + /* * @see org.eclipse.jface.preference.IPreferencePage#performOk() */ public void performApply() { @@ -145,7 +140,7 @@ public class CodeFormatterPreferencePage extends PropertyAndPreferencePage { super.performApply(); } - /* (non-Javadoc) + /* * @see org.eclipse.cdt.internal.ui.preferences.PropertyAndPreferencePage#setElement(org.eclipse.core.runtime.IAdaptable) */ public void setElement(IAdaptable element) { diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/CodeFormatterConfigurationBlock.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/CodeFormatterConfigurationBlock.java index 8e180e00f30..73611ba0cea 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/CodeFormatterConfigurationBlock.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/CodeFormatterConfigurationBlock.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2007 IBM Corporation and others. + * Copyright (c) 2000, 2008 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 @@ -98,10 +98,7 @@ public class CodeFormatterConfigurationBlock extends ProfileConfigurationBlock { */ public CodeFormatterConfigurationBlock(IProject project, PreferencesAccess access) { super(project, access, DIALOGSTORE_LASTSAVELOADPATH); - if (project == null) { - //TLETODO formatter customizable on project level? - fCustomCodeFormatterBlock= new CustomCodeFormatterBlock(access); - } + fCustomCodeFormatterBlock= new CustomCodeFormatterBlock(project, access); } protected IProfileVersioner createProfileVersioner() { @@ -116,12 +113,9 @@ public class CodeFormatterConfigurationBlock extends ProfileConfigurationBlock { return new FormatterProfileManager(profiles, context, access, profileVersioner); } - protected void configurePreview(Composite composite, int numColumns, ProfileManager profileManager) { - if (fCustomCodeFormatterBlock != null) { - fCustomCodeFormatterBlock.createContents(composite); - } - + fCustomCodeFormatterBlock.createContents(composite); + createLabel(composite, FormatterMessages.CodingStyleConfigurationBlock_preview_label_text, numColumns); TranslationUnitPreview result= new TranslationUnitPreview(profileManager.getSelected().getSettings(), composite); result.setFormatterId(fCustomCodeFormatterBlock.getFormatterId()); @@ -138,7 +132,6 @@ public class CodeFormatterConfigurationBlock extends ProfileConfigurationBlock { new PreviewController(profileManager); } - protected ModifyDialog createModifyDialog(Shell shell, Profile profile, ProfileManager profileManager, ProfileStore profileStore, boolean newProfile) { return new FormatterModifyDialog(shell, profile, profileManager, profileStore, newProfile, FORMATTER_DIALOG_PREFERENCE_KEY, DIALOGSTORE_LASTSAVELOADPATH); } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/CustomCodeFormatterBlock.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/CustomCodeFormatterBlock.java index 21224d9eba0..a55a5749250 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/CustomCodeFormatterBlock.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/CustomCodeFormatterBlock.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2006 QNX Software Systems and others. + * Copyright (c) 2000, 2008 QNX Software Systems 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 @@ -18,11 +18,13 @@ import java.util.Iterator; import java.util.Map; import java.util.Observable; +import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.IConfigurationElement; import org.eclipse.core.runtime.IExtension; import org.eclipse.core.runtime.IExtensionPoint; import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.preferences.IEclipsePreferences; +import org.eclipse.core.runtime.preferences.IScopeContext; import org.eclipse.jface.resource.JFaceResources; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; @@ -47,7 +49,7 @@ import org.eclipse.cdt.internal.ui.preferences.PreferencesAccess; */ public class CustomCodeFormatterBlock extends Observable { - private HashMap idMap = new HashMap(); + private HashMap idMap = new HashMap(); private IEclipsePreferences fPrefs; private String fDefaultFormatterId; private Combo fFormatterCombo; @@ -55,11 +57,29 @@ public class CustomCodeFormatterBlock extends Observable { private static final String ATTR_ID = "id"; //$NON-NLS-1$ private static final String DEFAULT = FormatterMessages.CustomCodeFormatterBlock_default_formatter; - public CustomCodeFormatterBlock(PreferencesAccess access) { - fPrefs = access.getInstanceScope().getNode(CUIPlugin.PLUGIN_ID); - IEclipsePreferences defaults= access.getDefaultScope().getNode(CUIPlugin.PLUGIN_ID); + public CustomCodeFormatterBlock(IProject project, PreferencesAccess access) { + final IScopeContext scope; + final IEclipsePreferences defaults; + if (project != null) { + scope= access.getProjectScope(project); + defaults= access.getInstanceScope().getNode(CCorePlugin.PLUGIN_ID); + } else { + scope= access.getInstanceScope(); + defaults= access.getDefaultScope().getNode(CCorePlugin.PLUGIN_ID); + } + fPrefs= scope.getNode(CCorePlugin.PLUGIN_ID); fDefaultFormatterId= defaults.get(CCorePreferenceConstants.CODE_FORMATTER, null); - + if (fDefaultFormatterId == null) { + // backward compatibility: use UI prefs + IEclipsePreferences instance= access.getInstanceScope().getNode(CUIPlugin.PLUGIN_ID); + fDefaultFormatterId= instance.get(CCorePreferenceConstants.CODE_FORMATTER, null); + if (fDefaultFormatterId != null) { + instance.remove(CCorePreferenceConstants.CODE_FORMATTER); + if (project != null) { + defaults.put(CCorePreferenceConstants.CODE_FORMATTER, fDefaultFormatterId); + } + } + } initializeFormatters(); } @@ -68,8 +88,8 @@ public class CustomCodeFormatterBlock extends Observable { return; } String text = fFormatterCombo.getText(); - String formatterId = (String)idMap.get(text); - if (formatterId != null && formatterId.length() > 0) { + String formatterId = idMap.get(text); + if (formatterId != null && !formatterId.equals(fDefaultFormatterId)) { fPrefs.put(CCorePreferenceConstants.CODE_FORMATTER, formatterId); } else { // simply reset to the default one. @@ -78,11 +98,7 @@ public class CustomCodeFormatterBlock extends Observable { } public void performDefaults() { - if (fDefaultFormatterId != null) { - fPrefs.put(CCorePreferenceConstants.CODE_FORMATTER, fDefaultFormatterId); - } else { - fPrefs.remove(CCorePreferenceConstants.CODE_FORMATTER); - } + fPrefs.remove(CCorePreferenceConstants.CODE_FORMATTER); if (fFormatterCombo == null) { return; @@ -110,7 +126,7 @@ public class CustomCodeFormatterBlock extends Observable { if (fFormatterCombo == null) { return fPrefs.get(CCorePreferenceConstants.CODE_FORMATTER, fDefaultFormatterId); } - String formatterId= (String)idMap.get(fFormatterCombo.getText()); + String formatterId= idMap.get(fFormatterCombo.getText()); return formatterId; } @@ -134,9 +150,9 @@ public class CustomCodeFormatterBlock extends Observable { handleFormatterChanged(); } }); - Iterator items = idMap.keySet().iterator(); + Iterator items = idMap.keySet().iterator(); while (items.hasNext()) { - fFormatterCombo.add((String) items.next()); + fFormatterCombo.add(items.next()); } final String noteTitle= FormatterMessages.CustomCodeFormatterBlock_formatter_note; @@ -175,8 +191,8 @@ public class CustomCodeFormatterBlock extends Observable { } private void initializeFormatters() { - idMap = new HashMap(); - idMap.put(DEFAULT, null); + idMap = new HashMap(); + idMap.put(DEFAULT, CCorePreferenceConstants.DEFAULT_CODE_FORMATTER); IExtensionPoint point = Platform.getExtensionRegistry().getExtensionPoint(CCorePlugin.PLUGIN_ID, CCorePlugin.FORMATTER_EXTPOINT_ID); if (point != null) { IExtension[] exts = point.getExtensions(); diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/EditorUtility.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/EditorUtility.java index 2dd4c48f88d..78906582d3e 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/EditorUtility.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/EditorUtility.java @@ -618,4 +618,28 @@ public class EditorUtility { } return store; } + + /** + * Returns the C project for a given editor input or null if no corresponding + * C project exists. + * + * @param input the editor input + * @return the corresponding C project + * + * @since 5.0 + */ + public static ICProject getCProject(IEditorInput input) { + ICProject cProject= null; + if (input instanceof IFileEditorInput) { + IProject project= ((IFileEditorInput)input).getFile().getProject(); + if (project != null) { + cProject= CoreModel.getDefault().create(project); + if (!cProject.exists()) + cProject= null; + } + } else if (input instanceof ITranslationUnitEditorInput) { + cProject= ((ITranslationUnitEditorInput)input).getTranslationUnit().getCProject(); + } + return cProject; + } }