tmf: Fix copy trace handler enabled in unrelated dialogs
authorPatrick Tasse <patrick.tasse@gmail.com>
Fri, 3 Oct 2014 20:23:43 +0000 (16:23 -0400)
committerPatrick Tasse <patrick.tasse@gmail.com>
Tue, 7 Oct 2014 16:44:59 +0000 (12:44 -0400)
Ctrl-C no longer opens the Copy Trace dialog from another dialog when
the current selection in the Project Explorer is a trace element.

The same fix is applied to the copy experiment handler.

Change-Id: I4eb9df578d2227b74312d7fe660b2a3caf2b2627
Signed-off-by: Patrick Tasse <patrick.tasse@gmail.com>
Reviewed-on: https://git.eclipse.org/r/34415
Tested-by: Hudson CI
Reviewed-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
Tested-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
org.eclipse.linuxtools.tmf.ui/plugin.xml
org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/project/handlers/CopyExperimentHandler.java
org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/project/handlers/CopyTraceHandler.java

index b1cd6838c10259db78fac2d43b9e8aaec16b0ea4..f336ed28a2dcc53ce1b8c8ed849c2cfbe8a69c3e 100644 (file)
                mnemonic="%command.copy.mnemonic"
                style="push">
             <visibleWhen
-                  checkEnabled="false">
-               <with
-                     variable="selection">
-                  <count
-                        value="1">
-                  </count>
-                  <iterate
-                        ifEmpty="false"
-                        operator="and">
-                     <or>
-                        <instanceof
-                              value="org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement">
-                        </instanceof>
-                        <instanceof
-                              value="org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement">
-                        </instanceof>
-                     </or>
-                  </iterate>
-               </with>
+                  checkEnabled="true">
             </visibleWhen>
          </command>
          <command
             commandId="org.eclipse.ui.edit.copy">
          <activeWhen>
             <and>
+               <with
+                     variable="activePart">
+                  <instanceof
+                        value="org.eclipse.ui.navigator.CommonNavigator">
+                  </instanceof>
+               </with>
                <count
                      value="1">
                </count>
                   <instanceof
                         value="org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement">
                   </instanceof>
+                  <not>
+                     <test
+                           forcePluginActivation="true"
+                           property="org.eclipse.linuxtools.tmf.ui.isExperimentTrace">
+                     </test>
+                  </not>
                </iterate>
             </and>
          </activeWhen>
             commandId="org.eclipse.ui.edit.copy">
          <activeWhen>
             <and>
+               <with
+                     variable="activePart">
+                  <instanceof
+                        value="org.eclipse.ui.navigator.CommonNavigator">
+                  </instanceof>
+               </with>
                <count
                      value="1">
                </count>
index 4b23853beeed170351e2cd14f7638ac05b9a6652..507011c8768926625cbd4caf5719a0b40e648538 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2009, 2013 Ericsson
+ * Copyright (c) 2009, 2014 Ericsson
  *
  * All rights reserved. This program and the accompanying materials are
  * made available under the terms of the Eclipse Public License v1.0 which
@@ -8,6 +8,7 @@
  *
  * Contributors:
  *   Francois Chouinard - Initial API and implementation
+ *   Patrick Tasse - Remove enable check
  *******************************************************************************/
 
 package org.eclipse.linuxtools.internal.tmf.ui.project.handlers;
@@ -16,64 +17,18 @@ import org.eclipse.core.commands.AbstractHandler;
 import org.eclipse.core.commands.ExecutionEvent;
 import org.eclipse.core.commands.ExecutionException;
 import org.eclipse.jface.viewers.ISelection;
-import org.eclipse.jface.viewers.ISelectionProvider;
-import org.eclipse.jface.viewers.TreeSelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement;
 import org.eclipse.linuxtools.tmf.ui.project.wizards.CopyExperimentDialog;
 import org.eclipse.swt.widgets.Shell;
-import org.eclipse.ui.IWorkbenchPage;
-import org.eclipse.ui.IWorkbenchPart;
-import org.eclipse.ui.IWorkbenchWindow;
-import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.handlers.HandlerUtil;
 
 /**
  * <b><u>CopyExperimentHandler</u></b>
  * <p>
- * TODO: Implement me. Please.
  */
 public class CopyExperimentHandler extends AbstractHandler {
 
-    private TmfExperimentElement fExperiment = null;
-
-    // ------------------------------------------------------------------------
-    // isEnabled
-    // ------------------------------------------------------------------------
-
-    @Override
-    public boolean isEnabled() {
-
-        // Check if we are closing down
-        IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
-        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) {
-            return false;
-        }
-        ISelection selection = selectionProvider.getSelection();
-
-        // Make sure there is only selection and that it is an experiment
-        fExperiment = null;
-        if (selection instanceof TreeSelection) {
-            TreeSelection sel = (TreeSelection) selection;
-            // There should be only one item selected as per the plugin.xml
-            Object element = sel.getFirstElement();
-            if (element instanceof TmfExperimentElement) {
-                fExperiment = (TmfExperimentElement) element;
-            }
-        }
-
-        return (fExperiment != null);
-    }
-
     // ------------------------------------------------------------------------
     // Execution
     // ------------------------------------------------------------------------
@@ -81,15 +36,16 @@ public class CopyExperimentHandler extends AbstractHandler {
     @Override
     public Object execute(ExecutionEvent event) throws ExecutionException {
 
-        // Check if we are closing down
-        IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
-        if (window == null) {
+        // Get selection already validated by handler in plugin.xml
+        ISelection selection = HandlerUtil.getCurrentSelectionChecked(event);
+        if (!(selection instanceof IStructuredSelection)) {
             return null;
         }
+        TmfExperimentElement experiment = (TmfExperimentElement) ((IStructuredSelection) selection).getFirstElement();
 
         // Fire the Copy Experiment dialog
-        Shell shell = window.getShell();
-        CopyExperimentDialog dialog = new CopyExperimentDialog(shell, fExperiment);
+        Shell shell = HandlerUtil.getActiveShellChecked(event);
+        CopyExperimentDialog dialog = new CopyExperimentDialog(shell, experiment);
         dialog.open();
 
         return null;
index 61496dfd5e0e63b49f3eef8cc2596d792ee45029..91d83be589e0e01844473a549712e65d4eb0187f 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2011, 2013 Ericsson
+ * Copyright (c) 2011, 2014 Ericsson
  *
  * All rights reserved. This program and the accompanying materials are
  * made available under the terms of the Eclipse Public License v1.0 which
@@ -8,6 +8,7 @@
  *
  * Contributors:
  *   Francois Chouinard - Initial API and implementation
+ *   Patrick Tasse - Remove enable check
  *******************************************************************************/
 
 package org.eclipse.linuxtools.internal.tmf.ui.project.handlers;
@@ -16,16 +17,11 @@ import org.eclipse.core.commands.AbstractHandler;
 import org.eclipse.core.commands.ExecutionEvent;
 import org.eclipse.core.commands.ExecutionException;
 import org.eclipse.jface.viewers.ISelection;
-import org.eclipse.jface.viewers.ISelectionProvider;
-import org.eclipse.jface.viewers.TreeSelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement;
-import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder;
 import org.eclipse.linuxtools.tmf.ui.project.wizards.CopyTraceDialog;
 import org.eclipse.swt.widgets.Shell;
-import org.eclipse.ui.IWorkbenchPage;
-import org.eclipse.ui.IWorkbenchPart;
-import org.eclipse.ui.IWorkbenchWindow;
-import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.handlers.HandlerUtil;
 
 /**
  * <b><u>CopyTraceHandler</u></b>
@@ -33,48 +29,6 @@ import org.eclipse.ui.PlatformUI;
  */
 public class CopyTraceHandler extends AbstractHandler {
 
-    private TmfTraceElement fTrace = null;
-
-    // ------------------------------------------------------------------------
-    // Validation
-    // ------------------------------------------------------------------------
-
-    @Override
-    public boolean isEnabled() {
-
-        // Check if we are closing down
-        IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
-        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) {
-            return false;
-        }
-        ISelection selection = selectionProvider.getSelection();
-
-        // Make sure a trace is selected
-        fTrace = null;
-        if (selection instanceof TreeSelection) {
-            TreeSelection sel = (TreeSelection) selection;
-            // There should be only one item selected as per the plugin.xml
-            Object element = sel.getFirstElement();
-            if (element instanceof TmfTraceElement) {
-                fTrace = (TmfTraceElement) element;
-            }
-        }
-
-        // We only enable opening from the Traces folder for now
-        return (fTrace != null && fTrace.getParent() instanceof TmfTraceFolder);
-    }
-
     // ------------------------------------------------------------------------
     // Execution
     // ------------------------------------------------------------------------
@@ -82,15 +36,16 @@ public class CopyTraceHandler extends AbstractHandler {
     @Override
     public Object execute(ExecutionEvent event) throws ExecutionException {
 
-        // Check if we are closing down
-        IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
-        if (window == null) {
+        // Get selection already validated by handler in plugin.xml
+        ISelection selection = HandlerUtil.getCurrentSelectionChecked(event);
+        if (!(selection instanceof IStructuredSelection)) {
             return null;
         }
+        TmfTraceElement trace = (TmfTraceElement) ((IStructuredSelection) selection).getFirstElement();
 
-        // Fire the Copy Experiment dialog
-        Shell shell = window.getShell();
-        CopyTraceDialog dialog = new CopyTraceDialog(shell, fTrace);
+        // Fire the Copy Trace dialog
+        Shell shell = HandlerUtil.getActiveShellChecked(event);
+        CopyTraceDialog dialog = new CopyTraceDialog(shell, trace);
         dialog.open();
 
         return null;
This page took 0.031403 seconds and 5 git commands to generate.