1
|
import java.io.File;
|
2
|
import java.net.URL;
|
3
|
import java.util.concurrent.atomic.AtomicReference;
|
4
|
|
5
|
import io.FileWorker;
|
6
|
import javafx.application.Application;
|
7
|
import javafx.application.Platform;
|
8
|
import javafx.beans.value.ChangeListener;
|
9
|
import javafx.beans.value.ObservableValue;
|
10
|
import javafx.geometry.Insets;
|
11
|
import javafx.geometry.Orientation;
|
12
|
import javafx.geometry.Pos;
|
13
|
import javafx.scene.Node;
|
14
|
import javafx.scene.Parent;
|
15
|
import javafx.scene.Scene;
|
16
|
import javafx.scene.control.Button;
|
17
|
import javafx.scene.control.Label;
|
18
|
import javafx.scene.control.Menu;
|
19
|
import javafx.scene.control.MenuBar;
|
20
|
import javafx.scene.control.MenuItem;
|
21
|
import javafx.scene.control.ProgressIndicator;
|
22
|
import javafx.scene.control.ScrollPane;
|
23
|
import javafx.scene.control.Separator;
|
24
|
import javafx.scene.control.SeparatorMenuItem;
|
25
|
import javafx.scene.effect.BlendMode;
|
26
|
import javafx.scene.image.Image;
|
27
|
import javafx.scene.image.ImageView;
|
28
|
import javafx.scene.input.Clipboard;
|
29
|
import javafx.scene.input.DataFormat;
|
30
|
import javafx.scene.input.KeyCode;
|
31
|
import javafx.scene.input.KeyCodeCombination;
|
32
|
import javafx.scene.input.KeyCombination;
|
33
|
import javafx.scene.layout.BorderPane;
|
34
|
import javafx.scene.layout.HBox;
|
35
|
import javafx.scene.layout.Priority;
|
36
|
import javafx.scene.layout.Region;
|
37
|
import javafx.scene.layout.VBox;
|
38
|
import javafx.scene.paint.Color;
|
39
|
import javafx.scene.web.WebEngine;
|
40
|
import javafx.scene.web.WebView;
|
41
|
import javafx.stage.FileChooser;
|
42
|
import javafx.stage.Stage;
|
43
|
|
44
|
/**
|
45
|
* VM arguments for Java 11: --module-path libs\javafx-sdk-11.0.2\lib --add-modules=javafx.controls
|
46
|
*/
|
47
|
public class Deserializer extends Application implements IConversionResults {
|
48
|
|
49
|
public static boolean blockFocusProperty = false;
|
50
|
|
51
|
private Stage stage;
|
52
|
private Label fullScreen;
|
53
|
|
54
|
private WebEngine webEngine;
|
55
|
private VBox outputLayout;
|
56
|
|
57
|
private ProgressIndicator clipBoardIndicator;
|
58
|
private Label clipBoardResults;
|
59
|
private Button setInputFromCB;
|
60
|
private String lastClipBoardData;
|
61
|
|
62
|
private Converter converter;
|
63
|
private AtomicReference<jDeserializeResults> results;
|
64
|
|
65
|
// TODO delete after deserializer debugging is complete...
|
66
|
private File defaultInput = new File("a2");
|
67
|
private File defaultOutput = new File("results.json");
|
68
|
private boolean testing;
|
69
|
|
70
|
@Override
|
71
|
public void init() throws Exception {
|
72
|
super.init();
|
73
|
testing = false;
|
74
|
lastClipBoardData = null;
|
75
|
converter = new Converter(this);
|
76
|
converter.start();
|
77
|
results = new AtomicReference<jDeserializeResults>(new jDeserializeResults());
|
78
|
}
|
79
|
|
80
|
@Override
|
81
|
public void stop() throws Exception {
|
82
|
super.stop();
|
83
|
converter.end();
|
84
|
converter.join();
|
85
|
Platform.exit();
|
86
|
System.exit(0);
|
87
|
}
|
88
|
|
89
|
@Override
|
90
|
public void start(Stage stage) throws Exception {
|
91
|
this.stage = stage;
|
92
|
stage.getIcons().add(new Image(getResource("logo.png")));
|
93
|
stage.setTitle("Java Object Universal Deserializer");
|
94
|
stage.setScene(createScene());
|
95
|
stage.setMinWidth(400);
|
96
|
stage.show();
|
97
|
|
98
|
stage.fullScreenProperty().addListener((obs, oldState, newState) -> {
|
99
|
changeFullScreenMenuItem();
|
100
|
});
|
101
|
|
102
|
stage.focusedProperty().addListener(new ChangeListener<Boolean>() {
|
103
|
@Override
|
104
|
public void changed(ObservableValue<? extends Boolean> obs, Boolean oldValue, Boolean newValue) {
|
105
|
if (newValue && !blockFocusProperty) {
|
106
|
// Available data formats.
|
107
|
/*Set<DataFormat> types = Clipboard.getSystemClipboard().getContentTypes();
|
108
|
System.out.println("Detected types: " + types.size());
|
109
|
for (DataFormat s : types) {
|
110
|
System.out.println(s);
|
111
|
}*/
|
112
|
|
113
|
DataFormat df = DataFormat.lookupMimeType("text/plain");
|
114
|
if (df != null) {
|
115
|
String data = (String) Clipboard.getSystemClipboard().getContent(df);
|
116
|
|
117
|
if (lastClipBoardData == null) {
|
118
|
lastClipBoardData = data;
|
119
|
} else if (data.equals(lastClipBoardData)) {
|
120
|
return;
|
121
|
}
|
122
|
|
123
|
changeIndicatorState(false, false);
|
124
|
setInputFromCB.setDisable(true);
|
125
|
converter.setInput(null, data, true);
|
126
|
}
|
127
|
}
|
128
|
}
|
129
|
});
|
130
|
}
|
131
|
|
132
|
private Scene createScene() {
|
133
|
createMenu();
|
134
|
Scene scene = new Scene(createLayout());
|
135
|
scene.getStylesheets().add(getResource("main.css"));
|
136
|
return scene;
|
137
|
}
|
138
|
|
139
|
private Parent createLayout() {
|
140
|
BorderPane layout = new BorderPane();
|
141
|
|
142
|
layout.setTop(createMenu());
|
143
|
layout.setCenter(createBodyLayout());
|
144
|
|
145
|
return layout;
|
146
|
}
|
147
|
|
148
|
private Parent createBodyLayout() {
|
149
|
Label forInputFile = new Label("Soubor k deserializaci:");
|
150
|
Label inputFile;
|
151
|
if (!testing) {
|
152
|
inputFile = new Label(" Ještě nebyl vybrán žádný soubor...");
|
153
|
inputFile.setStyle("-fx-font-style: italic;");
|
154
|
} else {
|
155
|
inputFile = new Label(defaultOutput.getAbsolutePath());
|
156
|
inputFile.setStyle("-fx-font-weight: bold;");
|
157
|
}
|
158
|
inputFile.setPrefWidth(500); // Value to cut the text.
|
159
|
inputFile.setMaxWidth(Double.MAX_VALUE);
|
160
|
|
161
|
Button setInputFile = new Button("Vstupní soubor");
|
162
|
setInputFile.setOnAction(event -> {
|
163
|
Deserializer.blockFocusProperty = true; // TODO Prohibition to process the selected file.
|
164
|
if (testing) {
|
165
|
converter.setInput(defaultInput, null, false);
|
166
|
} else {
|
167
|
FileChooser fCh = new FileChooser();
|
168
|
fCh.setInitialDirectory(new File("."));
|
169
|
// The input can be a zip file or file without a specific extension -> file chooser without extension filters...
|
170
|
// fCh.getExtensionFilters().add(new FileChooser.ExtensionFilter("Binary Files", "*.out"));
|
171
|
File file = fCh.showOpenDialog(stage);
|
172
|
if (file != null) {
|
173
|
setOutputLayoutDisabled(true);
|
174
|
|
175
|
inputFile.setText(file.getAbsolutePath());
|
176
|
inputFile.setStyle("-fx-font-weight: bold;");
|
177
|
|
178
|
converter.setInput(file, null, false);
|
179
|
}
|
180
|
}
|
181
|
Deserializer.blockFocusProperty = false;
|
182
|
});
|
183
|
|
184
|
HBox bIF = new HBox();
|
185
|
bIF.setAlignment(Pos.CENTER_RIGHT);
|
186
|
bIF.getChildren().add(setInputFile);
|
187
|
|
188
|
VBox forInput = new VBox();
|
189
|
forInput.setSpacing(5.0);
|
190
|
forInput.getChildren().addAll(forInputFile, inputFile, bIF);
|
191
|
|
192
|
|
193
|
clipBoardIndicator = new ProgressIndicator();
|
194
|
clipBoardIndicator.setMaxSize(38, 38);
|
195
|
clipBoardIndicator.setVisible(false);
|
196
|
clipBoardResults = new Label();
|
197
|
clipBoardResults.setMaxSize(38, 38);
|
198
|
clipBoardResults.setVisible(false);
|
199
|
HBox clipBoardStateLayout = new HBox();
|
200
|
clipBoardStateLayout.setSpacing(0);
|
201
|
clipBoardStateLayout.setMinSize(38, 38);
|
202
|
clipBoardStateLayout.setMaxSize(38, 38);
|
203
|
clipBoardStateLayout.getChildren().addAll(clipBoardResults, clipBoardIndicator);
|
204
|
|
205
|
Label forClipBoardInput = new Label("Vstup ze schránky");
|
206
|
setInputFromCB = new Button("Použít");
|
207
|
setInputFromCB.setOnAction(event -> {
|
208
|
setInputFromCB.setDisable(true);
|
209
|
results.get().merge();
|
210
|
webEngine.loadContent(results.get().getHtml(), "text/html");
|
211
|
setOutputLayoutDisabled(false);
|
212
|
});
|
213
|
setInputFromCB.setDisable(true);
|
214
|
|
215
|
HBox indicatorLayout = new HBox();
|
216
|
Region region2 = new Region();
|
217
|
HBox.setHgrow(region2, Priority.ALWAYS);
|
218
|
indicatorLayout.getChildren().addAll(forClipBoardInput, region2, clipBoardStateLayout);
|
219
|
|
220
|
HBox bInputCB = new HBox();
|
221
|
bInputCB.setAlignment(Pos.CENTER_RIGHT);
|
222
|
bInputCB.getChildren().add(setInputFromCB);
|
223
|
|
224
|
VBox forInput2 = new VBox();
|
225
|
forInput2.setSpacing(5.0);
|
226
|
forInput2.setMinWidth(175);
|
227
|
forInput2.getChildren().addAll(indicatorLayout, bInputCB);
|
228
|
|
229
|
|
230
|
|
231
|
Label forOutputFile = new Label("Výstupní soubor:");
|
232
|
Label outputFile;
|
233
|
if (!testing) {
|
234
|
outputFile = new Label(" Ještě nebyl vybrán žádný soubor...");
|
235
|
outputFile.setStyle("-fx-font-style: italic;");
|
236
|
} else {
|
237
|
outputFile = new Label(defaultOutput.getAbsolutePath());
|
238
|
outputFile.setStyle("-fx-font-weight: bold;");
|
239
|
}
|
240
|
|
241
|
Button setOutputFile = new Button("Uložit");
|
242
|
setOutputFile.setOnAction(event -> {
|
243
|
Deserializer.blockFocusProperty = true; // TODO Prohibition to process the selected file.
|
244
|
File jsonFile = null;
|
245
|
|
246
|
if (testing) {
|
247
|
jsonFile = defaultOutput;
|
248
|
} else {
|
249
|
FileChooser fCh = new FileChooser();
|
250
|
fCh.setInitialDirectory(new File("."));
|
251
|
fCh.getExtensionFilters().add(new FileChooser.ExtensionFilter("JSON Files", "*.json"));
|
252
|
File file = fCh.showSaveDialog(stage);
|
253
|
if (file != null) {
|
254
|
jsonFile = file;
|
255
|
outputFile.setText(file.getAbsolutePath());
|
256
|
outputFile.setStyle("-fx-font-weight: bold;");
|
257
|
}
|
258
|
}
|
259
|
|
260
|
if (jsonFile != null && results.get().contains(false)) {
|
261
|
String title = "Uložení JSON";
|
262
|
try {
|
263
|
FileWorker.saveJson(jsonFile, results.get().getJson());
|
264
|
Report.info(title, null, "Uložení JSON souboru proběhlo v pořádku.");
|
265
|
} catch (Exception e) {
|
266
|
Report.error(title, null, "Při ukládání JSON souboru nastala chyba.");
|
267
|
}
|
268
|
}
|
269
|
Deserializer.blockFocusProperty = false;
|
270
|
});
|
271
|
|
272
|
HBox bOF = new HBox();
|
273
|
bOF.setAlignment(Pos.CENTER_RIGHT);
|
274
|
bOF.getChildren().add(setOutputFile);
|
275
|
|
276
|
outputLayout = new VBox();
|
277
|
outputLayout.setSpacing(5.0);
|
278
|
outputLayout.getChildren().add(forOutputFile);
|
279
|
outputLayout.getChildren().add(outputFile);
|
280
|
outputLayout.getChildren().add(bOF);
|
281
|
setOutputLayoutDisabled(true);
|
282
|
|
283
|
|
284
|
|
285
|
WebView resultantJson = new WebView();
|
286
|
resultantJson.setPrefHeight(400);
|
287
|
webEngine = resultantJson.getEngine();
|
288
|
resultantJson.setBlendMode(BlendMode.DARKEN);
|
289
|
|
290
|
|
291
|
|
292
|
|
293
|
HBox header = new HBox();
|
294
|
header.setSpacing(10.0);
|
295
|
HBox.setHgrow(forInput, Priority.ALWAYS);
|
296
|
header.getChildren().addAll(forInput, new Separator(Orientation.VERTICAL), forInput2);
|
297
|
|
298
|
|
299
|
|
300
|
VBox layout = new VBox();
|
301
|
layout.setPadding(new Insets(10.0));
|
302
|
layout.setSpacing(10.0);
|
303
|
VBox.setVgrow(resultantJson, Priority.ALWAYS);
|
304
|
layout.getChildren().addAll(header, new Separator(), resultantJson, new Separator(), outputLayout);
|
305
|
|
306
|
return layout;
|
307
|
}
|
308
|
|
309
|
private Node createMenu() {
|
310
|
Menu menu = new Menu("Aplikace");
|
311
|
|
312
|
MenuItem fullScreen = createMenuItem(null, null, new KeyCodeCombination(KeyCode.F, KeyCombination.CONTROL_DOWN));
|
313
|
fullScreen.setOnAction(event -> {
|
314
|
stage.setFullScreen(!stage.isFullScreen());
|
315
|
});
|
316
|
this.fullScreen = (Label) fullScreen.getGraphic();
|
317
|
changeFullScreenMenuItem();
|
318
|
|
319
|
MenuItem close = createMenuItem("Ukončit", getResource("close.png"), new KeyCodeCombination(KeyCode.E, KeyCombination.CONTROL_DOWN));
|
320
|
close.setOnAction(event -> {
|
321
|
Platform.exit();
|
322
|
});
|
323
|
|
324
|
menu.getItems().addAll(fullScreen, new SeparatorMenuItem(), close);
|
325
|
|
326
|
MenuBar bar = new MenuBar();
|
327
|
bar.getMenus().add(menu);
|
328
|
return bar;
|
329
|
}
|
330
|
|
331
|
private void changeFullScreenMenuItem() {
|
332
|
fullScreen.setText(stage.isFullScreen() ? "Normální zobrazení" : "Plné zobrazení");
|
333
|
fullScreen.setGraphic(new ImageView(stage.isFullScreen() ? getResource("normal.png") : getResource("full.png")));
|
334
|
}
|
335
|
|
336
|
private MenuItem createMenuItem(String name, String icon, KeyCodeCombination keyCodeComb) {
|
337
|
Label label = new Label(name);
|
338
|
label.setGraphicTextGap(10);
|
339
|
label.getStyleClass().add("menu-graphics");
|
340
|
label.setMinWidth(175);
|
341
|
label.setMaxWidth(175);
|
342
|
if (icon != null)
|
343
|
label.setGraphic(new ImageView(new Image(icon)));
|
344
|
|
345
|
MenuItem menuItem = new MenuItem();
|
346
|
menuItem.setGraphic(label);
|
347
|
menuItem.setAccelerator(keyCodeComb);
|
348
|
return menuItem;
|
349
|
}
|
350
|
|
351
|
private void setOutputLayoutDisabled(boolean disable) {
|
352
|
outputLayout.setDisable(!testing && disable);
|
353
|
}
|
354
|
|
355
|
private void changeIndicatorState(boolean showResults, boolean result) {
|
356
|
clipBoardIndicator.setVisible(!showResults);
|
357
|
if (showResults) {
|
358
|
clipBoardResults.setGraphic(new ImageView(new Image(result ? getResource("ok.png") : getResource("error.png"))));
|
359
|
}
|
360
|
clipBoardResults.setVisible(showResults);
|
361
|
}
|
362
|
|
363
|
private String getResource(String path) {
|
364
|
return Deserializer.class.getResource(path).toString();
|
365
|
}
|
366
|
|
367
|
@Override
|
368
|
public void loadingInputFileError() {
|
369
|
Platform.runLater(()->{
|
370
|
Report.error("Načítání souboru", null, "Při načítání souboru došlo k chybě.");
|
371
|
});
|
372
|
}
|
373
|
|
374
|
@Override
|
375
|
public void completed(String html, String json, boolean testing) {
|
376
|
// HTML only for clip board content.
|
377
|
results.get().setData(html, json, testing);
|
378
|
|
379
|
Platform.runLater(()->{
|
380
|
boolean result = results.get().contains(testing);
|
381
|
if (testing) {
|
382
|
changeIndicatorState(testing, result);
|
383
|
setInputFromCB.setDisable(!result);
|
384
|
} else {
|
385
|
BadHttpServer.getIntance().setHtmlString(result ? results.get().getHtml() : "<b>--- CHYBA PŘI DESERIALIZACI! ---</b>");
|
386
|
Platform.runLater(() -> webEngine.load(BadHttpServer.getIntance().getURL()));
|
387
|
// TODO webEngine.loadContent(html, "text/html");
|
388
|
setOutputLayoutDisabled(!result);
|
389
|
}
|
390
|
});
|
391
|
}
|
392
|
|
393
|
}
|