tmf.ui: Introduce TmfFileDialogFactory
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / dialog / TmfFileDialogFactory.java
1 /*******************************************************************************
2 * Copyright (c) 2016 Ericsson
3 *
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 *******************************************************************************/
9
10 package org.eclipse.tracecompass.tmf.ui.dialog;
11
12 import java.util.ArrayList;
13 import java.util.Arrays;
14 import java.util.List;
15
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;
22
23 import com.google.common.annotations.VisibleForTesting;
24
25 /**
26 * A file dialog factory.
27 * <p>
28 * This allows file dialogs to be stubbed out for SWTBot tests.
29 *
30 * @author Matthew Khouzam
31 * @since 2.2
32 */
33 public final class TmfFileDialogFactory {
34 private static @Nullable String[] fOverridePaths = null;
35
36 /**
37 * File dialog factory, creates a {@link FileDialog}.
38 * <p>
39 * Constructs a new instance of this class given only its parent.
40 * </p>
41 * <p>
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
45 * </p>
46 *
47 * @param parent
48 * a shell which will be the parent of the new instance
49 * @return the {@link FileDialog}
50 *
51 * @exception IllegalArgumentException
52 * <ul>
53 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
54 * </ul>
55 * @exception SWTException
56 * <ul>
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>
61 * </ul>
62 */
63 public static FileDialog create(Shell parent) {
64 return create(parent, SWT.APPLICATION_MODAL);
65 }
66
67 /**
68 * File dialog factory, creates a {@link FileDialog}.
69 * <p>
70 * Constructs a new instance of this class given its parent and a style
71 * value describing its behavior and appearance.
72 * </p>
73 * <p>
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.
80 * </p>
81 * <p>
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
85 * </p>
86 *
87 * @param parent
88 * a shell which will be the parent of the new instance
89 * @param style
90 * the style of dialog to construct
91 * @return the {@link FileDialog}
92 *
93 * @exception IllegalArgumentException
94 * <ul>
95 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
96 * </ul>
97 * @exception SWTException
98 * <ul>
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>
103 * </ul>
104 *
105 * @see SWT#SAVE
106 * @see SWT#OPEN
107 * @see SWT#MULTI
108 */
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));
114 }
115 return new FileDialog(parent, style);
116 }
117
118 /**
119 * Set the override string name that will be returned for the next
120 * {@link FileDialog}. Must be called before creating the dialogs.
121 *
122 * This is a method aimed for testing, This should not be used in product
123 * code.
124 *
125 * @param paths
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.
131 */
132 @VisibleForTesting
133 @SuppressWarnings("null")
134 public static void setOverrideFiles(String... paths) {
135 fOverridePaths = paths;
136 }
137
138 private static FileDialog createNewFileDialog(Shell parent, int style, List<String> overridePaths) {
139 return new FileDialog(parent, style) {
140 @Override
141 public String open() {
142 return !overridePaths.isEmpty() ? overridePaths.get(0) : null;
143 }
144
145 @Override
146 protected void checkSubclass() {
147 /*
148 * do nothing, allow this class to be overridden without
149 * throwing a runtime exception
150 */
151 }
152
153 @Override
154 public String getFileName() {
155 return !overridePaths.isEmpty() ? getFileName(overridePaths.get(0)) : ""; //$NON-NLS-1$
156 }
157
158 @Override
159 public String[] getFileNames() {
160 List<String> outStrings = new ArrayList<>();
161 for (String entry : overridePaths) {
162 outStrings.add(getFileName(entry));
163 }
164 return outStrings.toArray(new String[outStrings.size()]);
165 }
166
167 @Override
168 public String getFilterPath() {
169 return !overridePaths.isEmpty() ? new Path(overridePaths.get(0)).removeLastSegments(1).toString() : ""; //$NON-NLS-1$
170 }
171
172 private String getFileName(String path) {
173 return new Path(path).lastSegment();
174 }
175 };
176 }
177 }
This page took 0.034186 seconds and 5 git commands to generate.