Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: cd28db37459320cdf538b2d63ba942e655ad6125 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*******************************************************************************
 * Copyright (c) 2001, 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
 *     Jens Lukowski/Innoopract - initial renaming/restructuring
 *     
 *******************************************************************************/
package org.eclipse.wst.sse.core.internal.encoding;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.core.runtime.preferences.DefaultScope;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.osgi.framework.Bundle;
import org.osgi.service.prefs.Preferences;


public class ContentBasedPreferenceGateway {
	private static String DEFAULT_LOCATION = "org.eclipse.wst.sse.core"; //$NON-NLS-1$
	private static String RUNTIME_XML_ID = "org.eclipse.core.runtime.xml"; //$NON-NLS-1$
	private static String SSE_XML_ID = "org.eclipse.wst.xml.core.xmlsource"; //$NON-NLS-1$

	/**
	 * @param pluginId
	 * @return
	 */
	private static boolean bundleExists(String pluginId) {

		// this just verifies there's really a plugin with this ID in "stack"
		Bundle bundle = Platform.getBundle(pluginId);
		return (!(bundle == null));
	}

	/**
	 * @param contentType
	 * @return
	 */
	private static String getContributorPluginId(IContentType contentType) {
		// TODO: need to have registration info here, but for now, we'll use
		// simple heuristic to cover the cases we know about.
		String fullId = null;
		if (contentType == null) {
			fullId = DEFAULT_LOCATION;
		} else {
			fullId = contentType.getId();
		}
		// only one known case, so far, of hard coded re-direction
		// (not sure this is even needed, but just in case).
		// We don't want to store/change runtime.xml preferences
		if (RUNTIME_XML_ID.equals(fullId)) {
			fullId = SSE_XML_ID;
		}
		String pluginId = inferPluginId(fullId);
		return pluginId;
	}

	private static Preferences getDefaultPreferences(IContentType contentType) {
		IEclipsePreferences eclipsePreferences = Platform.getPreferencesService().getRootNode();
		// TODO: eventaully need extension mechanism to avoid these hard coded
		// mechanism.
		// The idea is to load/store based on plugin's preferences, where the
		// content type was contributed
		// Eventually, too, we could do more "dynamic lookup" to get parent
		// types for defaults, etc.

		// Get default plugin preferences
		String pluginPreferenceLocation = DefaultScope.SCOPE + IPath.SEPARATOR + getContributorPluginId(contentType);
		Preferences pluginPreferences = eclipsePreferences.node(pluginPreferenceLocation);

		// the below code does not work at this time because content type
		// preferences are stored in the place as plugin preferences

		//		Preferences contentPreferences = null;
		//		if (contentType != null) {
		//			contentPreferences = pluginPreferences.node(contentType.getId());
		//		}
		//		else {
		//			contentPreferences = pluginPreferences.node(DEFAULT_LOCATION );
		//		}
		//
		//		return contentPreferences;

		return pluginPreferences;

	}

	private static Preferences getDefaultPreferences(String contentTypeId) {
		IContentType contentType = Platform.getContentTypeManager().getContentType(contentTypeId);
		return getDefaultPreferences(contentType);
	}

	public static Preferences getPreferences(IContentType contentType) {
		IEclipsePreferences eclipsePreferences = Platform.getPreferencesService().getRootNode();
		// TODO: eventaully need extension mechanism to avoid these hard coded
		// mechanism.
		// The idea is to load/store based on plugin's preferences, where the
		// content type was contributed
		// Eventually, too, we could do more "dynamic lookup" to get parent
		// types for defaults, etc.

		// Get instance plugin preferences
		String pluginPreferenceLocation = Plugin.PLUGIN_PREFERENCE_SCOPE + IPath.SEPARATOR + getContributorPluginId(contentType);
		Preferences pluginPreferences = eclipsePreferences.node(pluginPreferenceLocation);

		// the below code does not work at this time because content type
		// preferences are stored in the place as plugin preferences

		//		Preferences contentPreferences = null;
		//		if (contentType != null) {
		//			contentPreferences = pluginPreferences.node(contentType.getId());
		//		}
		//		else {
		//			contentPreferences = pluginPreferences.node(DEFAULT_LOCATION );
		//		}
		//
		//		return contentPreferences;
		return pluginPreferences;

	}

	public static Preferences getPreferences(String contentTypeId) {
		IContentType contentType = Platform.getContentTypeManager().getContentType(contentTypeId);
		return getPreferences(contentType);
	}

	public static String getPreferencesString(IContentType contentType, String key) {
		Preferences preferences = getPreferences(contentType);
		String value = preferences.get(key, getDefaultPreferences(contentType).get(key, null));
		return value;
	}

	public static String getPreferencesString(String contentTypeId, String key) {
		Preferences preferences = getPreferences(contentTypeId);
		String value = preferences.get(key, getDefaultPreferences(contentTypeId).get(key, null));
		return value;
	}

	/**
	 * @param fullId
	 * @return
	 */
	private static String inferPluginId(String fullId) {
		// simply trim off last "segment" from full ID.
		int lastSegmentPos = fullId.lastIndexOf('.');
		String pluginId = null;
		if (lastSegmentPos != -1) {
			pluginId = fullId.substring(0, lastSegmentPos);
		} else {
			// weird case? We'll at least put/get them somewhere
			pluginId = DEFAULT_LOCATION;
		}
		if (!bundleExists(pluginId)) {
			// use default location
			pluginId = DEFAULT_LOCATION;
		}
		return pluginId;
	}
}

Back to the top