tmf: Support folders in tracing projects
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / project / handlers / RenameTraceHandler.java
index d5d77821f15b7c26bbc0330904b8077e95c497b8..6458a73fb2901bc596b84ca0203faad3d34d28fa 100644 (file)
@@ -8,6 +8,7 @@
  *
  * Contributors:
  *   Francois Chouinard - Initial API and implementation
+ *   Patrick Tasse - Add support for folder elements
  *******************************************************************************/
 
 package org.eclipse.linuxtools.internal.tmf.ui.project.handlers;
@@ -20,181 +21,151 @@ import org.eclipse.core.commands.ExecutionException;
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IFolder;
 import org.eclipse.core.resources.IResource;
-import org.eclipse.core.resources.ResourcesPlugin;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.OperationCanceledException;
+import org.eclipse.jface.dialogs.MessageDialog;
 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.jface.window.Window;
-import org.eclipse.linuxtools.internal.tmf.ui.Activator;
-import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
-import org.eclipse.linuxtools.tmf.core.project.model.TmfTraceType;
-import org.eclipse.linuxtools.tmf.core.project.model.TraceTypeHelper;
-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;
-import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceTypeUIUtils;
 import org.eclipse.linuxtools.tmf.ui.project.wizards.RenameTraceDialog;
-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.actions.WorkspaceModifyOperation;
+import org.eclipse.ui.handlers.HandlerUtil;
 
 /**
- * <b><u>RenameTraceHandler</u></b>
- * <p>
- * TODO: Implement me. Please.
+ * Handler for the Rename Trace command.
  */
 public class RenameTraceHandler extends AbstractHandler {
 
-    private TmfTraceElement fTrace = null;
-
     // ------------------------------------------------------------------------
-    // isEnabled
+    // Execution
     // ------------------------------------------------------------------------
 
     @Override
-    public boolean isEnabled() {
+    public Object execute(ExecutionEvent event) throws ExecutionException {
 
         // Check if we are closing down
         IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
         if (window == null) {
-            return false;
+            return null;
         }
 
-        // 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
-        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();
+        ISelection selection = HandlerUtil.getCurrentSelection(event);
+        TmfTraceElement selectedTrace = null;
+        if (selection instanceof IStructuredSelection) {
+            Object element = ((IStructuredSelection) selection).getFirstElement();
             if (element instanceof TmfTraceElement) {
-                fTrace = (TmfTraceElement) element;
+                selectedTrace = (TmfTraceElement) element;
             }
         }
-
-        return (fTrace != null);
-    }
-
-    // ------------------------------------------------------------------------
-    // Execution
-    // ------------------------------------------------------------------------
-
-    @Override
-    public Object execute(ExecutionEvent event) throws ExecutionException {
-
-        // Check if we are closing down
-        IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
-        if (window == null) {
+        if (selectedTrace == null) {
             return null;
         }
 
         // If trace is under an experiment, use the original trace from the traces folder
-        fTrace = fTrace.getElementUnderTraceFolder();
+        final TmfTraceElement oldTrace = selectedTrace.getElementUnderTraceFolder();
 
-        // Fire the Rename Trace dialog
-        Shell shell = window.getShell();
-        TmfTraceFolder traceFolder = (TmfTraceFolder) fTrace.getParent();
-        final TmfTraceElement oldTrace = fTrace;
-        RenameTraceDialog dialog = new RenameTraceDialog(shell, fTrace);
+        RenameTraceDialog dialog = new RenameTraceDialog(window.getShell(), oldTrace);
         if (dialog.open() != Window.OK) {
             return null;
         }
 
-        // Locate the new trace object
-        TmfTraceElement trace = null;
-        String newTraceName = dialog.getNewTraceName();
-        for (TmfTraceElement element : traceFolder.getTraces()) {
-            if (element.getName().equals(newTraceName)) {
-                trace = element;
-                break;
-            }
-        }
-        final TmfTraceElement newTrace = trace;
-        if (newTrace == null) {
-            return null;
-        }
+        final TmfTraceFolder traceFolder = (TmfTraceFolder) oldTrace.getParent();
+        final String newName = (String) dialog.getFirstResult();
+
+        IFolder parentFolder = (IFolder) oldTrace.getParent().getResource();
+        final TmfTraceFolder tracesFolder = oldTrace.getProject().getTracesFolder();
+        final IPath newPath = parentFolder.getFullPath().append(newName);
 
         WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {
             @Override
-            protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException {
-                TmfExperimentFolder experimentFolder = newTrace.getProject().getExperimentsFolder();
-                for (final TmfExperimentElement experiment : experimentFolder.getExperiments()) {
-                    for (final TmfTraceElement expTrace : experiment.getTraces()) {
-                        if (expTrace.getName().equals(oldTrace.getName())) {
-                            // Create a link to the renamed trace
-                            createTraceLink(newTrace, experiment);
-                            // Remove the old trace link
-                            expTrace.getResource().delete(true, null);
+            public void execute(IProgressMonitor monitor) throws CoreException {
+                try {
+                    monitor.beginTask("", 1000); //$NON-NLS-1$
+                    if (monitor.isCanceled()) {
+                        throw new OperationCanceledException();
+                    }
+                    // Close the trace if open
+                    oldTrace.closeEditors();
+
+                    if (oldTrace.getResource() instanceof IFolder) {
+                        IFolder folder = (IFolder) oldTrace.getResource();
+                        IFile bookmarksFile = oldTrace.getBookmarksFile();
+                        if (bookmarksFile.exists()) {
+                            IFile newBookmarksFile = folder.getFile(bookmarksFile.getName().replace(oldTrace.getName(), newName));
+                            if (!newBookmarksFile.exists()) {
+                                IPath newBookmarksPath = newBookmarksFile.getFullPath();
+                                bookmarksFile.move(newBookmarksPath, IResource.FORCE | IResource.SHALLOW, monitor);
+                            }
                         }
                     }
+
+                    String newElementPath = newPath.makeRelativeTo(tracesFolder.getPath()).toString();
+                    oldTrace.renameSupplementaryFolder(newElementPath);
+                    oldTrace.getResource().move(newPath, IResource.FORCE | IResource.SHALLOW, monitor);
+                    if (monitor.isCanceled()) {
+                        throw new OperationCanceledException();
+                    }
+                } finally {
+                    monitor.done();
                 }
             }
         };
 
         try {
             PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation);
-        } catch (InterruptedException exception) {
-        } catch (InvocationTargetException | RuntimeException exception) {
-            Activator.getDefault().logError("Error renaming trace" + fTrace.getName(), exception); //$NON-NLS-1$
+        } catch (InterruptedException e) {
+            return null;
+        } catch (InvocationTargetException e) {
+            MessageDialog.openError(window.getShell(), e.toString(), e.getTargetException().toString());
+            return null;
         }
 
-        return null;
-    }
+        /* We need to split the WorkspaceModifyOperation so that the new model
+         * elements get created by the resource changed event */
+        operation = new WorkspaceModifyOperation() {
+            @Override
+            protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException {
 
-    private static void createTraceLink(TmfTraceElement trace,
-            final ITmfProjectModelElement experiment) {
-        try {
-            IResource resource = trace.getResource();
-            IPath location = resource.getLocation();
-            // Get the trace properties for this resource
-            String traceTypeId = resource.getPersistentProperty(TmfCommonConstants.TRACETYPE);
-            TraceTypeHelper traceType = TmfTraceType.getInstance().getTraceType(traceTypeId);
-            if (resource instanceof IFolder) {
-                IFolder folder = ((IFolder) experiment.getResource()).getFolder(trace.getName());
-                if (ResourcesPlugin.getWorkspace().validateLinkLocation(folder, location).isOK()) {
-                    folder.createLink(location, IResource.REPLACE, null);
-                    if (traceType != null) {
-                        TmfTraceTypeUIUtils.setTraceType(folder, traceType);
+                // Locate the new trace object
+                TmfTraceElement newTrace = null;
+                String newElementPath = oldTrace.getParent().getPath().append(newName).makeRelativeTo(tracesFolder.getPath()).toString();
+                for (TmfTraceElement element : traceFolder.getTraces()) {
+                    if (element.getElementPath().equals(newElementPath)) {
+                        newTrace = element;
+                        break;
                     }
                 }
-                else {
-                    Activator.getDefault().logError("RenamaeTraceHandler: Invalid Trace Location: " + location); //$NON-NLS-1$
+                if (newTrace == null) {
+                    return;
                 }
-            }
-            else {
-                IFile file = ((IFolder) experiment.getResource()).getFile(trace.getName());
-                if (ResourcesPlugin.getWorkspace().validateLinkLocation(file, location).isOK()) {
-                    file.createLink(location, IResource.REPLACE, null);
-                    if (traceType != null) {
-                        TmfTraceTypeUIUtils.setTraceType(file, traceType);
+
+                TmfExperimentFolder experimentFolder = newTrace.getProject().getExperimentsFolder();
+                for (final TmfExperimentElement experiment : experimentFolder.getExperiments()) {
+                    for (final TmfTraceElement expTrace : experiment.getTraces()) {
+                        if (expTrace.getElementPath().equals(oldTrace.getElementPath())) {
+                            experiment.removeTrace(expTrace);
+                            experiment.addTrace(newTrace);
+                        }
                     }
                 }
-                else {
-                    Activator.getDefault().logError("RenamaeTraceHandler: Invalid Trace Location: " + location); //$NON-NLS-1$
-                }
             }
-        } catch (CoreException e) {
-            Activator.getDefault().logError("Error renaming trace" + trace.getName(), e); //$NON-NLS-1$
+        };
+
+        try {
+            PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation);
+        } catch (InterruptedException e) {
+        } catch (InvocationTargetException e) {
+            MessageDialog.openError(window.getShell(), e.toString(), e.getTargetException().toString());
         }
-    }
 
+        return null;
+    }
 }
This page took 0.02768 seconds and 5 git commands to generate.