1 package com
.googlecode
.lanterna
.gui2
.dialogs
;
3 import com
.googlecode
.lanterna
.TerminalTextUtils
;
4 import com
.googlecode
.lanterna
.TerminalSize
;
5 import com
.googlecode
.lanterna
.gui2
.*;
8 import java
.util
.Arrays
;
9 import java
.util
.Comparator
;
12 * Dialog that allows the user to iterate the file system and pick file to open/save
16 public class FileDialog
extends DialogWindow
{
18 private final ActionListBox fileListBox
;
19 private final ActionListBox directoryListBox
;
20 private final TextBox fileBox
;
21 private final Button okButton
;
22 private final boolean showHiddenFilesAndDirs
;
24 private File directory
;
25 private File selectedFile
;
28 * Default constructor for {@code FileDialog}
29 * @param title Title of the dialog
30 * @param description Description of the dialog, is displayed at the top of the content area
31 * @param actionLabel Label to use on the "confirm" button, for example "open" or "save"
32 * @param dialogSize Rough estimation of how big you want the dialog to be
33 * @param showHiddenFilesAndDirs If {@code true}, hidden files and directories will be visible
34 * @param selectedObject Initially selected file node
40 TerminalSize dialogSize
,
41 boolean showHiddenFilesAndDirs
,
42 File selectedObject
) {
44 this.selectedFile
= null;
45 this.showHiddenFilesAndDirs
= showHiddenFilesAndDirs
;
47 if(selectedObject
== null || !selectedObject
.exists()) {
48 selectedObject
= new File("").getAbsoluteFile();
50 selectedObject
= selectedObject
.getAbsoluteFile();
52 Panel contentPane
= new Panel();
53 contentPane
.setLayoutManager(new GridLayout(2));
55 if(description
!= null) {
56 new Label(description
)
58 GridLayout
.createLayoutData(
59 GridLayout
.Alignment
.BEGINNING
,
60 GridLayout
.Alignment
.CENTER
,
68 int unitWidth
= dialogSize
.getColumns() / 3;
69 int unitHeight
= dialogSize
.getRows();
71 new FileSystemLocationLabel()
72 .setLayoutData(GridLayout
.createLayoutData(
73 GridLayout
.Alignment
.FILL
,
74 GridLayout
.Alignment
.CENTER
,
81 fileListBox
= new ActionListBox(new TerminalSize(unitWidth
* 2, unitHeight
));
82 fileListBox
.withBorder(Borders
.singleLine())
83 .setLayoutData(GridLayout
.createLayoutData(
84 GridLayout
.Alignment
.BEGINNING
,
85 GridLayout
.Alignment
.CENTER
,
89 directoryListBox
= new ActionListBox(new TerminalSize(unitWidth
, unitHeight
));
90 directoryListBox
.withBorder(Borders
.singleLine())
93 fileBox
= new TextBox()
94 .setLayoutData(GridLayout
.createLayoutData(
95 GridLayout
.Alignment
.FILL
,
96 GridLayout
.Alignment
.CENTER
,
103 new Separator(Direction
.HORIZONTAL
)
105 GridLayout
.createLayoutData(
106 GridLayout
.Alignment
.FILL
,
107 GridLayout
.Alignment
.CENTER
,
114 okButton
= new Button(actionLabel
, new OkHandler());
117 new Button(LocalizedString
.Cancel
.toString(), new CancelHandler()))
118 .setLayoutData(GridLayout
.createLayoutData(GridLayout
.Alignment
.END
, GridLayout
.Alignment
.CENTER
, false, false, 2, 1))
121 if(selectedObject
.isFile()) {
122 directory
= selectedObject
.getParentFile();
123 fileBox
.setText(selectedObject
.getName());
125 else if(selectedObject
.isDirectory()) {
126 directory
= selectedObject
;
129 reloadViews(directory
);
130 setComponent(contentPane
);
135 * @param textGUI Text GUI to add the dialog to
136 * @return The file which was selected in the dialog or {@code null} if the dialog was cancelled
139 public File
showDialog(WindowBasedTextGUI textGUI
) {
141 super.showDialog(textGUI
);
145 private class OkHandler
implements Runnable
{
148 if(!fileBox
.getText().isEmpty()) {
149 selectedFile
= new File(directory
, fileBox
.getText());
153 MessageDialog
.showMessageDialog(getTextGUI(), "Error", "Please select a valid file name", MessageDialogButton
.OK
);
158 private class CancelHandler
implements Runnable
{
166 private class DoNothing
implements Runnable
{
172 private void reloadViews(final File directory
) {
173 directoryListBox
.clearItems();
174 fileListBox
.clearItems();
175 File
[]entries
= directory
.listFiles();
176 if(entries
== null) {
179 Arrays
.sort(entries
, new Comparator
<File
>() {
181 public int compare(File o1
, File o2
) {
182 return o1
.getName().toLowerCase().compareTo(o2
.getName().toLowerCase());
185 directoryListBox
.addItem("..", new Runnable() {
188 FileDialog
.this.directory
= directory
.getAbsoluteFile().getParentFile();
189 reloadViews(directory
.getAbsoluteFile().getParentFile());
192 for(final File entry
: entries
) {
193 if(entry
.isHidden() && !showHiddenFilesAndDirs
) {
196 if(entry
.isDirectory()) {
197 directoryListBox
.addItem(entry
.getName(), new Runnable() {
200 FileDialog
.this.directory
= entry
;
206 fileListBox
.addItem(entry
.getName(), new Runnable() {
209 fileBox
.setText(entry
.getName());
210 setFocusedInteractable(okButton
);
215 if(fileListBox
.isEmpty()) {
216 fileListBox
.addItem("<empty>", new DoNothing());
220 private class FileSystemLocationLabel
extends Label
{
221 public FileSystemLocationLabel() {
223 setPreferredSize(TerminalSize
.ONE
);
227 public void onBeforeDrawing() {
228 TerminalSize area
= getSize();
229 String absolutePath
= directory
.getAbsolutePath();
230 int absolutePathLengthInColumns
= TerminalTextUtils
.getColumnWidth(absolutePath
);
231 if(area
.getColumns() < absolutePathLengthInColumns
) {
232 absolutePath
= absolutePath
.substring(absolutePathLengthInColumns
- area
.getColumns());
233 absolutePath
= "..." + absolutePath
.substring(Math
.min(absolutePathLengthInColumns
, 3));
235 setText(absolutePath
);