Close trace and improve error handling on delete and rename commands
authorPatrick Tasse <patrick.tasse@gmail.com>
Thu, 20 Dec 2012 22:47:11 +0000 (17:47 -0500)
committerPatrick Tasse <patrick.tasse@gmail.com>
Fri, 21 Dec 2012 22:19:39 +0000 (17:19 -0500)
- Close trace or experiment editor on delete and rename commands, to
dispose the trace(s) and release file resources to allow the file
operations to succeed
- Show message dialog to user on delete error
- Fix message dialog for rename error
- Move creation of bookmarks file to trace or experiment element
- Fix leaking of project model elements

Change-Id: I585df4fd4b3547dc4b71735ebbd47184585c0eef
Reviewed-on: https://git.eclipse.org/r/9339
Tested-by: Hudson CI
Reviewed-by: Bernd Hufmann <bhufmann@gmail.com>
IP-Clean: Bernd Hufmann <bhufmann@gmail.com>
Tested-by: Bernd Hufmann <bhufmann@gmail.com>
Reviewed-by: Patrick Tasse <patrick.tasse@gmail.com>
org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/project/handlers/DeleteExperimentHandler.java
org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/project/handlers/DeleteTraceHandler.java
org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/project/handlers/Messages.java
org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/project/handlers/OpenExperimentHandler.java
org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/project/handlers/OpenTraceHandler.java
org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/project/handlers/messages.properties
org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/project/model/TmfExperimentElement.java
org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/project/model/TmfProjectModelElement.java
org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/project/model/TmfTraceElement.java
org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/project/wizards/RenameExperimentDialog.java
org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/project/wizards/RenameTraceDialog.java

index 797e4313905b66f1512f18ca0f3d712d4a7b83b6..3dc939f479e0cd95f4e8f49fb694c9605dbbec22 100644 (file)
@@ -17,6 +17,7 @@ import java.util.Iterator;
 import org.eclipse.core.commands.AbstractHandler;
 import org.eclipse.core.commands.ExecutionEvent;
 import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IResource;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.jface.viewers.ISelection;
@@ -24,12 +25,16 @@ import org.eclipse.jface.viewers.TreeSelection;
 import org.eclipse.linuxtools.internal.tmf.ui.Activator;
 import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement;
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.MessageBox;
 import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.IEditorReference;
+import org.eclipse.ui.IWorkbench;
 import org.eclipse.ui.IWorkbenchPage;
 import org.eclipse.ui.IWorkbenchPart;
 import org.eclipse.ui.IWorkbenchWindow;
 import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.part.FileEditorInput;
 
 /**
  * <b><u>DeleteExperimentHandler</u></b>
@@ -70,12 +75,40 @@ public class DeleteExperimentHandler extends AbstractHandler {
             while (iterator.hasNext()) {
                 Object element = iterator.next();
                 if (element instanceof TmfExperimentElement) {
-                    TmfExperimentElement experiment = (TmfExperimentElement) element;
+                    final TmfExperimentElement experiment = (TmfExperimentElement) element;
                     IResource resource = experiment.getResource();
+
                     try {
+                        // Close the experiment if open
+                        IFile file = experiment.getBookmarksFile();
+                        FileEditorInput input = new FileEditorInput(file);
+                        IWorkbench wb = PlatformUI.getWorkbench();
+                        for (IWorkbenchWindow wbWindow : wb.getWorkbenchWindows()) {
+                            for (IWorkbenchPage wbPage : wbWindow.getPages()) {
+                                for (IEditorReference editorReference : wbPage.getEditorReferences()) {
+                                    if (editorReference.getEditorInput().equals(input)) {
+                                        wbPage.closeEditor(editorReference.getEditor(false), false);
+                                    }
+                                }
+                            }
+                        }
+
+                        // Finally, delete the experiment
                         resource.delete(true, null);
+
+                        // Refresh the project
                         experiment.getProject().refresh();
-                    } catch (CoreException e) {
+
+                    } catch (final CoreException e) {
+                        Display.getDefault().asyncExec(new Runnable() {
+                            @Override
+                            public void run() {
+                                final MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
+                                mb.setText(Messages.DeleteTraceHandler_Error + ' ' + experiment.getName());
+                                mb.setMessage(e.getMessage());
+                                mb.open();
+                            }
+                        });
                         Activator.getDefault().logError("Error deleting experiment: " + experiment.getName(), e); //$NON-NLS-1$
                     }
                 }
index 348c42c9d4c656f28a55d982bd1c591476711f12..7a911f0299dae8b07df40b5c6088eaca8e16e81b 100644 (file)
@@ -19,6 +19,7 @@ import java.util.List;
 import org.eclipse.core.commands.AbstractHandler;
 import org.eclipse.core.commands.ExecutionEvent;
 import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IResource;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IPath;
@@ -28,16 +29,21 @@ 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.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.swt.SWT;
+import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.MessageBox;
 import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.IEditorReference;
+import org.eclipse.ui.IWorkbench;
 import org.eclipse.ui.IWorkbenchPage;
 import org.eclipse.ui.IWorkbenchPart;
 import org.eclipse.ui.IWorkbenchWindow;
 import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.part.FileEditorInput;
 
 /**
  * <b><u>DeleteTraceHandler</u></b>
@@ -112,9 +118,23 @@ public class DeleteTraceHandler extends AbstractHandler {
         while (iterator.hasNext()) {
             Object element = iterator.next();
             if (element instanceof TmfTraceElement) {
-                TmfTraceElement trace = (TmfTraceElement) element;
+                final TmfTraceElement trace = (TmfTraceElement) element;
                 IResource resource = trace.getResource();
                 try {
+                    // Close the trace if open
+                    IFile file = trace.getBookmarksFile();
+                    FileEditorInput input = new FileEditorInput(file);
+                    IWorkbench wb = PlatformUI.getWorkbench();
+                    for (IWorkbenchWindow wbWindow : wb.getWorkbenchWindows()) {
+                        for (IWorkbenchPage wbPage : wbWindow.getPages()) {
+                            for (IEditorReference editorReference : wbPage.getEditorReferences()) {
+                                if (editorReference.getEditorInput().equals(input)) {
+                                    wbPage.closeEditor(editorReference.getEditor(false), false);
+                                }
+                            }
+                        }
+                    }
+
                     IPath path = resource.getLocation();
                     if (path != null && (trace.getParent() instanceof TmfTraceFolder)) {
                         String location = path.toString();
@@ -129,6 +149,18 @@ public class DeleteTraceHandler extends AbstractHandler {
                                 }
                             }
                             for (ITmfProjectModelElement child : toRemove) {
+                                // Close the experiment if open
+                                file = ((TmfExperimentElement) experiment).getBookmarksFile();
+                                input = new FileEditorInput(file);
+                                for (IWorkbenchWindow wbWindow : wb.getWorkbenchWindows()) {
+                                    for (IWorkbenchPage wbPage : wbWindow.getPages()) {
+                                        for (IEditorReference editorReference : wbPage.getEditorReferences()) {
+                                            if (editorReference.getEditorInput().equals(input)) {
+                                                wbPage.closeEditor(editorReference.getEditor(false), false);
+                                            }
+                                        }
+                                    }
+                                }
                                 experiment.removeChild(child);
                                 child.getResource().delete(true, null);
                             }
@@ -144,7 +176,16 @@ public class DeleteTraceHandler extends AbstractHandler {
                     // Refresh the project
                     trace.getProject().refresh();
 
-                } catch (CoreException e) {
+                } catch (final CoreException e) {
+                    Display.getDefault().asyncExec(new Runnable() {
+                        @Override
+                        public void run() {
+                            final MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
+                            mb.setText(Messages.DeleteTraceHandler_Error + ' ' + trace.getName());
+                            mb.setMessage(e.getMessage());
+                            mb.open();
+                        }
+                    });
                     Activator.getDefault().logError("Error deleting trace: " + trace.getName(), e); //$NON-NLS-1$
                 }
             }
index 33d90c7ade6ab9941a010b44fbcc22df47545294..049315a051f54fe6b1166301ca0d8f4587041e43 100644 (file)
@@ -37,7 +37,9 @@ public class Messages extends NLS {
 
     public static String DeleteDialog_Title;
     public static String DeleteTraceHandler_Message;
+    public static String DeleteTraceHandler_Error;
     public static String DeleteExperimentHandler_Message;
+    public static String DeleteExperimentHandler_Error;
 
     public static String SelectTraceTypeHandler_Title;
     public static String SelectTraceTypeHandler_InvalidTraceType;
index dc8740f406169fdc36495dc6d365d3e1616109c7..81b00398714208b1dc532bb463482dc347cf7cf3 100644 (file)
 
 package org.eclipse.linuxtools.internal.tmf.ui.project.handlers;
 
-import java.io.ByteArrayInputStream;
-import java.io.InputStream;
 import java.util.List;
 
 import org.eclipse.core.commands.AbstractHandler;
 import org.eclipse.core.commands.ExecutionEvent;
 import org.eclipse.core.commands.ExecutionException;
 import org.eclipse.core.resources.IFile;
-import org.eclipse.core.resources.IResource;
 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.event.ITmfEvent;
 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
@@ -54,8 +50,6 @@ import org.eclipse.ui.part.FileEditorInput;
  */
 public class OpenExperimentHandler extends AbstractHandler {
 
-    private static final String BOOKMARKS_HIDDEN_FILE = ".bookmarks"; //$NON-NLS-1$
-
     private TmfExperimentElement fExperiment = null;
 
     // ------------------------------------------------------------------------
@@ -115,22 +109,8 @@ public class OpenExperimentHandler extends AbstractHandler {
             public void run() {
 
                 final IFile file;
-
                 try {
-                    final IFile bookmarksFile = experimentElement.getProject().getExperimentsFolder().getResource().getFile(BOOKMARKS_HIDDEN_FILE);
-                    if (!bookmarksFile.exists()) {
-                        final InputStream source = new ByteArrayInputStream(new byte[0]);
-                        bookmarksFile.create(source, true, null);
-                    }
-                    bookmarksFile.setHidden(true);
-
-                    file = experimentElement.getResource().getFile(experimentElement.getName() + '_');
-                    if (!file.exists()) {
-                        file.createLink(bookmarksFile.getLocation(), IResource.REPLACE, null);
-                    }
-                    file.setHidden(true);
-                    file.setPersistentProperty(TmfCommonConstants.TRACETYPE, TmfExperiment.class.getCanonicalName());
-
+                    file = experimentElement.getBookmarksFile();
                 } catch (final CoreException e) {
                     Activator.getDefault().logError("Error opening experiment " + experimentElement.getName(), e); //$NON-NLS-1$
                     displayErrorMsg(Messages.OpenExperimentHandler_Error + "\n\n" + e.getMessage()); //$NON-NLS-1$
@@ -187,7 +167,7 @@ public class OpenExperimentHandler extends AbstractHandler {
                 final TmfExperiment experiment = new TmfExperiment(ITmfEvent.class, experimentElement.getName(), traces, cacheSize);
                 experiment.setBookmarksFile(file);
 
-                final String finalCommonEditorId = commonEditorId;
+                final String editorId = commonEditorId;
                 Display.getDefault().asyncExec(new Runnable() {
                     @Override
                     public void run() {
@@ -201,9 +181,9 @@ public class OpenExperimentHandler extends AbstractHandler {
                                 activePage.reuseEditor((IReusableEditor) editor, editorInput);
                                 activePage.activate(editor);
                             } else {
-                                activePage.openEditor(editorInput, finalCommonEditorId);
+                                activePage.openEditor(editorInput, editorId);
                             }
-                            IDE.setDefaultEditor(file, finalCommonEditorId);
+                            IDE.setDefaultEditor(file, editorId);
                             // editor should dispose the experiment on close
                         } catch (final CoreException e) {
                             Activator.getDefault().logError("Error opening experiment " + experimentElement.getName(), e); //$NON-NLS-1$
index 11cf23c01b1a3738463017d095255966d6fe4e9f..d759f78a60d1cb03ffb81c1457315233fc98936d 100644 (file)
 
 package org.eclipse.linuxtools.internal.tmf.ui.project.handlers;
 
-import java.io.ByteArrayInputStream;
-import java.io.InputStream;
-
 import org.eclipse.core.commands.AbstractHandler;
 import org.eclipse.core.commands.ExecutionEvent;
 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.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.event.ITmfEvent;
 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
-import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
 import org.eclipse.linuxtools.tmf.ui.editors.TmfEditorInput;
 import org.eclipse.linuxtools.tmf.ui.editors.TmfEventsEditor;
 import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement;
@@ -55,8 +48,6 @@ import org.eclipse.ui.part.FileEditorInput;
  */
 public class OpenTraceHandler extends AbstractHandler {
 
-    private static final String BOOKMARKS_HIDDEN_FILE = ".bookmarks"; //$NON-NLS-1$
-
     // ------------------------------------------------------------------------
     // Attributes
     // ------------------------------------------------------------------------
@@ -147,54 +138,32 @@ public class OpenTraceHandler extends AbstractHandler {
                     return;
                 }
 
-                final IResource resource = traceElement.getResource();
-                IFile file = null;
-                if (resource instanceof IFile) {
-                    file = (IFile) resource;
-                } else if (resource instanceof IFolder) {
-                    try {
-                        final IFile bookmarksFile = traceElement.getProject().getTracesFolder().getResource().getFile(BOOKMARKS_HIDDEN_FILE);
-                        if (!bookmarksFile.exists()) {
-                            final InputStream source = new ByteArrayInputStream(new byte[0]);
-                            bookmarksFile.create(source, true, null);
-                        }
-                        bookmarksFile.setHidden(true);
-
-                        final IFolder folder = (IFolder) resource;
-                        file = folder.getFile(traceElement.getName() + '_');
-                        if (!file.exists()) {
-                            file.createLink(bookmarksFile.getLocation(), IResource.REPLACE, null);
-                        }
-                        file.setHidden(true);
-                        file.setPersistentProperty(TmfCommonConstants.TRACETYPE, TmfTrace.class.getCanonicalName());
-                        IDE.setDefaultEditor(file, editorId);
-                        // editor should dispose the trace on close
-                    } catch (final CoreException e) {
-                        Activator.getDefault().logError("Error opening trace " + traceElement.getName(), e); //$NON-NLS-1$
-                        displayErrorMsg(Messages.OpenTraceHandler_Error + "\n\n" + e.getMessage()); //$NON-NLS-1$
-                        trace.dispose();
-                        return;
-                    }
+                final IFile file;
+                try {
+                    file = traceElement.getBookmarksFile();
+                } catch (final CoreException e) {
+                    Activator.getDefault().logError("Error opening trace " + traceElement.getName(), e); //$NON-NLS-1$
+                    displayErrorMsg(Messages.OpenTraceHandler_Error + "\n\n" + e.getMessage()); //$NON-NLS-1$
+                    trace.dispose();
+                    return;
                 }
 
-                final IFile editorFile = file;
                 Display.getDefault().asyncExec(new Runnable() {
                     @Override
                     public void run() {
                         try {
-                            final IEditorInput editorInput = new TmfEditorInput(editorFile, trace);
+                            final IEditorInput editorInput = new TmfEditorInput(file, trace);
                             final IWorkbench wb = PlatformUI.getWorkbench();
                             final IWorkbenchPage activePage = wb.getActiveWorkbenchWindow().getActivePage();
 
-                            final IEditorPart editor = activePage.findEditor(new FileEditorInput(editorFile));
+                            final IEditorPart editor = activePage.findEditor(new FileEditorInput(file));
                             if ((editor != null) && (editor instanceof IReusableEditor)) {
                                 activePage.reuseEditor((IReusableEditor) editor, editorInput);
                                 activePage.activate(editor);
                             } else {
                                 activePage.openEditor(editorInput, editorId);
-                                if (resource instanceof IFile) {
-                                    IDE.setDefaultEditor((IFile) resource, editorId);
-                                }
+                                IDE.setDefaultEditor(file, editorId);
+                                // editor should dispose the trace on close
                             }
                         } catch (final PartInitException e) {
                             displayErrorMsg(Messages.OpenTraceHandler_Error + "\n\n" + e.getMessage()); //$NON-NLS-1$
index fb72198a6f5cdf9690babd434f5069618be645cf..ba70355e9bb0663fb683beed097205be17552532 100644 (file)
@@ -13,7 +13,9 @@ OpenExperimentHandler_Error = Error opening experiment.
 # Delete message
 DeleteDialog_Title = Confirm Delete
 DeleteTraceHandler_Message = Are you sure you want to delete this trace?
+DeleteTraceHandler_Error = Error deleting trace
 DeleteExperimentHandler_Message = Are you sure you want to delete this experiment?
+DeleteExperimentHandler_Error = Error deleting experiment
 
 # Set Trace Type
 SelectTraceTypeHandler_Title = Validation Error
index f57192a6667b4de8db49eaa6e5419f11d3f7d4c0..385593af90ae98b5161c4e5386eab69dfdc19786 100644 (file)
@@ -1,22 +1,29 @@
 /*******************************************************************************
  * Copyright (c) 2010, 2012 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
  *******************************************************************************/
 
 package org.eclipse.linuxtools.tmf.ui.project.model;
 
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
+import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IFolder;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
+import org.eclipse.linuxtools.tmf.core.trace.TmfExperiment;
 import org.eclipse.ui.views.properties.IPropertyDescriptor;
 import org.eclipse.ui.views.properties.IPropertySource2;
 import org.eclipse.ui.views.properties.TextPropertyDescriptor;
@@ -26,7 +33,7 @@ import org.eclipse.ui.views.properties.TextPropertyDescriptor;
  * <p>
  * @version 1.0
  * @author Francois Chouinard
- * 
+ *
  */
 public class TmfExperimentElement extends TmfProjectModelElement implements IPropertySource2 {
 
@@ -54,11 +61,13 @@ public class TmfExperimentElement extends TmfProjectModelElement implements IPro
         sfLocationDescriptor.setCategory(sfInfoCategory);
     }
 
+    private static final String BOOKMARKS_HIDDEN_FILE = ".bookmarks"; //$NON-NLS-1$
+
     // ------------------------------------------------------------------------
     // Constructors
     // ------------------------------------------------------------------------
     /**
-     * Constructor 
+     * Constructor
      * @param name The name of the experiment
      * @param folder The folder reference
      * @param parent The experiment folder reference.
@@ -108,6 +117,32 @@ public class TmfExperimentElement extends TmfProjectModelElement implements IPro
         return traces;
     }
 
+    /**
+     * Returns the file resource used to store bookmarks.
+     * The linked file will be created if it doesn't exist.
+     * @return the bookmarks file
+     * @throws CoreException if the bookmarks file cannot be created
+     * @since 2.0
+     */
+    public IFile getBookmarksFile() throws CoreException {
+        IFile file = null;
+        final IFile bookmarksFile = getProject().getExperimentsFolder().getResource().getFile(BOOKMARKS_HIDDEN_FILE);
+        if (!bookmarksFile.exists()) {
+            final InputStream source = new ByteArrayInputStream(new byte[0]);
+            bookmarksFile.create(source, true, null);
+        }
+        bookmarksFile.setHidden(true);
+
+        final IFolder folder = (IFolder) fResource;
+        file = folder.getFile(getName() + '_');
+        if (!file.exists()) {
+            file.createLink(bookmarksFile.getLocation(), IResource.REPLACE, null);
+        }
+        file.setHidden(true);
+        file.setPersistentProperty(TmfCommonConstants.TRACETYPE, TmfExperiment.class.getCanonicalName());
+        return file;
+    }
+
     // ------------------------------------------------------------------------
     // IPropertySource2
     // ------------------------------------------------------------------------
@@ -137,18 +172,21 @@ public class TmfExperimentElement extends TmfProjectModelElement implements IPro
     @Override
     public Object getPropertyValue(Object id) {
 
-        if (sfName.equals(id))
+        if (sfName.equals(id)) {
             return getName();
+        }
 
-        if (sfPath.equals(id))
+        if (sfPath.equals(id)) {
             return getPath().toString();
+        }
 
-        if (sfLocation.equals(id))
+        if (sfLocation.equals(id)) {
             return getLocation().toString();
+        }
 
         return null;
     }
-    
+
     /*
      * (non-Javadoc)
      * @see org.eclipse.ui.views.properties.IPropertySource#resetPropertyValue(java.lang.Object)
index bf6cef78dc32e42eceaf8ffe86bb0b5a7078a514..82f399af22c4cf87cb55f8da44bf8cc03161461e 100644 (file)
@@ -82,6 +82,10 @@ public abstract class TmfProjectModelElement implements ITmfProjectModelElement,
         ResourcesPlugin.getWorkspace().addResourceChangeListener(this);
     }
 
+    private void dispose() {
+        ResourcesPlugin.getWorkspace().removeResourceChangeListener(this);
+    }
+
     // ------------------------------------------------------------------------
     // ITmfProjectModelElement
     // ------------------------------------------------------------------------
@@ -156,6 +160,9 @@ public abstract class TmfProjectModelElement implements ITmfProjectModelElement,
     @Override
     public void removeChild(ITmfProjectModelElement child) {
         fChildren.remove(child);
+        if (child instanceof TmfProjectModelElement) {
+            ((TmfProjectModelElement) child).dispose();
+        }
         refresh();
     }
     /*
index c30e692759f7ce10f7083c022ff49eb56b1b6f0e..8826f9a22e8577366dd795f345fcdd50151a1d83 100644 (file)
 
 package org.eclipse.linuxtools.tmf.ui.project.model;
 
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Map;
 
+import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IFolder;
 import org.eclipse.core.resources.IResource;
 import org.eclipse.core.runtime.CoreException;
@@ -33,6 +36,7 @@ import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlTraceDefin
 import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
+import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
 import org.eclipse.linuxtools.tmf.ui.editors.TmfEventsEditor;
 import org.eclipse.ui.IActionFilter;
 import org.eclipse.ui.views.properties.IPropertyDescriptor;
@@ -88,6 +92,8 @@ public class TmfTraceElement extends TmfProjectModelElement implements IActionFi
         sfIsLinkedDescriptor.setCategory(sfInfoCategory);
     }
 
+    private static final String BOOKMARKS_HIDDEN_FILE = ".bookmarks"; //$NON-NLS-1$
+
     // ------------------------------------------------------------------------
     // Attributes
     // ------------------------------------------------------------------------
@@ -262,6 +268,38 @@ public class TmfTraceElement extends TmfProjectModelElement implements IActionFi
         return null;
     }
 
+    /**
+     * Returns the file resource used to store bookmarks.
+     * If the trace resource is a file, it is returned directly.
+     * If the trace resource is a folder, a linked file is returned.
+     * The linked file will be created if it doesn't exist.
+     * @return the bookmarks file
+     * @throws CoreException if the bookmarks file cannot be created
+     * @since 2.0
+     */
+    public IFile getBookmarksFile() throws CoreException {
+        IFile file = null;
+        if (fResource instanceof IFile) {
+            file = (IFile) fResource;
+        } else if (fResource instanceof IFolder) {
+            final IFile bookmarksFile = getProject().getTracesFolder().getResource().getFile(BOOKMARKS_HIDDEN_FILE);
+            if (!bookmarksFile.exists()) {
+                final InputStream source = new ByteArrayInputStream(new byte[0]);
+                bookmarksFile.create(source, true, null);
+            }
+            bookmarksFile.setHidden(true);
+
+            final IFolder folder = (IFolder) fResource;
+            file = folder.getFile(getName() + '_');
+            if (!file.exists()) {
+                file.createLink(bookmarksFile.getLocation(), IResource.REPLACE, null);
+            }
+            file.setHidden(true);
+            file.setPersistentProperty(TmfCommonConstants.TRACETYPE, TmfTrace.class.getCanonicalName());
+        }
+        return file;
+    }
+
     /**
      * Returns the <code>TmfTraceElement</code> located under the <code>TmfTracesFolder</code>.
      *
index 046ab245c81d1a5e330bcfa07f84083fcbfa6c92..657691e90a2015319bb58a2826437b31805b52d4 100644 (file)
@@ -32,7 +32,6 @@ import org.eclipse.linuxtools.internal.tmf.ui.Activator;
 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.TmfProjectElement;
-import org.eclipse.osgi.util.NLS;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.graphics.Font;
 import org.eclipse.swt.layout.GridData;
@@ -44,9 +43,14 @@ import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Listener;
 import org.eclipse.swt.widgets.Shell;
 import org.eclipse.swt.widgets.Text;
+import org.eclipse.ui.IEditorReference;
+import org.eclipse.ui.IWorkbench;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.IWorkbenchWindow;
 import org.eclipse.ui.PlatformUI;
 import org.eclipse.ui.actions.WorkspaceModifyOperation;
 import org.eclipse.ui.dialogs.SelectionStatusDialog;
+import org.eclipse.ui.part.FileEditorInput;
 
 /**
  * Implementation of a dialog box to rename an experiment.
@@ -219,9 +223,23 @@ public class RenameExperimentDialog extends SelectionStatusDialog {
                     if (monitor.isCanceled()) {
                         throw new OperationCanceledException();
                     }
+                    // Close the experiment if open
+                    IFile file = fExperiment.getBookmarksFile();
+                    FileEditorInput input = new FileEditorInput(file);
+                    IWorkbench wb = PlatformUI.getWorkbench();
+                    for (IWorkbenchWindow wbWindow : wb.getWorkbenchWindows()) {
+                        for (IWorkbenchPage wbPage : wbWindow.getPages()) {
+                            for (IEditorReference editorReference : wbPage.getEditorReferences()) {
+                                if (editorReference.getEditorInput().equals(input)) {
+                                    wbPage.closeEditor(editorReference.getEditor(false), false);
+                                }
+                            }
+                        }
+                    }
+
                     IFolder folder = fExperiment.getResource();
-                    IFile bookmarksFile = folder.getFile(fExperiment.getName() + '_');
-                    IFile newBookmarksFile = folder.getFile(newName + '_');
+                    IFile bookmarksFile = fExperiment.getBookmarksFile();
+                    IFile newBookmarksFile = folder.getFile(bookmarksFile.getName().replace(fExperiment.getName(), newName));
                     if (bookmarksFile.exists()) {
                         if (!newBookmarksFile.exists()) {
                             IPath newBookmarksPath = newBookmarksFile.getFullPath();
@@ -242,7 +260,7 @@ public class RenameExperimentDialog extends SelectionStatusDialog {
         } catch (InterruptedException exception) {
             return null;
         } catch (InvocationTargetException exception) {
-            MessageDialog.openError(getShell(), "", NLS.bind("", exception.getTargetException().getMessage())); //$NON-NLS-1$ //$NON-NLS-2$
+            MessageDialog.openError(getShell(), "", exception.getTargetException().getMessage()); //$NON-NLS-1$
             return null;
         } catch (RuntimeException exception) {
             return null;
index 850e266b91eb60e8af3dae40ab4d231d2b9c4afe..269f05035d06f1ac2647f60176a21520c8ef5f2d 100644 (file)
@@ -29,10 +29,12 @@ import org.eclipse.core.runtime.Status;
 import org.eclipse.jface.dialogs.IDialogConstants;
 import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.linuxtools.internal.tmf.ui.Activator;
+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.TmfProjectElement;
 import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement;
 import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder;
-import org.eclipse.osgi.util.NLS;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.graphics.Font;
 import org.eclipse.swt.layout.GridData;
@@ -44,9 +46,14 @@ import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Listener;
 import org.eclipse.swt.widgets.Shell;
 import org.eclipse.swt.widgets.Text;
+import org.eclipse.ui.IEditorReference;
+import org.eclipse.ui.IWorkbench;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.IWorkbenchWindow;
 import org.eclipse.ui.PlatformUI;
 import org.eclipse.ui.actions.WorkspaceModifyOperation;
 import org.eclipse.ui.dialogs.SelectionStatusDialog;
+import org.eclipse.ui.part.FileEditorInput;
 
 /**
  * Implementation of a dialog box to rename a trace.
@@ -227,10 +234,43 @@ public class RenameTraceDialog extends SelectionStatusDialog {
                     if (monitor.isCanceled()) {
                         throw new OperationCanceledException();
                     }
+                    // Close the trace if open
+                    IFile file = fTrace.getBookmarksFile();
+                    FileEditorInput input = new FileEditorInput(file);
+                    IWorkbench wb = PlatformUI.getWorkbench();
+                    for (IWorkbenchWindow wbWindow : wb.getWorkbenchWindows()) {
+                        for (IWorkbenchPage wbPage : wbWindow.getPages()) {
+                            for (IEditorReference editorReference : wbPage.getEditorReferences()) {
+                                if (editorReference.getEditorInput().equals(input)) {
+                                    wbPage.closeEditor(editorReference.getEditor(false), false);
+                                }
+                            }
+                        }
+                    }
+                    TmfExperimentFolder experimentFolder = fTrace.getProject().getExperimentsFolder();
+                    for (final ITmfProjectModelElement experiment : experimentFolder.getChildren()) {
+                        for (final ITmfProjectModelElement trace : experiment.getChildren()) {
+                            if (trace.equals(fTrace)) {
+                                // Close the experiment if open
+                                file = ((TmfExperimentElement) experiment).getBookmarksFile();
+                                input = new FileEditorInput(file);
+                                for (IWorkbenchWindow wbWindow : wb.getWorkbenchWindows()) {
+                                    for (IWorkbenchPage wbPage : wbWindow.getPages()) {
+                                        for (IEditorReference editorReference : wbPage.getEditorReferences()) {
+                                            if (editorReference.getEditorInput().equals(input)) {
+                                                wbPage.closeEditor(editorReference.getEditor(false), false);
+                                            }
+                                        }
+                                    }
+                                }
+                            }
+                        }
+                    }
+
                     if (fTrace.getResource() instanceof IFolder) {
                         IFolder folder = (IFolder) fTrace.getResource();
-                        IFile bookmarksFile = folder.getFile(fTrace.getName() + '_');
-                        IFile newBookmarksFile = folder.getFile(newName + '_');
+                        IFile bookmarksFile = fTrace.getBookmarksFile();
+                        IFile newBookmarksFile = folder.getFile(bookmarksFile.getName().replace(fTrace.getName(), newName));
                         if (bookmarksFile.exists()) {
                             if (!newBookmarksFile.exists()) {
                                 IPath newBookmarksPath = newBookmarksFile.getFullPath();
@@ -255,7 +295,7 @@ public class RenameTraceDialog extends SelectionStatusDialog {
         } catch (InterruptedException exception) {
             return null;
         } catch (InvocationTargetException exception) {
-            MessageDialog.openError(getShell(), "", NLS.bind("", exception.getTargetException().getMessage())); //$NON-NLS-1$ //$NON-NLS-2$
+            MessageDialog.openError(getShell(), "", exception.getTargetException().getMessage()); //$NON-NLS-1$
             return null;
         } catch (RuntimeException exception) {
             return null;
This page took 0.035618 seconds and 5 git commands to generate.