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 @SuppressWarnings("unused")
94 private TDirectoryList directoryList
;
97 * The top row text field.
99 private TField entryField
;
102 * The Open or Save button.
104 private TButton openButton
;
107 * Update the fields in response to other field updates.
109 * @param enter if true, the user manually entered a filename
111 @SuppressWarnings("unused")
112 private void onUpdate(boolean enter
) throws IOException
{
113 String newFilename
= entryField
.getText();
114 File newFile
= new File(newFilename
);
115 if (newFile
.exists()) {
117 if (newFile
.isFile()) {
118 filename
= entryField
.getText();
119 getApplication().closeWindow(this);
121 if (newFile
.isDirectory()) {
122 treeViewRoot
= new TDirectoryTreeItem(treeView
,
123 entryField
.getText(), true);
124 treeView
.setTreeRoot(treeViewRoot
, true);
127 openButton
.setEnabled(false);
129 if (newFile
.isFile()) {
130 openButton
.setEnabled(true);
132 openButton
.setEnabled(false);
136 openButton
.setEnabled(false);
141 * Public constructor. The file open box will be centered on screen.
143 * @param application the TApplication that manages this window
144 * @param path path of selected file
145 * @param type one of the Type constants
147 public TFileOpenBox(final TApplication application
, final String path
,
148 final Type type
) throws IOException
{
150 // Register with the TApplication
151 super(application
, "", 0, 0, 76, 22, MODAL
);
154 entryField
= addField(1, 1, getWidth() - 4, false,
155 (new File(path
)).getCanonicalPath(),
160 // Add directory treeView
161 treeView
= addTreeView(1, 3, 30, getHeight() - 6,
166 treeViewRoot
= new TDirectoryTreeItem(treeView
, path
, true);
168 // Add directory files list
169 directoryList
= addDirectoryList(path
, 34, 3, 28, getHeight() - 6,
175 String openLabel
= "";
178 openLabel
= " &Open ";
179 setTitle("Open File...");
182 openLabel
= " &Save ";
183 setTitle("Save File...");
186 throw new IllegalArgumentException("Invalid type: " + type
);
189 // Setup button actions
190 openButton
= addButton(openLabel
, this.getWidth() - 12, 3,
195 openButton
.setEnabled(false);
197 addButton("&Cancel", getWidth() - 12, 5,
201 getApplication().closeWindow(TFileOpenBox
.this);
206 // Set the secondaryFiber to run me
207 getApplication().enableSecondaryEventReceiver(this);
209 // Yield to the secondary thread. When I come back from the
210 // constructor response will already be set.
211 getApplication().yield();
220 getScreen().vLineXY(33, 4, getHeight() - 6, GraphicsChars
.WINDOW_SIDE
,
227 * @param keypress keystroke event
230 public void onKeypress(final TKeypressEvent keypress
) {
231 // Escape - behave like cancel
232 if (keypress
.equals(kbEsc
)) {
235 getApplication().closeWindow(this);
240 super.onKeypress(keypress
);