1 /*******************************************************************************
2 * Copyright (c) 2016 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.tmf
.ui
.dialog
;
12 import java
.util
.ArrayList
;
13 import java
.util
.Arrays
;
14 import java
.util
.List
;
16 import org
.eclipse
.core
.runtime
.Path
;
17 import org
.eclipse
.jdt
.annotation
.Nullable
;
18 import org
.eclipse
.swt
.SWT
;
19 import org
.eclipse
.swt
.SWTException
;
20 import org
.eclipse
.swt
.widgets
.FileDialog
;
21 import org
.eclipse
.swt
.widgets
.Shell
;
23 import com
.google
.common
.annotations
.VisibleForTesting
;
26 * A file dialog factory.
28 * This allows file dialogs to be stubbed out for SWTBot tests.
30 * @author Matthew Khouzam
33 public final class TmfFileDialogFactory
{
34 private static @Nullable String
[] fOverridePaths
= null;
37 * File dialog factory, creates a {@link FileDialog}.
39 * Constructs a new instance of this class given only its parent.
42 * If the factory is overridden with {@link #setOverrideFiles(String...)},
43 * the FileDialog will return the set String when open is called instead of
44 * opening a system window
48 * a shell which will be the parent of the new instance
49 * @return the {@link FileDialog}
51 * @exception IllegalArgumentException
53 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
55 * @exception SWTException
57 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
58 * thread that created the parent</li>
59 * <li>ERROR_INVALID_SUBCLASS - if this class is not an
60 * allowed subclass</li>
63 public static FileDialog
create(Shell parent
) {
64 return create(parent
, SWT
.APPLICATION_MODAL
);
68 * File dialog factory, creates a {@link FileDialog}.
70 * Constructs a new instance of this class given its parent and a style
71 * value describing its behavior and appearance.
74 * The style value is either one of the style constants defined in class
75 * <code>SWT</code> which is applicable to instances of this class, or must
76 * be built by <em>bitwise OR</em>'ing together (that is, using the
77 * <code>int</code> "|" operator) two or more of those <code>SWT</code>
78 * style constants. The class description lists the style constants that are
79 * applicable to the class. Style bits are also inherited from superclasses.
82 * If the factory is overridden with {@link #setOverrideFiles(String[])},
83 * the FileDialog will return the set String when open is called instead of
84 * opening a system window
88 * a shell which will be the parent of the new instance
90 * the style of dialog to construct
91 * @return the {@link FileDialog}
93 * @exception IllegalArgumentException
95 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
97 * @exception SWTException
99 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
100 * thread that created the parent</li>
101 * <li>ERROR_INVALID_SUBCLASS - if this class is not an
102 * allowed subclass</li>
109 public static FileDialog
create(Shell parent
, int style
) {
110 String
[] overridePath
= fOverridePaths
;
111 if (overridePath
!= null) {
112 fOverridePaths
= null;
113 return createNewFileDialog(parent
, style
, Arrays
.asList(overridePath
));
115 return new FileDialog(parent
, style
);
119 * Set the override string name that will be returned for the next
120 * {@link FileDialog}. Must be called before creating the dialogs.
122 * This is a method aimed for testing, This should not be used in product
126 * the paths to override the {@link FileDialog}. They must be
127 * absolute. One or many absolute paths may be entered. When many
128 * paths are entered, it return an input of a multi-select action
129 * if paths is null, it will undo overriding, if paths is a zero
130 * length array, it will behave as if the dialog was cancelled.
133 @SuppressWarnings("null")
134 public static void setOverrideFiles(String
... paths
) {
135 fOverridePaths
= paths
;
138 private static FileDialog
createNewFileDialog(Shell parent
, int style
, List
<String
> overridePaths
) {
139 return new FileDialog(parent
, style
) {
141 public String
open() {
142 return !overridePaths
.isEmpty() ? overridePaths
.get(0) : null;
146 protected void checkSubclass() {
148 * do nothing, allow this class to be overridden without
149 * throwing a runtime exception
154 public String
getFileName() {
155 return !overridePaths
.isEmpty() ?
getFileName(overridePaths
.get(0)) : ""; //$NON-NLS-1$
159 public String
[] getFileNames() {
160 List
<String
> outStrings
= new ArrayList
<>();
161 for (String entry
: overridePaths
) {
162 outStrings
.add(getFileName(entry
));
164 return outStrings
.toArray(new String
[outStrings
.size()]);
168 public String
getFilterPath() {
169 return !overridePaths
.isEmpty() ?
new Path(overridePaths
.get(0)).removeLastSegments(1).toString() : ""; //$NON-NLS-1$
172 private String
getFileName(String path
) {
173 return new Path(path
).lastSegment();
This page took 0.034186 seconds and 5 git commands to generate.