Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d5cef632fa24dea21eec1485cbfbbb816d6844ea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*******************************************************************************
 * 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;

	public static ProfileChangeListener getInstance() {
		if (instance == null)
			instance = new ProfileChangeListener();
		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];
			IEclipsePreferences prefs = CodanPreferencesLoader.getProjectNode(project);
			if (prefs != null)
				prefs.addPreferenceChangeListener(new ProfileChangeListener(project));
		}
		// cannot do on plugin startup
		// CheckersRegistry.getInstance().getWorkspaceProfile().addProfileChangeListener(this);
	}


	public void preferenceChange(PreferenceChangeEvent event) {
		if (event.getSource() instanceof IEclipsePreferences) {
			//IEclipsePreferences ep = (IEclipsePreferences) event.getSource();
			if (GrepChecker.ID.equals(event.getKey())) {
				// severity or enablement has changed
				String val = (String) event.getNewValue();
				if (!val.startsWith("-")) {
					System.err.print("grep checker enabled :)");
				} else {
					System.err.print("grep checker disabled :(");
				}
				System.err.println(" for "+((project==null)?"workspace":project.getName()));
			}
		}
	}

	/*
	 * (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) {
		System.err.println("node removed " + event);
	}

	/**
	 * 
	 */
	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());
		}
	}
}

Back to the top