Revize 49fd8648
Přidáno uživatelem Michal Horký před asi 4 roky(ů)
project/Deserializer/src/Deserializer.java | ||
---|---|---|
1 | 1 |
import java.io.File; |
2 |
import java.io.IOException; |
|
2 | 3 |
|
4 |
import io.FileWorker; |
|
3 | 5 |
import javafx.application.Application; |
6 |
import javafx.application.Platform; |
|
7 |
import javafx.beans.value.ChangeListener; |
|
8 |
import javafx.beans.value.ObservableValue; |
|
4 | 9 |
import javafx.geometry.Insets; |
10 |
import javafx.geometry.Orientation; |
|
5 | 11 |
import javafx.geometry.Pos; |
12 |
import javafx.scene.Node; |
|
6 | 13 |
import javafx.scene.Parent; |
7 | 14 |
import javafx.scene.Scene; |
8 | 15 |
import javafx.scene.control.Button; |
9 | 16 |
import javafx.scene.control.Label; |
17 |
import javafx.scene.control.Menu; |
|
18 |
import javafx.scene.control.MenuBar; |
|
19 |
import javafx.scene.control.MenuItem; |
|
20 |
import javafx.scene.control.ProgressIndicator; |
|
21 |
import javafx.scene.control.Separator; |
|
22 |
import javafx.scene.control.SeparatorMenuItem; |
|
23 |
import javafx.scene.control.Alert.AlertType; |
|
24 |
import javafx.scene.image.Image; |
|
25 |
import javafx.scene.image.ImageView; |
|
26 |
import javafx.scene.input.KeyCode; |
|
27 |
import javafx.scene.input.KeyCodeCombination; |
|
28 |
import javafx.scene.input.KeyCombination; |
|
29 |
import javafx.scene.layout.BorderPane; |
|
10 | 30 |
import javafx.scene.layout.HBox; |
31 |
import javafx.scene.layout.Priority; |
|
32 |
import javafx.scene.layout.Region; |
|
11 | 33 |
import javafx.scene.layout.VBox; |
34 |
import javafx.scene.web.WebEngine; |
|
35 |
import javafx.scene.web.WebView; |
|
12 | 36 |
import javafx.stage.FileChooser; |
13 | 37 |
import javafx.stage.Stage; |
14 | 38 |
|
15 |
public class Deserializer extends Application { |
|
39 |
/** |
|
40 |
* VM arguments for Java 11: --module-path libs\javafx-sdk-11.0.2\lib --add-modules=javafx.controls |
|
41 |
*/ |
|
42 |
public class Deserializer extends Application implements IConversionResults { |
|
16 | 43 |
|
17 | 44 |
private Stage stage; |
18 |
private File inputFile; |
|
19 |
private File outputFile; |
|
45 |
private Label fullScreen; |
|
46 |
private WebEngine webEngine; |
|
47 |
private VBox outputLayout; |
|
20 | 48 |
|
21 |
Button convert;
|
|
49 |
private Converter converter;
|
|
22 | 50 |
|
23 | 51 |
// TODO delete after deserializer debugging is complete... |
52 |
private File defaultInput = new File("a2"); |
|
53 |
private File defaultOutput = new File("results.json"); |
|
24 | 54 |
private boolean testing; |
25 | 55 |
|
26 | 56 |
public static void main(String[] args) { |
... | ... | |
30 | 60 |
@Override |
31 | 61 |
public void init() throws Exception { |
32 | 62 |
super.init(); |
33 |
testing = false; |
|
63 |
testing = true; |
|
64 |
converter = new Converter(this); |
|
65 |
converter.start(); |
|
66 |
} |
|
67 |
|
|
68 |
@Override |
|
69 |
public void stop() throws Exception { |
|
70 |
super.stop(); |
|
71 |
converter.end(); |
|
72 |
converter.join(); |
|
34 | 73 |
} |
35 | 74 |
|
36 | 75 |
@Override |
37 | 76 |
public void start(Stage stage) throws Exception { |
38 | 77 |
this.stage = stage; |
39 |
stage.setTitle("Java object universal deserializer"); |
|
78 |
stage.getIcons().add(new Image("img/logo.png")); |
|
79 |
stage.setTitle("Java Object Universal Deserializer"); |
|
40 | 80 |
stage.setScene(createScene()); |
41 | 81 |
stage.setMinWidth(400); |
42 | 82 |
stage.show(); |
43 | 83 |
|
84 |
stage.fullScreenProperty().addListener((obs, oldState, newState) -> { |
|
85 |
changeFullScreenMenuItem(); |
|
86 |
}); |
|
87 |
|
|
88 |
stage.focusedProperty().addListener(new ChangeListener<Boolean>() |
|
89 |
{ |
|
90 |
@Override |
|
91 |
public void changed(ObservableValue<? extends Boolean> obs, Boolean oldValue, Boolean newValue) |
|
92 |
{ |
|
93 |
System.out.println(newValue); |
|
94 |
|
|
95 |
/* |
|
96 |
byte arr[] = FileWorker.loadByteArray(new File("b2")); |
|
97 |
byte arr2[] = FileWorker.loadByteArray(new File("b")); |
|
98 |
|
|
99 |
byte con[] = FileWorker.load(new File("b")); |
|
100 |
|
|
101 |
if (arr.length == arr2.length && arr2.length == con.length) { |
|
102 |
System.out.println("TRRUE"); |
|
103 |
for (int i = 0; i < arr.length; i++) { |
|
104 |
if (arr[i] != arr2[i] || arr2[i] != con[i]) { |
|
105 |
System.out.println("CHCHYBA!!!"); |
|
106 |
break; |
|
107 |
} |
|
108 |
} |
|
109 |
} |
|
110 |
System.out.println("COMPLETE"); |
|
111 |
*/ |
|
112 |
/*Set<DataFormat> types = Clipboard.getSystemClipboard().getContentTypes(); |
|
113 |
System.out.println("Detected types: " + types.size()); |
|
114 |
for (DataFormat s : types) { |
|
115 |
System.out.println(s); |
|
116 |
} |
|
117 |
|
|
118 |
try { |
|
119 |
byte[] orig = FileWorker.loadByteArray(new File("b")); |
|
120 |
//byte[] orig = FileWorker.getBBB(); |
|
121 |
|
|
122 |
DataFormat df = DataFormat.lookupMimeType("text/plain"); |
|
123 |
if (df != null) { |
|
124 |
// System.out.println(Clipboard.getSystemClipboard().getContent(df).getClass()); |
|
125 |
String data = (String) Clipboard.getSystemClipboard().getContent(df); |
|
126 |
byte[] buffer = data.getBytes(); |
|
127 |
|
|
128 |
boolean changeBites = false; |
|
129 |
|
|
130 |
List<Byte> bl = new ArrayList<Byte>(); |
|
131 |
for (int i = 0; i < buffer.length; i++) { |
|
132 |
if (changeBites) { |
|
133 |
buffer[i] = (byte) ((buffer[i] & 0x0F) | 0xE0); |
|
134 |
changeBites = false; |
|
135 |
} else if (buffer[i] == (byte) 0xC2 || buffer[i] == (byte) 0x0D) { |
|
136 |
continue; |
|
137 |
} else if (buffer[i] == (byte) 0xC3) { |
|
138 |
changeBites = true; |
|
139 |
continue; |
|
140 |
} |
|
141 |
bl.add(buffer[i]); |
|
142 |
} |
|
143 |
|
|
144 |
System.out.println(bl.size() + " " + orig.length); |
|
145 |
for (int i = 0; i < orig.length; i++) { |
|
146 |
if (bl.get(i) != orig[i]) { |
|
147 |
System.out.println(i); |
|
148 |
for (int j = i - 1; j < i + 50; j++) { |
|
149 |
System.out.println(String.format("%02X", bl.get(j)) + " " + String.format("%02X", orig[j])); |
|
150 |
} |
|
151 |
break; |
|
152 |
} |
|
153 |
} |
|
154 |
} |
|
155 |
} catch (Exception e) { |
|
156 |
e.printStackTrace(); |
|
157 |
}*/ |
|
158 |
} |
|
159 |
}); |
|
160 |
|
|
44 | 161 |
//tests |
45 | 162 |
//Converter thread = new Converter(this, new File("simple_arrays.out"), new File("bcha.json")); |
46 | 163 |
//thread.start(); |
47 | 164 |
} |
48 | 165 |
|
49 | 166 |
private Scene createScene() { |
50 |
return new Scene(createLayout()); |
|
167 |
createMenu(); |
|
168 |
Scene scene = new Scene(createLayout()); |
|
169 |
scene.getStylesheets().add("css/main.css"); |
|
170 |
return scene; |
|
51 | 171 |
} |
52 | 172 |
|
53 | 173 |
private Parent createLayout() { |
54 |
VBox layout = new VBox(); |
|
55 |
layout.setPadding(new Insets(10.0)); |
|
56 |
layout.setSpacing(10.0); |
|
174 |
BorderPane layout = new BorderPane(); |
|
175 |
|
|
176 |
layout.setTop(createMenu()); |
|
177 |
layout.setCenter(createBodyLayout()); |
|
57 | 178 |
|
179 |
return layout; |
|
180 |
} |
|
181 |
|
|
182 |
private Parent createBodyLayout() { |
|
58 | 183 |
Label forInputFile = new Label("Soubor k deserializaci:"); |
59 |
Label inputFile = new Label(" Ještě nebyl vybrán žádný soubor..."); |
|
60 |
inputFile.setStyle("-fx-font-style: italic;"); |
|
61 |
VBox forInput = new VBox(); |
|
62 |
forInput.getChildren().add(forInputFile); |
|
63 |
forInput.getChildren().add(inputFile); |
|
184 |
Label inputFile; |
|
185 |
if (!testing) { |
|
186 |
inputFile = new Label(" Ještě nebyl vybrán žádný soubor..."); |
|
187 |
inputFile.setStyle("-fx-font-style: italic;"); |
|
188 |
} else { |
|
189 |
inputFile = new Label(defaultOutput.getAbsolutePath()); |
|
190 |
inputFile.setStyle("-fx-font-weight: bold;"); |
|
191 |
} |
|
192 |
// TODO inputFile.setPrefWidth(400); |
|
193 |
inputFile.setWrapText(true); |
|
64 | 194 |
|
65 |
Label forOutputFile = new Label("Výstupní soubor:"); |
|
66 |
Label outputFile = new Label(" Ještě nebyl vybrán žádný soubor..."); |
|
67 |
outputFile.setStyle("-fx-font-style: italic;"); |
|
68 |
VBox forOutput = new VBox(); |
|
69 |
forOutput.getChildren().add(forOutputFile); |
|
70 |
forOutput.getChildren().add(outputFile); |
|
71 |
|
|
72 |
convert = new Button("Převést"); |
|
73 |
convert.setOnAction(event -> { |
|
74 |
// a.data a b.data are customers test files (binary files). |
|
75 |
convert.setDisable(true); |
|
76 |
Converter thread = new Converter(this, testing ? new File("a.data") : this.inputFile, testing ? new File("results.json") : this.outputFile); |
|
77 |
thread.start(); |
|
78 |
}); |
|
79 |
convert.setDisable(!testing); |
|
80 | 195 |
Button setInputFile = new Button("Vstupní soubor"); |
81 | 196 |
setInputFile.setOnAction(event -> { |
82 |
FileChooser fCh = new FileChooser(); |
|
83 |
fCh.setInitialDirectory(new File(".")); |
|
84 |
// The input can be a zip file or file without a specific extension -> file chooser without extension filters... |
|
85 |
// fCh.getExtensionFilters().add(new FileChooser.ExtensionFilter("Binary Files", "*.out")); |
|
86 |
File file = fCh.showOpenDialog(stage); |
|
87 |
if (file != null) { |
|
88 |
this.inputFile = file; |
|
89 |
inputFile.setText(this.inputFile == null ? "" : this.inputFile.getAbsolutePath()); |
|
90 |
inputFile.setStyle("-fx-font-weight: bold;"); |
|
91 |
convert.setDisable(this.inputFile == null || this.outputFile == null); |
|
197 |
if (testing) { |
|
198 |
converter.setInput(defaultInput); |
|
199 |
} else { |
|
200 |
FileChooser fCh = new FileChooser(); |
|
201 |
fCh.setInitialDirectory(new File(".")); |
|
202 |
// The input can be a zip file or file without a specific extension -> file chooser without extension filters... |
|
203 |
// fCh.getExtensionFilters().add(new FileChooser.ExtensionFilter("Binary Files", "*.out")); |
|
204 |
File file = fCh.showOpenDialog(stage); |
|
205 |
if (file != null) { |
|
206 |
setOutputLayoutDisabled(true); |
|
207 |
|
|
208 |
inputFile.setText(file.getAbsolutePath()); |
|
209 |
inputFile.setStyle("-fx-font-weight: bold;"); |
|
210 |
|
|
211 |
converter.setInput(file); |
|
212 |
} |
|
92 | 213 |
} |
93 | 214 |
}); |
94 |
Button setOutputFile = new Button("Výstupní soubor"); |
|
215 |
|
|
216 |
HBox bIF = new HBox(); |
|
217 |
bIF.setAlignment(Pos.CENTER_RIGHT); |
|
218 |
bIF.getChildren().add(setInputFile); |
|
219 |
|
|
220 |
VBox forInput = new VBox(); |
|
221 |
forInput.setSpacing(5.0); |
|
222 |
forInput.getChildren().addAll(forInputFile, inputFile, bIF); |
|
223 |
|
|
224 |
|
|
225 |
Label forClipBoardInput = new Label("Vstup ze schránky"); |
|
226 |
ProgressIndicator indicator = new ProgressIndicator(); |
|
227 |
indicator.setMaxHeight(38); // TODO |
|
228 |
Button setInputAccToCB = new Button("Použít"); |
|
229 |
setInputAccToCB.setOnAction(event -> { |
|
230 |
// TODO |
|
231 |
}); |
|
232 |
|
|
233 |
HBox indicatorLayout = new HBox(); |
|
234 |
Region region2 = new Region(); |
|
235 |
HBox.setHgrow(region2, Priority.ALWAYS); |
|
236 |
indicatorLayout.getChildren().addAll(forClipBoardInput, region2, indicator); |
|
237 |
|
|
238 |
HBox bInputCB = new HBox(); |
|
239 |
bInputCB.setAlignment(Pos.CENTER_RIGHT); |
|
240 |
bInputCB.getChildren().add(setInputAccToCB); |
|
241 |
|
|
242 |
VBox forInput2 = new VBox(); |
|
243 |
forInput2.setSpacing(5.0); |
|
244 |
forInput2.setMinWidth(175); |
|
245 |
forInput2.getChildren().addAll(indicatorLayout, bInputCB); |
|
246 |
|
|
247 |
|
|
248 |
|
|
249 |
Label forOutputFile = new Label("Výstupní soubor:"); |
|
250 |
Label outputFile; |
|
251 |
if (!testing) { |
|
252 |
outputFile = new Label(" Ještě nebyl vybrán žádný soubor..."); |
|
253 |
outputFile.setStyle("-fx-font-style: italic;"); |
|
254 |
} else { |
|
255 |
outputFile = new Label(defaultOutput.getAbsolutePath()); |
|
256 |
outputFile.setStyle("-fx-font-weight: bold;"); |
|
257 |
} |
|
258 |
|
|
259 |
Button setOutputFile = new Button("Uložit"); |
|
95 | 260 |
setOutputFile.setOnAction(event -> { |
96 |
FileChooser fCh = new FileChooser(); |
|
97 |
fCh.setInitialDirectory(new File(".")); |
|
98 |
fCh.getExtensionFilters().add(new FileChooser.ExtensionFilter("JSON Files", "*.json")); |
|
99 |
File file = fCh.showSaveDialog(stage); |
|
100 |
if (file != null) { |
|
101 |
this.outputFile = file; |
|
102 |
outputFile.setText(this.outputFile == null ? "" : this.outputFile.getAbsolutePath()); |
|
103 |
outputFile.setStyle("-fx-font-weight: bold;"); |
|
104 |
convert.setDisable(this.inputFile == null || this.outputFile == null); |
|
261 |
File jsonFile = null; |
|
262 |
|
|
263 |
if (testing) { |
|
264 |
jsonFile = defaultOutput; |
|
265 |
} else { |
|
266 |
FileChooser fCh = new FileChooser(); |
|
267 |
fCh.setInitialDirectory(new File(".")); |
|
268 |
fCh.getExtensionFilters().add(new FileChooser.ExtensionFilter("JSON Files", "*.json")); |
|
269 |
File file = fCh.showSaveDialog(stage); |
|
270 |
if (file != null) { |
|
271 |
jsonFile = file; |
|
272 |
outputFile.setText(file.getAbsolutePath()); |
|
273 |
outputFile.setStyle("-fx-font-weight: bold;"); |
|
274 |
} |
|
275 |
} |
|
276 |
|
|
277 |
if (jsonFile != null && webEngine.getDocument() != null) { |
|
278 |
String title = "Uložení JSON"; |
|
279 |
try { |
|
280 |
FileWorker.saveJson(jsonFile, "Ahoj"/* TODO webEngine.getDocument().getTextContent()*/); |
|
281 |
Report.info(title, null, "Uložení JSON souboru proběhlo v pořádku."); |
|
282 |
} catch (Exception e) { |
|
283 |
Report.error(title, null, "Při ukládání JSON souboru nastala chyba."); |
|
284 |
} |
|
105 | 285 |
} |
106 | 286 |
}); |
107 |
HBox footer = new HBox(); |
|
108 |
footer.setSpacing(10.0); |
|
109 |
footer.setAlignment(Pos.CENTER_RIGHT); |
|
110 |
footer.getChildren().add(setInputFile); |
|
111 |
footer.getChildren().add(setOutputFile); |
|
112 |
footer.getChildren().add(convert); |
|
113 | 287 |
|
114 |
layout.getChildren().add(forInput); |
|
115 |
layout.getChildren().add(forOutput); |
|
116 |
layout.getChildren().add(footer); |
|
288 |
HBox bOF = new HBox(); |
|
289 |
bOF.setAlignment(Pos.CENTER_RIGHT); |
|
290 |
bOF.getChildren().add(setOutputFile); |
|
291 |
|
|
292 |
outputLayout = new VBox(); |
|
293 |
outputLayout.setSpacing(5.0); |
|
294 |
outputLayout.getChildren().add(forOutputFile); |
|
295 |
outputLayout.getChildren().add(outputFile); |
|
296 |
outputLayout.getChildren().add(bOF); |
|
297 |
setOutputLayoutDisabled(true); |
|
298 |
|
|
299 |
|
|
300 |
WebView resultantJson = new WebView(); |
|
301 |
resultantJson.setPrefHeight(400); |
|
302 |
webEngine = resultantJson.getEngine(); |
|
303 |
|
|
304 |
|
|
305 |
|
|
306 |
HBox header = new HBox(); |
|
307 |
header.setSpacing(10.0); |
|
308 |
HBox.setHgrow(forInput, Priority.ALWAYS); |
|
309 |
header.getChildren().addAll(forInput, new Separator(Orientation.VERTICAL), forInput2); |
|
310 |
|
|
311 |
|
|
312 |
|
|
313 |
VBox layout = new VBox(); |
|
314 |
layout.setPadding(new Insets(10.0)); |
|
315 |
layout.setSpacing(10.0); |
|
316 |
layout.getChildren().addAll(header, new Separator(), resultantJson, new Separator(), outputLayout); |
|
117 | 317 |
|
118 | 318 |
return layout; |
119 | 319 |
} |
120 | 320 |
|
121 |
public void convertIsComplete() { |
|
122 |
convert.setDisable(false); |
|
321 |
private Node createMenu() { |
|
322 |
Menu menu = new Menu("Aplikace"); |
|
323 |
|
|
324 |
MenuItem fullScreen = createMenuItem(null, null, new KeyCodeCombination(KeyCode.F, KeyCombination.CONTROL_DOWN)); |
|
325 |
fullScreen.setOnAction(event -> { |
|
326 |
stage.setFullScreen(!stage.isFullScreen()); |
|
327 |
}); |
|
328 |
this.fullScreen = (Label) fullScreen.getGraphic(); |
|
329 |
changeFullScreenMenuItem(); |
|
330 |
|
|
331 |
MenuItem close = createMenuItem("Ukončit", "img/close.png", new KeyCodeCombination(KeyCode.E, KeyCombination.CONTROL_DOWN)); |
|
332 |
close.setOnAction(event -> { |
|
333 |
Platform.exit(); |
|
334 |
}); |
|
335 |
|
|
336 |
menu.getItems().addAll(fullScreen, new SeparatorMenuItem(), close); |
|
337 |
|
|
338 |
MenuBar bar = new MenuBar(); |
|
339 |
bar.getMenus().add(menu); |
|
340 |
return bar; |
|
341 |
} |
|
342 |
|
|
343 |
private void changeFullScreenMenuItem() { |
|
344 |
fullScreen.setText(stage.isFullScreen() ? "Normální zobrazení" : "Plné zobrazení"); |
|
345 |
fullScreen.setGraphic(new ImageView(stage.isFullScreen() ? "img/normal.png" : "img/full.png")); |
|
346 |
} |
|
347 |
|
|
348 |
private MenuItem createMenuItem(String name, String icon, KeyCodeCombination keyCodeComb) { |
|
349 |
Label label = new Label(name); |
|
350 |
label.setGraphicTextGap(10); |
|
351 |
label.getStyleClass().add("menu-graphics"); |
|
352 |
label.setMinWidth(175); |
|
353 |
label.setMaxWidth(175); |
|
354 |
if (icon != null) |
|
355 |
label.setGraphic(new ImageView(new Image(icon))); |
|
356 |
|
|
357 |
MenuItem menuItem = new MenuItem(); |
|
358 |
menuItem.setGraphic(label); |
|
359 |
menuItem.setAccelerator(keyCodeComb); |
|
360 |
return menuItem; |
|
361 |
} |
|
362 |
|
|
363 |
private void setOutputLayoutDisabled(boolean disable) { |
|
364 |
outputLayout.setDisable(!testing && disable); |
|
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(final String json) { |
|
376 |
Platform.runLater(()->{ |
|
377 |
String _json = json; |
|
378 |
setOutputLayoutDisabled(_json != null); |
|
379 |
if (_json == null) { |
|
380 |
_json = "--- Chyba při deserializaci! ---"; |
|
381 |
} |
|
382 |
webEngine.loadContent(_json, "text/plain"); |
|
383 |
}); |
|
123 | 384 |
} |
124 | 385 |
|
125 | 386 |
} |
Také k dispozici: Unified diff
#re #7879
Ještě není dokončeno - commit kvůli merge.