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
48 * filename = application.fileOpenBox("/path/to/file.ext",
49 * TFileOpenBox.Type.OPEN);
50 * if (filename != null) {
51 * ... the user selected a file, go open it ...
57 public class TFileOpenBox
extends TWindow
{
62 private static final ResourceBundle i18n
= ResourceBundle
.getBundle(TFileOpenBox
.class.getName());
64 // ------------------------------------------------------------------------
65 // Constants --------------------------------------------------------------
66 // ------------------------------------------------------------------------
69 * TFileOpenBox can be called for either Open or Save actions.
73 * Button will be labeled "Open".
78 * Button will be labeled "Save".
83 // ------------------------------------------------------------------------
84 // Variables --------------------------------------------------------------
85 // ------------------------------------------------------------------------
88 * String to return, or null if the user canceled.
90 private String filename
= null;
93 * The left-side tree view pane.
95 private TTreeViewWidget treeView
;
98 * The data behind treeView.
100 private TDirectoryTreeItem treeViewRoot
;
103 * The right-side directory list pane.
105 private TDirectoryList directoryList
;
108 * The top row text field.
110 private TField entryField
;
113 * The Open or Save button.
115 private TButton openButton
;
117 // ------------------------------------------------------------------------
118 // Constructors -----------------------------------------------------------
119 // ------------------------------------------------------------------------
122 * Public constructor. The file open box will be centered on screen.
124 * @param application the TApplication that manages this window
125 * @param path path of selected file
126 * @param type one of the Type constants
127 * @throws IOException of a java.io operation throws
129 public TFileOpenBox(final TApplication application
, final String path
,
130 final Type type
) throws IOException
{
132 // Register with the TApplication
133 super(application
, "", 0, 0, 76, 22, MODAL
);
136 entryField
= addField(1, 1, getWidth() - 4, false,
137 (new File(path
)).getCanonicalPath(),
141 checkFilename(entryField
.getText());
142 } catch (IOException e
) {
147 entryField
.onKeypress(new TKeypressEvent(kbEnd
));
149 // Add directory treeView
150 treeView
= addTreeViewWidget(1, 3, 30, getHeight() - 6,
153 TTreeItem item
= treeView
.getSelected();
154 File selectedDir
= ((TDirectoryTreeItem
) item
).getFile();
156 directoryList
.setPath(selectedDir
.getCanonicalPath());
157 openButton
.setEnabled(false);
159 } catch (IOException e
) {
165 treeViewRoot
= new TDirectoryTreeItem(treeView
, path
, true);
167 // Add directory files list
168 directoryList
= addDirectoryList(path
, 34, 3, 28, getHeight() - 6,
172 File newPath
= directoryList
.getPath();
173 entryField
.setText(newPath
.getCanonicalPath());
174 entryField
.onKeypress(new TKeypressEvent(kbEnd
));
175 openButton
.setEnabled(true);
176 activate(entryField
);
177 } catch (IOException e
) {
184 String openLabel
= "";
187 openLabel
= i18n
.getString("openButton");
188 setTitle(i18n
.getString("openTitle"));
191 openLabel
= i18n
.getString("saveButton");
192 setTitle(i18n
.getString("saveTitle"));
195 throw new IllegalArgumentException("Invalid type: " + type
);
198 // Setup button actions
199 openButton
= addButton(openLabel
, this.getWidth() - 12, 3,
203 checkFilename(entryField
.getText());
204 } catch (IOException e
) {
210 openButton
.setEnabled(false);
212 addButton(i18n
.getString("cancelButton"), getWidth() - 12, 5,
216 getApplication().closeWindow(TFileOpenBox
.this);
221 // Default to the directory list
222 activate(directoryList
);
224 // Set the secondaryFiber to run me
225 getApplication().enableSecondaryEventReceiver(this);
227 // Yield to the secondary thread. When I come back from the
228 // constructor response will already be set.
229 getApplication().yield();
232 // ------------------------------------------------------------------------
233 // Event handlers ---------------------------------------------------------
234 // ------------------------------------------------------------------------
239 * @param keypress keystroke event
242 public void onKeypress(final TKeypressEvent keypress
) {
243 // Escape - behave like cancel
244 if (keypress
.equals(kbEsc
)) {
247 getApplication().closeWindow(this);
251 if (treeView
.isActive()) {
252 if ((keypress
.equals(kbEnter
))
253 || (keypress
.equals(kbUp
))
254 || (keypress
.equals(kbDown
))
255 || (keypress
.equals(kbPgUp
))
256 || (keypress
.equals(kbPgDn
))
257 || (keypress
.equals(kbHome
))
258 || (keypress
.equals(kbEnd
))
260 // Tree view will be changing, update the directory list.
261 super.onKeypress(keypress
);
263 // This is the same action as treeView's enter.
264 TTreeItem item
= treeView
.getSelected();
265 File selectedDir
= ((TDirectoryTreeItem
) item
).getFile();
267 directoryList
.setPath(selectedDir
.getCanonicalPath());
268 openButton
.setEnabled(false);
270 } catch (IOException e
) {
278 super.onKeypress(keypress
);
281 // ------------------------------------------------------------------------
282 // TWidget ----------------------------------------------------------------
283 // ------------------------------------------------------------------------
291 getScreen().vLineXY(33, 4, getHeight() - 6, GraphicsChars
.WINDOW_SIDE
,
295 // ------------------------------------------------------------------------
296 // TFileOpenBox -----------------------------------------------------------
297 // ------------------------------------------------------------------------
300 * Get the return string.
302 * @return the filename the user selected, or null if they canceled.
304 public String
getFilename() {
309 * See if there is a valid filename to return. If the filename is a
312 * @param newFilename the filename to check and return
313 * @throws IOException of a java.io operation throws
315 private void checkFilename(final String newFilename
) throws IOException
{
316 File newFile
= new File(newFilename
);
317 if (newFile
.exists()) {
318 if (newFile
.isFile()) {
319 filename
= newFilename
;
320 getApplication().closeWindow(this);
323 if (newFile
.isDirectory()) {
324 treeViewRoot
= new TDirectoryTreeItem(treeView
,
326 treeView
.setTreeRoot(treeViewRoot
, true);
327 openButton
.setEnabled(false);
328 directoryList
.setPath(newFilename
);