1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-05 07:15:39 +02:00

220725: Support for project specific Code Style settings

This commit is contained in:
Anton Leherbauer 2008-03-03 09:42:59 +00:00
parent 72d87e8b69
commit 629404da42
9 changed files with 546 additions and 134 deletions

View file

@ -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 <code>null</code> 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;
}

View file

@ -1931,7 +1931,20 @@
</and>
</enabledWhen>
</page>
<page
name="%CodeFormatterPreferencePage.name"
class="org.eclipse.cdt.internal.ui.preferences.CodeFormatterPreferencePage"
category="org.eclipse.cdt.ui.newui.Page_head_general"
id="org.eclipse.cdt.ui.propertyPages.CodeFormatterPreferencePage">
<enabledWhen>
<adapt type="org.eclipse.core.resources.IProject">
<test property="org.eclipse.core.resources.projectNature" value="org.eclipse.cdt.core.cnature"/>
</adapt>
</enabledWhen>
</page>
</extension>
<extension
point="org.eclipse.cdt.ui.PathContainerPage">
<PathContainerPage

View file

@ -16,15 +16,18 @@ package org.eclipse.cdt.internal.ui.editor;
import java.text.CharacterIterator;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
@ -126,6 +129,7 @@ import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionContext;
import org.eclipse.ui.actions.ActionGroup;
import org.eclipse.ui.dnd.IDragAndDropService;
import org.eclipse.ui.editors.text.EditorsUI;
import org.eclipse.ui.editors.text.TextEditor;
import org.eclipse.ui.ide.IGotoMarker;
import org.eclipse.ui.navigator.ICommonMenuConstants;
@ -135,6 +139,7 @@ import org.eclipse.ui.part.IShowInTargetList;
import org.eclipse.ui.part.ShowInContext;
import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
import org.eclipse.ui.texteditor.AbstractMarkerAnnotationModel;
import org.eclipse.ui.texteditor.ChainedPreferenceStore;
import org.eclipse.ui.texteditor.ContentAssistAction;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.IEditorStatusLine;
@ -204,10 +209,12 @@ import org.eclipse.cdt.internal.ui.text.CWordFinder;
import org.eclipse.cdt.internal.ui.text.CWordIterator;
import org.eclipse.cdt.internal.ui.text.DocumentCharacterIterator;
import org.eclipse.cdt.internal.ui.text.ICReconcilingListener;
import org.eclipse.cdt.internal.ui.text.PreferencesAdapter;
import org.eclipse.cdt.internal.ui.text.Symbols;
import org.eclipse.cdt.internal.ui.text.c.hover.SourceViewerInformationControl;
import org.eclipse.cdt.internal.ui.text.contentassist.ContentAssistPreference;
import org.eclipse.cdt.internal.ui.util.CUIHelp;
import org.eclipse.cdt.internal.ui.util.EditorUtility;
import org.eclipse.cdt.internal.ui.viewsupport.ISelectionListenerWithAST;
import org.eclipse.cdt.internal.ui.viewsupport.SelectionListenerWithASTManager;
@ -372,10 +379,12 @@ public class CEditor extends TextEditor implements ISelectionChangedListener, IC
preferences.put(DefaultCodeFormatterConstants.FORMATTER_CURRENT_FILE, tu.getResource());
}
// custom formatter specified?
String customFormatterId= getPreferenceStore().getString(CCorePreferenceConstants.CODE_FORMATTER);
if (customFormatterId != null) {
preferences.put(CCorePreferenceConstants.CODE_FORMATTER, customFormatterId);
if (cProject == null) {
// custom formatter specified?
String customFormatterId= getPreferenceStore().getString(CCorePreferenceConstants.CODE_FORMATTER);
if (customFormatterId != null) {
preferences.put(CCorePreferenceConstants.CODE_FORMATTER, customFormatterId);
}
}
context.setProperty(FormattingContextProperties.CONTEXT_PREFERENCES, preferences);
@ -1234,17 +1243,6 @@ public class CEditor extends TextEditor implements ISelectionChangedListener, IC
* Default constructor.
*/
public CEditor() {
super();
}
/**
* @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#initializeEditor()
*/
protected void initializeEditor() {
IPreferenceStore store= CUIPlugin.getDefault().getCombinedPreferenceStore();
setPreferenceStore(store);
CTextTools textTools = CUIPlugin.getDefault().getTextTools();
setSourceViewerConfiguration(new CSourceViewerConfiguration(textTools.getColorManager(), store, this, textTools.getDocumentPartitioning()));
setDocumentProvider(CUIPlugin.getDefault().getDocumentProvider());
setEditorContextMenuId("#CEditorContext"); //$NON-NLS-1$
@ -1252,30 +1250,51 @@ public class CEditor extends TextEditor implements ISelectionChangedListener, IC
setOutlinerContextMenuId("#CEditorOutlinerContext"); //$NON-NLS-1$
fCEditorErrorTickUpdater = new CEditorErrorTickUpdater(this);
}
fStickyOccurrenceAnnotations= store.getBoolean(PreferenceConstants.EDITOR_STICKY_OCCURRENCES);
/**
* @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#initializeEditor()
*/
protected void initializeEditor() {
}
/**
* @see org.eclipse.ui.texteditor.AbstractTextEditor#doSetInput(org.eclipse.ui.IEditorInput)
*/
protected void doSetInput(IEditorInput input) throws CoreException {
boolean reuse= getEditorInput() != null;
ISourceViewer sourceViewer= getSourceViewer();
if (!(sourceViewer instanceof ISourceViewerExtension2)) {
setPreferenceStore(createCombinedPreferenceStore(input));
internalDoSetInput(input);
return;
}
// uninstall & unregister preference store listener
getSourceViewerDecorationSupport(sourceViewer).uninstall();
((ISourceViewerExtension2)sourceViewer).unconfigure();
setPreferenceStore(createCombinedPreferenceStore(input));
// install & register preference store listener
sourceViewer.configure(getSourceViewerConfiguration());
getSourceViewerDecorationSupport(sourceViewer).install(getPreferenceStore());
internalDoSetInput(input);
}
private void internalDoSetInput(IEditorInput input) throws CoreException {
ISourceViewer sourceViewer= getSourceViewer();
CSourceViewer cSourceViewer= null;
if (sourceViewer instanceof CSourceViewer) {
cSourceViewer= (CSourceViewer) sourceViewer;
}
IPreferenceStore store= getPreferenceStore();
if (cSourceViewer != null && isFoldingEnabled() && (store == null || !store.getBoolean(PreferenceConstants.EDITOR_SHOW_SEGMENTS)))
cSourceViewer.prepareDelayedProjection();
super.doSetInput(input);
if (reuse) {
// in case language changed, need to reconfigure the viewer
ISourceViewer viewer= getSourceViewer();
if (viewer instanceof ISourceViewerExtension2) {
ISourceViewerExtension2 viewerExt2= (ISourceViewerExtension2)viewer;
viewerExt2.unconfigure();
CSourceViewerConfiguration cConfig= (CSourceViewerConfiguration)getSourceViewerConfiguration();
cConfig.resetScanners();
viewer.configure(cConfig);
}
}
setOutlinePageInput(fOutlinePage, input);
if (fProjectionModelUpdater != null) {
@ -1284,7 +1303,6 @@ public class CEditor extends TextEditor implements ISelectionChangedListener, IC
if (fCEditorErrorTickUpdater != null) {
fCEditorErrorTickUpdater.updateEditorImage(getInputCElement());
}
ICElement element= getInputCElement();
if (element instanceof ITranslationUnit) {
fBracketMatcher.configure(((ITranslationUnit)element).getLanguage());
@ -1293,6 +1311,25 @@ public class CEditor extends TextEditor implements ISelectionChangedListener, IC
}
}
/*
* @see org.eclipse.ui.texteditor.AbstractTextEditor#setPreferenceStore(org.eclipse.jface.preference.IPreferenceStore)
* @since 5.0
*/
protected void setPreferenceStore(IPreferenceStore store) {
super.setPreferenceStore(store);
SourceViewerConfiguration sourceViewerConfiguration= getSourceViewerConfiguration();
if (!(sourceViewerConfiguration instanceof CSourceViewerConfiguration)) {
CTextTools textTools= CUIPlugin.getDefault().getTextTools();
setSourceViewerConfiguration(new CSourceViewerConfiguration(textTools.getColorManager(), store, this, ICPartitions.C_PARTITIONING));
}
if (getSourceViewer() instanceof CSourceViewer)
((CSourceViewer)getSourceViewer()).setPreferenceStore(store);
fMarkOccurrenceAnnotations= store.getBoolean(PreferenceConstants.EDITOR_MARK_OCCURRENCES);
fStickyOccurrenceAnnotations= store.getBoolean(PreferenceConstants.EDITOR_STICKY_OCCURRENCES);
}
/**
* Update the title image.
* @param image Title image.
@ -2171,6 +2208,8 @@ public class CEditor extends TextEditor implements ISelectionChangedListener, IC
if (sourceViewer instanceof ITextViewerExtension)
((ITextViewerExtension) sourceViewer).prependVerifyKeyListener(fBracketInserter);
if (isMarkingOccurrences())
installOccurrencesFinder(false);
}
/*
@ -2735,11 +2774,11 @@ public class CEditor extends TextEditor implements ISelectionChangedListener, IC
}
// delayed installation of mark occurrences
if (!fMarkOccurrenceAnnotations && isMarkingOccurrences())
getSite().getShell().getDisplay().asyncExec(new Runnable() {
public void run() {
installOccurrencesFinder(true);
}});
// if (!fMarkOccurrenceAnnotations && isMarkingOccurrences())
// getSite().getShell().getDisplay().asyncExec(new Runnable() {
// public void run() {
// installOccurrencesFinder(true);
// }});
}
/**
@ -3174,4 +3213,25 @@ public class CEditor extends TextEditor implements ISelectionChangedListener, IC
}
}
/**
* Creates and returns the preference store for this editor with the given input.
*
* @param input The editor input for which to create the preference store
* @return the preference store for this editor
*/
private IPreferenceStore createCombinedPreferenceStore(IEditorInput input) {
List<IPreferenceStore> stores= new ArrayList<IPreferenceStore>(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()]));
}
}

View file

@ -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<ITextPresentationListener>();
fTextPresentationListeners.remove(listener);
fTextPresentationListeners.add(0, listener);

View file

@ -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}.
* <p>
* This preference store is read-only i.e. write access
* throws an {@link java.lang.UnsupportedOperationException}.
* </p>
*/
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();
}
}

View file

@ -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) {

View file

@ -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);
}

View file

@ -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<String, String> idMap = new HashMap<String, String>();
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<String> 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<String, String>();
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();

View file

@ -618,4 +618,28 @@ public class EditorUtility {
}
return store;
}
/**
* Returns the C project for a given editor input or <code>null</code> 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;
}
}