2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2017 Kevin Lamonte
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
32 import java
.io
.IOException
;
33 import java
.util
.ResourceBundle
;
35 import jexer
.bits
.GraphicsChars
;
36 import jexer
.event
.TKeypressEvent
;
37 import jexer
.ttree
.TDirectoryTreeItem
;
38 import jexer
.ttree
.TTreeItem
;
39 import jexer
.ttree
.TTreeViewWidget
;
40 import static jexer
.TKeypress
.*;
43 * TFileOpenBox is a system-modal dialog for selecting a file to open. Call
49 * filename = application.fileOpenBox("/path/to/file.ext",
50 * TFileOpenBox.Type.OPEN);
51 * if (filename != null) {
52 * ... the user selected a file, go open it ...
58 public final class TFileOpenBox
extends TWindow
{
63 private static final ResourceBundle i18n
= ResourceBundle
.getBundle(TFileOpenBox
.class.getName());
65 // ------------------------------------------------------------------------
66 // Constants --------------------------------------------------------------
67 // ------------------------------------------------------------------------
70 * TFileOpenBox can be called for either Open or Save actions.
74 * Button will be labeled "Open".
79 * Button will be labeled "Save".
84 // ------------------------------------------------------------------------
85 // Variables --------------------------------------------------------------
86 // ------------------------------------------------------------------------
89 * String to return, or null if the user canceled.
91 private String filename
= null;
94 * The left-side tree view pane.
96 private TTreeViewWidget treeView
;
99 * The data behind treeView.
101 private TDirectoryTreeItem treeViewRoot
;
104 * The right-side directory list pane.
106 private TDirectoryList directoryList
;
109 * The top row text field.
111 private TField entryField
;
114 * The Open or Save button.
116 private TButton openButton
;
118 // ------------------------------------------------------------------------
119 // Constructors -----------------------------------------------------------
120 // ------------------------------------------------------------------------
123 * Public constructor. The file open box will be centered on screen.
125 * @param application the TApplication that manages this window
126 * @param path path of selected file
127 * @param type one of the Type constants
128 * @throws IOException of a java.io operation throws
130 public TFileOpenBox(final TApplication application
, final String path
,
131 final Type type
) throws IOException
{
133 // Register with the TApplication
134 super(application
, "", 0, 0, 76, 22, MODAL
);
137 entryField
= addField(1, 1, getWidth() - 4, false,
138 (new File(path
)).getCanonicalPath(),
142 checkFilename(entryField
.getText());
143 } catch (IOException e
) {
148 entryField
.onKeypress(new TKeypressEvent(kbEnd
));
150 // Add directory treeView
151 treeView
= addTreeViewWidget(1, 3, 30, getHeight() - 6,
154 TTreeItem item
= treeView
.getSelected();
155 File selectedDir
= ((TDirectoryTreeItem
) item
).getFile();
157 directoryList
.setPath(selectedDir
.getCanonicalPath());
158 openButton
.setEnabled(false);
160 } catch (IOException e
) {
166 treeViewRoot
= new TDirectoryTreeItem(treeView
, path
, true);
168 // Add directory files list
169 directoryList
= addDirectoryList(path
, 34, 3, 28, getHeight() - 6,
173 File newPath
= directoryList
.getPath();
174 entryField
.setText(newPath
.getCanonicalPath());
175 entryField
.onKeypress(new TKeypressEvent(kbEnd
));
176 openButton
.setEnabled(true);
177 activate(entryField
);
178 } catch (IOException e
) {
185 String openLabel
= "";
188 openLabel
= i18n
.getString("openButton");
189 setTitle(i18n
.getString("openTitle"));
192 openLabel
= i18n
.getString("saveButton");
193 setTitle(i18n
.getString("saveTitle"));
196 throw new IllegalArgumentException("Invalid type: " + type
);
199 // Setup button actions
200 openButton
= addButton(openLabel
, this.getWidth() - 12, 3,
204 checkFilename(entryField
.getText());
205 } catch (IOException e
) {
211 openButton
.setEnabled(false);
213 addButton(i18n
.getString("cancelButton"), getWidth() - 12, 5,
217 getApplication().closeWindow(TFileOpenBox
.this);
222 // Default to the directory list
223 activate(directoryList
);
225 // Set the secondaryFiber to run me
226 getApplication().enableSecondaryEventReceiver(this);
228 // Yield to the secondary thread. When I come back from the
229 // constructor response will already be set.
230 getApplication().yield();
233 // ------------------------------------------------------------------------
234 // Event handlers ---------------------------------------------------------
235 // ------------------------------------------------------------------------
240 * @param keypress keystroke event
243 public void onKeypress(final TKeypressEvent keypress
) {
244 // Escape - behave like cancel
245 if (keypress
.equals(kbEsc
)) {
248 getApplication().closeWindow(this);
252 if (treeView
.isActive()) {
253 if ((keypress
.equals(kbEnter
))
254 || (keypress
.equals(kbUp
))
255 || (keypress
.equals(kbDown
))
256 || (keypress
.equals(kbPgUp
))
257 || (keypress
.equals(kbPgDn
))
258 || (keypress
.equals(kbHome
))
259 || (keypress
.equals(kbEnd
))
261 // Tree view will be changing, update the directory list.
262 super.onKeypress(keypress
);
264 // This is the same action as treeView's enter.
265 TTreeItem item
= treeView
.getSelected();
266 File selectedDir
= ((TDirectoryTreeItem
) item
).getFile();
268 directoryList
.setPath(selectedDir
.getCanonicalPath());
269 openButton
.setEnabled(false);
271 } catch (IOException e
) {
279 super.onKeypress(keypress
);
282 // ------------------------------------------------------------------------
283 // TWidget ----------------------------------------------------------------
284 // ------------------------------------------------------------------------
292 getScreen().vLineXY(33, 4, getHeight() - 6, GraphicsChars
.WINDOW_SIDE
,
296 // ------------------------------------------------------------------------
297 // TFileOpenBox -----------------------------------------------------------
298 // ------------------------------------------------------------------------
301 * Get the return string.
303 * @return the filename the user selected, or null if they canceled.
305 public String
getFilename() {
310 * See if there is a valid filename to return. If the filename is a
313 * @param newFilename the filename to check and return
314 * @throws IOException of a java.io operation throws
316 private void checkFilename(final String newFilename
) throws IOException
{
317 File newFile
= new File(newFilename
);
318 if (newFile
.exists()) {
319 if (newFile
.isFile()) {
320 filename
= newFilename
;
321 getApplication().closeWindow(this);
324 if (newFile
.isDirectory()) {
325 treeViewRoot
= new TDirectoryTreeItem(treeView
,
327 treeView
.setTreeRoot(treeViewRoot
, true);
328 openButton
.setEnabled(false);
329 directoryList
.setPath(newFilename
);