1 /**********************************************************************
2 * Copyright (c) 2005, 2008, 2011 IBM Corporation and others.
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
7 * $Id: LoadersManager.java,v 1.5 2008/01/24 02:29:16 apnan Exp $
10 * IBM - Initial API and implementation
11 * Bernd Hufmann - Updated for TMF
12 **********************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.ui
.views
.uml2sd
.load
;
15 import java
.util
.ArrayList
;
16 import java
.util
.HashMap
;
17 import java
.util
.Iterator
;
18 import java
.util
.List
;
20 import org
.eclipse
.core
.runtime
.CoreException
;
21 import org
.eclipse
.core
.runtime
.IConfigurationElement
;
22 import org
.eclipse
.core
.runtime
.IExtension
;
23 import org
.eclipse
.core
.runtime
.IExtensionPoint
;
24 import org
.eclipse
.core
.runtime
.Platform
;
25 import org
.eclipse
.jface
.preference
.IPreferenceStore
;
26 import org
.eclipse
.linuxtools
.tmf
.Tracer
;
27 import org
.eclipse
.linuxtools
.tmf
.ui
.TmfUiPlugin
;
28 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.uml2sd
.SDView
;
29 import org
.eclipse
.ui
.IViewReference
;
30 import org
.eclipse
.ui
.IWorkbenchPage
;
33 * Manager class for the UML2SD extension point.
35 public class LoadersManager
{
37 // ------------------------------------------------------------------------
39 // ------------------------------------------------------------------------
41 public static final String LOADER_TAG
= "uml2SDLoader"; //$NON-NLS-1$
42 public static final String LOADER_PREFIX
= LOADER_TAG
+ "."; //$NON-NLS-1$
45 private static LoadersManager loadersManager
;
47 // Maps for caching information
48 protected HashMap
<String
, IUml2SDLoader
> fViewLoaderMap
= new HashMap
<String
, IUml2SDLoader
>();
49 protected HashMap
<String
, ArrayList
<IConfigurationElement
>> fViewLoadersList
= new HashMap
<String
, ArrayList
<IConfigurationElement
>>();
51 // ------------------------------------------------------------------------
53 // ------------------------------------------------------------------------
55 * This should not be used by the clients
57 private LoadersManager() {
60 // ------------------------------------------------------------------------
62 // ------------------------------------------------------------------------
64 * A static method to get the manager instance.
66 * @return the manager instance
68 public static LoadersManager
getInstance() {
69 if (loadersManager
== null) {
70 loadersManager
= new LoadersManager();
72 return loadersManager
;
76 * Creates a loader instance and associate it to the view. It requires
77 * that the loader-view-association was created by an eclipse extension.
79 * @param className The name of the class to create an instance from
80 * @param view The UML2 Sequence Diagram view instance
81 * @return The created loader
83 public IUml2SDLoader
createLoader(String className
, SDView view
) {
90 String viewId
= view
.getViewSite().getId();
92 // Get loaders from all extensions for given view
93 List
<IConfigurationElement
> loaderElements
= getLoaderConfigurationElements(viewId
);
94 IConfigurationElement ce
= getLoaderConfigurationElement(className
, loaderElements
);
97 // Assign a loader instance to this view
98 createLoaderForView(viewId
, ce
);
99 IUml2SDLoader loader
= fViewLoaderMap
.get(viewId
);
100 if (loader
!= null) {
101 loader
.setViewer(view
);
109 * Sets the loader to null for this view, a kind of clean-up while disposing.
111 * @param viewId the id of the view
113 public void resetLoader(String viewId
) {
114 IUml2SDLoader loader
= (IUml2SDLoader
) fViewLoaderMap
.get(viewId
);
115 if (loader
!= null) {
118 fViewLoaderMap
.put(viewId
, null);
122 * Returns the loader in use in given Sequence Diagram View
124 * @param viewId The Sequence Diagram viewId.
125 * @return the current loader if any - null otherwise
127 public IUml2SDLoader
getCurrentLoader(String viewId
) {
128 return getCurrentLoader(viewId
, null);
132 * Returns the loader in use in this Sequence Diagram View
134 * @param viewId The Sequence Diagram viewId
135 * @param view The Sequence Diagram view (if known). Use null to reference the primary SD View.
136 * @return the current loader if any - null otherwise
138 public IUml2SDLoader
getCurrentLoader(String viewId
, SDView view
) {
139 if (viewId
== null) {
143 IWorkbenchPage persp
= TmfUiPlugin
.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();
145 SDView sdView
= view
;
148 // Search the view corresponding to the viewId
149 if (sdView
== null) {
150 IViewReference viewref
= (IViewReference
) persp
.findViewReference(viewId
);
151 if (viewref
!= null) {
152 sdView
= (SDView
) viewref
.getView(false);
155 if (sdView
== null) {
156 // no corresponding view exists -> return null for the loader
161 // Return the loader corresponding to that view (if any)
162 IUml2SDLoader loader
= fViewLoaderMap
.get(viewId
);
163 if (loader
== null) {
164 createLastLoaderIfAny(viewId
);
165 loader
= fViewLoaderMap
.get(viewId
);
169 } catch (Exception e
) {
170 if (Tracer
.isErrorTraced()) {
171 Tracer
.traceError("Exception during getCurrentLoder(): " + e
); //$NON-NLS-1$
178 * Returns the loader class name that have been saved last time.
180 * @param viewId The view this loader belongs to
181 * @return the class name of the saved loader
183 public String
getSavedLoader(String viewId
) {
184 IPreferenceStore p
= TmfUiPlugin
.getDefault().getPreferenceStore();
185 return p
.getString(LOADER_PREFIX
+ viewId
);
189 * Saves the last loader in order to reload it on next session.
191 public void saveLastLoader(String id
, String id2
) {
192 IPreferenceStore p
= TmfUiPlugin
.getDefault().getPreferenceStore();
193 p
.setValue(LOADER_PREFIX
+ id2
, id
);
197 * Changes the current unique loader to the given secondary viewId.
199 * @param loader The current loader
200 * @param id the view secondary id or null
202 private void setCurrentLoader(IUml2SDLoader loader
, String id
) {
207 // Get the loader in use
208 IUml2SDLoader currentLoader
= fViewLoaderMap
.get(id
);
210 if ((currentLoader
!= null) && (currentLoader
!= loader
)) {
211 if (loader
!= null) {
212 IWorkbenchPage persp
= TmfUiPlugin
.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();
214 // Search view corresponding to the viewId
215 SDView sdview
= null;
216 IViewReference viewref
= (IViewReference
) persp
.findViewReference(id
);
217 if (viewref
!= null) {
218 sdview
= (SDView
) viewref
.getView(false);
221 // Make everything clean for the new loader
222 if (sdview
!= null) {
223 sdview
.resetProviders();
226 } catch (Exception e
) {
230 // The old loader is going to be kicked
231 currentLoader
.dispose();
234 // Replace the current loader by the new one in the map
235 fViewLoaderMap
.put(id
, loader
);
237 // Store this loader in the preferences to be able to restore it when the workbench will be re-launched
238 if (loader
!= null) {
239 saveLastLoader(loader
.getClass().getName(), id
);
244 * Creates the last loader and saves it. If not last is not available, it creates
245 * and saves the default loader, else no loader is created.
247 * @param viewId The view ID.
249 private void createLastLoaderIfAny(String viewId
) {
250 // Get saved loader from preferences
251 String loaderName
= getSavedLoader(viewId
);
253 // Get loaders from all extensions for given view
254 List
<IConfigurationElement
> loaderElements
= getLoaderConfigurationElements(viewId
);
255 IConfigurationElement ce
= getLoaderConfigurationElement(loaderName
, loaderElements
);
258 ce
= getDefaultLoader(loaderElements
);
262 createLoaderForView(viewId
, ce
);
267 * Gets a list of loader configuration elements from the extension point registry for a given view.
268 * @param viewId The view ID
269 * @return List of extension point configuration elements.
271 private List
<IConfigurationElement
> getLoaderConfigurationElements(String viewId
) {
272 List
<IConfigurationElement
> list
= (List
<IConfigurationElement
>) fViewLoadersList
.get(viewId
);
276 ArrayList
<IConfigurationElement
> ret
= new ArrayList
<IConfigurationElement
>();
277 IExtensionPoint iep
= Platform
.getExtensionRegistry().getExtensionPoint(TmfUiPlugin
.PLUGIN_ID
, LOADER_TAG
);
282 IExtension
[] ie
= iep
.getExtensions();
287 for (int i
= 0; i
< ie
.length
; i
++) {
288 IConfigurationElement c
[] = ie
[i
].getConfigurationElements();
289 for (int j
= 0; j
< c
.length
; j
++) {
290 if (viewId
.equals(c
[j
].getAttribute("view"))) { //$NON-NLS-1$
295 fViewLoadersList
.put(viewId
, ret
);
300 * Returns the loader configuration element for given loader class name and for the given
301 * list of configuration elements, if available else null.
303 * @param loaderClassName The loader class name.
304 * @param loaderElements The list of loader configuration elements
305 * @return Extension point configuration element
307 private IConfigurationElement
getLoaderConfigurationElement(String loaderClassName
, List
<IConfigurationElement
> loaderElements
) {
308 if (loaderClassName
!= null && loaderClassName
.length() > 0) {
309 // Find configuration element corresponding to the saved loader
310 for (Iterator
<IConfigurationElement
> i
= loaderElements
.iterator(); i
.hasNext();) {
311 IConfigurationElement ce
= (IConfigurationElement
) i
.next();
312 if (ce
.getAttribute("class").equals(loaderClassName
)) { //$NON-NLS-1$
321 * Returns the loader configuration element for the given list of configuration elements, if available else null.
322 * Note that if multiple default loaders are defined it selects the first one
324 * @param loaderElements The list of loader configuration elements
325 * @return The default extension point configuration element.
327 private IConfigurationElement
getDefaultLoader(List
<IConfigurationElement
> loaderElements
) {
328 // Look for a default loader
329 for (Iterator
<IConfigurationElement
> i
= loaderElements
.iterator(); i
.hasNext();) {
330 IConfigurationElement ce
= (IConfigurationElement
) i
.next();
331 if (Boolean
.valueOf(ce
.getAttribute("default")).booleanValue()) { //$NON-NLS-1$
339 * Creates an instance of the loader class for a given extension point configuration element and
340 * also sets it as current loader for the given view.
341 * @param viewId The view ID.
342 * @param ce The extension point configuration element
344 private void createLoaderForView(String viewId
, IConfigurationElement ce
) {
346 Object obj
= ce
.createExecutableExtension("class"); //$NON-NLS-1$
347 IUml2SDLoader l
= (IUml2SDLoader
) obj
;
348 if (viewId
!= null) {
349 setCurrentLoader(l
, viewId
);
351 } catch (CoreException e4
) {
352 System
.err
.println("Error 'uml2SDLoader' Extension point :" + e4
); //$NON-NLS-1$
353 } catch (Exception e5
) {
354 e5
.printStackTrace();
355 System
.err
.println("Error 'uml2SDLoader' Extension point :" + e5
); //$NON-NLS-1$