Drag and drop enhancements
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / project / handlers / SelectTraceTypeHandler.java
index 9b0eb92067016994a42d8865dbcfd2dcf49addfd..e434c081befad413d2b755bccf45dd5a1b0f36f2 100644 (file)
@@ -1,13 +1,14 @@
 /*******************************************************************************
- * Copyright (c) 2011 Ericsson
- * 
+ * Copyright (c) 2011, 2013 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
- * 
+ *
  * Contributors:
  *   Francois Chouinard - Initial API and implementation
+ *   Patrick Tasse - Fix propagation to experiment traces
  *******************************************************************************/
 
 package org.eclipse.linuxtools.internal.tmf.ui.project.handlers;
@@ -23,9 +24,10 @@ import org.eclipse.core.runtime.CoreException;
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.ISelectionProvider;
 import org.eclipse.jface.viewers.TreeSelection;
+import org.eclipse.linuxtools.internal.tmf.ui.Activator;
+import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
 import org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement;
-import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement;
 import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentFolder;
 import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement;
 import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder;
@@ -46,9 +48,9 @@ public class SelectTraceTypeHandler extends AbstractHandler {
     // Constants
     // ------------------------------------------------------------------------
 
-    private static final String BUNDLE_PARAMETER = "org.eclipse.linuxtools.tmf.ui.commandparameter.project.trace.select_trace_type.bundle"; //$NON-NLS-1$
-    private static final String TYPE_PARAMETER = "org.eclipse.linuxtools.tmf.ui.commandparameter.project.trace.select_trace_type.type"; //$NON-NLS-1$
-    private static final String ICON_PARAMETER = "org.eclipse.linuxtools.tmf.ui.commandparameter.project.trace.select_trace_type.icon"; //$NON-NLS-1$
+    private static final String BUNDLE_PARAMETER = "org.eclipse.linuxtools.tmf.ui.commandparameter.select_trace_type.bundle"; //$NON-NLS-1$
+    private static final String TYPE_PARAMETER = "org.eclipse.linuxtools.tmf.ui.commandparameter.select_trace_type.type"; //$NON-NLS-1$
+    private static final String ICON_PARAMETER = "org.eclipse.linuxtools.tmf.ui.commandparameter.select_trace_type.icon"; //$NON-NLS-1$
 
     // ------------------------------------------------------------------------
     // Attributes
@@ -65,22 +67,26 @@ public class SelectTraceTypeHandler extends AbstractHandler {
 
         // Check if we are closing down
         IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
-        if (window == null)
+        if (window == null) {
             return false;
+        }
 
         // Get the selection
         IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
         IWorkbenchPart part = page.getActivePart();
+        if (part == null) {
+            return false;
+        }
         ISelectionProvider selectionProvider = part.getSite().getSelectionProvider();
-        if (selectionProvider == null)
+        if (selectionProvider == null) {
             return false;
+        }
         ISelection selection = selectionProvider.getSelection();
 
         // Make sure selection contains only traces
         fSelection = null;
         if (selection instanceof TreeSelection) {
             fSelection = (TreeSelection) selection;
-            @SuppressWarnings("unchecked")
             Iterator<Object> iterator = fSelection.iterator();
             while (iterator.hasNext()) {
                 Object element = iterator.next();
@@ -103,22 +109,14 @@ public class SelectTraceTypeHandler extends AbstractHandler {
 
         // Check if we are closing down
         IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
-        if (window == null)
+        if (window == null) {
             return null;
+        }
 
         boolean ok = true;
         for (Object element : fSelection.toList()) {
             TmfTraceElement trace = (TmfTraceElement) element;
-            // If trace is under an experiment, use the original trace from the traces folder
-            if (trace.getParent() instanceof TmfExperimentElement) {
-                for (TmfTraceElement aTrace : trace.getProject().getTracesFolder().getTraces()) {
-                    if (aTrace.getName().equals(trace.getName())) {
-                        trace = aTrace;
-                        break;
-                    }
-                }
-            }
-
+            trace = trace.getElementUnderTraceFolder();
             IResource resource = trace.getResource();
             if (resource != null) {
                 try {
@@ -128,7 +126,7 @@ public class SelectTraceTypeHandler extends AbstractHandler {
                     String iconUrl = event.getParameter(ICON_PARAMETER);
                     ok &= propagateProperties(trace, bundleName, traceType, iconUrl);
                 } catch (CoreException e) {
-                    e.printStackTrace();
+                    Activator.getDefault().logError("Error selecting trace type for trace" + trace.getName(), e); //$NON-NLS-1$
                 }
             }
         }
@@ -144,12 +142,14 @@ public class SelectTraceTypeHandler extends AbstractHandler {
         return null;
     }
 
-    private boolean propagateProperties(TmfTraceElement trace, String bundleName, String traceType, String iconUrl) throws CoreException {
+    private static boolean propagateProperties(TmfTraceElement trace,
+            String bundleName, String traceType, String iconUrl)
+            throws CoreException {
 
         IResource svResource = trace.getResource();
-        String svBundleName = svResource.getPersistentProperty(TmfTraceElement.TRACEBUNDLE);
-        String svTraceType = svResource.getPersistentProperty(TmfTraceElement.TRACETYPE);
-        String svIconUrl = svResource.getPersistentProperty(TmfTraceElement.TRACEICON);
+        String svBundleName = svResource.getPersistentProperty(TmfCommonConstants.TRACEBUNDLE);
+        String svTraceType = svResource.getPersistentProperty(TmfCommonConstants.TRACETYPE);
+        String svIconUrl = svResource.getPersistentProperty(TmfCommonConstants.TRACEICON);
 
         setProperties(trace.getResource(), bundleName, traceType, iconUrl);
         trace.refreshTraceType();
@@ -167,7 +167,7 @@ public class SelectTraceTypeHandler extends AbstractHandler {
                 for (final ITmfProjectModelElement child : experiment.getChildren()) {
                     if (child instanceof TmfTraceElement) {
                         TmfTraceElement linkedTrace = (TmfTraceElement) child;
-                        if (linkedTrace.equals(trace)) {
+                        if (linkedTrace.getName().equals(trace.getName())) {
                             IResource resource = linkedTrace.getResource();
                             setProperties(resource, bundleName, traceType, iconUrl);
                             linkedTrace.refreshTraceType();
@@ -180,21 +180,23 @@ public class SelectTraceTypeHandler extends AbstractHandler {
         return true;
     }
 
-    private void setProperties(IResource resource, String bundleName, String traceType, String iconUrl) throws CoreException {
-        resource.setPersistentProperty(TmfTraceElement.TRACEBUNDLE, bundleName);
-        resource.setPersistentProperty(TmfTraceElement.TRACETYPE, traceType);
-        resource.setPersistentProperty(TmfTraceElement.TRACEICON, iconUrl);
+    private static void setProperties(IResource resource, String bundleName,
+            String traceType, String iconUrl) throws CoreException {
+        resource.setPersistentProperty(TmfCommonConstants.TRACEBUNDLE, bundleName);
+        resource.setPersistentProperty(TmfCommonConstants.TRACETYPE, traceType);
+        resource.setPersistentProperty(TmfCommonConstants.TRACEICON, iconUrl);
     }
 
-    private boolean validateTraceType(TmfTraceElement trace) {
+    private static boolean validateTraceType(TmfTraceElement trace) {
         IProject project = trace.getProject().getResource();
-        ITmfTrace<?> tmfTrace = null;
+        ITmfTrace tmfTrace = null;
         try {
             tmfTrace = trace.instantiateTrace();
             return (tmfTrace != null && tmfTrace.validate(project, trace.getLocation().getPath()));
         } finally {
-            if (tmfTrace != null)
+            if (tmfTrace != null) {
                 tmfTrace.dispose();
+            }
         }
     }
 
This page took 0.02714 seconds and 5 git commands to generate.