tmf.ui: Introduce TmfFileDialogFactory
authorMatthew Khouzam <matthew.khouzam@ericsson.com>
Mon, 3 Oct 2016 20:20:46 +0000 (16:20 -0400)
committerMatthew Khouzam <matthew.khouzam@ericsson.com>
Thu, 20 Oct 2016 20:08:25 +0000 (16:08 -0400)
This factory can be used to stub out native FileDialogs in TraceCompass.
This is very useful when it comes to writing SWTBot tests.

Change-Id: I2fbcaa20818b806d6367031aa85f44883d0bc08b
Signed-off-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/82398
Reviewed-by: Hudson CI
Reviewed-by: Patrick Tasse <patrick.tasse@gmail.com>
Tested-by: Patrick Tasse <patrick.tasse@gmail.com>
17 files changed:
analysis/org.eclipse.tracecompass.analysis.timing.ui/src/org/eclipse/tracecompass/internal/analysis/timing/ui/views/segmentstore/ExportToTsvAction.java
gdbtrace/org.eclipse.tracecompass.gdbtrace.ui/src/org/eclipse/tracecompass/internal/gdbtrace/ui/views/project/dialogs/SelectTraceExecutableDialog.java
lttng/org.eclipse.tracecompass.lttng2.control.ui/src/org/eclipse/tracecompass/internal/lttng2/control/ui/views/dialogs/OpenCommandScriptDialog.java
lttng/org.eclipse.tracecompass.lttng2.control.ui/src/org/eclipse/tracecompass/internal/lttng2/control/ui/views/preferences/ControlRemoteProfilesPreferencePage.java
tmf/org.eclipse.tracecompass.tmf.analysis.xml.ui/src/org/eclipse/tracecompass/internal/tmf/analysis/xml/ui/handler/ManageXMLAnalysisDialog.java
tmf/org.eclipse.tracecompass.tmf.remote.ui/src/org/eclipse/tracecompass/internal/tmf/remote/ui/wizards/fetch/preferences/RemoteProfilesPreferencePage.java
tmf/org.eclipse.tracecompass.tmf.ui/.settings/.api_filters
tmf/org.eclipse.tracecompass.tmf.ui/META-INF/MANIFEST.MF
tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/commands/ExportToTextCommandHandler.java
tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/commands/OpenFileHandler.java
tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/dialogs/ManageCustomParsersDialog.java
tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/project/wizards/importtrace/ImportTraceWizardPage.java
tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/project/wizards/tracepkg/AbstractTracePackageWizardPage.java
tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/symbols/BasicSymbolProviderPreferencePage.java
tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/dialog/TmfFileDialogFactory.java [new file with mode: 0644]
tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/views/colors/ColorsView.java
tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/views/filter/FilterView.java

index 1f5eb40b9657c8635ac2e3e3c8128111fa731d4e..21a9c90763b1b2c1fc4af04a19dfcbca4ce725e0 100644 (file)
@@ -18,6 +18,7 @@ import org.eclipse.jface.action.Action;
 import org.eclipse.swt.widgets.FileDialog;
 import org.eclipse.swt.widgets.Shell;
 import org.eclipse.tracecompass.internal.analysis.timing.ui.Activator;
+import org.eclipse.tracecompass.tmf.ui.dialog.TmfFileDialogFactory;
 
 /**
  * The export to TSV abstract action
@@ -55,7 +56,7 @@ public abstract class ExportToTsvAction extends Action {
         if (shell == null) {
             return;
         }
-        FileDialog fd = new FileDialog(shell);
+        FileDialog fd = TmfFileDialogFactory.create(shell);
         fd.setFilterExtensions(getExtension());
         String fileName = fd.open();
         if (fileName == null) {
index b298fbf8732ed3cb733278de3a2d55cc2ed037f4..a58e75193e71976706d49157be700841928a6c58 100644 (file)
@@ -42,6 +42,7 @@ import org.eclipse.swt.widgets.Listener;
 import org.eclipse.swt.widgets.Shell;
 import org.eclipse.swt.widgets.Text;
 import org.eclipse.tracecompass.internal.gdbtrace.ui.GdbTraceUIPlugin;
+import org.eclipse.tracecompass.tmf.ui.dialog.TmfFileDialogFactory;
 import org.eclipse.ui.dialogs.SelectionStatusDialog;
 
 /**
@@ -139,7 +140,7 @@ public class SelectTraceExecutableDialog extends SelectionStatusDialog {
         browseExecutableButton.addSelectionListener(new SelectionAdapter() {
             @Override
             public void widgetSelected(SelectionEvent event) {
-                FileDialog dlg = new FileDialog(shell);
+                FileDialog dlg = TmfFileDialogFactory.create(shell);
                 String workspacePath = ResourcesPlugin.getWorkspace().getRoot().getLocation().toOSString();
                 dlg.setFilterPath(workspacePath);
                 dlg.setText(Messages.SelectTraceExecutableDialog_ExecutablePrompt);
index bd648f559d0c192fae91d12583d18b691722332f..ff12778051ec08bb2217dd4fc97eeb5438349fad 100644 (file)
@@ -41,6 +41,7 @@ import org.eclipse.swt.widgets.Shell;
 import org.eclipse.tracecompass.internal.lttng2.control.ui.Activator;
 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.messages.Messages;
 import org.eclipse.tracecompass.tmf.core.io.BufferedRandomAccessFile;
+import org.eclipse.tracecompass.tmf.ui.dialog.TmfFileDialogFactory;
 
 import com.google.common.collect.ImmutableList;
 
@@ -228,7 +229,7 @@ public class OpenCommandScriptDialog extends Dialog implements ISelectCommandScr
     }
 
     private void handleFilePathBrowseButtonPressed(int fileDialogStyle) {
-        FileDialog dialog = new FileDialog(getShell(), fileDialogStyle | SWT.SHEET);
+        FileDialog dialog = TmfFileDialogFactory.create(getShell(), fileDialogStyle | SWT.SHEET);
         dialog.setFilterExtensions(new String[] { "*.*", "*.*" }); //$NON-NLS-1$ //$NON-NLS-2$
         dialog.setText(Messages.TraceControl_ExecuteScriptDialogTitle);
         String selectedFileName = dialog.open();
index 90244ee3c9871d94403543ba6fec50ceef14f2d6..7bb56ea0232c72a7113166ac6870065dfa6244aa 100644 (file)
@@ -39,6 +39,7 @@ import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.FileDialog;
 import org.eclipse.tracecompass.internal.lttng2.control.core.LttngProfileManager;
 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.messages.Messages;
+import org.eclipse.tracecompass.tmf.ui.dialog.TmfFileDialogFactory;
 import org.eclipse.ui.IWorkbench;
 import org.eclipse.ui.IWorkbenchPreferencePage;
 import org.eclipse.ui.dialogs.FilteredTree;
@@ -146,7 +147,7 @@ public class ControlRemoteProfilesPreferencePage extends PreferencePage implemen
 
             @Override
             public void widgetSelected(SelectionEvent e) {
-                FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(), SWT.OPEN);
+                FileDialog dialog = TmfFileDialogFactory.create(Display.getCurrent().getActiveShell(), SWT.OPEN);
                 dialog.setText(Messages.TraceControl_ImportProfileTitle);
                 dialog.setFilterExtensions(new String[] { "*.lttng", "*" }); //$NON-NLS-1$ //$NON-NLS-2$
                 String sourceFile = dialog.open();
index ed79990dd117562d644acab7456e26ae519fa566..280d1f7ddfb51841f2e7a829256462c9fe210e12 100644 (file)
@@ -44,6 +44,7 @@ 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;
@@ -264,7 +265,7 @@ public class ManageXMLAnalysisDialog extends Dialog {
     }
 
     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 });
@@ -306,7 +307,7 @@ public class ManageXMLAnalysisDialog extends Dialog {
     }
 
     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]);
index 4e7f7f7087140ecd2281f414a6dc12ef7259be63..c894f211cfaa2061f6d963025a11b17e1dbaddaf 100644 (file)
@@ -89,6 +89,7 @@ import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePa
 import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageTraceElement;
 import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceType;
 import org.eclipse.tracecompass.tmf.remote.core.proxy.TmfRemoteConnectionFactory;
+import org.eclipse.tracecompass.tmf.ui.dialog.TmfFileDialogFactory;
 import org.eclipse.ui.ISharedImages;
 import org.eclipse.ui.IWorkbench;
 import org.eclipse.ui.IWorkbenchCommandConstants;
@@ -585,7 +586,7 @@ public class RemoteProfilesPreferencePage extends PreferencePage implements IWor
         fImportButton.addSelectionListener(new SelectionAdapter() {
             @Override
             public void widgetSelected(SelectionEvent e) {
-                FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(), SWT.OPEN);
+                FileDialog dialog = TmfFileDialogFactory.create(Display.getCurrent().getActiveShell(), SWT.OPEN);
                 dialog.setText(RemoteMessages.RemoteProfilesPreferencePage_ImportFileDialogTitle);
                 dialog.setFilterExtensions(new String[] { "*.xml", "*" }); //$NON-NLS-1$ //$NON-NLS-2$
                 String path = dialog.open();
@@ -606,7 +607,7 @@ public class RemoteProfilesPreferencePage extends PreferencePage implements IWor
         fExportButton.addSelectionListener(new SelectionAdapter() {
             @Override
             public void widgetSelected(SelectionEvent e) {
-                FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(), SWT.SAVE);
+                FileDialog dialog = TmfFileDialogFactory.create(Display.getCurrent().getActiveShell(), SWT.SAVE);
                 dialog.setText(RemoteMessages.RemoteProfilesPreferencePage_ExportFileDialogTitle);
                 dialog.setFilterExtensions(new String[] { "*.xml", "*" }); //$NON-NLS-1$ //$NON-NLS-2$
                 String path = dialog.open();
index fd5c8a807d1da20283a1ad15a8064397397fafb5..ca530765012e2635548e181cc06b598b6688b479 100644 (file)
@@ -1,5 +1,13 @@
 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
 <component id="org.eclipse.tracecompass.tmf.ui" version="2">
+    <resource path="src/org/eclipse/tracecompass/tmf/ui/dialog/TmfFileDialogFactory.java" type="org.eclipse.tracecompass.tmf.ui.dialog.TmfFileDialogFactory">
+        <filter comment="For SWTBot, cannot extend FileDialog, which is a native dialog" id="571519004">
+            <message_arguments>
+                <message_argument value="org.eclipse.tracecompass.tmf.ui.dialog.TmfFileDialogFactory.createNewFileDialog(Shell, int, List&lt;String&gt;)"/>
+                <message_argument value="FileDialog"/>
+            </message_arguments>
+        </filter>
+    </resource>
     <resource path="src/org/eclipse/tracecompass/tmf/ui/editors/UnsortedPropertySheetPage.java" type="org.eclipse.tracecompass.tmf.ui.editors.UnsortedPropertySheetPage">
         <filter comment="See bug 109617." id="571473929">
             <message_arguments>
index 0b46129d753adb13475e849e1bcd6393d75780dd..6dfaeac829cfa0bda42a06adf1bc5fbf884a820f 100644 (file)
@@ -63,6 +63,7 @@ Export-Package: org.eclipse.tracecompass.internal.tmf.ui;x-friends:="org.eclipse
    org.eclipse.tracecompass.lttng2.kernel.ui",
  org.eclipse.tracecompass.tmf.ui,
  org.eclipse.tracecompass.tmf.ui.analysis,
+ org.eclipse.tracecompass.tmf.ui.dialog,
  org.eclipse.tracecompass.tmf.ui.editors,
  org.eclipse.tracecompass.tmf.ui.markers,
  org.eclipse.tracecompass.tmf.ui.project.model,
@@ -108,7 +109,8 @@ Export-Package: org.eclipse.tracecompass.internal.tmf.ui;x-friends:="org.eclipse
  org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model,
  org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets,
  org.eclipse.tracecompass.tmf.ui.widgets.virtualtable
-Import-Package: com.google.common.base,
+Import-Package: com.google.common.annotations,
+ com.google.common.base,
  com.google.common.cache,
  com.google.common.collect,
  org.eclipse.emf.common.util,
index f3ba8cd9839462504124e24faceaf59b29ed56e0..91b5dc7684ebf99edd33eba8f39efcbcbfc579f4 100644 (file)
@@ -25,6 +25,7 @@ import org.eclipse.swt.widgets.FileDialog;
 import org.eclipse.tracecompass.tmf.core.filter.ITmfFilter;
 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
+import org.eclipse.tracecompass.tmf.ui.dialog.TmfFileDialogFactory;
 import org.eclipse.tracecompass.tmf.ui.viewers.events.columns.TmfEventTableColumn;
 import org.eclipse.ui.PlatformUI;
 
@@ -54,7 +55,7 @@ public class ExportToTextCommandHandler extends AbstractHandler {
         ITmfTrace trace = TmfTraceManager.getInstance().getActiveTrace();
         ITmfFilter filter = TmfTraceManager.getInstance().getCurrentTraceContext().getFilter();
         if (trace != null) {
-            FileDialog fd = new FileDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.SAVE);
+            FileDialog fd = TmfFileDialogFactory.create(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.SAVE);
             fd.setFilterExtensions(new String[] { "*.csv", "*.*", "*" }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
             fd.setOverwrite(true);
             final String s = fd.open();
index eb94b9a0ae7f6e1effec9f6789a278a31a5dec4e..19477033ad3523f6f8b0e69847a2d946c4ba6085 100644 (file)
@@ -29,6 +29,7 @@ import org.eclipse.swt.widgets.Shell;
 import org.eclipse.tracecompass.internal.tmf.ui.Activator;
 import org.eclipse.tracecompass.internal.tmf.ui.ITmfUIPreferences;
 import org.eclipse.tracecompass.tmf.core.TmfCommonConstants;
+import org.eclipse.tracecompass.tmf.ui.dialog.TmfFileDialogFactory;
 import org.eclipse.tracecompass.tmf.ui.project.model.TmfOpenTraceHelper;
 import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectElement;
 import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectRegistry;
@@ -52,7 +53,7 @@ public class OpenFileHandler extends AbstractHandler {
 
         // Get trace path
         final Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
-        FileDialog fd = new FileDialog(shell);
+        FileDialog fd = TmfFileDialogFactory.create(shell);
         fd.setText(Messages.OpenFileHandler_SelectTraceFile);
         IEclipsePreferences defaultPreferences = InstanceScope.INSTANCE.getNode(Activator.PLUGIN_ID);
         String lastLocation = defaultPreferences.get(ITmfUIPreferences.PREF_SAVED_OPEN_FILE_LOCATION, null);
index d7c38d44f6c1198a727328f5e2b8518cb2f42920..2e7e837b8e83d48552472233f586b14c7f8188bd 100644 (file)
@@ -44,6 +44,7 @@ import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlTrace;
 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlTraceDefinition;
 import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceType;
 import org.eclipse.tracecompass.tmf.core.project.model.TraceTypeHelper;
+import org.eclipse.tracecompass.tmf.ui.dialog.TmfFileDialogFactory;
 
 /**
  * Dialog for custom text parsers.
@@ -243,7 +244,7 @@ public class ManageCustomParsersDialog extends Dialog {
 
             @Override
             public void widgetSelected(SelectionEvent e) {
-                FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(), SWT.OPEN);
+                FileDialog dialog = TmfFileDialogFactory.create(Display.getCurrent().getActiveShell(), SWT.OPEN);
                 dialog.setText(Messages.ManageCustomParsersDialog_ImportParserSelection);
                 dialog.setFilterExtensions(new String[] { "*.xml", "*" }); //$NON-NLS-1$ //$NON-NLS-2$
                 String path = dialog.open();
@@ -281,7 +282,7 @@ public class ManageCustomParsersDialog extends Dialog {
 
             @Override
             public void widgetSelected(SelectionEvent e) {
-                FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(), SWT.SAVE);
+                FileDialog dialog = TmfFileDialogFactory.create(Display.getCurrent().getActiveShell(), SWT.SAVE);
                 dialog.setText(NLS.bind(Messages.ManageCustomParsersDialog_ExportParserSelection, parserList.getSelection()[0]));
                 dialog.setFilterExtensions(new String[] { "*.xml", "*" }); //$NON-NLS-1$ //$NON-NLS-2$
                 String path = dialog.open();
index 00278eec886727f1c96f0ec79afff4a55e97912d..4bb30025280a62dbc5016d77a0b90d4601e028af 100644 (file)
@@ -79,6 +79,7 @@ import org.eclipse.tracecompass.tmf.core.TmfCommonConstants;
 import org.eclipse.tracecompass.tmf.core.TmfProjectNature;
 import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceType;
 import org.eclipse.tracecompass.tmf.core.util.Pair;
+import org.eclipse.tracecompass.tmf.ui.dialog.TmfFileDialogFactory;
 import org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentElement;
 import org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentFolder;
 import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectElement;
@@ -627,7 +628,7 @@ public class ImportTraceWizardPage extends WizardResourceImportPage {
      *            file extensions used to filter files shown to the user
      */
     protected void handleArchiveBrowseButtonPressed(String[] extensions) {
-        FileDialog dialog = new FileDialog(fArchiveNameField.getShell(), SWT.SHEET);
+        FileDialog dialog = TmfFileDialogFactory.create(fArchiveNameField.getShell(), SWT.SHEET);
         dialog.setFilterExtensions(extensions);
         dialog.setText(Messages.ImportTraceWizard_SelectTraceArchiveTitle);
         String fileName = fArchiveNameField.getText().trim();
index b8aa56bc3c13590fcacca14e1a4a8c4ea24863c0..55ad384d80f8a698646859aed87b52929f408ca3 100644 (file)
@@ -46,6 +46,7 @@ import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Listener;
 import org.eclipse.swt.widgets.TreeItem;
 import org.eclipse.tracecompass.internal.tmf.ui.Activator;
+import org.eclipse.tracecompass.tmf.ui.dialog.TmfFileDialogFactory;
 
 /**
  * An abstract wizard page containing common code useful for both import and
@@ -499,7 +500,7 @@ public abstract class AbstractTracePackageWizardPage extends WizardPage {
      * @param fileDialogStyle
      */
     private void handleFilePathBrowseButtonPressed(int fileDialogStyle) {
-        FileDialog dialog = new FileDialog(getContainer().getShell(), fileDialogStyle | SWT.SHEET);
+        FileDialog dialog = TmfFileDialogFactory.create(getContainer().getShell(), fileDialogStyle | SWT.SHEET);
         dialog.setFilterExtensions(new String[] { "*.zip;*.tar.gz;*.tar;*.tgz", "*.*" }); //$NON-NLS-1$ //$NON-NLS-2$
         dialog.setText(Messages.TracePackage_FileDialogTitle);
         String currentSourceString = getFilePathValue();
index 1d577290c4e5ad44a473f152bc19cbdfa89a2e78..fdd02086e1011ed926c35e11eaa4d7464f2a49e4 100644 (file)
@@ -25,6 +25,7 @@ import org.eclipse.swt.widgets.Control;
 import org.eclipse.swt.widgets.FileDialog;
 import org.eclipse.swt.widgets.Text;
 import org.eclipse.tracecompass.internal.tmf.ui.symbols.BasicSymbolProvider.SourceKind;
+import org.eclipse.tracecompass.tmf.ui.dialog.TmfFileDialogFactory;
 import org.eclipse.tracecompass.tmf.ui.symbols.AbstractSymbolProviderPreferencePage;
 
 /**
@@ -119,7 +120,7 @@ public class BasicSymbolProviderPreferencePage extends AbstractSymbolProviderPre
     }
 
     private void browseForFile(Text fileField, String dialogTitle) {
-        FileDialog fileDialog = new FileDialog(getShell(), SWT.OPEN);
+        FileDialog fileDialog = TmfFileDialogFactory.create(getShell(), SWT.OPEN);
         fileDialog.setText(dialogTitle);
         String filePath = fileDialog.open();
         if (filePath != null) {
diff --git a/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/dialog/TmfFileDialogFactory.java b/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/dialog/TmfFileDialogFactory.java
new file mode 100644 (file)
index 0000000..c13749b
--- /dev/null
@@ -0,0 +1,177 @@
+/*******************************************************************************
+ * Copyright (c) 2016 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
+ *******************************************************************************/
+
+package org.eclipse.tracecompass.tmf.ui.dialog;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.eclipse.core.runtime.Path;
+import org.eclipse.jdt.annotation.Nullable;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.SWTException;
+import org.eclipse.swt.widgets.FileDialog;
+import org.eclipse.swt.widgets.Shell;
+
+import com.google.common.annotations.VisibleForTesting;
+
+/**
+ * A file dialog factory.
+ * <p>
+ * This allows file dialogs to be stubbed out for SWTBot tests.
+ *
+ * @author Matthew Khouzam
+ * @since 2.2
+ */
+public final class TmfFileDialogFactory {
+    private static @Nullable String[] fOverridePaths = null;
+
+    /**
+     * File dialog factory, creates a {@link FileDialog}.
+     * <p>
+     * Constructs a new instance of this class given only its parent.
+     * </p>
+     * <p>
+     * If the factory is overridden with {@link #setOverrideFiles(String...)},
+     * the FileDialog will return the set String when open is called instead of
+     * opening a system window
+     * </p>
+     *
+     * @param parent
+     *            a shell which will be the parent of the new instance
+     * @return the {@link FileDialog}
+     *
+     * @exception IllegalArgumentException
+     *                <ul>
+     *                <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
+     *                </ul>
+     * @exception SWTException
+     *                <ul>
+     *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
+     *                thread that created the parent</li>
+     *                <li>ERROR_INVALID_SUBCLASS - if this class is not an
+     *                allowed subclass</li>
+     *                </ul>
+     */
+    public static FileDialog create(Shell parent) {
+        return create(parent, SWT.APPLICATION_MODAL);
+    }
+
+    /**
+     * File dialog factory, creates a {@link FileDialog}.
+     * <p>
+     * Constructs a new instance of this class given its parent and a style
+     * value describing its behavior and appearance.
+     * </p>
+     * <p>
+     * The style value is either one of the style constants defined in class
+     * <code>SWT</code> which is applicable to instances of this class, or must
+     * be built by <em>bitwise OR</em>'ing together (that is, using the
+     * <code>int</code> "|" operator) two or more of those <code>SWT</code>
+     * style constants. The class description lists the style constants that are
+     * applicable to the class. Style bits are also inherited from superclasses.
+     * </p>
+     * <p>
+     * If the factory is overridden with {@link #setOverrideFiles(String[])},
+     * the FileDialog will return the set String when open is called instead of
+     * opening a system window
+     * </p>
+     *
+     * @param parent
+     *            a shell which will be the parent of the new instance
+     * @param style
+     *            the style of dialog to construct
+     * @return the {@link FileDialog}
+     *
+     * @exception IllegalArgumentException
+     *                <ul>
+     *                <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
+     *                </ul>
+     * @exception SWTException
+     *                <ul>
+     *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
+     *                thread that created the parent</li>
+     *                <li>ERROR_INVALID_SUBCLASS - if this class is not an
+     *                allowed subclass</li>
+     *                </ul>
+     *
+     * @see SWT#SAVE
+     * @see SWT#OPEN
+     * @see SWT#MULTI
+     */
+    public static FileDialog create(Shell parent, int style) {
+        String[] overridePath = fOverridePaths;
+        if (overridePath != null) {
+            fOverridePaths = null;
+            return createNewFileDialog(parent, style, Arrays.asList(overridePath));
+        }
+        return new FileDialog(parent, style);
+    }
+
+    /**
+     * Set the override string name that will be returned for the next
+     * {@link FileDialog}. Must be called before creating the dialogs.
+     *
+     * This is a method aimed for testing, This should not be used in product
+     * code.
+     *
+     * @param paths
+     *            the paths to override the {@link FileDialog}. They must be
+     *            absolute. One or many absolute paths may be entered. When many
+     *            paths are entered, it return an input of a multi-select action
+     *            if paths is null, it will undo overriding, if paths is a zero
+     *            length array, it will behave as if the dialog was cancelled.
+     */
+    @VisibleForTesting
+    @SuppressWarnings("null")
+    public static void setOverrideFiles(String... paths) {
+        fOverridePaths = paths;
+    }
+
+    private static FileDialog createNewFileDialog(Shell parent, int style, List<String> overridePaths) {
+        return new FileDialog(parent, style) {
+            @Override
+            public String open() {
+                return !overridePaths.isEmpty() ? overridePaths.get(0) : null;
+            }
+
+            @Override
+            protected void checkSubclass() {
+                /*
+                 * do nothing, allow this class to be overridden without
+                 * throwing a runtime exception
+                 */
+            }
+
+            @Override
+            public String getFileName() {
+                return !overridePaths.isEmpty() ? getFileName(overridePaths.get(0)) : ""; //$NON-NLS-1$
+            }
+
+            @Override
+            public String[] getFileNames() {
+                List<String> outStrings = new ArrayList<>();
+                for (String entry : overridePaths) {
+                    outStrings.add(getFileName(entry));
+                }
+                return outStrings.toArray(new String[outStrings.size()]);
+            }
+
+            @Override
+            public String getFilterPath() {
+                return !overridePaths.isEmpty() ? new Path(overridePaths.get(0)).removeLastSegments(1).toString() : ""; //$NON-NLS-1$
+            }
+
+            private String getFileName(String path) {
+                return new Path(path).lastSegment();
+            }
+        };
+    }
+}
index 43ef060972a6ee14830f7b90a26c3c0ce936ccb5..6ec9935a5fd68688db2fcbac4a2251411297fcf1 100644 (file)
@@ -55,6 +55,7 @@ import org.eclipse.swt.widgets.Shell;
 import org.eclipse.tracecompass.internal.tmf.ui.Activator;
 import org.eclipse.tracecompass.internal.tmf.ui.Messages;
 import org.eclipse.tracecompass.tmf.core.filter.ITmfFilter;
+import org.eclipse.tracecompass.tmf.ui.dialog.TmfFileDialogFactory;
 import org.eclipse.tracecompass.tmf.ui.views.TmfView;
 import org.eclipse.tracecompass.tmf.ui.views.filter.FilterDialog;
 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.TimeGraphColorScheme;
@@ -346,7 +347,7 @@ public class ColorsView extends TmfView {
     private class ExportAction extends Action {
         @Override
         public void run() {
-            FileDialog fileDialog = new FileDialog(fShell, SWT.SAVE);
+            FileDialog fileDialog = TmfFileDialogFactory.create(fShell, SWT.SAVE);
             fileDialog.setFilterExtensions(new String[] {"*.xml"}); //$NON-NLS-1$
             fileDialog.setOverwrite(true);
             String pathName = fileDialog.open();
@@ -359,7 +360,7 @@ public class ColorsView extends TmfView {
     private class ImportAction extends Action {
         @Override
         public void run() {
-            FileDialog fileDialog = new FileDialog(fShell, SWT.OPEN);
+            FileDialog fileDialog = TmfFileDialogFactory.create(fShell, SWT.OPEN);
             fileDialog.setFilterExtensions(new String[] {"*.xml"}); //$NON-NLS-1$
             String pathName = fileDialog.open();
             if (pathName != null) {
index ba6add52595532dbc213a41fa0f5a5066495b183..6a0b872bb352979df6eaa15e7a3136d60ef7c713 100644 (file)
@@ -43,6 +43,7 @@ import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterNode;
 import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterRootNode;
 import org.eclipse.tracecompass.tmf.core.filter.xml.TmfFilterXMLParser;
 import org.eclipse.tracecompass.tmf.core.filter.xml.TmfFilterXMLWriter;
+import org.eclipse.tracecompass.tmf.ui.dialog.TmfFileDialogFactory;
 import org.eclipse.tracecompass.tmf.ui.views.TmfView;
 import org.eclipse.ui.IActionBars;
 import org.xml.sax.SAXException;
@@ -266,7 +267,7 @@ public class FilterView extends TmfView {
         @Override
         public void run() {
             try {
-                FileDialog dlg = new FileDialog(new Shell(), SWT.SAVE);
+                FileDialog dlg = TmfFileDialogFactory.create(new Shell(), SWT.SAVE);
                 dlg.setFilterNames(new String[] { Messages.FilterView_FileDialogFilterName + " (*.xml)" }); //$NON-NLS-1$
                 dlg.setFilterExtensions(new String[] { "*.xml" }); //$NON-NLS-1$
 
@@ -288,7 +289,7 @@ public class FilterView extends TmfView {
             if (fViewer != null) {
                 ITmfFilterTreeNode root = null;
                 try {
-                    FileDialog dlg = new FileDialog(new Shell(), SWT.OPEN);
+                    FileDialog dlg = TmfFileDialogFactory.create(new Shell(), SWT.OPEN);
                     dlg.setFilterNames(new String[] { Messages.FilterView_FileDialogFilterName + " (*.xml)" }); //$NON-NLS-1$
                     dlg.setFilterExtensions(new String[] { "*.xml" }); //$NON-NLS-1$
 
This page took 0.035324 seconds and 5 git commands to generate.