1 /**********************************************************************
2 * Copyright (c) 2005, 2008 IBM Corporation and others.
3 * Copyright (c) 2011, 2012 Ericsson.
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
11 * IBM - Initial API and implementation
12 * Bernd Hufmann - Updated for TMF
13 **********************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.ui
.views
.uml2sd
.load
;
16 import java
.util
.ArrayList
;
17 import java
.util
.HashMap
;
18 import java
.util
.Iterator
;
19 import java
.util
.List
;
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
;
26 import org
.eclipse
.core
.runtime
.IStatus
;
27 import org
.eclipse
.core
.runtime
.Platform
;
28 import org
.eclipse
.core
.runtime
.Status
;
29 import org
.eclipse
.jface
.preference
.IPreferenceStore
;
30 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.TmfUiPlugin
;
31 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.uml2sd
.SDView
;
32 import org
.eclipse
.ui
.IViewReference
;
33 import org
.eclipse
.ui
.IWorkbenchPage
;
36 * Manager class for the UML2SD extension point.
40 * @author Bernd Hufmann
42 public class LoadersManager
{
44 // ------------------------------------------------------------------------
46 // ------------------------------------------------------------------------
48 * The loader tag for the extension point.
50 public static final String LOADER_TAG
= "uml2SDLoader"; //$NON-NLS-1$
54 public static final String LOADER_PREFIX
= LOADER_TAG
+ "."; //$NON-NLS-1$
56 // ------------------------------------------------------------------------
58 // ------------------------------------------------------------------------
61 * The LoadersManager singleton instance.
63 private static LoadersManager fLoadersManager
;
66 * Map for caching information (view ID to loader class)
68 protected Map
<String
, IUml2SDLoader
> fViewLoaderMap
= new HashMap
<String
, IUml2SDLoader
>();
70 * Map for caching information (view ID to list of configuration elements)
72 protected Map
<String
, ArrayList
<IConfigurationElement
>> fViewLoadersList
= new HashMap
<String
, ArrayList
<IConfigurationElement
>>();
74 // ------------------------------------------------------------------------
76 // ------------------------------------------------------------------------
78 * This should not be used by the clients
80 protected LoadersManager() {
83 // ------------------------------------------------------------------------
85 // ------------------------------------------------------------------------
87 * A static method to get the manager instance.
89 * @return the manager instance
91 public synchronized static LoadersManager
getInstance() {
92 if (fLoadersManager
== null) {
93 fLoadersManager
= new LoadersManager();
95 return fLoadersManager
;
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.
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
106 public IUml2SDLoader
createLoader(String className
, SDView view
) {
113 String viewId
= view
.getViewSite().getId();
115 // Get loaders from all extensions for given view
116 List
<IConfigurationElement
> loaderElements
= getLoaderConfigurationElements(viewId
);
117 IConfigurationElement ce
= getLoaderConfigurationElement(className
, loaderElements
);
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
);
132 * Sets the loader to null for this view, a kind of clean-up while disposing.
134 * @param viewId the id of the view
136 public void resetLoader(String viewId
) {
137 IUml2SDLoader loader
= (IUml2SDLoader
) fViewLoaderMap
.get(viewId
);
138 if (loader
!= null) {
141 fViewLoaderMap
.put(viewId
, null);
145 * Returns the loader in use in given Sequence Diagram View
147 * @param viewId The Sequence Diagram viewId.
148 * @return the current loader if any - null otherwise
150 public IUml2SDLoader
getCurrentLoader(String viewId
) {
151 return getCurrentLoader(viewId
, null);
155 * Returns the loader in use in this Sequence Diagram View
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
161 public IUml2SDLoader
getCurrentLoader(String viewId
, SDView view
) {
162 if (viewId
== null) {
166 IWorkbenchPage persp
= TmfUiPlugin
.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();
168 SDView sdView
= view
;
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);
178 if (sdView
== null) {
179 // no corresponding view exists -> return null for the loader
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
);
192 } catch (Exception e
) {
193 TmfUiPlugin
.getDefault().logError("Error getting loader class", e
); //$NON-NLS-1$
199 * Returns the loader class name that have been saved last time.
201 * @param viewId The view this loader belongs to
202 * @return the class name of the saved loader
204 public String
getSavedLoader(String viewId
) {
205 IPreferenceStore p
= TmfUiPlugin
.getDefault().getPreferenceStore();
206 return p
.getString(LOADER_PREFIX
+ viewId
);
210 * Saves the last loader in order to reload it on next session.
212 public void saveLastLoader(String id
, String id2
) {
213 IPreferenceStore p
= TmfUiPlugin
.getDefault().getPreferenceStore();
214 p
.setValue(LOADER_PREFIX
+ id2
, id
);
218 * Changes the current unique loader to the given secondary viewId.
220 * @param loader The current loader
221 * @param id the view secondary id or null
223 private void setCurrentLoader(IUml2SDLoader loader
, String id
) {
228 // Get the loader in use
229 IUml2SDLoader currentLoader
= fViewLoaderMap
.get(id
);
231 if ((currentLoader
!= null) && (currentLoader
!= loader
)) {
232 if (loader
!= null) {
233 IWorkbenchPage persp
= TmfUiPlugin
.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();
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);
242 // Make everything clean for the new loader
243 if (sdview
!= null) {
244 sdview
.resetProviders();
247 } catch (Exception e
) {
248 TmfUiPlugin
.getDefault().logError("Error setting current loader class", e
); //$NON-NLS-1$
251 // The old loader is going to be kicked
252 currentLoader
.dispose();
255 // Replace the current loader by the new one in the map
256 fViewLoaderMap
.put(id
, loader
);
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
);
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.
268 * @param viewId The view ID.
270 private void createLastLoaderIfAny(String viewId
) {
271 // Get saved loader from preferences
272 String loaderName
= getSavedLoader(viewId
);
274 // Get loaders from all extensions for given view
275 List
<IConfigurationElement
> loaderElements
= getLoaderConfigurationElements(viewId
);
276 IConfigurationElement ce
= getLoaderConfigurationElement(loaderName
, loaderElements
);
279 ce
= getDefaultLoader(loaderElements
);
283 createLoaderForView(viewId
, ce
);
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.
292 private List
<IConfigurationElement
> getLoaderConfigurationElements(String viewId
) {
293 List
<IConfigurationElement
> list
= (List
<IConfigurationElement
>) fViewLoadersList
.get(viewId
);
298 ArrayList
<IConfigurationElement
> ret
= new ArrayList
<IConfigurationElement
>();
299 IExtensionPoint iep
= Platform
.getExtensionRegistry().getExtensionPoint(TmfUiPlugin
.PLUGIN_ID
, LOADER_TAG
);
304 IExtension
[] ie
= iep
.getExtensions();
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$
317 fViewLoadersList
.put(viewId
, ret
);
322 * Returns the loader configuration element for given loader class name and for the given
323 * list of configuration elements, if available else null.
325 * @param loaderClassName The loader class name.
326 * @param loaderElements The list of loader configuration elements
327 * @return Extension point configuration element
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$
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
346 * @param loaderElements The list of loader configuration elements
347 * @return The default extension point configuration element.
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$
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.
364 * @param viewId The view ID.
365 * @param ce The extension point configuration element
367 private void createLoaderForView(String viewId
, IConfigurationElement ce
) {
369 Object obj
= ce
.createExecutableExtension("class"); //$NON-NLS-1$
370 IUml2SDLoader l
= (IUml2SDLoader
) obj
;
371 if (viewId
!= null) {
372 setCurrentLoader(l
, viewId
);
374 } catch (CoreException e4
) {
375 TmfUiPlugin
.getDefault().logError("Error 'uml2SDLoader' Extension point", e4
); //$NON-NLS-1$
376 } catch (Exception e5
) {
377 TmfUiPlugin
.getDefault().logError("Error 'uml2SDLoader' Extension point", e5
); //$NON-NLS-1$