-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileInput.java
155 lines (144 loc) · 5.25 KB
/
FileInput.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.KeyCode;
import javafx.scene.input.DragEvent;
import javafx.scene.input.TransferMode;
import javafx.scene.input.Dragboard;
import javafx.scene.control.ComboBox;
import javafx.geometry.Pos;
import javafx.geometry.Insets;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
import java.io.File;
import java.nio.file.FileSystems;
public class FileInput
{
private TextField filearea;
private String filename;
private String dir;
//The program asks the user to input the filename if no file is found
public FileInput()
{
//basic window setting up
Group root = new Group();
Scene sc = new Scene(root);
Stage popup = new Stage();
popup.setScene(sc);
popup.setTitle("Input Filename");
//gets directory of the program
dir = FileSystems.getDefault().getPath(".").toAbsolutePath().toString();
//adds all local csv files found to a menu
ComboBox cb = new ComboBox();
File local = new File(dir);
for(File file : local.listFiles())
{
if(file.getName().endsWith(".csv"))
{
cb.getItems().add(file.toString());
}
else if(file.isDirectory()) //incase you put the files in a folder
{
File sub = file;
for(File subFile : sub.listFiles())
{
if(subFile.getName().endsWith(".csv"))
cb.getItems().add(subFile.toString());
}
}
}
try
{
cb.getSelectionModel().select(0); //defaults to the first option
}
catch(Exception e)
{
cb.setValue("No CSV Files Found!"); //if there's no files available
}
//sets the filename on click
cb.setOnAction(new EventHandler<ActionEvent>(){
@Override public void handle(ActionEvent e)
{
filename = cb.getValue().toString();
}
});
Label message = new Label("Please select a file from the drop down menu");
Label message2 = new Label("(The file/folder must be in the same directory as this program)");
Label message5halves = new Label("OR drag and drop the file into this window");
Label message3 = new Label();
Label spacer = new Label("");
//checks to see if the file is legit and sends it off
Button button = new Button("OK");
button.setOnAction(new EventHandler<ActionEvent>(){
@Override public void handle(ActionEvent e)
{
File file = new File(filename);
message3.setText("");
if(file.exists())
popup.close();
else
message3.setText("The file could not be found!");
}
});
sc.setOnKeyPressed(new EventHandler<KeyEvent>(){
@Override
public void handle(KeyEvent e)
{
if(e.getCode() == KeyCode.ENTER)
{
File file = new File(filename);
message3.setText("");
if(file.exists())
popup.close();
else
message3.setText("The file could not be found!");
}
}
});
//adds everything to the window
VBox vb = new VBox(message, message2, message5halves, message3, cb, spacer, button);
vb.setAlignment(Pos.CENTER);
vb.setPadding(new Insets(10, 20, 20, 20));
//additional drag and drop feature for fun
vb.setOnDragOver(new EventHandler<DragEvent>(){
@Override
public void handle(DragEvent ev)
{
if(ev.getGestureSource() != vb && ev.getDragboard().hasFiles()) {
ev.acceptTransferModes(TransferMode.COPY_OR_MOVE);
}
ev.consume();
}
});
vb.setOnDragDropped(new EventHandler<DragEvent>(){
@Override
public void handle(DragEvent ev)
{
Dragboard db = ev.getDragboard();
if(db.hasFiles())
{
filename = db.getFiles().toString();
filename = filename.substring(1, filename.length() - 1);
File file = new File(filename);
message3.setText("");
if(file.exists())
popup.close();
else
message3.setText("The file could not be dropped!");
}
}
});
root.getChildren().add(vb);
popup.showAndWait();
}
public String getFileName()
{
return filename;
}
}