Projekt

Obecné

Profil

Stáhnout (12.3 KB) Statistiky
| Větev: | Tag: | Revize:
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
	}
86
	
87
	@Override
88
	public void start(Stage stage) throws Exception {
89
		this.stage = stage;
90
		stage.getIcons().add(new Image(getResource("logo.png")));
91
		stage.setTitle("Java Object Universal Deserializer");
92
		stage.setScene(createScene());
93
		stage.setMinWidth(400);
94
		stage.show();
95
        
96
		stage.fullScreenProperty().addListener((obs, oldState, newState) -> {
97
			changeFullScreenMenuItem();
98
		});
99
		
100
		stage.focusedProperty().addListener(new ChangeListener<Boolean>() {
101
			@Override
102
			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
					DataFormat df = DataFormat.lookupMimeType("text/plain");
112
			    	if (df != null) {
113
			    		String data = (String) Clipboard.getSystemClipboard().getContent(df);
114
			    		
115
			    		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
			    	}
125
				}
126
			}
127
		});
128
	}
129
	
130
	private Scene createScene() {
131
		createMenu();
132
		Scene scene = new Scene(createLayout());
133
		scene.getStylesheets().add(getResource("main.css"));
134
		return scene;
135
	}
136
	
137
	private Parent createLayout() {
138
		BorderPane layout = new BorderPane();
139
		
140
		layout.setTop(createMenu());
141
		layout.setCenter(createBodyLayout());
142
		
143
		return layout;
144
	}
145
	
146
	private Parent createBodyLayout() {
147
		Label forInputFile = new Label("Soubor k deserializaci:");
148
		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
		inputFile.setPrefWidth(500); // Value to cut the text.
157
		inputFile.setMaxWidth(Double.MAX_VALUE);
158
		
159
		Button setInputFile = new Button("Vstupní soubor");
160
		setInputFile.setOnAction(event -> {
161
			Deserializer.blockFocusProperty = true; // TODO Prohibition to process the selected file.
162
			if (testing) {
163
				converter.setInput(defaultInput, null, false);
164
			} 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
					converter.setInput(file, null, false);
177
				}
178
			}
179
			Deserializer.blockFocusProperty = false;
180
		});
181
		
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
		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
		Label forClipBoardInput = new Label("Vstup ze schránky");
204
		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
		});
211
		setInputFromCB.setDisable(true);
212
		
213
		HBox indicatorLayout = new HBox();
214
		Region region2 = new Region();
215
        HBox.setHgrow(region2, Priority.ALWAYS);
216
		indicatorLayout.getChildren().addAll(forClipBoardInput, region2, clipBoardStateLayout);
217
		
218
		HBox bInputCB = new HBox();
219
		bInputCB.setAlignment(Pos.CENTER_RIGHT);
220
		bInputCB.getChildren().add(setInputFromCB);
221
		
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
		setOutputFile.setOnAction(event -> {
241
			Deserializer.blockFocusProperty = true; // TODO Prohibition to process the selected file.
242
			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
			if (jsonFile != null && results.get().contains(false)) {
259
				String title = "Uložení JSON";
260
				try {
261
					FileWorker.saveJson(jsonFile, results.get().getJson());
262
					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
			}
267
			Deserializer.blockFocusProperty = false;
268
		});
269
		
270
		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

    
283
        WebView resultantJson = new WebView();
284
		resultantJson.setPrefHeight(400);
285
		webEngine = resultantJson.getEngine();
286
		resultantJson.setBlendMode(BlendMode.DARKEN);
287
		
288
		
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
		VBox.setVgrow(resultantJson, Priority.ALWAYS);
302
		layout.getChildren().addAll(header, new Separator(), resultantJson, new Separator(), outputLayout);
303
		
304
		return layout;
305
	}
306
	
307
	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
        MenuItem close = createMenuItem("Ukončit", getResource("close.png"), new KeyCodeCombination(KeyCode.E, KeyCombination.CONTROL_DOWN));
318
        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
		fullScreen.setGraphic(new ImageView(stage.isFullScreen() ? getResource("normal.png") : getResource("full.png")));
332
	}
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
	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
	@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
	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
		Platform.runLater(()->{
378
			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
			}
388
        });
389
	}
390
	
391
}
(4-4/7)