1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-12 10:45:37 +02:00

Fixed codan example for pref change listener

Change-Id: I8693a4bdfd14a05ca077ec3357f82a2358bee07d
This commit is contained in:
Alena Laskavaia 2016-01-11 13:22:52 -05:00 committed by Gerrit Code Review @ Eclipse.org
parent 8b68a2bb8e
commit d310621847
2 changed files with 38 additions and 63 deletions

View file

@ -10,7 +10,7 @@
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.codan.examples; package org.eclipse.cdt.codan.examples;
import org.eclipse.cdt.codan.examples.uicontrib.ProfileChangeListener; import org.eclipse.cdt.codan.examples.uicontrib.GrepCheckerExamplePreferenceChangeListener;
import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Plugin; import org.eclipse.core.runtime.Plugin;
import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.Status;
@ -37,10 +37,11 @@ public class Activator extends Plugin {
* @see * @see
* org.eclipse.core.runtime.Plugins#start(org.osgi.framework.BundleContext) * org.eclipse.core.runtime.Plugins#start(org.osgi.framework.BundleContext)
*/ */
@Override
public void start(BundleContext context) throws Exception { public void start(BundleContext context) throws Exception {
super.start(context); super.start(context);
plugin = this; plugin = this;
ProfileChangeListener.getInstance(); GrepCheckerExamplePreferenceChangeListener.getInstance();
} }
/* /*
@ -49,10 +50,11 @@ public class Activator extends Plugin {
* @see * @see
* org.eclipse.core.runtime.Plugin#stop(org.osgi.framework.BundleContext) * org.eclipse.core.runtime.Plugin#stop(org.osgi.framework.BundleContext)
*/ */
@Override
public void stop(BundleContext context) throws Exception { public void stop(BundleContext context) throws Exception {
plugin = null; plugin = null;
super.stop(context); super.stop(context);
ProfileChangeListener.getInstance().dispose(); GrepCheckerExamplePreferenceChangeListener.getInstance().dispose();
} }
/** /**

View file

@ -11,14 +11,10 @@
package org.eclipse.cdt.codan.examples.uicontrib; package org.eclipse.cdt.codan.examples.uicontrib;
import org.eclipse.cdt.codan.core.CodanCorePlugin; import org.eclipse.cdt.codan.core.CodanCorePlugin;
import org.eclipse.cdt.codan.core.model.IProblem; import org.eclipse.cdt.codan.examples.Activator;
import org.eclipse.cdt.codan.core.model.IProblemProfile;
import org.eclipse.cdt.codan.core.model.IProblemProfileChangeListener;
import org.eclipse.cdt.codan.core.model.ProblemProfileChangeEvent;
import org.eclipse.cdt.codan.examples.checkers.GrepChecker; import org.eclipse.cdt.codan.examples.checkers.GrepChecker;
import org.eclipse.cdt.codan.internal.core.CodanPreferencesLoader; import org.eclipse.cdt.codan.internal.core.CodanPreferencesLoader;
import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace; import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.preferences.IEclipsePreferences; import org.eclipse.core.runtime.preferences.IEclipsePreferences;
@ -28,23 +24,27 @@ import org.eclipse.core.runtime.preferences.IEclipsePreferences.NodeChangeEvent;
import org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent; import org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent;
/** /**
* Example of property change listener for changing error profiles * Example of property change listener for changing error profiles using eclipse preferences.
* Too see debug prints uncomment printout line in {@link #trace(String) function }
* In this examples plugin this listener is activated on startup by calling ProfileChangeListener.getInstance(), see
* {@link Activator#start(org.osgi.framework.BundleContext)}
* Note: this example will not properly listen on properties for new or deleted projects
*/ */
public class ProfileChangeListener implements INodeChangeListener, IPreferenceChangeListener, IProblemProfileChangeListener { public class GrepCheckerExamplePreferenceChangeListener implements INodeChangeListener, IPreferenceChangeListener {
static ProfileChangeListener instance; static GrepCheckerExamplePreferenceChangeListener instance;
public static ProfileChangeListener getInstance() { public static GrepCheckerExamplePreferenceChangeListener getInstance() {
if (instance == null) if (instance == null)
instance = new ProfileChangeListener(); instance = new GrepCheckerExamplePreferenceChangeListener();
return instance; return instance;
} }
private IProject project; private IProject project;
private ProfileChangeListener(IProject project) { private GrepCheckerExamplePreferenceChangeListener(IProject project) {
this.project = project; this.project = project;
} }
private ProfileChangeListener() { private GrepCheckerExamplePreferenceChangeListener() {
CodanCorePlugin.getDefault().getStorePreferences().addNodeChangeListener(this); CodanCorePlugin.getDefault().getStorePreferences().addNodeChangeListener(this);
CodanCorePlugin.getDefault().getStorePreferences().addPreferenceChangeListener(this); CodanCorePlugin.getDefault().getStorePreferences().addPreferenceChangeListener(this);
IWorkspace root = ResourcesPlugin.getWorkspace(); IWorkspace root = ResourcesPlugin.getWorkspace();
@ -53,51 +53,41 @@ public class ProfileChangeListener implements INodeChangeListener, IPreferenceCh
IProject project = projects[i]; IProject project = projects[i];
IEclipsePreferences prefs = CodanPreferencesLoader.getProjectNode(project); IEclipsePreferences prefs = CodanPreferencesLoader.getProjectNode(project);
if (prefs != null) if (prefs != null)
prefs.addPreferenceChangeListener(new ProfileChangeListener(project)); prefs.addPreferenceChangeListener(new GrepCheckerExamplePreferenceChangeListener(project));
} }
// cannot do on plugin startup
// CheckersRegistry.getInstance().getWorkspaceProfile().addProfileChangeListener(this);
} }
@Override
public void preferenceChange(PreferenceChangeEvent event) { public void preferenceChange(PreferenceChangeEvent event) {
if (event.getSource() instanceof IEclipsePreferences) { if (event.getSource() instanceof IEclipsePreferences) {
//IEclipsePreferences ep = (IEclipsePreferences) event.getSource(); //IEclipsePreferences ep = (IEclipsePreferences) event.getSource();
if (GrepChecker.ID.equals(event.getKey())) { if (GrepChecker.ID.equals(event.getKey())) {
// severity or enablement has changed // severity or enablement has changed
String val = (String) event.getNewValue(); String val = (String) event.getNewValue();
if (!val.startsWith("-")) { String fors = (" for " + ((project == null) ? "workspace" : project.getName()));
System.err.print("grep checker enabled :)"); if (val != null && !val.startsWith("-")) {
trace("grep checker enabled :)"+fors);
} else { } else {
System.err.print("grep checker disabled :("); trace("grep checker disabled :("+fors);
} }
System.err.println(" for "+((project==null)?"workspace":project.getName()));
} }
} }
} }
/* private void trace(String message) {
* (non-Javadoc) // NOTE: uncomment this to see what this guy is listening on
* // System.err.print("example codan pref listener: " + message);
* @see }
* org.eclipse.core.runtime.preferences.IEclipsePreferences.INodeChangeListener
* #added(org.eclipse.core.runtime.preferences.IEclipsePreferences. @Override
* NodeChangeEvent)
*/
public void added(NodeChangeEvent event) { public void added(NodeChangeEvent event) {
System.err.println("node added " + event); trace("node added " + event);
} }
/* @Override
* (non-Javadoc)
*
* @see
* org.eclipse.core.runtime.preferences.IEclipsePreferences.INodeChangeListener
* #removed(org.eclipse.core.runtime.preferences.IEclipsePreferences.
* NodeChangeEvent)
*/
public void removed(NodeChangeEvent event) { public void removed(NodeChangeEvent event) {
System.err.println("node removed " + event); trace("node removed " + event);
} }
/** /**
@ -107,21 +97,4 @@ public class ProfileChangeListener implements INodeChangeListener, IPreferenceCh
CodanCorePlugin.getDefault().getStorePreferences().removeNodeChangeListener(this); CodanCorePlugin.getDefault().getStorePreferences().removeNodeChangeListener(this);
CodanCorePlugin.getDefault().getStorePreferences().removePreferenceChangeListener(this); CodanCorePlugin.getDefault().getStorePreferences().removePreferenceChangeListener(this);
} }
/*
* (non-Javadoc)
*
* @see org.eclipse.cdt.codan.internal.core.CheckersRegistry.
* IProblemProfileChangeListener
* #profileChange(org.eclipse.cdt.codan.internal
* .core.CheckersRegistry.ProblemProfileChangeEvent)
*/
public void profileChange(ProblemProfileChangeEvent event) {
if (event.getKey().equals(ProblemProfileChangeEvent.PROBLEM_KEY)) {
IResource resource = (IResource) event.getSource();
IProblemProfile profile = (IProblemProfile) event.getNewValue();
IProblem pp = profile.findProblem(GrepChecker.ID);
System.err.println(pp.getName() + " enabled " + pp.isEnabled());
}
}
} }