diff --git a/codan/org.eclipse.cdt.codan.ui/.classpath b/codan/org.eclipse.cdt.codan.ui/.classpath new file mode 100644 index 00000000000..64c5e31b7a2 --- /dev/null +++ b/codan/org.eclipse.cdt.codan.ui/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/codan/org.eclipse.cdt.codan.ui/.project b/codan/org.eclipse.cdt.codan.ui/.project new file mode 100644 index 00000000000..1a97756c65e --- /dev/null +++ b/codan/org.eclipse.cdt.codan.ui/.project @@ -0,0 +1,28 @@ + + + org.eclipse.cdt.codan.ui + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.pde.ManifestBuilder + + + + + org.eclipse.pde.SchemaBuilder + + + + + + org.eclipse.pde.PluginNature + org.eclipse.jdt.core.javanature + + diff --git a/codan/org.eclipse.cdt.codan.ui/.settings/org.eclipse.jdt.core.prefs b/codan/org.eclipse.cdt.codan.ui/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 00000000000..57703d3f61d --- /dev/null +++ b/codan/org.eclipse.cdt.codan.ui/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,8 @@ +#Fri Apr 03 21:34:42 EDT 2009 +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 +org.eclipse.jdt.core.compiler.compliance=1.5 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.5 diff --git a/codan/org.eclipse.cdt.codan.ui/META-INF/MANIFEST.MF b/codan/org.eclipse.cdt.codan.ui/META-INF/MANIFEST.MF new file mode 100644 index 00000000000..876a5db02d7 --- /dev/null +++ b/codan/org.eclipse.cdt.codan.ui/META-INF/MANIFEST.MF @@ -0,0 +1,15 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: Code Analyzers Framework +Bundle-SymbolicName: org.eclipse.cdt.codan.ui; singleton:=true +Bundle-Version: 1.0.0 +Bundle-Activator: org.eclipse.cdt.codan.ui.Activator +Bundle-Vendor: Eclipse +Require-Bundle: org.eclipse.ui, + org.eclipse.core.runtime, + org.eclipse.core.resources, + org.eclipse.cdt.core;bundle-version="5.1.0", + org.eclipse.cdt.codan.core;bundle-version="1.0.0" +Bundle-RequiredExecutionEnvironment: J2SE-1.5 +Bundle-ActivationPolicy: lazy + diff --git a/codan/org.eclipse.cdt.codan.ui/build.properties b/codan/org.eclipse.cdt.codan.ui/build.properties new file mode 100644 index 00000000000..2b0d95b6b02 --- /dev/null +++ b/codan/org.eclipse.cdt.codan.ui/build.properties @@ -0,0 +1,5 @@ +source.. = src/ +output.. = bin/ +bin.includes = plugin.xml,\ + META-INF/,\ + . diff --git a/codan/org.eclipse.cdt.codan.ui/plugin.xml b/codan/org.eclipse.cdt.codan.ui/plugin.xml new file mode 100644 index 00000000000..c4c7b989aea --- /dev/null +++ b/codan/org.eclipse.cdt.codan.ui/plugin.xml @@ -0,0 +1,65 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/preferences/BuildPropertyPage.java b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/preferences/BuildPropertyPage.java new file mode 100644 index 00000000000..0ae613fad17 --- /dev/null +++ b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/preferences/BuildPropertyPage.java @@ -0,0 +1,88 @@ +package org.eclipse.cdt.codan.internal.ui.preferences; + +import org.eclipse.cdt.codan.ui.actions.ToggleNatureAction; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.runtime.IAdaptable; +import org.eclipse.jface.preference.BooleanFieldEditor; +import org.eclipse.jface.preference.FieldEditorPreferencePage; +import org.eclipse.ui.IWorkbenchPropertyPage; + +public class BuildPropertyPage extends FieldEditorPreferencePage implements + IWorkbenchPropertyPage { + private IAdaptable element; + + /** + * + */ + public BuildPropertyPage() { + setPreferenceStore(org.eclipse.cdt.codan.ui.Activator.getDefault() + .getPreferenceStore()); + } + + /* + * (non-Javadoc) + * + * @see + * org.eclipse.jface.preference.FieldEditorPreferencePage#createFieldEditors + * () + */ + @Override + protected void createFieldEditors() { + addField(new BooleanFieldEditor(PreferenceConstants.P_RUN_ON_BUILD, + "&Run with Build", getFieldEditorParent())); + } + + @Override + public boolean performOk() { + boolean result = super.performOk(); + if (result) { + IAdaptable res = getElement(); + if (res instanceof IProject) { + boolean runOnBuild = getPreferenceStore().getBoolean( + PreferenceConstants.P_RUN_ON_BUILD); + new ToggleNatureAction().toggleNature((IProject) res, + runOnBuild); + // if (runOnBuild == false) { + // boolean openQuestion = MessageDialog + // .openQuestion( + // getShell(), + // "Confirmation", + // "Do you want to remove existing problems? If build is disabled they won't be updated anymore."); + // if (openQuestion == true) { + // ErrorReporter.deleteAllMarkers(); + // } + // } + return true; + } + } + return result; + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.ui.IWorkbenchPropertyPage#getElement() + */ + public IAdaptable getElement() { + return element; + } + + /* + * (non-Javadoc) + * + * @see + * org.eclipse.ui.IWorkbenchPropertyPage#setElement(org.eclipse.core.runtime + * .IAdaptable) + */ + public void setElement(IAdaptable element) { + this.element = element; + setPreferenceStore(new PropertyStore((IResource) getElement(), + org.eclipse.cdt.codan.ui.Activator.getDefault() + .getPreferenceStore(), getPageId())); + } + + protected String getPageId() { + return "org.eclipse.cdt.codan.internal.ui.preferences.CodanPreferencePage"; + } +} diff --git a/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/preferences/CheckedTreeEditor.java b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/preferences/CheckedTreeEditor.java new file mode 100644 index 00000000000..006a33569d8 --- /dev/null +++ b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/preferences/CheckedTreeEditor.java @@ -0,0 +1,417 @@ +/******************************************************************************* + * Copyright (c) 2000, 2006 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.codan.internal.ui.preferences; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import org.eclipse.jface.preference.FieldEditor; +import org.eclipse.jface.viewers.CheckStateChangedEvent; +import org.eclipse.jface.viewers.CheckboxTreeViewer; +import org.eclipse.jface.viewers.ICheckStateListener; +import org.eclipse.jface.viewers.ITreeContentProvider; +import org.eclipse.jface.viewers.StructuredViewer; +import org.eclipse.swt.SWT; +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.Shell; +import org.eclipse.swt.widgets.Tree; + +/** + * An abstract field editor that manages a checked tree. Values in the tree + * should be set outside by setting getTreeViewer().setContentProvider() and + * getTreeViewer().setInput() Control stores checked elements only in preference + * storage. + * + *
+ * Subclasses may implement the parseString, + * createList, storeValue and + * getListSeparator framework methods. + *
parseString
createList
storeValue
getListSeparator
null
+ * This method is internal to the framework; subclassers should not call + * this method. + *
+ * Subclasses may implement this method. + *
+ * This page is used to modify preferences only. They are stored in the + * preference store that belongs to the main plug-in class. That way, + * preferences can be accessed directly via the preference store. + */ +public class CodanPreferencePage extends FieldEditorOverlayPage implements + IWorkbenchPreferencePage { + public CodanPreferencePage() { + super(GRID); + setPreferenceStore(new ScopedPreferenceStore(new InstanceScope(), + CodanCorePlugin.PLUGIN_ID)); + setDescription("Code Analyzers Preference Page"); + } + + protected String getPageId() { + return "org.eclipse.cdt.codan.internal.ui.preferences.CodanPreferencePage"; + } + + /** + * Creates the field editors. Field editors are abstractions of the common + * GUI blocks needed to manipulate various types of preferences. Each field + * editor knows how to save and restore itself. + */ + public void createFieldEditors() { + CheckedTreeEditor checkedTreeEditor = new ProblemsTreeEditor( + getFieldEditorParent()); + addField(checkedTreeEditor); + } + + /* + * (non-Javadoc) + * + * @see + * org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench) + */ + public void init(IWorkbench workbench) { + } +} \ No newline at end of file diff --git a/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/preferences/FieldEditorOverlayPage.java b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/preferences/FieldEditorOverlayPage.java new file mode 100644 index 00000000000..441f1b5f9e2 --- /dev/null +++ b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/preferences/FieldEditorOverlayPage.java @@ -0,0 +1,356 @@ +/******************************************************************************* + * Copyright (c) 2003 Berthold Daum. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * Berthold Daum + *******************************************************************************/ +package org.eclipse.cdt.codan.internal.ui.preferences; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.eclipse.core.resources.IResource; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IAdaptable; +import org.eclipse.core.runtime.QualifiedName; +import org.eclipse.jface.preference.FieldEditor; +import org.eclipse.jface.preference.FieldEditorPreferencePage; +import org.eclipse.jface.preference.IPreferenceNode; +import org.eclipse.jface.preference.IPreferencePage; +import org.eclipse.jface.preference.IPreferenceStore; +import org.eclipse.jface.preference.PreferenceDialog; +import org.eclipse.jface.preference.PreferenceManager; +import org.eclipse.jface.preference.PreferenceNode; +import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.BusyIndicator; +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.ui.IWorkbenchPropertyPage; + +/** + * @author Berthold Daum + */ +public abstract class FieldEditorOverlayPage + extends FieldEditorPreferencePage + implements IWorkbenchPropertyPage { + + /*** Name of resource property for the selection of workbench or project settings ***/ + public static final String USEPROJECTSETTINGS = "useProjectSettings"; //$NON-NLS-1$ + + private static final String FALSE = "false"; //$NON-NLS-1$ + private static final String TRUE = "true"; //$NON-NLS-1$ + + // Stores all created field editors + private List editors = new ArrayList(); + + // Stores owning element of properties + private IAdaptable element; + + // Additional buttons for property pages + private Button useWorkspaceSettingsButton, + useProjectSettingsButton, + configureButton; + + // Overlay preference store for property pages + private IPreferenceStore overlayStore; + + // The image descriptor of this pages title image + private ImageDescriptor image; + + // Cache for page id + private String pageId; + + /** + * Constructor + * @param style - layout style + */ + public FieldEditorOverlayPage(int style) { + super(style); + } + + /** + * Constructor + * @param title - title string + * @param style - layout style + */ + public FieldEditorOverlayPage(String title, int style) { + super(title, style); + } + + /** + * Constructor + * @param title - title string + * @param image - title image + * @param style - layout style + */ + public FieldEditorOverlayPage( + String title, + ImageDescriptor image, + int style) { + super(title, image, style); + this.image = image; + } + + /** + * Returns the id of the current preference page as defined in plugin.xml + * Subclasses must implement. + * + * @return - the qualifier + */ + protected abstract String getPageId(); + + /** + * Receives the object that owns the properties shown in this property page. + * @see org.eclipse.ui.IWorkbenchPropertyPage#setElement(org.eclipse.core.runtime.IAdaptable) + */ + public void setElement(IAdaptable element) { + this.element = element; + } + + /** + * Delivers the object that owns the properties shown in this property page. + * @see org.eclipse.ui.IWorkbenchPropertyPage#getElement() + */ + public IAdaptable getElement() { + return element; + } + + /** + * Returns true if this instance represents a property page + * @return - true for property pages, false for preference pages + */ + public boolean isPropertyPage() { + return getElement() != null; + } + + /** + * We override the addField method. This allows us to store each field editor added by subclasses + * in a list for later processing. + * + * @see org.eclipse.jface.preference.FieldEditorPreferencePage#addField(org.eclipse.jface.preference.FieldEditor) + */ + protected void addField(FieldEditor editor) { + editors.add(editor); + super.addField(editor); + } + + /** + * We override the createControl method. + * In case of property pages we create a new PropertyStore as local preference store. + * After all control have been create, we enable/disable these controls. + * + * @see org.eclipse.jface.preference.PreferencePage#createControl() + */ + public void createControl(Composite parent) { + // Special treatment for property pages + if (isPropertyPage()) { + // Cache the page id + pageId = getPageId(); + // Create an overlay preference store and fill it with properties + overlayStore = + new PropertyStore( + (IResource) getElement(), + super.getPreferenceStore(), + pageId); + // Set overlay store as current preference store + } + super.createControl(parent); + // Update state of all subclass controls + if (isPropertyPage()) + updateFieldEditors(); + } + + /** + * We override the createContents method. + * In case of property pages we insert two radio buttons at the top of the page. + * + * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite) + */ + protected Control createContents(Composite parent) { + if (isPropertyPage()) + createSelectionGroup(parent); + return super.createContents(parent); + } + + /** + * Creates and initializes a selection group with two choice buttons and one push button. + * @param parent - the parent composite + */ + private void createSelectionGroup(Composite parent) { + Composite comp = new Composite(parent, SWT.NONE); + GridLayout layout = new GridLayout(2, false); + layout.marginHeight = 0; + layout.marginWidth = 0; + comp.setLayout(layout); + comp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); + Composite radioGroup = new Composite(comp, SWT.NONE); + radioGroup.setLayout(new GridLayout()); + radioGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); + useWorkspaceSettingsButton = createRadioButton(radioGroup, Messages.getString("OverlayPage.Use_Workspace_Settings")); //$NON-NLS-1$ + useProjectSettingsButton = createRadioButton(radioGroup, Messages.getString("OverlayPage.Use_Project_Settings")); //$NON-NLS-1$ + configureButton = new Button(comp, SWT.PUSH); + configureButton.setText(Messages.getString("OverlayPage.Configure_Workspace_Settings")); //$NON-NLS-1$ + configureButton.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent e) { + configureWorkspaceSettings(); + } + }); + // Set workspace/project radio buttons + try { + String use = + ((IResource) getElement()).getPersistentProperty( + new QualifiedName(pageId, USEPROJECTSETTINGS)); + if (TRUE.equals(use)) { + useProjectSettingsButton.setSelection(true); + configureButton.setEnabled(false); + } else + useWorkspaceSettingsButton.setSelection(true); + } catch (CoreException e) { + useWorkspaceSettingsButton.setSelection(true); + } + } + + /** + * Convenience method creating a radio button + * @param parent - the parent composite + * @param label - the button label + * @return - the new button + */ + private Button createRadioButton(Composite parent, String label) { + final Button button = new Button(parent, SWT.RADIO); + button.setText(label); + button.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent e) { + configureButton.setEnabled( + button == useWorkspaceSettingsButton); + updateFieldEditors(); + } + }); + return button; + } + + /** + * Returns in case of property pages the overlay store, + * in case of preference pages the standard preference store + * @see org.eclipse.jface.preference.PreferencePage#getPreferenceStore() + */ + public IPreferenceStore getPreferenceStore() { + if (isPropertyPage()) + return overlayStore; + return super.getPreferenceStore(); + } + + /* + * Enables or disables the field editors and buttons of this page + */ + private void updateFieldEditors() { + // We iterate through all field editors + boolean enabled = useProjectSettingsButton.getSelection(); + updateFieldEditors(enabled); + } + + /** + * Enables or disables the field editors and buttons of this page + * Subclasses may override. + * @param enabled - true if enabled + */ + protected void updateFieldEditors(boolean enabled) { + Composite parent = getFieldEditorParent(); + Iterator it = editors.iterator(); + while (it.hasNext()) { + FieldEditor editor = (FieldEditor) it.next(); + editor.setEnabled(enabled, parent); + } + } + + /** + * We override the performOk method. In case of property pages we copy the values in the + * overlay store into the property values of the selected project. + * We also save the state of the radio buttons. + * + * @see org.eclipse.jface.preference.IPreferencePage#performOk() + */ + public boolean performOk() { + boolean result = super.performOk(); + if (result && isPropertyPage()) { + // Save state of radiobuttons in project properties + IResource resource = (IResource) getElement(); + try { + String value = + (useProjectSettingsButton.getSelection()) ? TRUE : FALSE; + resource.setPersistentProperty( + new QualifiedName(pageId, USEPROJECTSETTINGS), + value); + } catch (CoreException e) { + } + } + return result; + } + + /** + * We override the performDefaults method. In case of property pages we + * switch back to the workspace settings and disable the field editors. + * + * @see org.eclipse.jface.preference.PreferencePage#performDefaults() + */ + protected void performDefaults() { + if (isPropertyPage()) { + useWorkspaceSettingsButton.setSelection(true); + useProjectSettingsButton.setSelection(false); + configureButton.setEnabled(true); + updateFieldEditors(); + } + super.performDefaults(); + } + + /** + * Creates a new preferences page and opens it + */ + protected void configureWorkspaceSettings() { + try { + // create a new instance of the current class + IPreferencePage page = + (IPreferencePage) this.getClass().newInstance(); + page.setTitle(getTitle()); + page.setImageDescriptor(image); + // and show it + showPreferencePage(pageId, page); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + + /** + * Show a single preference pages + * @param id - the preference page identification + * @param page - the preference page + */ + protected void showPreferencePage(String id, IPreferencePage page) { + final IPreferenceNode targetNode = new PreferenceNode(id, page); + PreferenceManager manager = new PreferenceManager(); + manager.addToRoot(targetNode); + final PreferenceDialog dialog = + new PreferenceDialog(getControl().getShell(), manager); + BusyIndicator.showWhile(getControl().getDisplay(), new Runnable() { + public void run() { + dialog.create(); + dialog.setMessage(targetNode.getLabelText()); + dialog.open(); + } + }); + } + +} diff --git a/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/preferences/Messages.java b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/preferences/Messages.java new file mode 100644 index 00000000000..4ef5f89be83 --- /dev/null +++ b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/preferences/Messages.java @@ -0,0 +1,47 @@ +/******************************************************************************* + * Copyright (c) 2003 Berthold Daum. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * Berthold Daum + *******************************************************************************/ + +package org.eclipse.cdt.codan.internal.ui.preferences; + +import java.util.ResourceBundle; + + +public class Messages { + + private final static String RESOURCE_BUNDLE = "org.eclipse.cdt.codan.internal.ui.preferences.Messages";//$NON-NLS-1$ + + private static ResourceBundle fgResourceBundle = null; + + private static boolean notRead = true; + + public Messages() { + } + public static ResourceBundle getResourceBundle() { + if (notRead) { + notRead = false; + try { + fgResourceBundle = ResourceBundle.getBundle(RESOURCE_BUNDLE); + } + catch (Exception e) { + } + } + + return fgResourceBundle; + } + public static String getString(String key) { + try { + return getResourceBundle().getString(key); + } catch (Exception e) { + return "!" + key + "!";//$NON-NLS-2$ //$NON-NLS-1$ + } + } +} + diff --git a/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/preferences/Messages.properties b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/preferences/Messages.properties new file mode 100644 index 00000000000..689b7ee63a9 --- /dev/null +++ b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/preferences/Messages.properties @@ -0,0 +1,4 @@ +OverlayPage.Use_Workspace_Settings=Use &workspace settings +OverlayPage.Use_Project_Settings=Use pr&oject settings +OverlayPage.Configure_Workspace_Settings=&Configure Workspace Settings ... +PropertyStore.Cannot_write_resource_property=Cannot write resource property diff --git a/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/preferences/OverlayPage.java b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/preferences/OverlayPage.java new file mode 100644 index 00000000000..ddcac1652fb --- /dev/null +++ b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/preferences/OverlayPage.java @@ -0,0 +1,334 @@ +/******************************************************************************* + * Copyright (c) 2003 Berthold Daum. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * Berthold Daum + *******************************************************************************/ +package org.eclipse.cdt.codan.internal.ui.preferences; + +import org.eclipse.core.resources.IResource; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.QualifiedName; +import org.eclipse.jface.preference.IPreferenceNode; +import org.eclipse.jface.preference.IPreferencePage; +import org.eclipse.jface.preference.IPreferenceStore; +import org.eclipse.jface.preference.PreferenceDialog; +import org.eclipse.jface.preference.PreferenceManager; +import org.eclipse.jface.preference.PreferenceNode; +import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.BusyIndicator; +import org.eclipse.swt.custom.CTabFolder; +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.TabFolder; +import org.eclipse.ui.dialogs.PropertyPage; +import org.eclipse.ui.part.PageBook; + +/** + * @author Berthold Daum + */ +public abstract class OverlayPage extends PropertyPage { + + /*** Name of resource property for the selection of workbench or project settings ***/ + public static final String USEPROJECTSETTINGS = "useProjectSettings"; //$NON-NLS-1$ + + private static final String FALSE = "false"; //$NON-NLS-1$ + private static final String TRUE = "true"; //$NON-NLS-1$ + + // Additional buttons for property pages + private Button useWorkspaceSettingsButton, + useProjectSettingsButton, + configureButton; + + // Overlay preference store for property pages + private PropertyStore overlayStore; + + // The image descriptor of this pages title image + private ImageDescriptor image; + + // Cache for page id + private String pageId; + + // Container for subclass controls + private Composite contents; + + /** + * Constructor + */ + public OverlayPage() { + super(); + } + + /** + * Constructor + * @param title - title string + */ + public OverlayPage(String title) { + super(); + setTitle(title); + } + + /** + * Constructor + * @param title - title string + * @param image - title image + */ + public OverlayPage(String title, ImageDescriptor image) { + super(); + setTitle(title); + setImageDescriptor(image); + this.image = image; + } + + /** + * Returns the id of the current preference page as defined in plugin.xml + * Subclasses must implement. + * + * @return - the qualifier + */ + protected abstract String getPageId(); + + /** + * Returns true if this instance represents a property page + * @return - true for property pages, false for preference pages + */ + public boolean isPropertyPage() { + return getElement() != null; + } + + /** + * We need to implement createContents method. In case of property pages we insert two radio buttons + * and a push button at the top of the page. Below this group we create a new composite for the contents + * created by subclasses. + * + * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite) + */ + protected Control createContents(Composite parent) { + if (isPropertyPage()) + createSelectionGroup(parent); + contents = new Composite(parent, SWT.NONE); + GridLayout layout = new GridLayout(); + layout.marginHeight = 0; + layout.marginWidth = 0; + contents.setLayout(layout); + contents.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); + return contents; + } + + /** + * Creates and initializes a selection group with two choice buttons and one push button. + * @param parent - the parent composite + */ + private void createSelectionGroup(Composite parent) { + Composite comp = new Composite(parent, SWT.NONE); + GridLayout layout = new GridLayout(2, false); + layout.marginHeight = 0; + layout.marginWidth = 0; + comp.setLayout(layout); + comp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); + Composite radioGroup = new Composite(comp, SWT.NONE); + radioGroup.setLayout(new GridLayout()); + radioGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); + useWorkspaceSettingsButton = createRadioButton(radioGroup, Messages.getString("OverlayPage.Use_Workspace_Settings")); //$NON-NLS-1$ + useProjectSettingsButton = createRadioButton(radioGroup, Messages.getString("OverlayPage.Use_Project_Settings")); //$NON-NLS-1$ + configureButton = new Button(comp, SWT.PUSH); + configureButton.setText(Messages.getString("OverlayPage.Configure_Workspace_Settings")); //$NON-NLS-1$ + configureButton.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent e) { + configureWorkspaceSettings(); + } + }); + // Set workspace/project radio buttons + try { + String use = + ((IResource) getElement()).getPersistentProperty( + new QualifiedName(pageId, USEPROJECTSETTINGS)); + if (TRUE.equals(use)) { + useProjectSettingsButton.setSelection(true); + configureButton.setEnabled(false); + } else + useWorkspaceSettingsButton.setSelection(true); + } catch (CoreException e) { + useWorkspaceSettingsButton.setSelection(true); + } + } + + /** + * Convenience method creating a radio button + * @param parent - the parent composite + * @param label - the button label + * @return - the new button + */ + private Button createRadioButton(Composite parent, String label) { + final Button button = new Button(parent, SWT.RADIO); + button.setText(label); + button.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent e) { + configureButton.setEnabled( + button == useWorkspaceSettingsButton); + setControlsEnabled(); + } + }); + return button; + } + + /** + * In case of property pages we create a new PropertyStore as local overlay store. + * After all controls have been create, we enable/disable these controls + * + * @see org.eclipse.jface.preference.PreferencePage#createControl() + */ + public void createControl(Composite parent) { + // Special treatment for property pages + if (isPropertyPage()) { + // Cache the page id + pageId = getPageId(); + // Create an overlay preference store and fill it with properties + overlayStore = + new PropertyStore( + (IResource) getElement(), + super.getPreferenceStore(), + pageId); + // Set overlay store as current preference store + } + super.createControl(parent); + // Update enablement of all subclass controls + if (isPropertyPage()) + setControlsEnabled(); + } + + /* + * Returns in case of property pages the overlay store - otherwise the standard preference store + * @see org.eclipse.jface.preference.PreferencePage#getPreferenceStore() + */ + public IPreferenceStore getPreferenceStore() { + if (isPropertyPage()) + return overlayStore; + return super.getPreferenceStore(); + } + + /** + * Enables or disables the controls of this page + */ + private void setControlsEnabled() { + boolean enabled = useProjectSettingsButton.getSelection(); + setControlsEnabled(enabled); + } + + /** + * Enables or disables the controls of this page + * Subclasses may override. + * + * @param enabled - true if controls shall be enabled + */ + protected void setControlsEnabled(boolean enabled) { + setControlsEnabled(contents, enabled); + } + + /** + * Enables or disables a tree of controls starting at the specified root. + * We spare tabbed notebooks and pagebooks to allow for user navigation. + * + * @param root - the root composite + * @param enabled - true if controls shall be enabled + */ + private void setControlsEnabled(Composite root, boolean enabled) { + Control[] children = root.getChildren(); + for (int i = 0; i < children.length; i++) { + Control child = children[i]; + if (!(child instanceof CTabFolder) && !(child instanceof TabFolder) && !(child instanceof PageBook)) + child.setEnabled(enabled); + if (child instanceof Composite) + setControlsEnabled((Composite) child, enabled); + } + } + + /** + * We override the performOk method. In case of property pages + * we save the state of the radio buttons. + * + * @see org.eclipse.jface.preference.IPreferencePage#performOk() + */ + public boolean performOk() { + boolean result = super.performOk(); + if (result && isPropertyPage()) { + // Save state of radiobuttons in project properties + IResource resource = (IResource) getElement(); + try { + String value = + (useProjectSettingsButton.getSelection()) ? TRUE : FALSE; + resource.setPersistentProperty( + new QualifiedName(pageId, USEPROJECTSETTINGS), + value); + } catch (CoreException e) { + } + } + return result; + } + + /** + * We override the performDefaults method. In case of property pages we + * switch back to the workspace settings and disable the page controls. + * + * @see org.eclipse.jface.preference.PreferencePage#performDefaults() + */ + protected void performDefaults() { + if (isPropertyPage()) { + useWorkspaceSettingsButton.setSelection(true); + useProjectSettingsButton.setSelection(false); + configureButton.setEnabled(true); + setControlsEnabled(); + } + super.performDefaults(); + } + + /** + * Creates a new preferences page and opens it + */ + protected void configureWorkspaceSettings() { + try { + // create a new instance of the current class + IPreferencePage page = + (IPreferencePage) this.getClass().newInstance(); + page.setTitle(getTitle()); + page.setImageDescriptor(image); + // and show it + showPreferencePage(pageId, page); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + + /** + * Show a single preference pages + * @param id - the preference page identification + * @param page - the preference page + */ + protected void showPreferencePage(String id, IPreferencePage page) { + final IPreferenceNode targetNode = new PreferenceNode(id, page); + PreferenceManager manager = new PreferenceManager(); + manager.addToRoot(targetNode); + final PreferenceDialog dialog = + new PreferenceDialog(getControl().getShell(), manager); + BusyIndicator.showWhile(getControl().getDisplay(), new Runnable() { + public void run() { + dialog.create(); + dialog.setMessage(targetNode.getLabelText()); + dialog.open(); + } + }); + } + +} diff --git a/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/preferences/PreferenceConstants.java b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/preferences/PreferenceConstants.java new file mode 100644 index 00000000000..fc685504842 --- /dev/null +++ b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/preferences/PreferenceConstants.java @@ -0,0 +1,18 @@ +/******************************************************************************* + * Copyright (c) 2009 Alena Laskavaia + * 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: + * Alena Laskavaia - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.codan.internal.ui.preferences; + +/** + * Constant definitions for plug-in preferences + */ +public class PreferenceConstants { + public static final String P_RUN_ON_BUILD = "booleanPreference"; +} diff --git a/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/preferences/PreferenceInitializer.java b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/preferences/PreferenceInitializer.java new file mode 100644 index 00000000000..a2adeb4f82e --- /dev/null +++ b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/preferences/PreferenceInitializer.java @@ -0,0 +1,32 @@ +/******************************************************************************* + * Copyright (c) 2009 Alena Laskavaia + * 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: + * Alena Laskavaia - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.codan.internal.ui.preferences; + +import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer; +import org.eclipse.jface.preference.IPreferenceStore; + +/** + * Class used to initialize default preference values. + */ +public class PreferenceInitializer extends AbstractPreferenceInitializer { + + /* + * (non-Javadoc) + * + * @see org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer#initializeDefaultPreferences() + */ + public void initializeDefaultPreferences() { + IPreferenceStore store = org.eclipse.cdt.codan.ui.Activator + .getDefault().getPreferenceStore(); + store.setDefault(PreferenceConstants.P_RUN_ON_BUILD, false); + } + +} diff --git a/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/preferences/ProblemsTreeEditor.java b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/preferences/ProblemsTreeEditor.java new file mode 100644 index 00000000000..cca679dc72c --- /dev/null +++ b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/preferences/ProblemsTreeEditor.java @@ -0,0 +1,146 @@ +/******************************************************************************* + * Copyright (c) 2009 Alena Laskavaia + * 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: + * Alena Laskavaia - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.codan.internal.ui.preferences; + +import org.eclipse.cdt.codan.core.model.CheckersRegisry; +import org.eclipse.cdt.codan.core.model.CodanProblem; +import org.eclipse.cdt.codan.core.model.CodanSeverity; +import org.eclipse.cdt.codan.core.model.IProblem; +import org.eclipse.cdt.codan.core.model.IProblemCategory; +import org.eclipse.jface.viewers.BaseLabelProvider; +import org.eclipse.jface.viewers.CheckStateChangedEvent; +import org.eclipse.jface.viewers.IBaseLabelProvider; +import org.eclipse.jface.viewers.IContentProvider; +import org.eclipse.jface.viewers.ITableLabelProvider; +import org.eclipse.jface.viewers.ITreeContentProvider; +import org.eclipse.jface.viewers.Viewer; +import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.TreeColumn; + +public class ProblemsTreeEditor extends CheckedTreeEditor { + public ProblemsTreeEditor() { + super(); + // TODO Auto-generated constructor stub + } + + class ProblemsContentProvider implements IContentProvider, + ITreeContentProvider { + public void dispose() { + // TODO Auto-generated method stub + } + + public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { + // TODO Auto-generated method stub + } + + public Object[] getChildren(Object parentElement) { + if (parentElement instanceof Object[]) + return (Object[]) parentElement; + if (parentElement instanceof IProblemCategory) { + return ((IProblemCategory) parentElement).getChildren(); + } + return new Object[0]; + } + + public Object getParent(Object element) { + return null; + } + + public boolean hasChildren(Object element) { + return getChildren(element).length > 0; + } + + public Object[] getElements(Object inputElement) { + return getChildren(inputElement); + } + } + + class ProblemsLabelProvider extends BaseLabelProvider implements + IBaseLabelProvider, ITableLabelProvider { + public Image getColumnImage(Object element, int columnIndex) { + // TODO Auto-generated method stub + return null; + } + + public String getColumnText(Object element, int columnIndex) { + if (element instanceof IProblem) { + IProblem p = (IProblem) element; + if (columnIndex == 0) + return p.getName(); + if (columnIndex == 1) + return p.getSeverity().toString(); + } + if (element instanceof IProblemCategory) { + IProblemCategory p = (IProblemCategory) element; + if (columnIndex == 0) + return p.getName(); + } + return null; + } + } + + @Override + protected String unparseElement(Object element) { + IProblem p = ((IProblem) element); + return p.getId() + ":" + p.getSeverity(); + } + + @Override + protected Object parseObject(String string) { + String[] pair = string.split(":"); + if (pair.length == 0) + return null; + String id = pair[0]; + String arg = ""; + if (pair.length > 1) { + arg = pair[1]; + } + CodanSeverity sev; + try { + sev = CodanSeverity.valueOf(arg); + } catch (RuntimeException e) { + sev = CodanSeverity.Warning; + } + IProblem prob = CheckersRegisry.getInstance().findProblem(id); + if (prob instanceof CodanProblem) { + ((CodanProblem) prob).setSeverity(sev); + } + return prob; + } + + public void checkStateChanged(CheckStateChangedEvent event) { + Object element = event.getElement(); + if (element instanceof CodanProblem) { + ((CodanProblem) element).setEnabled(event.getChecked()); + } + } + + public ProblemsTreeEditor(Composite parent) { + super("problems", "Problems", parent); + setEmptySelectionAllowed(true); + getTreeViewer().getTree().setHeaderVisible(true); + // getTreeViewer().getTree(). + getTreeViewer().setContentProvider(new ProblemsContentProvider()); + getTreeViewer().setLabelProvider(new ProblemsLabelProvider()); + // column Name + TreeColumn column = new TreeColumn(getTreeViewer().getTree(), SWT.NONE); + column.setWidth(300); + column.setText("Name"); + // column Severity + TreeColumn column2 = new TreeColumn(getTreeViewer().getTree(), SWT.NONE); + column2.setWidth(100); + column2.setText("Severity"); + getTreeViewer().setInput( + CheckersRegisry.getInstance().getProblemsTree()); + } +} diff --git a/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/preferences/PropertyStore.java b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/preferences/PropertyStore.java new file mode 100644 index 00000000000..cb30907d4e2 --- /dev/null +++ b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/preferences/PropertyStore.java @@ -0,0 +1,234 @@ +/******************************************************************************* + * Copyright (c) 2003 Berthold Daum. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * Berthold Daum + *******************************************************************************/ + +package org.eclipse.cdt.codan.internal.ui.preferences; + +import java.io.IOException; +import java.io.OutputStream; + +import org.eclipse.core.resources.IResource; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.QualifiedName; +import org.eclipse.jface.preference.IPreferenceStore; +import org.eclipse.jface.preference.PreferenceStore; + +/** + * @author Berthold Daum + * + */ +public class PropertyStore extends PreferenceStore { + + private IResource resource; + private IPreferenceStore workbenchStore; + private String pageId; + private boolean inserting = false; + + public PropertyStore( + IResource resource, + IPreferenceStore workbenchStore, + String pageId) { + this.resource = resource; + this.workbenchStore = workbenchStore; + this.pageId = pageId; + } + + /*** Write modified values back to properties ***/ + + /* (non-Javadoc) + * @see org.eclipse.jface.preference.IPersistentPreferenceStore#save() + */ + public void save() throws IOException { + writeProperties(); + } + + /* (non-Javadoc) + * @see org.eclipse.jface.preference.PreferenceStore#save(java.io.OutputStream, java.lang.String) + */ + public void save(OutputStream out, String header) throws IOException { + writeProperties(); + } + + /** + * Writes modified preferences into resource properties. + */ + private void writeProperties() throws IOException { + String[] preferences = super.preferenceNames(); + for (int i = 0; i < preferences.length; i++) { + String name = preferences[i]; + try { + setProperty(name, getString(name)); + } catch (CoreException e) { + throw new IOException(Messages.getString("PropertyStore.Cannot_write_resource_property") + name); //$NON-NLS-1$ + } + } + } + + /** + * Convenience method to set a property + * @param name - the preference name + * @param value - the property value or null to delete the property + * @throws CoreException + */ + private void setProperty(String name, String value) throws CoreException { + resource.setPersistentProperty(new QualifiedName(pageId, name), value); + } + + /*** Get default values (Delegate to workbench store) ***/ + + /* (non-Javadoc) + * @see org.eclipse.jface.preference.IPreferenceStore#getDefaultBoolean(java.lang.String) + */ + public boolean getDefaultBoolean(String name) { + return workbenchStore.getDefaultBoolean(name); + } + + /* (non-Javadoc) + * @see org.eclipse.jface.preference.IPreferenceStore#getDefaultDouble(java.lang.String) + */ + public double getDefaultDouble(String name) { + return workbenchStore.getDefaultDouble(name); + } + + /* (non-Javadoc) + * @see org.eclipse.jface.preference.IPreferenceStore#getDefaultFloat(java.lang.String) + */ + public float getDefaultFloat(String name) { + return workbenchStore.getDefaultFloat(name); + } + + /* (non-Javadoc) + * @see org.eclipse.jface.preference.IPreferenceStore#getDefaultInt(java.lang.String) + */ + public int getDefaultInt(String name) { + return workbenchStore.getDefaultInt(name); + } + + /* (non-Javadoc) + * @see org.eclipse.jface.preference.IPreferenceStore#getDefaultLong(java.lang.String) + */ + public long getDefaultLong(String name) { + return workbenchStore.getDefaultLong(name); + } + + /* (non-Javadoc) + * @see org.eclipse.jface.preference.IPreferenceStore#getDefaultString(java.lang.String) + */ + public String getDefaultString(String name) { + return workbenchStore.getDefaultString(name); + } + + /*** Get property values ***/ + + /* (non-Javadoc) + * @see org.eclipse.jface.preference.IPreferenceStore#getBoolean(java.lang.String) + */ + public boolean getBoolean(String name) { + insertValue(name); + return super.getBoolean(name); + } + + /* (non-Javadoc) + * @see org.eclipse.jface.preference.IPreferenceStore#getDouble(java.lang.String) + */ + public double getDouble(String name) { + insertValue(name); + return super.getDouble(name); + } + + /* (non-Javadoc) + * @see org.eclipse.jface.preference.IPreferenceStore#getFloat(java.lang.String) + */ + public float getFloat(String name) { + insertValue(name); + return super.getFloat(name); + } + + /* (non-Javadoc) + * @see org.eclipse.jface.preference.IPreferenceStore#getInt(java.lang.String) + */ + public int getInt(String name) { + insertValue(name); + return super.getInt(name); + } + + /* (non-Javadoc) + * @see org.eclipse.jface.preference.IPreferenceStore#getLong(java.lang.String) + */ + public long getLong(String name) { + insertValue(name); + return super.getLong(name); + } + + /* (non-Javadoc) + * @see org.eclipse.jface.preference.IPreferenceStore#getString(java.lang.String) + */ + public String getString(String name) { + insertValue(name); + return super.getString(name); + } + + /** + * @param name + */ + private synchronized void insertValue(String name) { + if (inserting) + return; + if (super.contains(name)) + return; + inserting = true; + String prop = null; + try { + prop = getProperty(name); + } catch (CoreException e) { + } + if (prop == null) + prop = workbenchStore.getString(name); + if (prop != null) + setValue(name, prop); + inserting = false; + } + + /** + * Convenience method to fetch a property + * @param name - the preference name + * @return - the property value + * @throws CoreException + */ + private String getProperty(String name) throws CoreException { + return resource.getPersistentProperty(new QualifiedName(pageId, name)); + } + + /*** Misc ***/ + + /* (non-Javadoc) + * @see org.eclipse.jface.preference.IPreferenceStore#contains(java.lang.String) + */ + public boolean contains(String name) { + return workbenchStore.contains(name); + } + + /* (non-Javadoc) + * @see org.eclipse.jface.preference.IPreferenceStore#setToDefault(java.lang.String) + */ + public void setToDefault(String name) { + setValue(name, getDefaultString(name)); + } + + /* (non-Javadoc) + * @see org.eclipse.jface.preference.IPreferenceStore#isDefault(java.lang.String) + */ + public boolean isDefault(String name) { + String defaultValue = getDefaultString(name); + if (defaultValue == null) return false; + return defaultValue.equals(getString(name)); + } + +} diff --git a/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/ui/Activator.java b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/ui/Activator.java new file mode 100644 index 00000000000..4a44a4b4b23 --- /dev/null +++ b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/ui/Activator.java @@ -0,0 +1,93 @@ +package org.eclipse.cdt.codan.ui; + +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.ui.plugin.AbstractUIPlugin; +import org.osgi.framework.BundleContext; + +/** + * The activator class controls the plug-in life cycle + */ +public class Activator extends AbstractUIPlugin { + + // The plug-in ID + public static final String PLUGIN_ID = "org.eclipse.cdt.codan.ui"; + + // The shared instance + private static Activator plugin; + + /** + * The constructor + */ + public Activator() { + } + + /* + * (non-Javadoc) + * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext) + */ + public void start(BundleContext context) throws Exception { + super.start(context); + plugin = this; + } + + /* + * (non-Javadoc) + * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext) + */ + public void stop(BundleContext context) throws Exception { + plugin = null; + super.stop(context); + } + + /** + * Returns the shared instance + * + * @return the shared instance + */ + public static Activator getDefault() { + return plugin; + } + + /** + * Returns an image descriptor for the image file at the given + * plug-in relative path + * + * @param path the path + * @return the image descriptor + */ + public static ImageDescriptor getImageDescriptor(String path) { + return imageDescriptorFromPlugin(PLUGIN_ID, path); + } + + /** + * Logs the specified status with this plug-in's log. + * + * @param status + * status to log + */ + public static void log(IStatus status) { + getDefault().getLog().log(status); + } + + /** + * Logs an internal error with the specified throwable + * + * @param e + * the exception to be logged + */ + public static void log(Throwable e) { + log(new Status(IStatus.ERROR, PLUGIN_ID, 1, "Internal Error", e)); //$NON-NLS-1$ + } + + /** + * Logs an internal error with the specified message. + * + * @param message + * the error message to log + */ + public static void log(String message) { + log(new Status(IStatus.ERROR, PLUGIN_ID, 1, message, null)); + } +} diff --git a/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/ui/actions/RunCodeAnalysis.java b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/ui/actions/RunCodeAnalysis.java new file mode 100644 index 00000000000..5f8a439861f --- /dev/null +++ b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/ui/actions/RunCodeAnalysis.java @@ -0,0 +1,47 @@ +/******************************************************************************* + * Copyright (c) 2009 Alena Laskavaia + * 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: + * Alena Laskavaia - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.codan.ui.actions; + +import java.util.Iterator; + +import org.eclipse.cdt.codan.core.builder.CodanBuilder; +import org.eclipse.core.resources.IResource; +import org.eclipse.jface.action.IAction; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.ui.IObjectActionDelegate; +import org.eclipse.ui.IWorkbenchPart; + +public class RunCodeAnalysis implements IObjectActionDelegate { + private ISelection sel; + + public void setActivePart(IAction action, IWorkbenchPart targetPart) { + // TODO Auto-generated method stub + + } + + public void run(IAction action) { + for (Iterator iterator = ((IStructuredSelection) sel).iterator(); iterator + .hasNext();) { + Object o = iterator.next(); + if (o instanceof IResource) { + IResource res = (IResource) o; + new CodanBuilder().new CodanResourceVisitor().visit(res); + } + } + + } + + public void selectionChanged(IAction action, ISelection selection) { + this.sel = selection; + + } +} diff --git a/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/ui/actions/ToggleNatureAction.java b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/ui/actions/ToggleNatureAction.java new file mode 100644 index 00000000000..efc9f756cc6 --- /dev/null +++ b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/ui/actions/ToggleNatureAction.java @@ -0,0 +1,130 @@ +/******************************************************************************* + * Copyright (c) 2009 Alena Laskavaia + * 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: + * Alena Laskavaia - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.codan.ui.actions; + +import java.util.Iterator; + +import org.eclipse.cdt.codan.core.builder.CodeAnlysisNature; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IProjectDescription; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IAdaptable; +import org.eclipse.jface.action.IAction; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.ui.IObjectActionDelegate; +import org.eclipse.ui.IWorkbenchPart; + +public class ToggleNatureAction implements IObjectActionDelegate { + private ISelection selection; + + /* + * (non-Javadoc) + * + * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction) + */ + public void run(IAction action) { + if (selection instanceof IStructuredSelection) { + for (Iterator it = ((IStructuredSelection) selection).iterator(); it + .hasNext();) { + Object element = it.next(); + IProject project = null; + if (element instanceof IProject) { + project = (IProject) element; + } else if (element instanceof IAdaptable) { + project = (IProject) ((IAdaptable) element) + .getAdapter(IProject.class); + } + if (project != null) { + toggleNature(project, !hasCodanNature(project)); + } + } + } + } + + /* + * (non-Javadoc) + * + * @see + * org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action + * .IAction, org.eclipse.jface.viewers.ISelection) + */ + public void selectionChanged(IAction action, ISelection selection) { + this.selection = selection; + } + + /* + * (non-Javadoc) + * + * @see + * org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface. + * action.IAction, org.eclipse.ui.IWorkbenchPart) + */ + public void setActivePart(IAction action, IWorkbenchPart targetPart) { + } + + public boolean hasCodanNature(IProject project) { + IProjectDescription description; + try { + description = project.getDescription(); + String[] natures = description.getNatureIds(); + for (int i = 0; i < natures.length; ++i) { + if (CodeAnlysisNature.NATURE_ID.equals(natures[i])) { + return true; + } + } + } catch (CoreException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return false; + } + + /** + * Toggles sample nature on a project + * + * @param project + * to have sample nature added or removed + */ + public void toggleNature(IProject project, boolean add) { + try { + IProjectDescription description = project.getDescription(); + String[] natures = description.getNatureIds(); + for (int i = 0; i < natures.length; ++i) { + if (CodeAnlysisNature.NATURE_ID.equals(natures[i])) { + if (add == false) { + // Remove the nature + String[] newNatures = new String[natures.length - 1]; + System.arraycopy(natures, 0, newNatures, 0, i); + System.arraycopy(natures, i + 1, newNatures, i, + natures.length - i - 1); + description.setNatureIds(newNatures); + project.setDescription(description, null); + return; + } else { + // already there no need to add + add = false; + break; + } + } + } + if (add) { + // Add the nature + String[] newNatures = new String[natures.length + 1]; + System.arraycopy(natures, 0, newNatures, 0, natures.length); + newNatures[natures.length] = CodeAnlysisNature.NATURE_ID; + description.setNatureIds(newNatures); + project.setDescription(description, null); + } + } catch (CoreException e) { + } + } +}