Projekt

Obecné

Profil

Stáhnout (12.3 KB) Statistiky
| Větev: | Tag: | Revize:
1 03a2d08f Michal Horký
import java.io.File;
2 a5d16f0a horkmi
import java.net.URL;
3
import java.util.concurrent.atomic.AtomicReference;
4 03a2d08f Michal Horký
5 49fd8648 Michal Horký
import io.FileWorker;
6 03a2d08f Michal Horký
import javafx.application.Application;
7 49fd8648 Michal Horký
import javafx.application.Platform;
8
import javafx.beans.value.ChangeListener;
9
import javafx.beans.value.ObservableValue;
10 03a2d08f Michal Horký
import javafx.geometry.Insets;
11 49fd8648 Michal Horký
import javafx.geometry.Orientation;
12 03a2d08f Michal Horký
import javafx.geometry.Pos;
13 49fd8648 Michal Horký
import javafx.scene.Node;
14 03a2d08f Michal Horký
import javafx.scene.Parent;
15
import javafx.scene.Scene;
16
import javafx.scene.control.Button;
17
import javafx.scene.control.Label;
18 49fd8648 Michal Horký
import javafx.scene.control.Menu;
19
import javafx.scene.control.MenuBar;
20
import javafx.scene.control.MenuItem;
21
import javafx.scene.control.ProgressIndicator;
22 a5d16f0a horkmi
import javafx.scene.control.ScrollPane;
23 49fd8648 Michal Horký
import javafx.scene.control.Separator;
24
import javafx.scene.control.SeparatorMenuItem;
25 a5d16f0a horkmi
import javafx.scene.effect.BlendMode;
26 49fd8648 Michal Horký
import javafx.scene.image.Image;
27
import javafx.scene.image.ImageView;
28 a5d16f0a horkmi
import javafx.scene.input.Clipboard;
29
import javafx.scene.input.DataFormat;
30 49fd8648 Michal Horký
import javafx.scene.input.KeyCode;
31
import javafx.scene.input.KeyCodeCombination;
32
import javafx.scene.input.KeyCombination;
33
import javafx.scene.layout.BorderPane;
34 03a2d08f Michal Horký
import javafx.scene.layout.HBox;
35 49fd8648 Michal Horký
import javafx.scene.layout.Priority;
36
import javafx.scene.layout.Region;
37 03a2d08f Michal Horký
import javafx.scene.layout.VBox;
38 a5d16f0a horkmi
import javafx.scene.paint.Color;
39 49fd8648 Michal Horký
import javafx.scene.web.WebEngine;
40
import javafx.scene.web.WebView;
41 03a2d08f Michal Horký
import javafx.stage.FileChooser;
42
import javafx.stage.Stage;
43
44 49fd8648 Michal Horký
/**
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 03a2d08f Michal Horký
	
49 a5d16f0a horkmi
	public static boolean blockFocusProperty = false;
50
	
51 03a2d08f Michal Horký
	private Stage stage;
52 49fd8648 Michal Horký
	private Label fullScreen;
53 a5d16f0a horkmi
	
54 49fd8648 Michal Horký
	private WebEngine webEngine;
55
	private VBox outputLayout;
56 03a2d08f Michal Horký
	
57 a5d16f0a horkmi
	private ProgressIndicator clipBoardIndicator;
58
	private Label clipBoardResults;
59
	private Button setInputFromCB;
60
	private String lastClipBoardData;
61
	
62 49fd8648 Michal Horký
	private Converter converter;
63 a5d16f0a horkmi
	private AtomicReference<jDeserializeResults> results;
64 03a2d08f Michal Horký
	
65
	// TODO delete after deserializer debugging is complete...
66 49fd8648 Michal Horký
	private File defaultInput = new File("a2");
67
	private File defaultOutput = new File("results.json");
68 03a2d08f Michal Horký
	private boolean testing;
69
	
70
	@Override
71
	public void init() throws Exception {
72
		super.init();
73 a5d16f0a horkmi
		testing = false;
74
		lastClipBoardData = null;
75 49fd8648 Michal Horký
		converter = new Converter(this);
76
		converter.start();
77 a5d16f0a horkmi
		results = new AtomicReference<jDeserializeResults>(new jDeserializeResults());
78 49fd8648 Michal Horký
	}
79
	
80
	@Override
81
	public void stop() throws Exception {
82
		super.stop();
83
		converter.end();
84
		converter.join();
85 efc1d63e @havlijan17
		Platform.exit();
86
		System.exit(0);
87 03a2d08f Michal Horký
	}
88
	
89
	@Override
90
	public void start(Stage stage) throws Exception {
91
		this.stage = stage;
92 a5d16f0a horkmi
		stage.getIcons().add(new Image(getResource("logo.png")));
93 49fd8648 Michal Horký
		stage.setTitle("Java Object Universal Deserializer");
94 03a2d08f Michal Horký
		stage.setScene(createScene());
95
		stage.setMinWidth(400);
96
		stage.show();
97 efc1d63e @havlijan17
98 49fd8648 Michal Horký
		stage.fullScreenProperty().addListener((obs, oldState, newState) -> {
99
			changeFullScreenMenuItem();
100
		});
101
		
102 a5d16f0a horkmi
		stage.focusedProperty().addListener(new ChangeListener<Boolean>() {
103 49fd8648 Michal Horký
			@Override
104 a5d16f0a horkmi
			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 49fd8648 Michal Horký
					DataFormat df = DataFormat.lookupMimeType("text/plain");
114
			    	if (df != null) {
115
			    		String data = (String) Clipboard.getSystemClipboard().getContent(df);
116
			    		
117 a5d16f0a horkmi
			    		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 49fd8648 Michal Horký
			    	}
127 a5d16f0a horkmi
				}
128 49fd8648 Michal Horký
			}
129
		});
130 03a2d08f Michal Horký
	}
131
	
132
	private Scene createScene() {
133 49fd8648 Michal Horký
		createMenu();
134
		Scene scene = new Scene(createLayout());
135 a5d16f0a horkmi
		scene.getStylesheets().add(getResource("main.css"));
136 49fd8648 Michal Horký
		return scene;
137 03a2d08f Michal Horký
	}
138
	
139
	private Parent createLayout() {
140 49fd8648 Michal Horký
		BorderPane layout = new BorderPane();
141
		
142
		layout.setTop(createMenu());
143
		layout.setCenter(createBodyLayout());
144 03a2d08f Michal Horký
		
145 49fd8648 Michal Horký
		return layout;
146
	}
147
	
148
	private Parent createBodyLayout() {
149 03a2d08f Michal Horký
		Label forInputFile = new Label("Soubor k deserializaci:");
150 49fd8648 Michal Horký
		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 a5d16f0a horkmi
		inputFile.setPrefWidth(500); // Value to cut the text.
159
		inputFile.setMaxWidth(Double.MAX_VALUE);
160 03a2d08f Michal Horký
		
161 b639237c Michal Horký
		Button setInputFile = new Button("Vstupní soubor");
162 03a2d08f Michal Horký
		setInputFile.setOnAction(event -> {
163 a5d16f0a horkmi
			Deserializer.blockFocusProperty = true; // TODO Prohibition to process the selected file.
164 49fd8648 Michal Horký
			if (testing) {
165 a5d16f0a horkmi
				converter.setInput(defaultInput, null, false);
166 49fd8648 Michal Horký
			} 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 a5d16f0a horkmi
					converter.setInput(file, null, false);
179 49fd8648 Michal Horký
				}
180 03a2d08f Michal Horký
			}
181 a5d16f0a horkmi
			Deserializer.blockFocusProperty = false;
182 03a2d08f Michal Horký
		});
183 49fd8648 Michal Horký
		
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 a5d16f0a horkmi
		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 49fd8648 Michal Horký
		Label forClipBoardInput = new Label("Vstup ze schránky");
206 a5d16f0a horkmi
		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 49fd8648 Michal Horký
		});
213 a5d16f0a horkmi
		setInputFromCB.setDisable(true);
214 49fd8648 Michal Horký
		
215
		HBox indicatorLayout = new HBox();
216
		Region region2 = new Region();
217
        HBox.setHgrow(region2, Priority.ALWAYS);
218 a5d16f0a horkmi
		indicatorLayout.getChildren().addAll(forClipBoardInput, region2, clipBoardStateLayout);
219 49fd8648 Michal Horký
		
220
		HBox bInputCB = new HBox();
221
		bInputCB.setAlignment(Pos.CENTER_RIGHT);
222 a5d16f0a horkmi
		bInputCB.getChildren().add(setInputFromCB);
223 49fd8648 Michal Horký
		
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 03a2d08f Michal Horký
		setOutputFile.setOnAction(event -> {
243 a5d16f0a horkmi
			Deserializer.blockFocusProperty = true; // TODO Prohibition to process the selected file.
244 49fd8648 Michal Horký
			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 a5d16f0a horkmi
			if (jsonFile != null && results.get().contains(false)) {
261 49fd8648 Michal Horký
				String title = "Uložení JSON";
262
				try {
263 a5d16f0a horkmi
					FileWorker.saveJson(jsonFile, results.get().getJson());
264 49fd8648 Michal Horký
					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 03a2d08f Michal Horký
			}
269 a5d16f0a horkmi
			Deserializer.blockFocusProperty = false;
270 03a2d08f Michal Horký
		});
271
		
272 49fd8648 Michal Horký
		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 a5d16f0a horkmi
285
        WebView resultantJson = new WebView();
286 49fd8648 Michal Horký
		resultantJson.setPrefHeight(400);
287
		webEngine = resultantJson.getEngine();
288 a5d16f0a horkmi
		resultantJson.setBlendMode(BlendMode.DARKEN);
289
		
290 49fd8648 Michal Horký
		
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 a5d16f0a horkmi
		VBox.setVgrow(resultantJson, Priority.ALWAYS);
304 49fd8648 Michal Horký
		layout.getChildren().addAll(header, new Separator(), resultantJson, new Separator(), outputLayout);
305 03a2d08f Michal Horký
		
306
		return layout;
307
	}
308
	
309 49fd8648 Michal Horký
	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 a5d16f0a horkmi
        MenuItem close = createMenuItem("Ukončit", getResource("close.png"), new KeyCodeCombination(KeyCode.E, KeyCombination.CONTROL_DOWN));
320 49fd8648 Michal Horký
        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 a5d16f0a horkmi
		fullScreen.setGraphic(new ImageView(stage.isFullScreen() ? getResource("normal.png") : getResource("full.png")));
334 49fd8648 Michal Horký
	}
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 a5d16f0a horkmi
	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 49fd8648 Michal Horký
	@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 a5d16f0a horkmi
	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 49fd8648 Michal Horký
		Platform.runLater(()->{
380 a5d16f0a horkmi
			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 49fd8648 Michal Horký
			}
390
        });
391 03a2d08f Michal Horký
	}
392
	
393
}