Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: eefd7b8f20e87803700bfeb2d64a3a502d1f841e (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*****************************************************************************
 * Copyright (c) 2010, 2016 CEA LIST, Christian W. Damus, 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:
 *  Remi Schnekenburger (CEA LIST) remi.schnekenburger@cea.fr - Initial API and implementation
 *  Christian W. Damus - bug 485220
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.extendedtypes.internal.ui.preferences;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.papyrus.infra.extendedtypes.internal.ui.LogUtil;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.WorkbenchException;
import org.eclipse.ui.XMLMemento;
import org.eclipse.ui.preferences.ScopedPreferenceStore;

/**
 * Preferences management for extended types
 */
public class ExtendedTypesPreferences {

	/** id for the preference store for extended types redefinitions */
	public final static String EXTENDED_TYPES_REDEFINITIONS = "extendedTypesRedefinitions"; //$NON-NLS-1$

	/** id for the node: extended types redefinition */
	public final static String EXTENDED_TYPES_REDEFINITION = "extendedTypesRedefinition"; //$NON-NLS-1$

	public static final String EXTENDED_TYPES_SET_WORKSPACE_DEFINITION = "extendedTypeSetsWorkspaceDefinition";

	/** name of the ID attribute */
	public final static String ID = "id"; //$NON-NLS-1$

	/** name for the field giving the path to the XML file */
	public final static String PATH = "path"; //$NON-NLS-1$

	private static IPreferenceStore preferences;

	/**
	 * Returns the preference store used to store elementTypes preferences.
	 *
	 * @return the preference store of the plugin
	 */
	protected synchronized static IPreferenceStore getPreferenceStore() {
		if (preferences == null) {
			preferences = new ScopedPreferenceStore(InstanceScope.INSTANCE,
					org.eclipse.papyrus.infra.extendedtypes.Activator.PLUGIN_ID);
		}

		return preferences;
	}

	/**
	 * Retrieves the root memento from the plugin preferences for all extended types redefinition
	 * redefinitions.
	 *
	 * @return the root memento if there were existing customizations, a newly created one otherwise (empty one)
	 */
	protected static XMLMemento getLocalRedefinitions() {
		String sValue = getPreferenceStore().getString(EXTENDED_TYPES_REDEFINITIONS);
		try {
			if (sValue != null && !sValue.equals("")) { //$NON-NLS-1$
				XMLMemento rootMemento = XMLMemento.createReadRoot(new StringReader(sValue));
				return rootMemento;
			} else {
				return XMLMemento.createWriteRoot(EXTENDED_TYPES_REDEFINITIONS);
			}
		} catch (WorkbenchException e) {
			LogUtil.LOG.error("Impossible to read preferences for extended types local redefinitions", e);
		}
		return null;
	}

	/**
	 * Register a new local redefinition of a extendedTypes.
	 *
	 * @param extendedTypesID
	 *            the id of the extendedTypes to register
	 * @param path
	 *            the path to the configuration of the extendedTypes
	 * @return the memento that has been registered
	 */
	public static IMemento registerLocalRedefinition(String extendedTypesID, String path) {
		XMLMemento rootMemento = getLocalRedefinitions();
		// try to find an existing local definition for this extendedTypes
		IMemento memento = getExtendedTypesRedefinitionNode(extendedTypesID);
		// if one exists, remove it from the preferences
		if (memento != null) {
			unregisterLocalRedefinition(extendedTypesID);
		}
		// then register the new one
		IMemento newMemento = rootMemento.createChild(EXTENDED_TYPES_REDEFINITION);
		newMemento.putString(ID, extendedTypesID);
		newMemento.putString(PATH, path);
		saveLocalRedefinitions(rootMemento);
		return newMemento;
	}

	/**
	 * Returns the memento associated to the extendedTypes, or <code>null</code> if none exists
	 *
	 * @param extendedTypesID
	 *            the identifier of the extendedTypes to find
	 * @return the memento found or <code>null</code> if no customization exists for this extendedTypes
	 */
	private static IMemento getExtendedTypesRedefinitionNode(String extendedTypesID) {
		XMLMemento rootMemento = getLocalRedefinitions();
		IMemento[] redefinitions = rootMemento.getChildren(EXTENDED_TYPES_REDEFINITION);
		for (IMemento redefinitionMemento : redefinitions) {
			String extendedTypesNodeID = redefinitionMemento.getString(ID);
			// check equals. extendedTypes ID is not null, as checked at the begining of the method.
			if (extendedTypesID.equals(extendedTypesNodeID)) {
				return redefinitionMemento;
			}
		}
		return null;
	}

	/**
	 * Returns the memento associated to the extendedTypes set definition in workspace, or <code>null</code> if none exists
	 *
	 * @return the memento found or <code>null</code> if no customization exists for this extendedTypes
	 */
	protected static IMemento[] getWorkspaceDefinitions() {
		XMLMemento rootMemento = getLocalRedefinitions();
		IMemento[] workspaceDefinitions = rootMemento.getChildren(EXTENDED_TYPES_SET_WORKSPACE_DEFINITION);
		return workspaceDefinitions;
	}

	/**
	 * Returns the memento associated to the extendedTypes set definition in workspace, or <code>null</code> if none exists
	 *
	 * @return the memento found or <code>null</code> if no customization exists for this extendedTypes
	 */
	protected static IMemento getWorkspaceDefinition(String extendedTypeSetsID) {
		if (extendedTypeSetsID == null) {
			return null;
		}
		IMemento[] workspaceDefinitions = getWorkspaceDefinitions();
		if (workspaceDefinitions == null || workspaceDefinitions.length == 0) {
			return null;
		}
		for (IMemento memento : workspaceDefinitions) {
			String id = memento.getString(ID);
			if (extendedTypeSetsID.equals(id)) {
				return memento;
			}
		}
		return null;
	}

	/**
	 * Returns the path for a given extended type local redefinition
	 *
	 * @param extendedTypesID
	 *            the unique identifier of the extended type to retrieve
	 * @return the path to the configuration of the extended types or <code>null</code> if no customization exists for this extended type
	 *         configuration
	 */
	public static String getExtendedTypesRedefinition(String extendedTypesID) {
		if (extendedTypesID == null) {
			LogUtil.LOG.debug("Trying to find preferences for a null extended type set identifier");
		}
		IMemento memento = getExtendedTypesRedefinitionNode(extendedTypesID);
		if (memento != null) {
			return memento.getString(PATH);
		}
		return null;
	}

	/**
	 * Unregister a specific local redefinition
	 *
	 * @param extendedTypesID
	 *            the identifier of the extended types set to unregister
	 */
	public static void unregisterLocalRedefinition(String extendedTypesID) {
		XMLMemento rootMemento = getLocalRedefinitions();
		// no remove method...
		// so, creation of a new root memento, then, duplicate all entries
		// except the one to
		// delete...
		XMLMemento newRootMemento = XMLMemento.createWriteRoot(EXTENDED_TYPES_REDEFINITIONS);
		for (IMemento memento : rootMemento.getChildren(EXTENDED_TYPES_REDEFINITION)) {
			if (!memento.getString(ID).equals(extendedTypesID)) {
				newRootMemento.putMemento(memento);
			}
		}
		for (IMemento memento : rootMemento.getChildren(EXTENDED_TYPES_SET_WORKSPACE_DEFINITION)) {
			newRootMemento.putMemento(memento);
		}
		// save new Memento
		saveLocalRedefinitions(newRootMemento);
	}

	/**
	 * @param extendedTypesID
	 */
	public static void unregisterWorkspaceDefinition(String extendedTypesID) {
		XMLMemento rootMemento = getLocalRedefinitions();
		// no remove method...
		// so, creation of a new root memento, then, duplicate all entries
		// except the one to
		// delete...
		XMLMemento newRootMemento = XMLMemento.createWriteRoot(EXTENDED_TYPES_REDEFINITIONS);
		for (IMemento memento : rootMemento.getChildren(EXTENDED_TYPES_REDEFINITION)) {
			newRootMemento.putMemento(memento);
		}
		for (IMemento memento : rootMemento.getChildren(EXTENDED_TYPES_SET_WORKSPACE_DEFINITION)) {
			if (!memento.getString(ID).equals(extendedTypesID)) {
				newRootMemento.putMemento(memento);
			}
		}
		// save new Memento
		saveLocalRedefinitions(newRootMemento);
	}

	/**
	 * saves the given root memento with the given key in the preference area
	 *
	 * @param xmlMemento
	 *            the memento to save
	 * @param key
	 *            the key for the preference store
	 */
	private static void saveMemento(XMLMemento xmlMemento, String key) {
		// save memento
		StringWriter writer = new StringWriter();
		try {
			xmlMemento.save(writer);
			if (getPreferenceStore() != null) {
				getPreferenceStore().setValue(key, writer.toString());
			}
		} catch (IOException e) {
			LogUtil.LOG.error("input/ouput exception", e);
		}
	}

	/**
	 * Saves the set of local redefinitions into the preference store
	 *
	 * @param rootMemento
	 *            the memento to save
	 */
	public static void saveLocalRedefinitions(XMLMemento rootMemento) {
		saveMemento(rootMemento, EXTENDED_TYPES_REDEFINITIONS);
	}

	/**
	 * Returns all the paths in the workspace that should be an extended type set to load, with the id as a key
	 *
	 * @return
	 */
	public static Map<String, String> getLocalExtendedTypesDefinitions() {
		IMemento[] mementos = getWorkspaceDefinitions();
		if (mementos != null && mementos.length > 0) {
			Map<String, String> idToPath = new HashMap<String, String>();
			for (IMemento memento : mementos) {
				String id = memento.getString(ID);
				String path = memento.getString(PATH);
				if (id != null && !"".equals(id) && path != null && !"".equals(PATH)) {
					idToPath.put(id, path);
				}
			}
			return idToPath;
		}
		return null;
	}

	/**
	 * Register a new local redefinition of a extendedTypes.
	 *
	 * @param extendedTypesID
	 *            the id of the extendedTypes to register
	 * @param path
	 *            the path to the configuration of the extendedTypes
	 * @return the memento that has been registered
	 */
	public static IMemento registerWorkspaceDefinition(String extendedTypesID, String path) {
		XMLMemento rootMemento = getLocalRedefinitions();
		// try to find an existing local definition for this extendedTypes
		IMemento memento = getWorkspaceDefinition(extendedTypesID);
		// if one exists, remove it from the preferences
		if (memento != null) {
			unregisterWorkspaceDefinition(extendedTypesID);
		}
		// then register the new one
		IMemento newMemento = rootMemento.createChild(EXTENDED_TYPES_SET_WORKSPACE_DEFINITION);
		newMemento.putString(ID, extendedTypesID);
		newMemento.putString(PATH, path);
		saveLocalRedefinitions(rootMemento);
		return newMemento;
	}
}

Back to the top