-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImportData.java
More file actions
388 lines (271 loc) · 12.3 KB
/
Copy pathImportData.java
File metadata and controls
388 lines (271 loc) · 12.3 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package wf.util;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.regex.Pattern;
import javax.imageio.ImageIO;
import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.Label;
import javafx.scene.control.Separator;
import javafx.scene.control.TextField;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.transform.Scale;
import javafx.scene.transform.Transform;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
/**
* @author Fabio Schirru
*/
public class ImportData {
//private final FormNodeGenerator fng = FormNodeGenerator.getInstance();
//private final DialogMessage dm = DialogMessage.getInstance();
private final Button impDial_import_bt = new Button("Import");
private final Button impDial_exit_bt = new Button("Close");
private final ButtonBar button_bar = new ButtonBar();
private final Label exportDir_lb = new Label("Directory");
private final Label exportFileName_lb = new Label("File Name:");
private final GridPane container_gp = new GridPane();
private final Group root_gp = new Group();
private final HBox expDial_hb = new HBox();
private final Label dirSelector_lb = new Label("Directory Browser");
private final Separator hSeparator = new Separator();
private final Scene impDial_sc = new Scene(root_gp);
private final StackPane dirSelector_sp = new StackPane();
private final TextField exportDir_tf = new TextField();
private final TextField fileNameDir_tf = new TextField();
private final TreeItem<File> dummyRoot = new TreeItem<>();
private final TreeView<File> tree = new TreeView<>(dummyRoot);
private final Stage expDial_st = new Stage();
private final VBox expDial_vb = new VBox();
private final VBox expDialRight_vb = new VBox();
private String selectedPath;
private String fullFileName;
private String fullFileNameImg;
private String date_time = "";
//private final String defaultDir = "/common/home/rifr/scheidenb/lnx/";
private final String defaultDir = "/common/home/rifr/fschirru/lnx/";
private final Pattern fileName_validStringText = Pattern.compile("([a-zA-Z0-9_]{0,20})?$");
private static final ImportData INSTANCE = new ImportData();
public ImportData() {
setStyle();
createTreeView();
exportDir_tf.setDisable(true);
expDial_st.setTitle("Export Data");
expDial_st.setResizable(false);
expDial_st.initModality(Modality.APPLICATION_MODAL);
expDial_st.setScene(impDial_sc);
expDial_st.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(final WindowEvent event) {
event.consume();
}
});
impDial_import_bt.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(final ActionEvent event) {
final Task<Void> task = new Task<Void>() {
@Override
public Void call() throws Exception {
Platform.runLater(new Runnable() {
@Override
public void run() {
//start dialog message
//dm.setIconAndText(null, "Please wait while generating the pdf file...");
//dm.showMessangerStage();
}
});
Thread.sleep(1000);
fullFileNameImg = exportDir_tf.getText() + "/" + fileNameDir_tf.getText() + "_"
+ date_time + ".png";
fullFileName = exportDir_tf.getText() + "/" + fileNameDir_tf.getText() + "_"
+ date_time + ".pdf";
final File file = new File(fullFileNameImg);
Platform.runLater(new Runnable() {
@Override
public void run() {
}
});
return null;
}
};
task.setOnSucceeded(e -> {
//System.out.println("Action performed");
//dm.closeMessangerStage();
expDial_st.close();
});
task.setOnFailed(e -> {
System.out.println(e);
System.out.println("Action not performed");
//dm.closeMessangerStage();
//expDial_st.close();
});
final Thread thread = new Thread(task);
thread.setDaemon(true);
thread.start();
}
});
impDial_exit_bt.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(final ActionEvent event) {
expDial_st.close();
}
});
fileNameDir_tf.textProperty().addListener((observable, oldValue, newValue) -> {
if (!newValue.matches(fileName_validStringText.toString())) {
fileNameDir_tf.setText(oldValue);
}
});
exportDir_tf.setPrefWidth(500);
fileNameDir_tf.setPrefWidth(500);
container_gp.setHgap(10);
container_gp.setVgap(10);
container_gp.add(exportDir_lb, 0, 1);
container_gp.add(exportDir_tf, 1, 1);
container_gp.add(exportFileName_lb, 0, 2);
container_gp.add(fileNameDir_tf, 1, 2);
tree.setPadding(new Insets(10, 10, 10, 10));
tree.setPrefWidth(500);
tree.setPrefHeight(500);
dirSelector_sp.getChildren().add(tree);
dirSelector_sp.setPadding(new Insets(0, 0, 0, 0));
StackPane.setAlignment(dirSelector_lb, Pos.TOP_LEFT);
dirSelector_sp.getChildren().add(dirSelector_lb);
expDialRight_vb.setSpacing(10);
expDialRight_vb.getChildren().add(container_gp);
expDial_hb.setSpacing(10);
expDial_hb.getChildren().addAll(dirSelector_sp, expDialRight_vb);
button_bar.getButtons().addAll(impDial_import_bt, impDial_exit_bt);
expDial_vb.setSpacing(10);
expDial_vb.setPadding(new Insets(20, 10, 20, 10));
expDial_vb.getChildren().addAll(expDial_hb, hSeparator, button_bar);
root_gp.getChildren().add(expDial_vb);
impDial_import_bt.disableProperty().bind(
Bindings.isEmpty(exportDir_tf.textProperty()).or(Bindings.isEmpty(fileNameDir_tf.textProperty())));
}
private TreeItem<File> createNode(final File file) {
return new TreeItem<File>(file) {
private boolean isLeaf;
private boolean isFirstTimeChildren = true;
private boolean isFirstTimeLeaf = true;
@Override
public ObservableList<TreeItem<File>> getChildren() {
if (isFirstTimeChildren) {
isFirstTimeChildren = false;
super.getChildren().setAll(buildChildren(this));
}
return super.getChildren();
}
@Override
public boolean isLeaf() {
if (isFirstTimeLeaf) {
isFirstTimeLeaf = false;
final File f = getValue();
isLeaf = f.isFile();
}
return isLeaf;
}
private ObservableList<TreeItem<File>> buildChildren(
final TreeItem<File> TreeItem) {
final File f = TreeItem.getValue();
final FileFilter directoryFilter = new FileFilter() {
@Override
public boolean accept(final File file_) {
// return file.isDirectory();
return !file_.isHidden() && file_.isDirectory();
}
};
if (f == null) {
return FXCollections.emptyObservableList();
}
if (f.isFile()) {
return FXCollections.emptyObservableList();
}
final File[] files = f.listFiles(directoryFilter);
if (files != null) {
final ObservableList<TreeItem<File>> children = FXCollections.observableArrayList();
for (final File childFile : files) {
children.add(createNode(childFile));
}
return children;
}
return FXCollections.emptyObservableList();
}
};
}
private void createTreeView() {
File[] paths;
// returns pathnames for files and directory
paths = File.listRoots();
// System.out.println(paths);
// for each pathname in pathname array
for (final File path : paths) {
// prints file and directory paths
// System.out.println("Drive Name: " + path);
// final String new_path = path + "\\";
// System.out.println(new_path);
// final TreeItem<File> root = createNode(new File("/"));
dummyRoot.getChildren().add(createNode(new File(path.toString())));
}
// final TreeView<File> tree = new TreeView<>(dummyRoot);
tree.setPrefSize(300, 200);
tree.setShowRoot(false);
tree.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Object>() {
@Override
public void changed(final ObservableValue<? extends Object> observable, final Object oldValue,
final Object newValue) {
if (tree.getSelectionModel().getSelectedItem() != null) {
selectedPath = tree.getSelectionModel().getSelectedItem().toString();
if (selectedPath != null) {
// System.out.println(selectedItem);
// String path = selectedItem.toString();
//System.out.println(selectedPath.substring(18, selectedPath.length() - 2));
selectedPath = selectedPath.substring(18, selectedPath.length() - 2).replace("\\", "/");
exportDir_tf.setText(selectedPath);
}
}
}
});
}
public void showImportDialog() {
expDial_st.showAndWait();
impDial_exit_bt.requestFocus();
}
private void setStyle() {
hSeparator.setStyle("-fx-background-color: white; -fx-border-style: solid; -fx-border-width: 0.1px;");
dirSelector_sp.setStyle("-fx-border-color: gray; -fx-border-width: 1;");
dirSelector_lb.setStyle("-fx-translate-x: 4; -fx-translate-y: -8; -fx-background-color: white;");
}
public String getSelectedDir() {
return exportDir_tf.getText();
}
public void setDateAndTime(final String file_info) {
date_time = file_info;
}
public static ImportData getInstance() {
return INSTANCE;
}
}