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