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