Commit | Line | Data |
---|---|---|
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 | **********************************************************************/ | |
14 | package org.eclipse.linuxtools.tmf.ui.views.uml2sd.load; | |
15 | ||
16 | import java.util.ArrayList; | |
17 | import java.util.HashMap; | |
18 | import java.util.Iterator; | |
19 | import java.util.List; | |
eb63f5ff | 20 | import java.util.Map; |
73005152 BH |
21 | |
22 | import org.eclipse.core.runtime.CoreException; | |
23 | import org.eclipse.core.runtime.IConfigurationElement; | |
24 | import org.eclipse.core.runtime.IExtension; | |
25 | import org.eclipse.core.runtime.IExtensionPoint; | |
df0b8ff4 | 26 | import org.eclipse.core.runtime.IStatus; |
73005152 | 27 | import org.eclipse.core.runtime.Platform; |
df0b8ff4 | 28 | import org.eclipse.core.runtime.Status; |
73005152 | 29 | import org.eclipse.jface.preference.IPreferenceStore; |
d34665f9 | 30 | import org.eclipse.linuxtools.internal.tmf.ui.TmfUiPlugin; |
73005152 BH |
31 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.SDView; |
32 | import org.eclipse.ui.IViewReference; | |
33 | import org.eclipse.ui.IWorkbenchPage; | |
34 | ||
35 | /** | |
36 | * Manager class for the UML2SD extension point. | |
df0b8ff4 BH |
37 | * |
38 | * @version 1.0 | |
39 | * @author sveyrier | |
40 | * @author Bernd Hufmann | |
73005152 BH |
41 | */ |
42 | public class LoadersManager { | |
43 | ||
44 | // ------------------------------------------------------------------------ | |
df0b8ff4 | 45 | // Constants |
73005152 | 46 | // ------------------------------------------------------------------------ |
df0b8ff4 BH |
47 | /** |
48 | * The loader tag for the extension point. | |
49 | */ | |
73005152 | 50 | public static final String LOADER_TAG = "uml2SDLoader"; //$NON-NLS-1$ |
df0b8ff4 BH |
51 | /** |
52 | * The loader prefix. | |
53 | */ | |
73005152 BH |
54 | public static final String LOADER_PREFIX = LOADER_TAG + "."; //$NON-NLS-1$ |
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 | */ | |
eb63f5ff | 68 | protected Map<String, IUml2SDLoader> fViewLoaderMap = new HashMap<String, IUml2SDLoader>(); |
df0b8ff4 BH |
69 | /** |
70 | * Map for caching information (view ID to list of configuration elements) | |
71 | */ | |
eb63f5ff | 72 | protected Map<String, ArrayList<IConfigurationElement>> fViewLoadersList = new HashMap<String, ArrayList<IConfigurationElement>>(); |
73005152 BH |
73 | |
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. | |
88 | * | |
89 | * @return the manager instance | |
90 | */ | |
df0b8ff4 | 91 | public synchronized static LoadersManager getInstance() { |
eb63f5ff BH |
92 | if (fLoadersManager == null) { |
93 | fLoadersManager = new LoadersManager(); | |
73005152 | 94 | } |
eb63f5ff | 95 | return fLoadersManager; |
73005152 BH |
96 | } |
97 | ||
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. | |
101 | * | |
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 | } | |
128 | return null; | |
129 | } | |
130 | ||
131 | /** | |
132 | * Sets the loader to null for this view, a kind of clean-up while disposing. | |
133 | * | |
134 | * @param viewId the id of the view | |
135 | */ | |
136 | public void resetLoader(String viewId) { | |
137 | IUml2SDLoader loader = (IUml2SDLoader) fViewLoaderMap.get(viewId); | |
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 | |
146 | * | |
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 | |
156 | * | |
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 | ||
166 | IWorkbenchPage persp = TmfUiPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage(); | |
167 | ||
168 | SDView sdView = view; | |
169 | ||
170 | try { | |
171 | // Search the view corresponding to the viewId | |
172 | if (sdView == null) { | |
173 | IViewReference viewref = (IViewReference) persp.findViewReference(viewId); | |
174 | if (viewref != null) { | |
175 | sdView = (SDView) viewref.getView(false); | |
176 | } | |
177 | ||
178 | if (sdView == null) { | |
179 | // no corresponding view exists -> return null for the loader | |
180 | return null; | |
181 | } | |
182 | } | |
183 | ||
184 | // Return the loader corresponding to that view (if any) | |
185 | IUml2SDLoader loader = fViewLoaderMap.get(viewId); | |
186 | if (loader == null) { | |
187 | createLastLoaderIfAny(viewId); | |
188 | loader = fViewLoaderMap.get(viewId); | |
189 | } | |
190 | ||
191 | return loader; | |
192 | } catch (Exception e) { | |
df0b8ff4 | 193 | TmfUiPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, TmfUiPlugin.PLUGIN_ID, "Error getting loader class", e)); //$NON-NLS-1$ |
73005152 BH |
194 | } |
195 | return null; | |
196 | } | |
197 | ||
198 | /** | |
199 | * Returns the loader class name that have been saved last time. | |
200 | * | |
201 | * @param viewId The view this loader belongs to | |
202 | * @return the class name of the saved loader | |
203 | */ | |
204 | public String getSavedLoader(String viewId) { | |
205 | IPreferenceStore p = TmfUiPlugin.getDefault().getPreferenceStore(); | |
206 | return p.getString(LOADER_PREFIX + viewId); | |
207 | } | |
208 | ||
209 | /** | |
210 | * Saves the last loader in order to reload it on next session. | |
211 | */ | |
212 | public void saveLastLoader(String id, String id2) { | |
213 | IPreferenceStore p = TmfUiPlugin.getDefault().getPreferenceStore(); | |
214 | p.setValue(LOADER_PREFIX + id2, id); | |
215 | } | |
216 | ||
217 | /** | |
218 | * Changes the current unique loader to the given secondary viewId. | |
219 | * | |
220 | * @param loader The current loader | |
221 | * @param id the view secondary id or null | |
222 | */ | |
223 | private void setCurrentLoader(IUml2SDLoader loader, String id) { | |
224 | if (id == null) { | |
225 | return; | |
226 | } | |
227 | ||
228 | // Get the loader in use | |
229 | IUml2SDLoader currentLoader = fViewLoaderMap.get(id); | |
230 | ||
231 | if ((currentLoader != null) && (currentLoader != loader)) { | |
232 | if (loader != null) { | |
233 | IWorkbenchPage persp = TmfUiPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage(); | |
234 | try { | |
235 | // Search view corresponding to the viewId | |
236 | SDView sdview = null; | |
237 | IViewReference viewref = (IViewReference) persp.findViewReference(id); | |
238 | if (viewref != null) { | |
239 | sdview = (SDView) viewref.getView(false); | |
240 | } | |
241 | ||
242 | // Make everything clean for the new loader | |
243 | if (sdview != null) { | |
244 | sdview.resetProviders(); | |
245 | } | |
246 | ||
247 | } catch (Exception e) { | |
df0b8ff4 | 248 | TmfUiPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, TmfUiPlugin.PLUGIN_ID, "Error setting current loader class", e)); //$NON-NLS-1$ |
73005152 BH |
249 | } |
250 | } | |
251 | // The old loader is going to be kicked | |
252 | currentLoader.dispose(); | |
253 | } | |
254 | ||
255 | // Replace the current loader by the new one in the map | |
256 | fViewLoaderMap.put(id, loader); | |
257 | ||
258 | // Store this loader in the preferences to be able to restore it when the workbench will be re-launched | |
259 | if (loader != null) { | |
260 | saveLastLoader(loader.getClass().getName(), id); | |
261 | } | |
262 | } | |
263 | ||
264 | /** | |
265 | * Creates the last loader and saves it. If not last is not available, it creates | |
266 | * and saves the default loader, else no loader is created. | |
267 | * | |
268 | * @param viewId The view ID. | |
269 | */ | |
270 | private void createLastLoaderIfAny(String viewId) { | |
271 | // Get saved loader from preferences | |
272 | String loaderName = getSavedLoader(viewId); | |
273 | ||
274 | // Get loaders from all extensions for given view | |
275 | List<IConfigurationElement> loaderElements = getLoaderConfigurationElements(viewId); | |
276 | IConfigurationElement ce = getLoaderConfigurationElement(loaderName, loaderElements); | |
277 | ||
278 | if (ce == null) { | |
279 | ce = getDefaultLoader(loaderElements); | |
280 | } | |
281 | ||
282 | if (ce != null) { | |
283 | createLoaderForView(viewId, ce); | |
284 | } | |
285 | } | |
286 | ||
287 | /** | |
288 | * Gets a list of loader configuration elements from the extension point registry for a given view. | |
289 | * @param viewId The view ID | |
290 | * @return List of extension point configuration elements. | |
291 | */ | |
292 | private List<IConfigurationElement> getLoaderConfigurationElements(String viewId) { | |
293 | List<IConfigurationElement> list = (List<IConfigurationElement>) fViewLoadersList.get(viewId); | |
294 | if (list != null) { | |
295 | return list; | |
296 | } | |
df0b8ff4 | 297 | |
73005152 BH |
298 | ArrayList<IConfigurationElement> ret = new ArrayList<IConfigurationElement>(); |
299 | IExtensionPoint iep = Platform.getExtensionRegistry().getExtensionPoint(TmfUiPlugin.PLUGIN_ID, LOADER_TAG); | |
300 | if (iep == null) { | |
301 | return ret; | |
302 | } | |
303 | ||
304 | IExtension[] ie = iep.getExtensions(); | |
305 | if (ie == null) { | |
306 | return ret; | |
307 | } | |
308 | ||
309 | for (int i = 0; i < ie.length; i++) { | |
310 | IConfigurationElement c[] = ie[i].getConfigurationElements(); | |
311 | for (int j = 0; j < c.length; j++) { | |
312 | if (viewId.equals(c[j].getAttribute("view"))) { //$NON-NLS-1$ | |
313 | ret.add(c[j]); | |
314 | } | |
315 | } | |
316 | } | |
317 | fViewLoadersList.put(viewId, ret); | |
318 | return ret; | |
319 | } | |
320 | ||
321 | /** | |
322 | * Returns the loader configuration element for given loader class name and for the given | |
323 | * list of configuration elements, if available else null. | |
324 | * | |
325 | * @param loaderClassName The loader class name. | |
326 | * @param loaderElements The list of loader configuration elements | |
327 | * @return Extension point configuration element | |
328 | */ | |
329 | private IConfigurationElement getLoaderConfigurationElement(String loaderClassName, List<IConfigurationElement> loaderElements) { | |
330 | if (loaderClassName != null && loaderClassName.length() > 0) { | |
331 | // Find configuration element corresponding to the saved loader | |
332 | for (Iterator<IConfigurationElement> i = loaderElements.iterator(); i.hasNext();) { | |
333 | IConfigurationElement ce = (IConfigurationElement) i.next(); | |
334 | if (ce.getAttribute("class").equals(loaderClassName)) { //$NON-NLS-1$ | |
335 | return ce; | |
336 | } | |
337 | } | |
338 | } | |
339 | return null; | |
340 | } | |
341 | ||
342 | /** | |
343 | * Returns the loader configuration element for the given list of configuration elements, if available else null. | |
344 | * Note that if multiple default loaders are defined it selects the first one | |
345 | ||
346 | * @param loaderElements The list of loader configuration elements | |
347 | * @return The default extension point configuration element. | |
348 | */ | |
349 | private IConfigurationElement getDefaultLoader(List<IConfigurationElement> loaderElements) { | |
350 | // Look for a default loader | |
351 | for (Iterator<IConfigurationElement> i = loaderElements.iterator(); i.hasNext();) { | |
352 | IConfigurationElement ce = (IConfigurationElement) i.next(); | |
353 | if (Boolean.valueOf(ce.getAttribute("default")).booleanValue()) { //$NON-NLS-1$ | |
354 | return ce; | |
355 | } | |
356 | } | |
357 | return null; | |
358 | } | |
359 | ||
360 | /** | |
361 | * Creates an instance of the loader class for a given extension point configuration element and | |
362 | * also sets it as current loader for the given view. | |
df0b8ff4 | 363 | * |
73005152 BH |
364 | * @param viewId The view ID. |
365 | * @param ce The extension point configuration element | |
366 | */ | |
367 | private void createLoaderForView(String viewId, IConfigurationElement ce) { | |
368 | try { | |
369 | Object obj = ce.createExecutableExtension("class"); //$NON-NLS-1$ | |
370 | IUml2SDLoader l = (IUml2SDLoader) obj; | |
371 | if (viewId != null) { | |
372 | setCurrentLoader(l, viewId); | |
373 | } | |
374 | } catch (CoreException e4) { | |
df0b8ff4 | 375 | TmfUiPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, TmfUiPlugin.PLUGIN_ID, "Error 'uml2SDLoader' Extension point", e4)); //$NON-NLS-1$ |
73005152 | 376 | } catch (Exception e5) { |
df0b8ff4 | 377 | TmfUiPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, TmfUiPlugin.PLUGIN_ID, "Error 'uml2SDLoader' Extension point", e5)); //$NON-NLS-1$ |
73005152 BH |
378 | } |
379 | } | |
380 | } |