Skip to content

Commit 51fc872

Browse files
committed
Add basic console input parsing to open files
1 parent acf0633 commit 51fc872

File tree

4 files changed

+131
-24
lines changed

4 files changed

+131
-24
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ javafx {
1212
modules = [ 'javafx.controls' ]
1313
}
1414

15-
mainClassName = 'HelloFX'
15+
mainClassName = 'App'

src/main/java/App.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import javafx.application.Application;
2+
import javafx.scene.Scene;
3+
import javafx.scene.control.Label;
4+
import javafx.scene.layout.StackPane;
5+
import javafx.scene.layout.VBox;
6+
import javafx.stage.Stage;
7+
8+
import java.awt.*;
9+
import java.io.File;
10+
11+
public class App extends Application {
12+
13+
/**
14+
* Attempt listening for APP_OPEN_FILE events as soon as possible.
15+
*/
16+
static {
17+
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.APP_OPEN_FILE)) {
18+
Desktop.getDesktop().setOpenFileHandler(event -> {
19+
for (File file : event.getFiles()) {
20+
Operator.parseInput(file.getAbsolutePath());
21+
}
22+
});
23+
}
24+
}
25+
26+
@Override
27+
public void start(Stage stage) {
28+
// Check for files in provided console parameters
29+
Parameters params = getParameters();
30+
for (String param : params.getUnnamed()) Operator.parseInput(param);
31+
for (String param : params.getRaw()) Operator.parseInput(param);
32+
33+
if (Operator.getOpenFile() == null && !Operator.chooseNewFile()) {
34+
// Failed...
35+
return;
36+
}
37+
38+
String javaVersion = System.getProperty("java.version");
39+
String javafxVersion = System.getProperty("javafx.version");
40+
Label l = new Label("Hello, JavaFX " + javafxVersion + ", running on Java " + javaVersion + ".");
41+
// TODO: Show which file was opened.
42+
43+
String chosenFile = "Opened file: " + Operator.getOpenFile().getAbsolutePath();
44+
45+
Scene scene = new Scene(new VBox(l, new Label(chosenFile)), 320, 240);
46+
stage.setScene(scene);
47+
stage.show();
48+
}
49+
50+
public static void main(String[] args) {
51+
launch();
52+
}
53+
}

src/main/java/HelloFX.java

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/main/java/Operator.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import javafx.stage.FileChooser;
2+
3+
import java.io.File;
4+
5+
public class Operator {
6+
public static final String[] EXTENSIONS = new String[] {"pew"};
7+
public static final String DEFAULT_EXTENSION = EXTENSIONS[0];
8+
public static final String[] MIME_TYPES = new String[] {"public.data, com.example.pew"};
9+
public static final String DEFAULT_FOLDER = "/Users/Shared";
10+
11+
private static File openFile = null;
12+
13+
private Operator() {}
14+
15+
public static File getOpenFile() {
16+
return openFile;
17+
}
18+
19+
/**
20+
* Checks whether an input parameter is a path to a valid file.
21+
*
22+
* @param input Console input parameter.
23+
* @return True if input was path to a valid file.
24+
*/
25+
public static boolean parseInput(String input) {
26+
File file = new File(input);
27+
28+
if (!file.exists() || file.isDirectory()) {
29+
return false;
30+
}
31+
32+
boolean validExtension = false;
33+
for (String ext : EXTENSIONS) {
34+
if (!file.getName().endsWith(ext)) continue;
35+
validExtension = true;
36+
break;
37+
}
38+
39+
if (!validExtension) return false;
40+
41+
openFile = file;
42+
return true;
43+
}
44+
45+
/**
46+
* Prompts the user to choose a file from storage.
47+
*
48+
* @return True if successfully found a file.
49+
*/
50+
public static boolean chooseNewFile() {
51+
FileChooser fileChooser = new FileChooser();
52+
53+
// Start from existing directory
54+
if (getOpenFile() != null) {
55+
fileChooser.setInitialDirectory(openFile.getParentFile());
56+
} else {
57+
File dir = new File(DEFAULT_FOLDER);
58+
if (dir.exists() && dir.isDirectory()) {
59+
fileChooser.setInitialDirectory(dir);
60+
}
61+
}
62+
63+
// Set extension filter
64+
for (String extension : EXTENSIONS) {
65+
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter(
66+
extension + " files", "*." + extension
67+
));
68+
}
69+
70+
// Show open file dialog
71+
File candidateFile = fileChooser.showOpenDialog(null);
72+
if (candidateFile == null) return false;
73+
74+
// Parse chosen file
75+
return parseInput(candidateFile.getAbsolutePath());
76+
}
77+
}

0 commit comments

Comments
 (0)