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