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