Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7e0c54883db8e31faa212d8a87fd36310f5631ab (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
/*******************************************************************************
 * Copyright (c) 2015 vogella GmbH.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Simon Scholz <simon.scholz@vogella.com> - initial API and implementation
 *******************************************************************************/
package org.eclipse.e4.tools.preference.spy.handler;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.preferences.BundleDefaultsScope;
import org.eclipse.core.runtime.preferences.ConfigurationScope;
import org.eclipse.core.runtime.preferences.DefaultScope;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.IPreferenceNodeVisitor;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.e4.core.services.events.IEventBroker;
import org.eclipse.e4.tools.preference.spy.Activator;
import org.eclipse.e4.tools.preference.spy.constants.PreferenceSpyEventTopics;
import org.eclipse.e4.tools.preference.spy.model.PreferenceEntry;
import org.eclipse.e4.tools.preference.spy.model.PreferenceNodeEntry;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.swt.widgets.Shell;
import org.osgi.service.prefs.BackingStoreException;

public class ShowAllPreferencesHandler {
	@Execute
	public void execute(Shell shell, IEventBroker eventBroker) {
		Map<String, PreferenceNodeEntry> preferenceEntries = new HashMap<String, PreferenceNodeEntry>();
		IEclipsePreferences bundleDefaultsScopePreferences = BundleDefaultsScope.INSTANCE.getNode("");
		IEclipsePreferences configurationScopePreferences = ConfigurationScope.INSTANCE.getNode("");
		IEclipsePreferences defaultScopePreferences = DefaultScope.INSTANCE.getNode("");
		IEclipsePreferences instanceScopePreferences = InstanceScope.INSTANCE.getNode("");
		try {
			bundleDefaultsScopePreferences.accept(new PrefereneGatherer(preferenceEntries));
			configurationScopePreferences.accept(new PrefereneGatherer(preferenceEntries));
			defaultScopePreferences.accept(new PrefereneGatherer(preferenceEntries));
			instanceScopePreferences.accept(new PrefereneGatherer(preferenceEntries));
		} catch (BackingStoreException e) {
			Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e);
			ErrorDialog.openError(shell, "BackingStoreException", e.getLocalizedMessage(), status);
		}
		eventBroker.post(PreferenceSpyEventTopics.PREFERENCESPY_PREFERENCE_SHOW, preferenceEntries.values());
	}

	private class PrefereneGatherer implements IPreferenceNodeVisitor {

		private Map<String, PreferenceNodeEntry> preferenceEntries;

		public PrefereneGatherer(Map<String, PreferenceNodeEntry> preferenceEntries) {
			this.preferenceEntries = preferenceEntries;
		}

		@Override
		public boolean visit(IEclipsePreferences node) throws BackingStoreException {
			// only show nodes, which have changed keys
			String[] keys = node.keys();
			if (keys.length <= 0) {
				return true;
			}
			PreferenceNodeEntry preferenceNodeEntry = preferenceEntries.get(node.absolutePath());
			if (null == preferenceNodeEntry) {
				preferenceNodeEntry = new PreferenceNodeEntry(node.absolutePath());
				preferenceEntries.put(node.absolutePath(), preferenceNodeEntry);
			}
			for (String key : keys) {
				String value = node.get(key, "*default*");
				PreferenceEntry preferenceEntry = new PreferenceEntry(node.absolutePath(), key, value, value);
				preferenceNodeEntry.addChildren(preferenceEntry);
			}
			return true;
		}
	}

}

Back to the top