/*******************************************************************************
- * Copyright (c) 2009, 2010, 2011 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
* accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
- *
+ *
* Contributors:
* Francois Chouinard - Initial API and implementation
+ * Patrick Tasse - Add support for folder elements
*******************************************************************************/
package org.eclipse.linuxtools.internal.tmf.ui.project.handlers;
import java.lang.reflect.InvocationTargetException;
-import java.util.ArrayList;
-import java.util.List;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
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.tmf.core.TmfCommonConstants;
-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.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
- // ------------------------------------------------------------------------
-
- @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();
- 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();
- if (element instanceof TmfTraceElement) {
- fTrace = (TmfTraceElement) element;
- }
- }
-
- return (fTrace != null);
- }
-
// ------------------------------------------------------------------------
// Execution
// ------------------------------------------------------------------------
// Check if we are closing down
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
- if (window == null)
- return null;
-
- // If trace is under an experiment, use the original trace from the traces folder
- fTrace = fTrace.getElementUnderTraceFolder();
-
- // Fire the Rename Trace dialog
- Shell shell = window.getShell();
- TmfTraceFolder traceFolder = (TmfTraceFolder) fTrace.getParent();
- TmfTraceElement oldTrace = fTrace;
- RenameTraceDialog dialog = new RenameTraceDialog(shell, fTrace);
- if (dialog.open() != Window.OK)
+ if (window == null) {
return null;
+ }
- // Locate the new trace object
- TmfTraceElement newTrace = null;
- String newTraceName = dialog.getNewTraceName();
- for (ITmfProjectModelElement element : traceFolder.getChildren()) {
+ ISelection selection = HandlerUtil.getCurrentSelection(event);
+ TmfTraceElement selectedTrace = null;
+ if (selection instanceof IStructuredSelection) {
+ Object element = ((IStructuredSelection) selection).getFirstElement();
if (element instanceof TmfTraceElement) {
- TmfTraceElement trace = (TmfTraceElement) element;
- if (trace.getName().equals(newTraceName)) {
- newTrace = trace;
- break;
- }
+ selectedTrace = (TmfTraceElement) element;
}
}
- if (newTrace == null)
+ if (selectedTrace == null) {
+ return null;
+ }
+
+ // If trace is under an experiment, use the original trace from the traces folder
+ final TmfTraceElement oldTrace = selectedTrace.getElementUnderTraceFolder();
+
+ RenameTraceDialog dialog = new RenameTraceDialog(window.getShell(), oldTrace);
+ if (dialog.open() != Window.OK) {
return null;
+ }
- List<WorkspaceModifyOperation> removeOps = new ArrayList<WorkspaceModifyOperation>();
- TmfExperimentFolder experimentFolder = newTrace.getProject().getExperimentsFolder();
- for (final ITmfProjectModelElement experiment : experimentFolder.getChildren()) {
- for (final ITmfProjectModelElement trace : experiment.getChildren()) {
- if (trace.equals(oldTrace)) {
- // Create a link to the renamed trace
- createTraceLink(newTrace, experiment);
-
- // Queue the removal of the old trace link
- removeOps.add(new WorkspaceModifyOperation() {
- @Override
- protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException {
- experiment.removeChild(trace);
- trace.getResource().delete(true, null);
- experiment.refresh();
+ 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
+ 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();
}
}
- }
+ };
- for (WorkspaceModifyOperation operation : removeOps) {
- try {
- PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation);
- } catch (InterruptedException exception) {
- } catch (InvocationTargetException exception) {
- } catch (RuntimeException exception) {
- }
+ try {
+ PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation);
+ } catch (InterruptedException e) {
+ return null;
+ } catch (InvocationTargetException e) {
+ MessageDialog.openError(window.getShell(), e.toString(), e.getTargetException().toString());
+ return null;
}
-
- return null;
- }
- private void createTraceLink(TmfTraceElement trace, final ITmfProjectModelElement experiment) {
- try {
- IResource resource = trace.getResource();
- IPath location = resource.getLocation();
- // Get the trace properties for this resource
- String traceBundle = resource.getPersistentProperty(TmfCommonConstants.TRACEBUNDLE);
- String traceTypeId = resource.getPersistentProperty(TmfCommonConstants.TRACETYPE);
- String traceIcon = resource.getPersistentProperty(TmfCommonConstants.TRACEICON);
- 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);
- folder.setPersistentProperty(TmfCommonConstants.TRACEBUNDLE, traceBundle);
- folder.setPersistentProperty(TmfCommonConstants.TRACETYPE, traceTypeId);
- folder.setPersistentProperty(TmfCommonConstants.TRACEICON, traceIcon);
- }
- else {
- System.out.println("Invalid Trace Location"); //$NON-NLS-1$
+ /* 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 {
+
+ // 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 {
- IFile file = ((IFolder) experiment.getResource()).getFile(trace.getName());
- if (ResourcesPlugin.getWorkspace().validateLinkLocation(file, location).isOK()) {
- file.createLink(location, IResource.REPLACE, null);
- file.setPersistentProperty(TmfCommonConstants.TRACEBUNDLE, traceBundle);
- file.setPersistentProperty(TmfCommonConstants.TRACETYPE, traceTypeId);
- file.setPersistentProperty(TmfCommonConstants.TRACEICON, traceIcon);
+ if (newTrace == null) {
+ return;
}
- else {
- System.out.println("Invalid Trace Location"); //$NON-NLS-1$
+
+ 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);
+ }
+ }
}
}
- experiment.refresh();
- } catch (CoreException e) {
- e.printStackTrace();
+ };
+
+ try {
+ PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation);
+ } catch (InterruptedException e) {
+ } catch (InvocationTargetException e) {
+ MessageDialog.openError(window.getShell(), e.toString(), e.getTargetException().toString());
}
- }
+ return null;
+ }
}