Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 5ae26965

Přidáno uživatelem Michal Horký před asi 4 roky(ů)

re #7890

Tvora deamonů vyřešena přes použití ACE knihovny. Deamoni se tedy již nevytváří - knihovna se o plynulé zobrazení stará sama. Na základě zákazníkovi prosby zobrazovány i obsahy souborů bez serializovaných objektů, bylo potřeba rozšířit formátování - soubory mohou být opravdu různé, knihovna zvýrazňuje syntaxe u více jak 100 různých formátů.

Zobrazit rozdíly:

project/Deserializer/src/Deserializer.java
1 1
import java.io.File;
2
import java.net.URL;
3
import java.util.concurrent.atomic.AtomicReference;
4 2

  
5 3
import io.FileWorker;
6 4
import javafx.application.Application;
7 5
import javafx.application.Platform;
8
import javafx.beans.value.ChangeListener;
9
import javafx.beans.value.ObservableValue;
10 6
import javafx.geometry.Insets;
11
import javafx.geometry.Orientation;
12 7
import javafx.geometry.Pos;
13 8
import javafx.scene.Node;
14 9
import javafx.scene.Parent;
15 10
import javafx.scene.Scene;
16 11
import javafx.scene.control.Button;
12
import javafx.scene.control.ComboBox;
17 13
import javafx.scene.control.Label;
18 14
import javafx.scene.control.Menu;
19 15
import javafx.scene.control.MenuBar;
20 16
import javafx.scene.control.MenuItem;
21
import javafx.scene.control.ProgressIndicator;
22
import javafx.scene.control.ScrollPane;
23 17
import javafx.scene.control.Separator;
24 18
import javafx.scene.control.SeparatorMenuItem;
25
import javafx.scene.effect.BlendMode;
19
import javafx.scene.control.Tab;
20
import javafx.scene.control.TabPane;
21
import javafx.scene.control.TabPane.TabClosingPolicy;
26 22
import javafx.scene.image.Image;
27 23
import javafx.scene.image.ImageView;
28
import javafx.scene.input.Clipboard;
29
import javafx.scene.input.DataFormat;
30 24
import javafx.scene.input.KeyCode;
31 25
import javafx.scene.input.KeyCodeCombination;
32 26
import javafx.scene.input.KeyCombination;
33 27
import javafx.scene.layout.BorderPane;
34 28
import javafx.scene.layout.HBox;
35 29
import javafx.scene.layout.Priority;
36
import javafx.scene.layout.Region;
37 30
import javafx.scene.layout.VBox;
38
import javafx.scene.paint.Color;
39
import javafx.scene.web.WebEngine;
40
import javafx.scene.web.WebView;
41 31
import javafx.stage.FileChooser;
42 32
import javafx.stage.Stage;
43 33

  
......
46 36
 */
47 37
public class Deserializer extends Application implements IConversionResults {
48 38
	
49
	public static boolean blockFocusProperty = false;
50
	
51 39
	private Stage stage;
52 40
	private Label fullScreen;
53 41
	
54
	private WebEngine webEngine;
42
	private Editor editor;
55 43
	private VBox outputLayout;
56 44
	
57
	private ProgressIndicator clipBoardIndicator;
58
	private Label clipBoardResults;
59
	private Button setInputFromCB;
60
	private String lastClipBoardData;
61
	
62 45
	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 46
	
70 47
	@Override
71 48
	public void init() throws Exception {
72 49
		super.init();
73
		testing = false;
74
		lastClipBoardData = null;
75 50
		converter = new Converter(this);
76 51
		converter.start();
77
		results = new AtomicReference<jDeserializeResults>(new jDeserializeResults());
78 52
	}
79 53
	
80 54
	@Override
......
83 57
		converter.end();
84 58
		converter.join();
85 59
		Platform.exit();
86
		System.exit(0);
87 60
	}
88 61
	
89 62
	@Override
......
92 65
		stage.getIcons().add(new Image(getResource("logo.png")));
93 66
		stage.setTitle("Java Object Universal Deserializer");
94 67
		stage.setScene(createScene());
95
		stage.setMinWidth(400);
96 68
		stage.show();
69
		
70
		stage.setMinWidth(stage.getWidth());
71
		stage.setMinHeight(stage.getHeight());
97 72

  
98 73
		stage.fullScreenProperty().addListener((obs, oldState, newState) -> {
99 74
			changeFullScreenMenuItem();
100 75
		});
101
		
102
		stage.focusedProperty().addListener(new ChangeListener<Boolean>() {
103
			@Override
104
			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
					DataFormat df = DataFormat.lookupMimeType("text/plain");
114
			    	if (df != null) {
115
			    		String data = (String) Clipboard.getSystemClipboard().getContent(df);
116
			    		
117
			    		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
			    	}
127
				}
128
			}
129
		});
130 76
	}
131 77
	
132 78
	private Scene createScene() {
......
145 91
		return layout;
146 92
	}
147 93
	
148
	private Parent createBodyLayout() {
149
		Label forInputFile = new Label("Soubor k deserializaci:");
150
		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
		inputFile.setPrefWidth(500); // Value to cut the text.
159
		inputFile.setMaxWidth(Double.MAX_VALUE);
160
		
161
		Button setInputFile = new Button("Vstupní soubor");
162
		setInputFile.setOnAction(event -> {
163
			Deserializer.blockFocusProperty = true; // TODO Prohibition to process the selected file.
164
			if (testing) {
165
				converter.setInput(defaultInput, null, false);
166
			} 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
					converter.setInput(file, null, false);
179
				}
180
			}
181
			Deserializer.blockFocusProperty = false;
182
		});
183
		
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
		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
		Label forClipBoardInput = new Label("Vstup ze schránky");
206
		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
		});
213
		setInputFromCB.setDisable(true);
214
		
215
		HBox indicatorLayout = new HBox();
216
		Region region2 = new Region();
217
        HBox.setHgrow(region2, Priority.ALWAYS);
218
		indicatorLayout.getChildren().addAll(forClipBoardInput, region2, clipBoardStateLayout);
219
		
220
		HBox bInputCB = new HBox();
221
		bInputCB.setAlignment(Pos.CENTER_RIGHT);
222
		bInputCB.getChildren().add(setInputFromCB);
223
		
224
		VBox forInput2 = new VBox();
225
		forInput2.setSpacing(5.0);
226
		forInput2.setMinWidth(175);
227
		forInput2.getChildren().addAll(indicatorLayout, bInputCB);
228
		
229
		
230
		
94
	private Node createBodyLayout() {
95
		TabPane header = new TabPane();
96
        header.setTabClosingPolicy(TabClosingPolicy.UNAVAILABLE);
97
        header.getTabs().add(createInputFileTab());
98
        
99
        
100
        editor = new Editor();
101
        
102
        ComboBox<String> modeBox = new ComboBox<String>();
103
        modeBox.setItems(FileWorker.getAceModes());
104
        modeBox.getSelectionModel().select(Editor.DEFAULT_MODE);
105
        modeBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
106
        	if (newValue != null && oldValue != newValue) {
107
        		editor.setMode(modeBox.getValue());
108
            }
109
        });
110
        
111
        ComboBox<String> themeBox = new ComboBox<String>();
112
        themeBox.setItems(FileWorker.getAceThemes());
113
        themeBox.getSelectionModel().select(Editor.DEFAULT_THEME);
114
        themeBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
115
        	if (newValue != null && oldValue != newValue) {
116
        		editor.setTheme(themeBox.getValue());
117
            }
118
        });
119
        
120
        HBox aceSettings = new HBox();
121
        aceSettings.setPadding(new Insets(0, 10, 0, 0));
122
        aceSettings.setSpacing(10.0);
123
        aceSettings.setAlignment(Pos.CENTER_RIGHT);
124
        aceSettings.getChildren().addAll(new Label("Nastavení editoru (režim / téma):"), modeBox, new Label("/"), themeBox);
125
        
126
        VBox editorLayout = new VBox();
127
        editorLayout.setSpacing(5.0);
128
        editorLayout.getChildren().addAll(editor.getView(), aceSettings);
129
        
130
        
231 131
		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
		}
132
		Label outputFile = new Label("    Ještě nebyl vybrán žádný soubor...");
133
		outputFile.setStyle("-fx-font-style: italic;");
240 134
		
241
		Button setOutputFile = new Button("Uložit");
242
		setOutputFile.setOnAction(event -> {
243
			Deserializer.blockFocusProperty = true; // TODO Prohibition to process the selected file.
135
		Button save = new Button("Uložit");
136
		save.setOnAction(event -> {
244 137
			File jsonFile = null;
245 138
			
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
				}
139
			FileChooser fCh = new FileChooser();
140
			fCh.setInitialDirectory(new File("."));
141
			fCh.getExtensionFilters().add(new FileChooser.ExtensionFilter("JSON Files", "*.json"));
142
			File file = fCh.showSaveDialog(stage);
143
			if (file != null) {
144
				jsonFile = file;
145
				outputFile.setText(file.getAbsolutePath());
146
				outputFile.setStyle("-fx-font-weight: bold;");
258 147
			}
259 148
			
260
			if (jsonFile != null && results.get().contains(false)) {
149
			if (jsonFile != null) {
261 150
				String title = "Uložení JSON";
262 151
				try {
263
					FileWorker.saveJson(jsonFile, results.get().getJson());
152
					FileWorker.saveJson(jsonFile, editor.getJson());
264 153
					Report.info(title, null, "Uložení JSON souboru proběhlo v pořádku.");
265 154
				} catch (Exception e) {
266 155
					Report.error(title, null, "Při ukládání JSON souboru nastala chyba.");
267 156
				}
268 157
			}
269
			Deserializer.blockFocusProperty = false;
270 158
		});
271 159
		
272 160
		HBox bOF = new HBox();
273 161
		bOF.setAlignment(Pos.CENTER_RIGHT);
274
		bOF.getChildren().add(setOutputFile);
162
		bOF.getChildren().add(save);
275 163
		
276 164
		outputLayout = new VBox();
165
		outputLayout.setPadding(new Insets(0, 10, 10, 10));
277 166
		outputLayout.setSpacing(5.0);
278 167
		outputLayout.getChildren().add(forOutputFile);
279 168
		outputLayout.getChildren().add(outputFile);
......
281 170
		setOutputLayoutDisabled(true);
282 171
		
283 172
		
284

  
285
        WebView resultantJson = new WebView();
286
		resultantJson.setPrefHeight(400);
287
		webEngine = resultantJson.getEngine();
288
		resultantJson.setBlendMode(BlendMode.DARKEN);
289
		
290
		
291
		
173
		VBox layout = new VBox();
174
		layout.setSpacing(10.0);
175
		VBox.setVgrow(editor.getView(), Priority.ALWAYS);
176
		layout.getChildren().addAll(header, new Separator(), editorLayout, new Separator(), outputLayout);
292 177
		
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);
178
		return layout;
179
	}
180
	
181
	private Tab createInputFileTab() {
182
		Label forInputFile = new Label("Soubor k deserializaci:");
183
		Label inputFile = new Label("    Ještě nebyl vybrán žádný soubor...");
184
		inputFile.setStyle("-fx-font-style: italic;");
185
		inputFile.setPrefWidth(500); // Value to cut the text.
186
		inputFile.setMaxWidth(Double.MAX_VALUE);
297 187
		
188
		Button setInputFile = new Button("Vstupní soubor");
189
		setInputFile.setOnAction(event -> {
190
			FileChooser fCh = new FileChooser();
191
			fCh.setInitialDirectory(new File("."));
192
			// The input can be a zip file or file without a specific extension -> file chooser without extension filters...
193
			// fCh.getExtensionFilters().add(new FileChooser.ExtensionFilter("Binary Files", "*.out"));
194
			File file = fCh.showOpenDialog(stage);
195
			if (file != null) {
196
				setOutputLayoutDisabled(true);
197
				
198
				inputFile.setText(file.getAbsolutePath());
199
				inputFile.setStyle("-fx-font-weight: bold;");
200
				
201
				converter.setInput(file);
202
			}
203
		});
298 204
		
205
		HBox bIF = new HBox();
206
		bIF.setAlignment(Pos.CENTER_RIGHT);
207
		bIF.getChildren().add(setInputFile);
299 208
		
300
		VBox layout = new VBox();
301
		layout.setPadding(new Insets(10.0));
302
		layout.setSpacing(10.0);
303
		VBox.setVgrow(resultantJson, Priority.ALWAYS);
304
		layout.getChildren().addAll(header, new Separator(), resultantJson, new Separator(), outputLayout);
209
		VBox forInput = new VBox();
210
		forInput.setAlignment(Pos.CENTER_LEFT);
211
		forInput.setPadding(new Insets(10, 10, 0, 10));
212
		forInput.setSpacing(5.0);
213
		forInput.getChildren().addAll(forInputFile, inputFile, bIF);
305 214
		
306
		return layout;
215
		return new Tab("Soubor", forInput);
307 216
	}
308 217
	
309 218
	private Node createMenu() {
......
349 258
	}
350 259
	
351 260
	private void setOutputLayoutDisabled(boolean disable) {
352
		outputLayout.setDisable(!testing && disable);
353
	}
354
	
355
	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);
261
		outputLayout.setDisable(disable);
361 262
	}
362 263
	
363 264
	private String getResource(String path) {
......
372 273
	}
373 274

  
374 275
	@Override
375
	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
		Platform.runLater(()->{
380
			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);
276
	public void completed(String html, String json, String loaded) {
277
		Platform.runLater(() -> {
278
			String _html = html; String _json = json;
279
			
280
			boolean showData = html != null && html.length() > 0 && json != null && json.length() > 0;
281
			
282
			if (!showData && Report.confirm("Výsledek deserializace", null, "Při deserializaci došlo k chybě. Je ale možné, že nejde o serializovaná data. Chcete zobrazit, co bylo načteno?")) {
283
				_html = loaded.trim();
284
				_json = loaded.trim();
285
				showData = true;
286
			}
287
			
288
			if (showData) {
289
				editor.setContent(_html, _json);
290
				setOutputLayoutDisabled(false);
389 291
			}
390 292
        });
391 293
	}

Také k dispozici: Unified diff