X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2FTFileOpenBox.java;h=a2cc0cf7e48f2e68efebfefec3c66d14775796c9;hb=f19b48e27a56ebab18687debd9ef52581a03f06d;hp=18d65f22ea047b83cb17b709df4e7a71cc5e173d;hpb=d6ee0801333ff93dffd851f4c1a44519c96c371d;p=fanfix.git
diff --git a/src/jexer/TFileOpenBox.java b/src/jexer/TFileOpenBox.java
index 18d65f2..a2cc0cf 100644
--- a/src/jexer/TFileOpenBox.java
+++ b/src/jexer/TFileOpenBox.java
@@ -3,7 +3,7 @@
*
* The MIT License (MIT)
*
- * Copyright (C) 2017 Kevin Lamonte
+ * Copyright (C) 2019 Kevin Lamonte
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
@@ -30,8 +30,10 @@ package jexer;
import java.io.File;
import java.io.IOException;
+import java.util.List;
import java.util.ResourceBundle;
+import jexer.backend.SwingTerminal;
import jexer.bits.GraphicsChars;
import jexer.event.TKeypressEvent;
import jexer.ttree.TDirectoryTreeItem;
@@ -43,10 +45,9 @@ import static jexer.TKeypress.*;
* TFileOpenBox is a system-modal dialog for selecting a file to open. Call
* it like:
*
- *
*
* {@code
- * filename = application.fileOpenBox("/path/to/file.ext",
+ * filename = fileOpenBox("/path/to/file.ext",
* TFileOpenBox.Type.OPEN);
* if (filename != null) {
* ... the user selected a file, go open it ...
@@ -55,7 +56,7 @@ import static jexer.TKeypress.*;
*
*
*/
-public final class TFileOpenBox extends TWindow {
+public class TFileOpenBox extends TWindow {
/**
* Translated strings.
@@ -78,7 +79,12 @@ public final class TFileOpenBox extends TWindow {
/**
* Button will be labeled "Save".
*/
- SAVE
+ SAVE,
+
+ /**
+ * Button will be labeled "Select".
+ */
+ SELECT
}
// ------------------------------------------------------------------------
@@ -115,6 +121,11 @@ public final class TFileOpenBox extends TWindow {
*/
private TButton openButton;
+ /**
+ * The type of box this is (OPEN, SAVE, or SELECT).
+ */
+ private Type type = Type.OPEN;
+
// ------------------------------------------------------------------------
// Constructors -----------------------------------------------------------
// ------------------------------------------------------------------------
@@ -130,6 +141,21 @@ public final class TFileOpenBox extends TWindow {
public TFileOpenBox(final TApplication application, final String path,
final Type type) throws IOException {
+ this(application, path, type, null);
+ }
+
+ /**
+ * Public constructor. The file open box will be centered on screen.
+ *
+ * @param application the TApplication that manages this window
+ * @param path path of selected file
+ * @param type one of the Type constants
+ * @param filters a list of strings that files must match to be displayed
+ * @throws IOException of a java.io operation throws
+ */
+ public TFileOpenBox(final TApplication application, final String path,
+ final Type type, final List filters) throws IOException {
+
// Register with the TApplication
super(application, "", 0, 0, 76, 22, MODAL);
@@ -141,7 +167,11 @@ public final class TFileOpenBox extends TWindow {
try {
checkFilename(entryField.getText());
} catch (IOException e) {
- e.printStackTrace();
+ // If the backend is Swing, we can emit the stack
+ // trace to stderr. Otherwise, just squash it.
+ if (getScreen() instanceof SwingTerminal) {
+ e.printStackTrace();
+ }
}
}
}, null);
@@ -155,10 +185,17 @@ public final class TFileOpenBox extends TWindow {
File selectedDir = ((TDirectoryTreeItem) item).getFile();
try {
directoryList.setPath(selectedDir.getCanonicalPath());
- openButton.setEnabled(false);
+ entryField.setText(selectedDir.getCanonicalPath());
+ if (type == Type.OPEN) {
+ openButton.setEnabled(false);
+ }
activate(treeView);
} catch (IOException e) {
- e.printStackTrace();
+ // If the backend is Swing, we can emit the stack
+ // trace to stderr. Otherwise, just squash it.
+ if (getScreen() instanceof SwingTerminal) {
+ e.printStackTrace();
+ }
}
}
}
@@ -175,12 +212,34 @@ public final class TFileOpenBox extends TWindow {
entryField.onKeypress(new TKeypressEvent(kbEnd));
openButton.setEnabled(true);
activate(entryField);
+ checkFilename(entryField.getText());
} catch (IOException e) {
- e.printStackTrace();
+ // If the backend is Swing, we can emit the stack
+ // trace to stderr. Otherwise, just squash it.
+ if (getScreen() instanceof SwingTerminal) {
+ e.printStackTrace();
+ }
}
}
- }
- );
+ },
+ new TAction() {
+ public void DO() {
+ try {
+ File newPath = directoryList.getPath();
+ entryField.setText(newPath.getCanonicalPath());
+ entryField.onKeypress(new TKeypressEvent(kbEnd));
+ openButton.setEnabled(true);
+ activate(entryField);
+ } catch (IOException e) {
+ // If the backend is Swing, we can emit the stack
+ // trace to stderr. Otherwise, just squash it.
+ if (getScreen() instanceof SwingTerminal) {
+ e.printStackTrace();
+ }
+ }
+ }
+ },
+ filters);
String openLabel = "";
switch (type) {
@@ -192,9 +251,14 @@ public final class TFileOpenBox extends TWindow {
openLabel = i18n.getString("saveButton");
setTitle(i18n.getString("saveTitle"));
break;
+ case SELECT:
+ openLabel = i18n.getString("selectButton");
+ setTitle(i18n.getString("selectTitle"));
+ break;
default:
throw new IllegalArgumentException("Invalid type: " + type);
}
+ this.type = type;
// Setup button actions
openButton = addButton(openLabel, this.getWidth() - 12, 3,
@@ -203,12 +267,18 @@ public final class TFileOpenBox extends TWindow {
try {
checkFilename(entryField.getText());
} catch (IOException e) {
- e.printStackTrace();
+ // If the backend is Swing, we can emit the stack
+ // trace to stderr. Otherwise, just squash it.
+ if (getScreen() instanceof SwingTerminal) {
+ e.printStackTrace();
+ }
}
}
}
);
- openButton.setEnabled(false);
+ if (type == Type.OPEN) {
+ openButton.setEnabled(false);
+ }
addButton(i18n.getString("cancelButton"), getWidth() - 12, 5,
new TAction() {
@@ -266,10 +336,16 @@ public final class TFileOpenBox extends TWindow {
File selectedDir = ((TDirectoryTreeItem) item).getFile();
try {
directoryList.setPath(selectedDir.getCanonicalPath());
- openButton.setEnabled(false);
+ if (type == Type.OPEN) {
+ openButton.setEnabled(false);
+ }
activate(treeView);
} catch (IOException e) {
- e.printStackTrace();
+ // If the backend is Swing, we can emit the stack trace
+ // to stderr. Otherwise, just squash it.
+ if (getScreen() instanceof SwingTerminal) {
+ e.printStackTrace();
+ }
}
return;
}
@@ -289,7 +365,7 @@ public final class TFileOpenBox extends TWindow {
@Override
public void draw() {
super.draw();
- getScreen().vLineXY(33, 4, getHeight() - 6, GraphicsChars.WINDOW_SIDE,
+ vLineXY(33, 4, getHeight() - 6, GraphicsChars.WINDOW_SIDE,
getBackground());
}
@@ -316,7 +392,7 @@ public final class TFileOpenBox extends TWindow {
private void checkFilename(final String newFilename) throws IOException {
File newFile = new File(newFilename);
if (newFile.exists()) {
- if (newFile.isFile()) {
+ if (newFile.isFile() || (type == Type.SELECT)) {
filename = newFilename;
getApplication().closeWindow(this);
return;
@@ -325,9 +401,15 @@ public final class TFileOpenBox extends TWindow {
treeViewRoot = new TDirectoryTreeItem(treeView,
newFilename, true);
treeView.setTreeRoot(treeViewRoot, true);
- openButton.setEnabled(false);
+ if (type == Type.OPEN) {
+ openButton.setEnabled(false);
+ }
directoryList.setPath(newFilename);
}
+ } else if (type != Type.OPEN) {
+ filename = newFilename;
+ getApplication().closeWindow(this);
+ return;
}
}