Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 49905504a42f0e04c8991efc3a32f606f0874594 (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
/****************************************************************************
 * Copyright (c) 2012 CEA LIST.
 *
 * 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:
 *		Patrick Tessier (CEA LIST) - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.extensionpoints.editors.definition;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.TreeMap;

import org.eclipse.papyrus.extensionpoints.editors.Activator;
import org.eclipse.papyrus.extensionpoints.editors.utils.IDirectEditorsIds;

/**
 * the goal of this class is to store all direct editors and to provides them by taking account
 * object to edit, constraint, and priority
 *
 */
public class DirectEditorRegistry {

	// map of direct editor indexed by priorities
	protected TreeMap<Integer, List<IDirectEditorExtensionPoint>> editorMap = new TreeMap<Integer, List<IDirectEditorExtensionPoint>>();
	// list of objects that can be edited
	protected ArrayList<String> objectToEdits = new ArrayList<String>();

	/**
	 * add a direct editor
	 * if this direct editor is already used as default in preferences, its priority becomes 0
	 *
	 * @param directEditor
	 *            a direct editor, cannot be null
	 */
	public void add(IDirectEditorExtensionPoint directEditor) {
		assert (directEditor != null);
		objectToEdits.add(directEditor.getObjectToEdit());
		Integer priority = directEditor.getPriority();

		// take in account priority of preferences
		String preferedLanguage = Activator.getDefault().getPreferenceStore().getString(IDirectEditorsIds.EDITOR_FOR_ELEMENT + directEditor.getObjectToEdit());

		// if the language equals is store in preferences this is the default direct editor
		if (preferedLanguage.equals(directEditor.getLanguage())) {
			priority = new Integer(0);
		}
		List<IDirectEditorExtensionPoint> currentValue = editorMap.get(priority);
		if (currentValue == null) {
			currentValue = new ArrayList<IDirectEditorExtensionPoint>();
		}

		currentValue.add(directEditor);
		editorMap.put(priority, currentValue);
	}

	/**
	 * put in preferences the editor with the more important priority
	 */
	protected void adaptPreferences() {
		Iterator<String> iter = objectToEdits.iterator();
		IDirectEditorExtensionPoint defaultDirectEditor = null;
		while (iter.hasNext()) {
			String objectToEdit = iter.next();
			defaultDirectEditor = getDefaultDirectEditor(objectToEdit);

			String id = IDirectEditorsIds.EDITOR_FOR_ELEMENT + defaultDirectEditor.getObjectToEdit();
			String language = defaultDirectEditor.getLanguage();
			// if preference set direct editor as default, do nothing
			if (!Activator.getDefault().getPreferenceStore().getString(id).equals(IDirectEditorsIds.SIMPLE_DIRECT_EDITOR)) {
				Activator.getDefault().getPreferenceStore().setValue(id, language);
			}
		}
	}

	@Override
	public String toString() {
		String out = "";
		Iterator<Integer> keyIterator = editorMap.keySet().iterator();

		while (keyIterator.hasNext()) {
			Integer index = keyIterator.next();
			out = out + "\n[" + index + "]" + "=[" + editorMap.get(index) + "]";

		}
		return out;
	}

	/**
	 * used to fill the registry with an array of direct editors
	 *
	 * @param directEditors
	 *            cannot be null
	 */
	public void init(IDirectEditorExtensionPoint[] directEditors) {
		editorMap = new TreeMap<Integer, List<IDirectEditorExtensionPoint>>();
		objectToEdits = new ArrayList<String>();
		for (int i = 0; i < directEditors.length; i++) {
			add(directEditors[i]);

		}
		adaptPreferences();

	}

	/**
	 * get the direct editor with the higher priority for a given object to edit.
	 *
	 * @param ObjectToEdit
	 *            the string that represents the element to edit
	 * @return a direct editor, it can be null
	 */
	public IDirectEditorExtensionPoint getDefaultDirectEditor(String ObjectToEdit) {
		Iterator<Integer> keyIterator = editorMap.keySet().iterator();

		while (keyIterator.hasNext()) {
			Integer index = keyIterator.next();
			Iterator<IDirectEditorExtensionPoint> iter = editorMap.get(index).iterator();
			while (iter.hasNext()) {
				IDirectEditorExtensionPoint directEditorExtensionPoint = iter.next();
				if (directEditorExtensionPoint.getObjectToEdit().equals(ObjectToEdit)) {
					return directEditorExtensionPoint;
				}

			}
		}
		return null;
	}


}

Back to the top