1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-31 21:05:37 +02:00

added example of property change listener

This commit is contained in:
Alena Laskavaia 2011-03-11 03:27:36 +00:00
parent de346dcf4b
commit 084629fb9c
2 changed files with 119 additions and 0 deletions

View file

@ -10,6 +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.core.runtime.Plugin; import org.eclipse.core.runtime.Plugin;
import org.osgi.framework.BundleContext; import org.osgi.framework.BundleContext;
@ -37,6 +38,7 @@ public class Activator extends Plugin {
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();
} }
/* /*
@ -48,6 +50,7 @@ public class Activator extends Plugin {
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();
} }
/** /**

View file

@ -0,0 +1,116 @@
/*******************************************************************************
* Copyright (c) 2009,2010 QNX Software Systems
* 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:
* QNX Software Systems (Alena Laskavaia) - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.codan.examples.uicontrib;
import org.eclipse.cdt.codan.core.CodanCorePlugin;
import org.eclipse.cdt.codan.core.model.IProblem;
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.internal.core.CodanPreferencesLoader;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.IEclipsePreferences.INodeChangeListener;
import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener;
import org.eclipse.core.runtime.preferences.IEclipsePreferences.NodeChangeEvent;
import org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent;
/**
* Example of property change listener for changing error profiles
*/
public class ProfileChangeListener implements INodeChangeListener, IPreferenceChangeListener, IProblemProfileChangeListener {
static ProfileChangeListener instance = new ProfileChangeListener();
public static ProfileChangeListener getInstance(){
return instance;
}
private IProject project;
private ProfileChangeListener(IProject project) {
this.project = project;
}
private ProfileChangeListener() {
CodanCorePlugin.getDefault().getStorePreferences().addNodeChangeListener(this);
CodanCorePlugin.getDefault().getStorePreferences().addPreferenceChangeListener(this);
IWorkspace root = ResourcesPlugin.getWorkspace();
IProject[] projects = root.getRoot().getProjects();
for (int i = 0; i < projects.length; i++) {
IProject project = projects[i];
CodanPreferencesLoader.getProjectNode(project).addPreferenceChangeListener(new ProfileChangeListener(project));
}
// cannot do on plugin startup
// CheckersRegistry.getInstance().getWorkspaceProfile().addProfileChangeListener(this);
}
/* (non-Javadoc)
* @see org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener#preferenceChange(org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent)
*/
public void preferenceChange(PreferenceChangeEvent event) {
if (event.getSource() instanceof IEclipsePreferences) {
IEclipsePreferences ep = (IEclipsePreferences) event.getSource();
if (project!=null) {
if (GrepChecker.ID.equals(event.getKey())) {
// severity or enablement has changed
String val = (String) event.getNewValue();
if (!val.startsWith("-")) {
System.err.println("grep checker enabled!");
}
}
}
}
}
/* (non-Javadoc)
* @see org.eclipse.core.runtime.preferences.IEclipsePreferences.INodeChangeListener#added(org.eclipse.core.runtime.preferences.IEclipsePreferences.NodeChangeEvent)
*/
public void added(NodeChangeEvent event) {
System.err.println("node added "+event);
}
/* (non-Javadoc)
* @see org.eclipse.core.runtime.preferences.IEclipsePreferences.INodeChangeListener#removed(org.eclipse.core.runtime.preferences.IEclipsePreferences.NodeChangeEvent)
*/
public void removed(NodeChangeEvent event) {
// TODO Auto-generated method stub
}
/**
*
*/
public void dispose() {
CodanCorePlugin.getDefault().getStorePreferences().removeNodeChangeListener(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());
}
}
}