Merge branch 'master' into lttng_2_0_control_dev
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / uml2sd / load / LoadersManager.java
1 /**********************************************************************
2 * Copyright (c) 2005, 2008, 2011 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 * $Id: LoadersManager.java,v 1.5 2008/01/24 02:29:16 apnan Exp $
8 *
9 * Contributors:
10 * IBM - Initial API and implementation
11 * Bernd Hufmann - Updated for TMF
12 **********************************************************************/
13 package org.eclipse.linuxtools.tmf.ui.views.uml2sd.load;
14
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.Iterator;
18 import java.util.List;
19
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IConfigurationElement;
22 import org.eclipse.core.runtime.IExtension;
23 import org.eclipse.core.runtime.IExtensionPoint;
24 import org.eclipse.core.runtime.Platform;
25 import org.eclipse.jface.preference.IPreferenceStore;
26 import org.eclipse.linuxtools.internal.tmf.core.Tracer;
27 import org.eclipse.linuxtools.internal.tmf.ui.TmfUiPlugin;
28 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.SDView;
29 import org.eclipse.ui.IViewReference;
30 import org.eclipse.ui.IWorkbenchPage;
31
32 /**
33 * Manager class for the UML2SD extension point.
34 */
35 public class LoadersManager {
36
37 // ------------------------------------------------------------------------
38 // Attributes
39 // ------------------------------------------------------------------------
40
41 public static final String LOADER_TAG = "uml2SDLoader"; //$NON-NLS-1$
42 public static final String LOADER_PREFIX = LOADER_TAG + "."; //$NON-NLS-1$
43
44 // The instance
45 private static LoadersManager loadersManager;
46
47 // Maps for caching information
48 protected HashMap<String, IUml2SDLoader> fViewLoaderMap = new HashMap<String, IUml2SDLoader>();
49 protected HashMap<String, ArrayList<IConfigurationElement>> fViewLoadersList = new HashMap<String, ArrayList<IConfigurationElement>>();
50
51 // ------------------------------------------------------------------------
52 // Constructor
53 // ------------------------------------------------------------------------
54 /**
55 * This should not be used by the clients
56 */
57 private LoadersManager() {
58 }
59
60 // ------------------------------------------------------------------------
61 // Operations
62 // ------------------------------------------------------------------------
63 /**
64 * A static method to get the manager instance.
65 *
66 * @return the manager instance
67 */
68 public static LoadersManager getInstance() {
69 if (loadersManager == null) {
70 loadersManager = new LoadersManager();
71 }
72 return loadersManager;
73 }
74
75 /**
76 * Creates a loader instance and associate it to the view. It requires
77 * that the loader-view-association was created by an eclipse extension.
78 *
79 * @param className The name of the class to create an instance from
80 * @param view The UML2 Sequence Diagram view instance
81 * @return The created loader
82 */
83 public IUml2SDLoader createLoader(String className, SDView view) {
84
85 // Safety check
86 if (view == null) {
87 return null;
88 }
89
90 String viewId = view.getViewSite().getId();
91
92 // Get loaders from all extensions for given view
93 List<IConfigurationElement> loaderElements = getLoaderConfigurationElements(viewId);
94 IConfigurationElement ce = getLoaderConfigurationElement(className, loaderElements);
95
96 if (ce != null) {
97 // Assign a loader instance to this view
98 createLoaderForView(viewId, ce);
99 IUml2SDLoader loader = fViewLoaderMap.get(viewId);
100 if (loader != null) {
101 loader.setViewer(view);
102 return loader;
103 }
104 }
105 return null;
106 }
107
108 /**
109 * Sets the loader to null for this view, a kind of clean-up while disposing.
110 *
111 * @param viewId the id of the view
112 */
113 public void resetLoader(String viewId) {
114 IUml2SDLoader loader = (IUml2SDLoader) fViewLoaderMap.get(viewId);
115 if (loader != null) {
116 loader.dispose();
117 }
118 fViewLoaderMap.put(viewId, null);
119 }
120
121 /**
122 * Returns the loader in use in given Sequence Diagram View
123 *
124 * @param viewId The Sequence Diagram viewId.
125 * @return the current loader if any - null otherwise
126 */
127 public IUml2SDLoader getCurrentLoader(String viewId) {
128 return getCurrentLoader(viewId, null);
129 }
130
131 /**
132 * Returns the loader in use in this Sequence Diagram View
133 *
134 * @param viewId The Sequence Diagram viewId
135 * @param view The Sequence Diagram view (if known). Use null to reference the primary SD View.
136 * @return the current loader if any - null otherwise
137 */
138 public IUml2SDLoader getCurrentLoader(String viewId, SDView view) {
139 if (viewId == null) {
140 return null;
141 }
142
143 IWorkbenchPage persp = TmfUiPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();
144
145 SDView sdView = view;
146
147 try {
148 // Search the view corresponding to the viewId
149 if (sdView == null) {
150 IViewReference viewref = (IViewReference) persp.findViewReference(viewId);
151 if (viewref != null) {
152 sdView = (SDView) viewref.getView(false);
153 }
154
155 if (sdView == null) {
156 // no corresponding view exists -> return null for the loader
157 return null;
158 }
159 }
160
161 // Return the loader corresponding to that view (if any)
162 IUml2SDLoader loader = fViewLoaderMap.get(viewId);
163 if (loader == null) {
164 createLastLoaderIfAny(viewId);
165 loader = fViewLoaderMap.get(viewId);
166 }
167
168 return loader;
169 } catch (Exception e) {
170 if (Tracer.isErrorTraced()) {
171 Tracer.traceError("Exception during getCurrentLoder(): " + e); //$NON-NLS-1$
172 }
173 }
174 return null;
175 }
176
177 /**
178 * Returns the loader class name that have been saved last time.
179 *
180 * @param viewId The view this loader belongs to
181 * @return the class name of the saved loader
182 */
183 public String getSavedLoader(String viewId) {
184 IPreferenceStore p = TmfUiPlugin.getDefault().getPreferenceStore();
185 return p.getString(LOADER_PREFIX + viewId);
186 }
187
188 /**
189 * Saves the last loader in order to reload it on next session.
190 */
191 public void saveLastLoader(String id, String id2) {
192 IPreferenceStore p = TmfUiPlugin.getDefault().getPreferenceStore();
193 p.setValue(LOADER_PREFIX + id2, id);
194 }
195
196 /**
197 * Changes the current unique loader to the given secondary viewId.
198 *
199 * @param loader The current loader
200 * @param id the view secondary id or null
201 */
202 private void setCurrentLoader(IUml2SDLoader loader, String id) {
203 if (id == null) {
204 return;
205 }
206
207 // Get the loader in use
208 IUml2SDLoader currentLoader = fViewLoaderMap.get(id);
209
210 if ((currentLoader != null) && (currentLoader != loader)) {
211 if (loader != null) {
212 IWorkbenchPage persp = TmfUiPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();
213 try {
214 // Search view corresponding to the viewId
215 SDView sdview = null;
216 IViewReference viewref = (IViewReference) persp.findViewReference(id);
217 if (viewref != null) {
218 sdview = (SDView) viewref.getView(false);
219 }
220
221 // Make everything clean for the new loader
222 if (sdview != null) {
223 sdview.resetProviders();
224 }
225
226 } catch (Exception e) {
227 e.printStackTrace();
228 }
229 }
230 // The old loader is going to be kicked
231 currentLoader.dispose();
232 }
233
234 // Replace the current loader by the new one in the map
235 fViewLoaderMap.put(id, loader);
236
237 // Store this loader in the preferences to be able to restore it when the workbench will be re-launched
238 if (loader != null) {
239 saveLastLoader(loader.getClass().getName(), id);
240 }
241 }
242
243 /**
244 * Creates the last loader and saves it. If not last is not available, it creates
245 * and saves the default loader, else no loader is created.
246 *
247 * @param viewId The view ID.
248 */
249 private void createLastLoaderIfAny(String viewId) {
250 // Get saved loader from preferences
251 String loaderName = getSavedLoader(viewId);
252
253 // Get loaders from all extensions for given view
254 List<IConfigurationElement> loaderElements = getLoaderConfigurationElements(viewId);
255 IConfigurationElement ce = getLoaderConfigurationElement(loaderName, loaderElements);
256
257 if (ce == null) {
258 ce = getDefaultLoader(loaderElements);
259 }
260
261 if (ce != null) {
262 createLoaderForView(viewId, ce);
263 }
264 }
265
266 /**
267 * Gets a list of loader configuration elements from the extension point registry for a given view.
268 * @param viewId The view ID
269 * @return List of extension point configuration elements.
270 */
271 private List<IConfigurationElement> getLoaderConfigurationElements(String viewId) {
272 List<IConfigurationElement> list = (List<IConfigurationElement>) fViewLoadersList.get(viewId);
273 if (list != null) {
274 return list;
275 }
276 ArrayList<IConfigurationElement> ret = new ArrayList<IConfigurationElement>();
277 IExtensionPoint iep = Platform.getExtensionRegistry().getExtensionPoint(TmfUiPlugin.PLUGIN_ID, LOADER_TAG);
278 if (iep == null) {
279 return ret;
280 }
281
282 IExtension[] ie = iep.getExtensions();
283 if (ie == null) {
284 return ret;
285 }
286
287 for (int i = 0; i < ie.length; i++) {
288 IConfigurationElement c[] = ie[i].getConfigurationElements();
289 for (int j = 0; j < c.length; j++) {
290 if (viewId.equals(c[j].getAttribute("view"))) { //$NON-NLS-1$
291 ret.add(c[j]);
292 }
293 }
294 }
295 fViewLoadersList.put(viewId, ret);
296 return ret;
297 }
298
299 /**
300 * Returns the loader configuration element for given loader class name and for the given
301 * list of configuration elements, if available else null.
302 *
303 * @param loaderClassName The loader class name.
304 * @param loaderElements The list of loader configuration elements
305 * @return Extension point configuration element
306 */
307 private IConfigurationElement getLoaderConfigurationElement(String loaderClassName, List<IConfigurationElement> loaderElements) {
308 if (loaderClassName != null && loaderClassName.length() > 0) {
309 // Find configuration element corresponding to the saved loader
310 for (Iterator<IConfigurationElement> i = loaderElements.iterator(); i.hasNext();) {
311 IConfigurationElement ce = (IConfigurationElement) i.next();
312 if (ce.getAttribute("class").equals(loaderClassName)) { //$NON-NLS-1$
313 return ce;
314 }
315 }
316 }
317 return null;
318 }
319
320 /**
321 * Returns the loader configuration element for the given list of configuration elements, if available else null.
322 * Note that if multiple default loaders are defined it selects the first one
323
324 * @param loaderElements The list of loader configuration elements
325 * @return The default extension point configuration element.
326 */
327 private IConfigurationElement getDefaultLoader(List<IConfigurationElement> loaderElements) {
328 // Look for a default loader
329 for (Iterator<IConfigurationElement> i = loaderElements.iterator(); i.hasNext();) {
330 IConfigurationElement ce = (IConfigurationElement) i.next();
331 if (Boolean.valueOf(ce.getAttribute("default")).booleanValue()) { //$NON-NLS-1$
332 return ce;
333 }
334 }
335 return null;
336 }
337
338 /**
339 * Creates an instance of the loader class for a given extension point configuration element and
340 * also sets it as current loader for the given view.
341 * @param viewId The view ID.
342 * @param ce The extension point configuration element
343 */
344 private void createLoaderForView(String viewId, IConfigurationElement ce) {
345 try {
346 Object obj = ce.createExecutableExtension("class"); //$NON-NLS-1$
347 IUml2SDLoader l = (IUml2SDLoader) obj;
348 if (viewId != null) {
349 setCurrentLoader(l, viewId);
350 }
351 } catch (CoreException e4) {
352 System.err.println("Error 'uml2SDLoader' Extension point :" + e4); //$NON-NLS-1$
353 } catch (Exception e5) {
354 e5.printStackTrace();
355 System.err.println("Error 'uml2SDLoader' Extension point :" + e5); //$NON-NLS-1$
356 }
357 }
358 }
This page took 0.047558 seconds and 6 git commands to generate.