Projekt

Obecné

Profil

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

    
3
import io.FileWorker;
4
import javafx.application.Application;
5
import javafx.application.Platform;
6
import javafx.geometry.Insets;
7
import javafx.geometry.Pos;
8
import javafx.scene.Node;
9
import javafx.scene.Parent;
10
import javafx.scene.Scene;
11
import javafx.scene.control.Button;
12
import javafx.scene.control.ComboBox;
13
import javafx.scene.control.Label;
14
import javafx.scene.control.Menu;
15
import javafx.scene.control.MenuBar;
16
import javafx.scene.control.MenuItem;
17
import javafx.scene.control.Separator;
18
import javafx.scene.control.SeparatorMenuItem;
19
import javafx.scene.control.Tab;
20
import javafx.scene.control.TabPane;
21
import javafx.scene.control.TabPane.TabClosingPolicy;
22
import javafx.scene.image.Image;
23
import javafx.scene.image.ImageView;
24
import javafx.scene.input.KeyCode;
25
import javafx.scene.input.KeyCodeCombination;
26
import javafx.scene.input.KeyCombination;
27
import javafx.scene.layout.BorderPane;
28
import javafx.scene.layout.HBox;
29
import javafx.scene.layout.Priority;
30
import javafx.scene.layout.VBox;
31
import javafx.stage.FileChooser;
32
import javafx.stage.Stage;
33

    
34
/**
35
 * VM arguments for Java 11: --module-path libs\javafx-sdk-11.0.2\lib --add-modules=javafx.controls
36
 */
37
public class Deserializer extends Application implements IConversionResults {
38
	
39
	private Stage stage;
40
	private Label fullScreen;
41
	
42
	private Editor editor;
43
	private VBox outputLayout;
44
	
45
	private Converter converter;
46
	
47
	@Override
48
	public void init() throws Exception {
49
		super.init();
50
		converter = new Converter(this);
51
		converter.start();
52
	}
53
	
54
	@Override
55
	public void stop() throws Exception {
56
		super.stop();
57
		converter.end();
58
		converter.join();
59
		Platform.exit();
60
	}
61
	
62
	@Override
63
	public void start(Stage stage) throws Exception {
64
		this.stage = stage;
65
		stage.getIcons().add(new Image(getResource("logo.png")));
66
		stage.setTitle("Java Object Universal Deserializer");
67
		stage.setScene(createScene());
68
		stage.show();
69
		
70
		stage.setMinWidth(stage.getWidth());
71
		stage.setMinHeight(stage.getHeight());
72

    
73
		stage.fullScreenProperty().addListener((obs, oldState, newState) -> {
74
			changeFullScreenMenuItem();
75
		});
76
	}
77
	
78
	private Scene createScene() {
79
		createMenu();
80
		Scene scene = new Scene(createLayout());
81
		scene.getStylesheets().add(getResource("main.css"));
82
		return scene;
83
	}
84
	
85
	private Parent createLayout() {
86
		BorderPane layout = new BorderPane();
87
		
88
		layout.setTop(createMenu());
89
		layout.setCenter(createBodyLayout());
90
		
91
		return layout;
92
	}
93
	
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
        
131
		Label forOutputFile = new Label("Výstupní soubor:");
132
		Label outputFile = new Label("    Ještě nebyl vybrán žádný soubor...");
133
		outputFile.setStyle("-fx-font-style: italic;");
134
		
135
		Button save = new Button("Uložit");
136
		save.setOnAction(event -> {
137
			File jsonFile = null;
138
			
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;");
147
			}
148
			
149
			if (jsonFile != null) {
150
				String title = "Uložení JSON";
151
				try {
152
					FileWorker.saveJson(jsonFile, editor.getJson());
153
					Report.info(title, null, "Uložení JSON souboru proběhlo v pořádku.");
154
				} catch (Exception e) {
155
					Report.error(title, null, "Při ukládání JSON souboru nastala chyba.");
156
				}
157
			}
158
		});
159
		
160
		HBox bOF = new HBox();
161
		bOF.setAlignment(Pos.CENTER_RIGHT);
162
		bOF.getChildren().add(save);
163
		
164
		outputLayout = new VBox();
165
		outputLayout.setPadding(new Insets(0, 10, 10, 10));
166
		outputLayout.setSpacing(5.0);
167
		outputLayout.getChildren().add(forOutputFile);
168
		outputLayout.getChildren().add(outputFile);
169
		outputLayout.getChildren().add(bOF);
170
		setOutputLayoutDisabled(true);
171
		
172
		
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);
177
		
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);
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
		});
204
		
205
		HBox bIF = new HBox();
206
		bIF.setAlignment(Pos.CENTER_RIGHT);
207
		bIF.getChildren().add(setInputFile);
208
		
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);
214
		
215
		return new Tab("Soubor", forInput);
216
	}
217
	
218
	private Node createMenu() {
219
		Menu menu = new Menu("Aplikace");
220
		
221
		MenuItem fullScreen = createMenuItem(null, null, new KeyCodeCombination(KeyCode.F, KeyCombination.CONTROL_DOWN));
222
		fullScreen.setOnAction(event -> {
223
			stage.setFullScreen(!stage.isFullScreen());
224
		});
225
		this.fullScreen = (Label) fullScreen.getGraphic();
226
		changeFullScreenMenuItem();
227
        
228
        MenuItem close = createMenuItem("Ukončit", getResource("close.png"), new KeyCodeCombination(KeyCode.E, KeyCombination.CONTROL_DOWN));
229
        close.setOnAction(event -> {
230
			Platform.exit();
231
		});
232
        
233
        menu.getItems().addAll(fullScreen, new SeparatorMenuItem(), close);
234
        
235
        MenuBar bar = new MenuBar();
236
        bar.getMenus().add(menu);
237
        return bar;
238
	}
239
	
240
	private void changeFullScreenMenuItem() {
241
		fullScreen.setText(stage.isFullScreen() ? "Normální zobrazení" : "Plné zobrazení");
242
		fullScreen.setGraphic(new ImageView(stage.isFullScreen() ? getResource("normal.png") : getResource("full.png")));
243
	}
244
	
245
	private MenuItem createMenuItem(String name, String icon, KeyCodeCombination keyCodeComb) {
246
		Label label = new Label(name);
247
		label.setGraphicTextGap(10);
248
		label.getStyleClass().add("menu-graphics");
249
		label.setMinWidth(175);
250
		label.setMaxWidth(175);
251
		if (icon != null)
252
			label.setGraphic(new ImageView(new Image(icon)));
253
		
254
		MenuItem menuItem = new MenuItem();
255
		menuItem.setGraphic(label);
256
		menuItem.setAccelerator(keyCodeComb);
257
        return menuItem;
258
	}
259
	
260
	private void setOutputLayoutDisabled(boolean disable) {
261
		outputLayout.setDisable(disable);
262
	}
263
	
264
	private String getResource(String path) {
265
		return Deserializer.class.getResource(path).toString();
266
	}
267
	
268
	@Override
269
	public void loadingInputFileError() {
270
		Platform.runLater(()->{
271
			Report.error("Načítání souboru", null, "Při načítání souboru došlo k chybě.");
272
        });
273
	}
274

    
275
	@Override
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);
291
			}
292
        });
293
	}
294
	
295
}
(3-3/6)