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>
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
if (shell == null) {
return;
}
- FileDialog fd = new FileDialog(shell);
+ FileDialog fd = TmfFileDialogFactory.create(shell);
fd.setFilterExtensions(getExtension());
String fileName = fd.open();
if (fileName == null) {
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;
/**
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);
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;
}
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();
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;
@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();
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;
}
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 });
}
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]);
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;
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();
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();
<?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<String>)"/>
+ <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>
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,
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,
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;
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();
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;
// 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);
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.
@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();
@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();
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;
* 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();
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
* @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();
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;
/**
}
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) {
--- /dev/null
+/*******************************************************************************
+ * 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();
+ }
+ };
+ }
+}
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;
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();
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) {
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;
@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$
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$