tmf.xml: Fix XML analysis manager dialog
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.analysis.xml.ui / src / org / eclipse / tracecompass / internal / tmf / analysis / xml / ui / handler / ManageXMLAnalysisDialog.java
index 321b5fd91510c367c97eac4eee51a7bfaa72574b..48cb2effcadee6d1d967fdf7becd130a7865ed64 100644 (file)
@@ -12,6 +12,7 @@ import java.io.File;
 import java.util.ArrayList;
 import java.util.Map;
 
+import org.eclipse.core.filesystem.EFS;
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.IResource;
 import org.eclipse.core.resources.ResourcesPlugin;
@@ -19,6 +20,7 @@ import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Path;
+import org.eclipse.jdt.annotation.Nullable;
 import org.eclipse.jface.dialogs.Dialog;
 import org.eclipse.jface.dialogs.IDialogConstants;
 import org.eclipse.jface.dialogs.MessageDialog;
@@ -42,15 +44,20 @@ import org.eclipse.swt.widgets.Shell;
 import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.module.XmlAnalysisModuleSource;
 import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.module.XmlUtils;
 import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.Activator;
+import org.eclipse.tracecompass.tmf.ui.dialog.TmfFileDialogFactory;
 import org.eclipse.tracecompass.tmf.ui.project.model.TmfCommonProjectElement;
 import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectElement;
 import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectModelElement;
 import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectRegistry;
 import org.eclipse.tracecompass.tmf.ui.project.model.TraceUtils;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IPropertyListener;
+import org.eclipse.ui.ISaveablePart;
 import org.eclipse.ui.IWorkbenchPage;
 import org.eclipse.ui.IWorkbenchPart;
 import org.eclipse.ui.IWorkbenchWindow;
 import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.ide.IDE;
 
 /**
  * Dialog for XML analysis files
@@ -64,6 +71,7 @@ public class ManageXMLAnalysisDialog extends Dialog {
     private Button fDeleteButton;
     private Button fImportButton;
     private Button fExportButton;
+    private Button fEditButton;
     private Label fInvalidFileLabel;
 
     /**
@@ -101,11 +109,9 @@ public class ManageXMLAnalysisDialog extends Dialog {
             @Override
             public void widgetSelected(SelectionEvent e) {
                 if (fAnalysesList.getSelectionCount() == 0) {
-                    fDeleteButton.setEnabled(false);
-                    fExportButton.setEnabled(false);
+                    setButtonsEnabled(false);
                 } else {
-                    fDeleteButton.setEnabled(true);
-                    fExportButton.setEnabled(true);
+                    setButtonsEnabled(true);
                     handleSelection(fAnalysesList.getSelection());
                 }
             }
@@ -150,6 +156,22 @@ public class ManageXMLAnalysisDialog extends Dialog {
             }
         });
 
+        fEditButton = new Button(buttonContainer, SWT.PUSH);
+        fEditButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
+        fEditButton.setText(Messages.ManageXMLAnalysisDialog_Edit);
+        fEditButton.setEnabled(false);
+        fEditButton.addSelectionListener(new SelectionListener() {
+            @Override
+            public void widgetDefaultSelected(SelectionEvent e) {
+            }
+
+            @Override
+            public void widgetSelected(SelectionEvent e) {
+                editAnalysis();
+            }
+
+        });
+
         fDeleteButton = new Button(buttonContainer, SWT.PUSH);
         fDeleteButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
         fDeleteButton.setText(Messages.ManageXMLAnalysisDialog_Delete);
@@ -172,6 +194,12 @@ public class ManageXMLAnalysisDialog extends Dialog {
         return composite;
     }
 
+    private void setButtonsEnabled(boolean enable) {
+        fDeleteButton.setEnabled(enable);
+        fExportButton.setEnabled(enable);
+        fEditButton.setEnabled(enable);
+    }
+
     private void handleSelection(String[] selection) {
         Map<String, File> files = XmlUtils.listFiles();
         File file = files.get(createXmlFileString(selection[0]));
@@ -234,45 +262,53 @@ public class ManageXMLAnalysisDialog extends Dialog {
             IPath path = new Path(file);
             fAnalysesList.add(path.removeFileExtension().toString());
         }
-        fDeleteButton.setEnabled(false);
-        fExportButton.setEnabled(false);
+        setButtonsEnabled(false);
     }
 
     private void importAnalysis() {
-        FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(), SWT.OPEN);
+        FileDialog dialog = TmfFileDialogFactory.create(Display.getCurrent().getActiveShell(), SWT.OPEN);
         dialog.setText(Messages.ManageXMLAnalysisDialog_SelectFileImport);
         dialog.setFilterNames(new String[] { Messages.ManageXMLAnalysisDialog_ImportXmlFile + " (*.xml)" }); //$NON-NLS-1$
         dialog.setFilterExtensions(new String[] { XML_FILTER_EXTENSION });
         String path = dialog.open();
         if (path != null) {
             File file = new File(path);
-            IStatus status = XmlUtils.xmlValidate(file);
-            if (status.isOK()) {
-                deleteSupplementaryFile(file.getName());
+            if (loadXmlFile(file, true)) {
+                fillAnalysesList();
+            }
+        }
+    }
+
+    private static boolean loadXmlFile(File file, boolean addFile) {
+        IStatus status = XmlUtils.xmlValidate(file);
+        if (status.isOK()) {
+            deleteSupplementaryFile(file.getName());
+            if (addFile) {
                 status = XmlUtils.addXmlFile(file);
-                if (status.isOK()) {
-                    fillAnalysesList();
-                    XmlAnalysisModuleSource.notifyModuleChange();
-                    /*
-                     * FIXME: It refreshes the list of analysis under a trace,
-                     * but since modules are instantiated when the trace opens,
-                     * the changes won't apply to an opened trace, it needs to
-                     * be closed then reopened
-                     */
-                    refreshProject();
-                } else {
-                    Activator.logError(Messages.ManageXMLAnalysisDialog_ImportFileFailed);
-                    TraceUtils.displayErrorMsg(Messages.ManageXMLAnalysisDialog_ImportFileFailed, status.getMessage());
-                }
-            } else {
-                Activator.logError(Messages.ManageXMLAnalysisDialog_ImportFileFailed);
-                TraceUtils.displayErrorMsg(Messages.ManageXMLAnalysisDialog_ImportFileFailed, status.getMessage());
             }
+            if (status.isOK()) {
+                XmlAnalysisModuleSource.notifyModuleChange();
+                /*
+                 * FIXME: It refreshes the list of analysis under a trace,
+                 * but since modules are instantiated when the trace opens,
+                 * the changes won't apply to an opened trace, it needs to
+                 * be closed then reopened
+                 */
+                refreshProject();
+                return true;
+            }
+
+            Activator.logError(Messages.ManageXMLAnalysisDialog_ImportFileFailed);
+            TraceUtils.displayErrorMsg(Messages.ManageXMLAnalysisDialog_ImportFileFailed, status.getMessage());
+        } else {
+            Activator.logError(Messages.ManageXMLAnalysisDialog_ImportFileFailed);
+            TraceUtils.displayErrorMsg(Messages.ManageXMLAnalysisDialog_ImportFileFailed, status.getMessage());
         }
+        return false;
     }
 
     private void exportAnalysis() {
-        FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(), SWT.SAVE);
+        FileDialog dialog = TmfFileDialogFactory.create(Display.getCurrent().getActiveShell(), SWT.SAVE);
         dialog.setText(NLS.bind(Messages.ManageXMLAnalysisDialog_SelectFileExport, fAnalysesList.getSelection()[0]));
         dialog.setFilterExtensions(new String[] { XML_FILTER_EXTENSION, "*" }); //$NON-NLS-1$
         String selection = createXmlFileString(fAnalysesList.getSelection()[0]);
@@ -285,6 +321,35 @@ public class ManageXMLAnalysisDialog extends Dialog {
         }
     }
 
+    private void editAnalysis() {
+        String selection = createXmlFileString(fAnalysesList.getSelection()[0]);
+        @Nullable
+        File file = XmlUtils.listFiles().get(selection);
+        if (file == null) {
+            Activator.logError(NLS.bind(Messages.ManageXMLAnalysisDialog_FailedToEdit, selection));
+            TraceUtils.displayErrorMsg(NLS.bind(Messages.ManageXMLAnalysisDialog_FailedToEdit, selection), NLS.bind(Messages.ManageXMLAnalysisDialog_FailedToEdit, selection));
+            return;
+        }
+        try {
+            IEditorPart editorPart = IDE.openEditorOnFileStore(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), EFS.getStore(file.toURI()));
+            editorPart.addPropertyListener(new IPropertyListener() {
+                @Override
+                public void propertyChanged(Object source, int propId) {
+                    if (ISaveablePart.PROP_DIRTY == propId) {
+                        if (!editorPart.isDirty()) {
+                            // Editor is not dirty anymore, i.e. it was saved
+                            loadXmlFile(file, false);
+                        }
+                    }
+                }
+            });
+            close();
+        } catch (CoreException e) {
+            Activator.logError(NLS.bind(Messages.ManageXMLAnalysisDialog_FailedToEdit, selection));
+            TraceUtils.displayErrorMsg(NLS.bind(Messages.ManageXMLAnalysisDialog_FailedToEdit, selection), e.getMessage());
+        }
+    }
+
     private void deleteAnalysis() {
         boolean confirm = MessageDialog.openQuestion(
                 getShell(),
This page took 0.032047 seconds and 5 git commands to generate.