2 * Jexer - Java Text User Interface
4 * License: LGPLv3 or later
6 * This module is licensed under the GNU Lesser General Public License
7 * Version 3. Please see the file "COPYING" in this directory for more
8 * information about the GNU Lesser General Public License Version 3.
10 * Copyright (C) 2015 Kevin Lamonte
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 3 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this program; if not, see
24 * http://www.gnu.org/licenses/, or write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
34 import java
.io
.IOException
;
36 import jexer
.bits
.GraphicsChars
;
37 import jexer
.event
.TKeypressEvent
;
38 import static jexer
.TKeypress
.*;
41 * TFileOpenBox is a system-modal dialog for selecting a file to open. Call
47 * filename = application.fileOpenBox("/path/to/file.ext",
48 * TFileOpenBox.Type.OPEN);
49 * if (filename != null) {
50 * ... the user selected a file, go open it ...
56 public final class TFileOpenBox
extends TWindow
{
59 * TFileOpenBox can be called for either Open or Save actions.
67 * String to return, or null if the user canceled.
69 private String filename
= null;
72 * Get the return string.
74 * @return the filename the user selected, or null if they canceled.
76 public String
getFilename() {
81 * The left-side tree view pane.
83 private TTreeView treeView
;
86 * The data behind treeView.
88 private TDirectoryTreeItem treeViewRoot
;
91 * The right-side directory list pane.
93 private TDirectoryList directoryList
;
96 * The top row text field.
98 private TField entryField
;
101 * The Open or Save button.
103 private TButton openButton
;
106 * See if there is a valid filename to return. If the filename is a
109 * @param newFilename the filename to check and return
111 private void checkFilename(final String newFilename
) throws IOException
{
112 File newFile
= new File(newFilename
);
113 if (newFile
.exists()) {
114 if (newFile
.isFile()) {
115 filename
= newFilename
;
116 getApplication().closeWindow(this);
119 if (newFile
.isDirectory()) {
120 treeViewRoot
= new TDirectoryTreeItem(treeView
, newFilename
,
122 treeView
.setTreeRoot(treeViewRoot
, true);
124 openButton
.setEnabled(false);
125 directoryList
.setPath(newFilename
);
131 * Public constructor. The file open box will be centered on screen.
133 * @param application the TApplication that manages this window
134 * @param path path of selected file
135 * @param type one of the Type constants
137 public TFileOpenBox(final TApplication application
, final String path
,
138 final Type type
) throws IOException
{
140 // Register with the TApplication
141 super(application
, "", 0, 0, 76, 22, MODAL
);
144 entryField
= addField(1, 1, getWidth() - 4, false,
145 (new File(path
)).getCanonicalPath(),
149 checkFilename(entryField
.getText());
150 } catch (IOException e
) {
155 entryField
.onKeypress(new TKeypressEvent(kbEnd
));
157 // Add directory treeView
158 treeView
= addTreeView(1, 3, 30, getHeight() - 6,
161 TTreeItem item
= treeView
.getSelected();
162 File selectedDir
= ((TDirectoryTreeItem
) item
).getFile();
164 directoryList
.setPath(selectedDir
.getCanonicalPath());
165 openButton
.setEnabled(false);
166 activate(directoryList
);
167 } catch (IOException e
) {
173 treeViewRoot
= new TDirectoryTreeItem(treeView
, path
, true);
175 // Add directory files list
176 directoryList
= addDirectoryList(path
, 34, 3, 28, getHeight() - 6,
180 File newPath
= directoryList
.getPath();
181 entryField
.setText(newPath
.getCanonicalPath());
182 entryField
.onKeypress(new TKeypressEvent(kbEnd
));
183 openButton
.setEnabled(true);
184 activate(entryField
);
185 } catch (IOException e
) {
192 String openLabel
= "";
195 openLabel
= " &Open ";
196 setTitle("Open File...");
199 openLabel
= " &Save ";
200 setTitle("Save File...");
203 throw new IllegalArgumentException("Invalid type: " + type
);
206 // Setup button actions
207 openButton
= addButton(openLabel
, this.getWidth() - 12, 3,
211 checkFilename(entryField
.getText());
212 } catch (IOException e
) {
218 openButton
.setEnabled(false);
220 addButton("&Cancel", getWidth() - 12, 5,
224 getApplication().closeWindow(TFileOpenBox
.this);
229 // Default to the directory list
230 activate(directoryList
);
232 // Set the secondaryFiber to run me
233 getApplication().enableSecondaryEventReceiver(this);
235 // Yield to the secondary thread. When I come back from the
236 // constructor response will already be set.
237 getApplication().yield();
246 getScreen().vLineXY(33, 4, getHeight() - 6, GraphicsChars
.WINDOW_SIDE
,
253 * @param keypress keystroke event
256 public void onKeypress(final TKeypressEvent keypress
) {
257 // Escape - behave like cancel
258 if (keypress
.equals(kbEsc
)) {
261 getApplication().closeWindow(this);
266 super.onKeypress(keypress
);