ss: Move plugins to Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / uml2sd / load / LoadersManager.java
old mode 100755 (executable)
new mode 100644 (file)
index 582b54f..6afa542
@@ -1,21 +1,22 @@
 /**********************************************************************
- * Copyright (c) 2005, 2008, 2011 IBM Corporation and others.
+ * Copyright (c) 2005, 2013 IBM Corporation, Ericsson
  * All rights reserved.   This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
  * http://www.eclipse.org/legal/epl-v10.html
- * $Id: LoadersManager.java,v 1.5 2008/01/24 02:29:16 apnan Exp $
- * 
- * Contributors: 
- * IBM - Initial API and implementation
- * Bernd Hufmann - Updated for TMF
+ *
+ * Contributors:
+ *     IBM - Initial API and implementation
+ *     Bernd Hufmann - Updated for TMF
  **********************************************************************/
+
 package org.eclipse.linuxtools.tmf.ui.views.uml2sd.load;
 
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IConfigurationElement;
@@ -23,38 +24,59 @@ import org.eclipse.core.runtime.IExtension;
 import org.eclipse.core.runtime.IExtensionPoint;
 import org.eclipse.core.runtime.Platform;
 import org.eclipse.jface.preference.IPreferenceStore;
-import org.eclipse.linuxtools.tmf.core.Tracer;
-import org.eclipse.linuxtools.tmf.ui.TmfUiPlugin;
+import org.eclipse.linuxtools.internal.tmf.ui.Activator;
 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.SDView;
 import org.eclipse.ui.IViewReference;
 import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.IWorkbenchWindow;
+import org.eclipse.ui.PlatformUI;
 
 /**
  * Manager class for the UML2SD extension point.
+ *
+ * @version 1.0
+ * @author sveyrier
+ * @author Bernd Hufmann
  */
 public class LoadersManager {
 
     // ------------------------------------------------------------------------
-    // Attributes
+    // Constants
     // ------------------------------------------------------------------------
-
+    /**
+     * The loader tag for the extension point.
+     */
     public static final String LOADER_TAG = "uml2SDLoader"; //$NON-NLS-1$
+    /**
+     * The loader prefix.
+     */
     public static final String LOADER_PREFIX = LOADER_TAG + "."; //$NON-NLS-1$
 
-    // The instance
-    private static LoadersManager loadersManager;
+    // ------------------------------------------------------------------------
+    // Attributes
+    // ------------------------------------------------------------------------
+
+    /**
+     * The LoadersManager singleton instance.
+     */
+    private static LoadersManager fLoadersManager;
+
+    /**
+     * Map for caching information (view ID to loader class)
+     */
+    private Map<String, IUml2SDLoader> fViewLoaderMap = new HashMap<>();
+    /**
+     * Map for caching information (view ID to list of configuration elements)
+     */
+    private Map<String, ArrayList<IConfigurationElement>> fViewLoadersList = new HashMap<>();
 
-    // Maps for caching information
-    protected HashMap<String, IUml2SDLoader> fViewLoaderMap = new HashMap<String, IUml2SDLoader>();
-    protected HashMap<String, ArrayList<IConfigurationElement>> fViewLoadersList = new HashMap<String, ArrayList<IConfigurationElement>>();
-    
     // ------------------------------------------------------------------------
     // Constructor
     // ------------------------------------------------------------------------
     /**
      * This should not be used by the clients
      */
-    private LoadersManager() {
+    protected LoadersManager() {
     }
 
     // ------------------------------------------------------------------------
@@ -62,20 +84,20 @@ public class LoadersManager {
     // ------------------------------------------------------------------------
     /**
      * A static method to get the manager instance.
-     * 
+     *
      * @return the manager instance
      */
-    public static LoadersManager getInstance() {
-        if (loadersManager == null) {
-            loadersManager = new LoadersManager();
+    public static synchronized LoadersManager getInstance() {
+        if (fLoadersManager == null) {
+            fLoadersManager = new LoadersManager();
         }
-        return loadersManager;
+        return fLoadersManager;
     }
-    
+
     /**
      * Creates a loader instance and associate it to the view. It requires
      * that the loader-view-association was created by an eclipse extension.
-     * 
+     *
      * @param className The name of the class to create an instance from
      * @param view The UML2 Sequence Diagram view instance
      * @return The created loader
@@ -102,16 +124,16 @@ public class LoadersManager {
                 return loader;
             }
         }
-        return null; 
+        return null;
     }
 
     /**
      * Sets the loader to null for this view, a kind of clean-up while disposing.
-     * 
+     *
      * @param viewId the id of the view
      */
     public void resetLoader(String viewId) {
-        IUml2SDLoader loader = (IUml2SDLoader) fViewLoaderMap.get(viewId);
+        IUml2SDLoader loader = fViewLoaderMap.get(viewId);
         if (loader != null) {
             loader.dispose();
         }
@@ -120,7 +142,7 @@ public class LoadersManager {
 
     /**
      * Returns the loader in use in given Sequence Diagram View
-     * 
+     *
      * @param viewId The Sequence Diagram viewId.
      * @return the current loader if any - null otherwise
      */
@@ -130,7 +152,7 @@ public class LoadersManager {
 
     /**
      * Returns the loader in use in this Sequence Diagram View
-     * 
+     *
      * @param viewId The Sequence Diagram viewId
      * @param view The Sequence Diagram view (if known). Use null to reference the primary SD View.
      * @return the current loader if any - null otherwise
@@ -140,18 +162,24 @@ public class LoadersManager {
             return null;
         }
 
-        IWorkbenchPage persp = TmfUiPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();
+        IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
+        // During Eclipse shutdown the active workbench window is null
+        if (window == null) {
+            return null;
+        }
+
+        IWorkbenchPage persp = window.getActivePage();
 
         SDView sdView = view;
 
         try {
             // Search the view corresponding to the viewId
             if (sdView == null) {
-                IViewReference viewref = (IViewReference) persp.findViewReference(viewId);
+                IViewReference viewref = persp.findViewReference(viewId);
                 if (viewref != null) {
                     sdView = (SDView) viewref.getView(false);
                 }
-                
+
                 if (sdView == null) {
                     // no corresponding view exists -> return null for the loader
                     return null;
@@ -167,35 +195,38 @@ public class LoadersManager {
 
             return loader;
         } catch (Exception e) {
-            if (Tracer.isErrorTraced()) {
-                Tracer.traceError("Exception during getCurrentLoder(): " + e); //$NON-NLS-1$
-            }
+            Activator.getDefault().logError("Error getting loader class", e); //$NON-NLS-1$
         }
         return null;
     }
 
     /**
      * Returns the loader class name that have been saved last time.
-     * 
+     *
      * @param viewId The view this loader belongs to
      * @return the class name of the saved loader
      */
     public String getSavedLoader(String viewId) {
-        IPreferenceStore p = TmfUiPlugin.getDefault().getPreferenceStore();
+        IPreferenceStore p = Activator.getDefault().getPreferenceStore();
         return p.getString(LOADER_PREFIX + viewId);
     }
 
     /**
      * Saves the last loader in order to reload it on next session.
+     *
+     * @param id
+     *            Standalone ID of the loader
+     * @param id2
+     *            Suffix ID of the loader
      */
     public void saveLastLoader(String id, String id2) {
-        IPreferenceStore p = TmfUiPlugin.getDefault().getPreferenceStore();
+        IPreferenceStore p = Activator.getDefault().getPreferenceStore();
         p.setValue(LOADER_PREFIX + id2, id);
     }
 
     /**
      * Changes the current unique loader to the given secondary viewId.
-     * 
+     *
      * @param loader The current loader
      * @param id the view secondary id or null
      */
@@ -209,11 +240,16 @@ public class LoadersManager {
 
         if ((currentLoader != null) && (currentLoader != loader)) {
             if (loader != null) {
-                IWorkbenchPage persp = TmfUiPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();
+                IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
+                // During Eclipse shutdown the active workbench window is null
+                if (window == null) {
+                    return;
+                }
+                IWorkbenchPage persp = window.getActivePage();
                 try {
                     // Search view corresponding to the viewId
                     SDView sdview = null;
-                    IViewReference viewref = (IViewReference) persp.findViewReference(id);
+                    IViewReference viewref = persp.findViewReference(id);
                     if (viewref != null) {
                         sdview = (SDView) viewref.getView(false);
                     }
@@ -224,7 +260,7 @@ public class LoadersManager {
                     }
 
                 } catch (Exception e) {
-                    e.printStackTrace();
+                    Activator.getDefault().logError("Error setting current loader class", e); //$NON-NLS-1$
                 }
             }
             // The old loader is going to be kicked
@@ -239,11 +275,11 @@ public class LoadersManager {
             saveLastLoader(loader.getClass().getName(), id);
         }
     }
-    
+
     /**
      * Creates the last loader and saves it. If not last is not available, it creates
      * and saves the default loader, else no loader is created.
-     * 
+     *
      * @param viewId The view ID.
      */
     private void createLastLoaderIfAny(String viewId) {
@@ -269,16 +305,17 @@ public class LoadersManager {
      * @return List of extension point configuration elements.
      */
     private List<IConfigurationElement> getLoaderConfigurationElements(String viewId) {
-        List<IConfigurationElement> list = (List<IConfigurationElement>) fViewLoadersList.get(viewId);
+        List<IConfigurationElement> list = fViewLoadersList.get(viewId);
         if (list != null) {
             return list;
         }
-        ArrayList<IConfigurationElement> ret = new ArrayList<IConfigurationElement>();
-        IExtensionPoint iep = Platform.getExtensionRegistry().getExtensionPoint(TmfUiPlugin.PLUGIN_ID, LOADER_TAG);
+
+        ArrayList<IConfigurationElement> ret = new ArrayList<>();
+        IExtensionPoint iep = Platform.getExtensionRegistry().getExtensionPoint(Activator.PLUGIN_ID, LOADER_TAG);
         if (iep == null) {
             return ret;
         }
-        
+
         IExtension[] ie = iep.getExtensions();
         if (ie == null) {
             return ret;
@@ -297,18 +334,19 @@ public class LoadersManager {
     }
 
     /**
-     * Returns the loader configuration element for given loader class name and for the given 
+     * Returns the loader configuration element for given loader class name and for the given
      * list of configuration elements, if available else null.
-     * 
+     *
      * @param loaderClassName The loader class name.
      * @param loaderElements  The list of loader configuration elements
-     * @return Extension point configuration element 
+     * @return Extension point configuration element
      */
-    private IConfigurationElement getLoaderConfigurationElement(String loaderClassName, List<IConfigurationElement> loaderElements) {
+    private static IConfigurationElement getLoaderConfigurationElement(
+            String loaderClassName, List<IConfigurationElement> loaderElements) {
         if (loaderClassName != null && loaderClassName.length() > 0) {
             // Find configuration element corresponding to the saved loader
             for (Iterator<IConfigurationElement> i = loaderElements.iterator(); i.hasNext();) {
-                IConfigurationElement ce = (IConfigurationElement) i.next();
+                IConfigurationElement ce = i.next();
                 if (ce.getAttribute("class").equals(loaderClassName)) { //$NON-NLS-1$
                     return ce;
                 }
@@ -318,16 +356,17 @@ public class LoadersManager {
     }
 
     /**
-     * Returns the loader configuration element for the given list of configuration elements, if available else null. 
+     * Returns the loader configuration element for the given list of configuration elements, if available else null.
      * Note that if multiple default loaders are defined it selects the first one
 
      * @param loaderElements The list of loader configuration elements
      * @return The default extension point configuration element.
      */
-    private IConfigurationElement getDefaultLoader(List<IConfigurationElement> loaderElements) {
+    private static IConfigurationElement getDefaultLoader(
+            List<IConfigurationElement> loaderElements) {
         // Look for a default loader
         for (Iterator<IConfigurationElement> i = loaderElements.iterator(); i.hasNext();) {
-            IConfigurationElement ce = (IConfigurationElement) i.next();
+            IConfigurationElement ce = i.next();
             if (Boolean.valueOf(ce.getAttribute("default")).booleanValue()) { //$NON-NLS-1$
                 return ce;
             }
@@ -338,6 +377,7 @@ public class LoadersManager {
     /**
      * Creates an instance of the loader class for a given extension point configuration element and
      * also sets it as current loader for the given view.
+     *
      * @param viewId The view ID.
      * @param ce The extension point configuration element
      */
@@ -349,10 +389,9 @@ public class LoadersManager {
                 setCurrentLoader(l, viewId);
             }
         } catch (CoreException e4) {
-            System.err.println("Error 'uml2SDLoader' Extension point :" + e4); //$NON-NLS-1$
+            Activator.getDefault().logError("Error 'uml2SDLoader' Extension point", e4); //$NON-NLS-1$
         } catch (Exception e5) {
-            e5.printStackTrace();
-            System.err.println("Error 'uml2SDLoader' Extension point :" + e5); //$NON-NLS-1$
+            Activator.getDefault().logError("Error 'uml2SDLoader' Extension point", e5); //$NON-NLS-1$
         }
     }
 }
This page took 0.030051 seconds and 5 git commands to generate.