1
|
package deserialize;
|
2
|
|
3
|
import java.io.File;
|
4
|
|
5
|
import javafx.application.Application;
|
6
|
import javafx.geometry.Insets;
|
7
|
import javafx.geometry.Pos;
|
8
|
import javafx.scene.Parent;
|
9
|
import javafx.scene.Scene;
|
10
|
import javafx.scene.control.Button;
|
11
|
import javafx.scene.control.Label;
|
12
|
import javafx.scene.control.Separator;
|
13
|
import javafx.scene.layout.HBox;
|
14
|
import javafx.scene.layout.Pane;
|
15
|
import javafx.scene.layout.Priority;
|
16
|
import javafx.scene.layout.VBox;
|
17
|
import javafx.stage.FileChooser;
|
18
|
import javafx.stage.Stage;
|
19
|
|
20
|
import serialize.Example;
|
21
|
|
22
|
public class Deserializer extends Application {
|
23
|
|
24
|
private Stage stage;
|
25
|
private File inputFile;
|
26
|
private File outputFile;
|
27
|
|
28
|
// TODO delete after deserializer debugging is complete...
|
29
|
private boolean testing;
|
30
|
|
31
|
public static void main(String[] args) {
|
32
|
launch(args);
|
33
|
}
|
34
|
|
35
|
@Override
|
36
|
public void init() throws Exception {
|
37
|
super.init();
|
38
|
testing = true;
|
39
|
}
|
40
|
|
41
|
@Override
|
42
|
public void start(Stage stage) throws Exception {
|
43
|
this.stage = stage;
|
44
|
stage.setTitle("Java object universal deserializer");
|
45
|
stage.setScene(createScene());
|
46
|
stage.setMinWidth(400);
|
47
|
stage.show();
|
48
|
}
|
49
|
|
50
|
private Scene createScene() {
|
51
|
return new Scene(createLayout());
|
52
|
}
|
53
|
|
54
|
private Parent createLayout() {
|
55
|
VBox layout = new VBox();
|
56
|
layout.setPadding(new Insets(10.0));
|
57
|
layout.setSpacing(10.0);
|
58
|
|
59
|
Label forDefaultBinFile = new Label("Chcete vytvo?it defaultn? bin?rn? soubor?");
|
60
|
Button createBinFile = new Button("Vytvo?it");
|
61
|
createBinFile.setOnAction(event -> {
|
62
|
FileChooser fCh = new FileChooser();
|
63
|
fCh.setInitialDirectory(new File("."));
|
64
|
fCh.getExtensionFilters().add(new FileChooser.ExtensionFilter("Binary Files", "*.out"));
|
65
|
File file = fCh.showSaveDialog(stage);
|
66
|
if (file != null) {
|
67
|
createSerializedFile(file);
|
68
|
}
|
69
|
});
|
70
|
final Pane spacer = new Pane();
|
71
|
HBox.setHgrow(spacer, Priority.ALWAYS);
|
72
|
HBox header = new HBox();
|
73
|
header.setAlignment(Pos.CENTER_LEFT);
|
74
|
header.getChildren().add(forDefaultBinFile);
|
75
|
header.getChildren().add(spacer);
|
76
|
header.getChildren().add(createBinFile);
|
77
|
|
78
|
Label forInputFile = new Label("Soubor k deserializaci:");
|
79
|
Label inputFile = new Label(" Je?t? nebyl vybr?n ??dn? soubor...");
|
80
|
inputFile.setStyle("-fx-font-style: italic;");
|
81
|
VBox forInput = new VBox();
|
82
|
forInput.getChildren().add(forInputFile);
|
83
|
forInput.getChildren().add(inputFile);
|
84
|
|
85
|
Label forOutputFile = new Label("V?stupn? soubor:");
|
86
|
Label outputFile = new Label(" Je?t? nebyl vybr?n ??dn? soubor...");
|
87
|
outputFile.setStyle("-fx-font-style: italic;");
|
88
|
VBox forOutput = new VBox();
|
89
|
forOutput.getChildren().add(forOutputFile);
|
90
|
forOutput.getChildren().add(outputFile);
|
91
|
|
92
|
Button convert = new Button("P?ev?st");
|
93
|
convert.setOnAction(event -> {
|
94
|
// a.data a b.data customers test files (binary files)
|
95
|
ToJSON thread = new ToJSON(testing ? new File("data.out") : this.inputFile, testing ? new File("results.json") : this.outputFile);
|
96
|
thread.start();
|
97
|
});
|
98
|
convert.setDisable(!testing);
|
99
|
Button setInputFile = new Button("Vstupn? soubor");
|
100
|
setInputFile.setOnAction(event -> {
|
101
|
FileChooser fCh = new FileChooser();
|
102
|
fCh.setInitialDirectory(new File("."));
|
103
|
// The input can be a zip file or file without a specific extension -> file chooser without extension filters...
|
104
|
// fCh.getExtensionFilters().add(new FileChooser.ExtensionFilter("Binary Files", "*.out"));
|
105
|
File file = fCh.showOpenDialog(stage);
|
106
|
if (file != null) {
|
107
|
this.inputFile = file;
|
108
|
inputFile.setText(this.inputFile == null ? "" : this.inputFile.getAbsolutePath());
|
109
|
inputFile.setStyle("-fx-font-weight: bold;");
|
110
|
convert.setDisable(this.inputFile == null || this.outputFile == null);
|
111
|
}
|
112
|
});
|
113
|
Button setOutputFile = new Button("V?stupn? soubor");
|
114
|
setOutputFile.setOnAction(event -> {
|
115
|
FileChooser fCh = new FileChooser();
|
116
|
fCh.setInitialDirectory(new File("."));
|
117
|
fCh.getExtensionFilters().add(new FileChooser.ExtensionFilter("JSON Files", "*.json"));
|
118
|
File file = fCh.showSaveDialog(stage);
|
119
|
if (file != null) {
|
120
|
this.outputFile = file;
|
121
|
outputFile.setText(this.outputFile == null ? "" : this.outputFile.getAbsolutePath());
|
122
|
outputFile.setStyle("-fx-font-weight: bold;");
|
123
|
convert.setDisable(this.inputFile == null || this.outputFile == null);
|
124
|
}
|
125
|
});
|
126
|
HBox footer = new HBox();
|
127
|
footer.setSpacing(10.0);
|
128
|
footer.setAlignment(Pos.CENTER_RIGHT);
|
129
|
footer.getChildren().add(setInputFile);
|
130
|
footer.getChildren().add(setOutputFile);
|
131
|
footer.getChildren().add(convert);
|
132
|
|
133
|
layout.getChildren().add(header);
|
134
|
layout.getChildren().add(new Separator());
|
135
|
layout.getChildren().add(forInput);
|
136
|
layout.getChildren().add(forOutput);
|
137
|
layout.getChildren().add(footer);
|
138
|
|
139
|
return layout;
|
140
|
}
|
141
|
|
142
|
private void createSerializedFile(File file) {
|
143
|
String title = "Vytvo?en? defaultn?ho souboru";
|
144
|
if (Example.serialize(file)) {
|
145
|
Report.info(title, null, "Vytvo?en? prob?hlo v po??dku.");
|
146
|
} else {
|
147
|
Report.error(title, null, "Vytvo?en? souboru se nepovedlo.");
|
148
|
}
|
149
|
}
|
150
|
|
151
|
}
|