Projekt

Obecné

Profil

Stáhnout (11.5 KB) Statistiky
| Větev: | Tag: | Revize:
1
import java.io.File;
2
import java.io.IOException;
3

    
4
import io.FileWorker;
5
import javafx.application.Application;
6
import javafx.application.Platform;
7
import javafx.beans.value.ChangeListener;
8
import javafx.beans.value.ObservableValue;
9
import javafx.geometry.Insets;
10
import javafx.geometry.Orientation;
11
import javafx.geometry.Pos;
12
import javafx.scene.Node;
13
import javafx.scene.Parent;
14
import javafx.scene.Scene;
15
import javafx.scene.control.Button;
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;
30
import javafx.scene.layout.HBox;
31
import javafx.scene.layout.Priority;
32
import javafx.scene.layout.Region;
33
import javafx.scene.layout.VBox;
34
import javafx.scene.web.WebEngine;
35
import javafx.scene.web.WebView;
36
import javafx.stage.FileChooser;
37
import javafx.stage.Stage;
38

    
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 {
43
	
44
	private Stage stage;
45
	private Label fullScreen;
46
	private WebEngine webEngine;
47
	private VBox outputLayout;
48
	
49
	private Converter converter;
50
	
51
	// TODO delete after deserializer debugging is complete...
52
	private File defaultInput = new File("a2");
53
	private File defaultOutput = new File("results.json");
54
	private boolean testing;
55
	
56
	public static void main(String[] args) {
57
		launch(args);
58
	}
59

    
60
	@Override
61
	public void init() throws Exception {
62
		super.init();
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();
73
	}
74
	
75
	@Override
76
	public void start(Stage stage) throws Exception {
77
		this.stage = stage;
78
		stage.getIcons().add(new Image("img/logo.png"));
79
		stage.setTitle("Java Object Universal Deserializer");
80
		stage.setScene(createScene());
81
		stage.setMinWidth(400);
82
		stage.show();
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
		
161
		//tests
162
		//Converter thread = new Converter(this, new File("simple_arrays.out"), new File("bcha.json"));
163
		//thread.start();
164
	}
165
	
166
	private Scene createScene() {
167
		createMenu();
168
		Scene scene = new Scene(createLayout());
169
		scene.getStylesheets().add("css/main.css");
170
		return scene;
171
	}
172
	
173
	private Parent createLayout() {
174
		BorderPane layout = new BorderPane();
175
		
176
		layout.setTop(createMenu());
177
		layout.setCenter(createBodyLayout());
178
		
179
		return layout;
180
	}
181
	
182
	private Parent createBodyLayout() {
183
		Label forInputFile = new Label("Soubor k deserializaci:");
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);
194
		
195
		Button setInputFile = new Button("Vstupní soubor");
196
		setInputFile.setOnAction(event -> {
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
				}
213
			}
214
		});
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");
260
		setOutputFile.setOnAction(event -> {
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
				}
285
			}
286
		});
287
		
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);
317
		
318
		return layout;
319
	}
320
	
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
        });
384
	}
385
	
386
}
(3-3/5)