Projekt

Obecné

Profil

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

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

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

    
77
		stage.fullScreenProperty().addListener((obs, oldState, newState) -> {
78
			changeFullScreenMenuItem();
79
		});
80
	}
81
	
82
	private Scene createScene() {
83
		createMenu();
84
		Scene scene = new Scene(createLayout());
85
		scene.getStylesheets().add(getResource("main.css"));
86
		return scene;
87
	}
88
	
89
	private Parent createLayout() {
90
		BorderPane layout = new BorderPane();
91
		
92
		layout.setTop(createMenu());
93
		layout.setCenter(createBodyLayout());
94
		
95
		return layout;
96
	}
97
	
98
	private Node createBodyLayout() {
99
		TabPane header = new TabPane();
100
        header.setTabClosingPolicy(TabClosingPolicy.UNAVAILABLE);
101
        header.getTabs().add(createInputFileTab());
102
        header.getTabs().add(createQueryTab());
103
        
104
        
105
        editor = new Editor();
106
        
107
        ComboBox<String> modeBox = new ComboBox<String>();
108
        modeBox.setItems(FileWorker.getAceModes());
109
        modeBox.getSelectionModel().select(Editor.DEFAULT_MODE);
110
        modeBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
111
        	if (newValue != null && oldValue != newValue) {
112
        		editor.setMode(modeBox.getValue());
113
            }
114
        });
115
        
116
        ComboBox<String> themeBox = new ComboBox<String>();
117
        themeBox.setItems(FileWorker.getAceThemes());
118
        themeBox.getSelectionModel().select(Editor.DEFAULT_THEME);
119
        themeBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
120
        	if (newValue != null && oldValue != newValue) {
121
        		editor.setTheme(themeBox.getValue());
122
            }
123
        });
124
        
125
        HBox aceSettings = new HBox();
126
        aceSettings.setPadding(new Insets(0, 10, 0, 0));
127
        aceSettings.setSpacing(10.0);
128
        aceSettings.setAlignment(Pos.CENTER_RIGHT);
129
        aceSettings.getChildren().addAll(new Label("Nastavení editoru (režim / téma):"), modeBox, new Label("/"), themeBox);
130
        
131
        VBox editorLayout = new VBox();
132
        editorLayout.setSpacing(5.0);
133
        editorLayout.getChildren().addAll(editor.getView(), aceSettings);
134
        
135
        
136
		Label forOutputFile = new Label("Výstupní soubor:");
137
		Label outputFile = new Label("    Ještě nebyl vybrán žádný soubor...");
138
		outputFile.setStyle("-fx-font-style: italic;");
139
		
140
		Button save = new Button("Uložit");
141
		save.setOnAction(event -> {
142
			File jsonFile = null;
143
			
144
			FileChooser fCh = new FileChooser();
145
			fCh.setInitialDirectory(new File("."));
146
			fCh.getExtensionFilters().add(new FileChooser.ExtensionFilter("JSON Files", "*.json"));
147
			File file = fCh.showSaveDialog(stage);
148
			if (file != null) {
149
				jsonFile = file;
150
				outputFile.setText(file.getAbsolutePath());
151
				outputFile.setStyle("-fx-font-weight: bold;");
152
			}
153
			
154
			if (jsonFile != null) {
155
				String title = "Uložení JSON";
156
				try {
157
					FileWorker.saveJson(jsonFile, editor.getJson());
158
					Report.info(title, null, "Uložení JSON souboru proběhlo v pořádku.");
159
				} catch (Exception e) {
160
					Report.error(title, null, "Při ukládání JSON souboru nastala chyba.");
161
				}
162
			}
163
		});
164
		
165
		HBox bOF = new HBox();
166
		bOF.setAlignment(Pos.CENTER_RIGHT);
167
		bOF.getChildren().add(save);
168
		
169
		outputLayout = new VBox();
170
		outputLayout.setPadding(new Insets(0, 10, 10, 10));
171
		outputLayout.setSpacing(5.0);
172
		outputLayout.getChildren().add(forOutputFile);
173
		outputLayout.getChildren().add(outputFile);
174
		outputLayout.getChildren().add(bOF);
175
		setOutputLayoutDisabled(true);
176
		
177
		
178
		VBox layout = new VBox();
179
		layout.setSpacing(10.0);
180
		VBox.setVgrow(editor.getView(), Priority.ALWAYS);
181
		layout.getChildren().addAll(header, new Separator(), editorLayout, new Separator(), outputLayout);
182
		
183
		return layout;
184
	}
185
	
186
	private Tab createQueryTab() {
187
		String promptTexts[] = new String[] { "jdbc:mysql://server/nazevDB", "jdbc:oracle:thin:@server:port:nazevDB" };
188
		
189
		Label rdbmsLabel = new Label("Typ DB");
190
		Label urlLabel = new Label("URL");
191
		ComboBox<String> rdbms = new ComboBox<String>(FXCollections.observableArrayList(new String[] { "MySQL", "ORACLE" }));
192
		TextField url = new TextField();
193
		
194
		rdbms.setOnAction(event -> {
195
			url.setPromptText(promptTexts[rdbms.getSelectionModel().getSelectedIndex()]);
196
		});
197
		
198
		rdbms.getSelectionModel().selectFirst();
199
		url.setPromptText(promptTexts[0]);
200

    
201
		
202
		Label userLabel = new Label("Uživatel");
203
		Label passwordLabel = new Label("Heslo");
204
		TextField user = new TextField();
205
		PasswordField password = new PasswordField();
206
		
207
		VBox rdbmsLayout = new VBox();
208
		rdbmsLayout.getChildren().addAll(rdbmsLabel, rdbms);
209
		VBox urlLayout = new VBox();
210
		urlLayout.getChildren().addAll(urlLabel, url);
211
		VBox userLayout = new VBox();
212
		userLayout.getChildren().addAll(userLabel, user);
213
		VBox passwordLayout = new VBox();
214
		passwordLayout.getChildren().addAll(passwordLabel, password);
215
		
216
		rdbmsLabel.setStyle("-fx-font-weight: bold;");
217
		urlLabel.setStyle("-fx-font-weight: bold;");
218
		userLabel.setStyle("-fx-font-weight: bold;");
219
		passwordLabel.setStyle("-fx-font-weight: bold;");
220
		
221
		url.setMinWidth(400);
222
		user.setMinWidth(100);
223
		user.setPrefWidth(200);
224
		user.setMaxWidth(300);
225
		password.setMinWidth(100);
226
		password.setPrefWidth(200);
227
		password.setMaxWidth(300);
228
	
229
		HBox.setHgrow(urlLayout, Priority.ALWAYS);
230
		
231
		HBox connLayout = new HBox();
232
		connLayout.setSpacing(10);
233
		connLayout.getChildren().addAll(rdbmsLayout, urlLayout, userLayout, passwordLayout);
234
		
235
		
236
		Label select = new Label("SELECT"), from = new Label("FROM"), where = new Label("WHERE"), queryEnd = new Label(";");
237
		TextField column = new TextField(), table = new TextField(), cond = new TextField();
238
		
239
		select.setStyle("-fx-font-weight: bold;");
240
		from.setStyle("-fx-font-weight: bold;");
241
		where.setStyle("-fx-font-weight: bold;");
242
		queryEnd.setStyle("-fx-font-weight: bold;");
243
		
244
		column.setPromptText("sloupec");
245
		table.setPromptText("tabulka");
246
		cond.setPromptText("podminka");
247
		
248
		HBox.setHgrow(column, Priority.ALWAYS);
249
		HBox.setHgrow(table, Priority.ALWAYS);
250
		HBox.setHgrow(cond, Priority.ALWAYS);
251
		
252
		HBox queryLayout = new HBox();
253
		queryLayout.setAlignment(Pos.CENTER);
254
		queryLayout.setSpacing(5.0);
255
		queryLayout.getChildren().addAll(select, column, from, table, where, cond, queryEnd);
256
		
257
		Button setQuery = new Button("Spustit");
258
		setQuery.setOnAction(event -> {
259
			String driver = rdbms.getValue().equals("MySQL") ? "com.mysql.cj.jdbc.Driver" : "oracle.jdbc.driver.OracleDriver";
260
			converter.setInput(null, new DB_Messenger(driver, url.getText().trim(), user.getText().trim(), password.getText().trim(),
261
					table.getText().trim(), cond.getText().trim(), column.getText().trim()));
262
		});
263
		
264
		HBox bIF = new HBox();
265
		bIF.setAlignment(Pos.CENTER_RIGHT);
266
		bIF.getChildren().add(setQuery);
267
		
268
		VBox forInput = new VBox();
269
		forInput.setPadding(new Insets(10, 10, 0, 10));
270
		forInput.setSpacing(5.0);
271
		forInput.getChildren().addAll(connLayout, queryLayout, bIF);
272
		
273
		return new Tab("SQL", forInput);
274
	}
275
	
276
	private Tab createInputFileTab() {
277
		Label forInputFile = new Label("Soubor k deserializaci:");
278
		Label inputFile = new Label("    Ještě nebyl vybrán žádný soubor...");
279
		inputFile.setStyle("-fx-font-style: italic;");
280
		inputFile.setPrefWidth(500); // Value to cut the text.
281
		inputFile.setMaxWidth(Double.MAX_VALUE);
282
		
283
		Button setInputFile = new Button("Vstupní soubor");
284
		setInputFile.setOnAction(event -> {
285
			FileChooser fCh = new FileChooser();
286
			fCh.setInitialDirectory(new File("."));
287
			// The input can be a zip file or file without a specific extension -> file chooser without extension filters...
288
			// fCh.getExtensionFilters().add(new FileChooser.ExtensionFilter("Binary Files", "*.out"));
289
			File file = fCh.showOpenDialog(stage);
290
			if (file != null) {
291
				setOutputLayoutDisabled(true);
292
				
293
				inputFile.setText(file.getAbsolutePath());
294
				inputFile.setStyle("-fx-font-weight: bold;");
295
				
296
				converter.setInput(file, null);
297
			}
298
		});
299
		
300
		HBox bIF = new HBox();
301
		bIF.setAlignment(Pos.CENTER_RIGHT);
302
		bIF.getChildren().add(setInputFile);
303
		
304
		VBox forInput = new VBox();
305
		forInput.setAlignment(Pos.CENTER_LEFT);
306
		forInput.setPadding(new Insets(10, 10, 0, 10));
307
		forInput.setSpacing(5.0);
308
		forInput.getChildren().addAll(forInputFile, inputFile, bIF);
309
		
310
		return new Tab("Soubor", forInput);
311
	}
312
	
313
	private Node createMenu() {
314
		Menu menu = new Menu("Aplikace");
315
		
316
		MenuItem fullScreen = createMenuItem(null, null, new KeyCodeCombination(KeyCode.F, KeyCombination.CONTROL_DOWN));
317
		fullScreen.setOnAction(event -> {
318
			stage.setFullScreen(!stage.isFullScreen());
319
		});
320
		this.fullScreen = (Label) fullScreen.getGraphic();
321
		changeFullScreenMenuItem();
322
        
323
        MenuItem close = createMenuItem("Ukončit", getResource("close.png"), new KeyCodeCombination(KeyCode.E, KeyCombination.CONTROL_DOWN));
324
        close.setOnAction(event -> {
325
			Platform.exit();
326
		});
327
        
328
        menu.getItems().addAll(fullScreen, new SeparatorMenuItem(), close);
329
        
330
        MenuBar bar = new MenuBar();
331
        bar.getMenus().add(menu);
332
        return bar;
333
	}
334
	
335
	private void changeFullScreenMenuItem() {
336
		fullScreen.setText(stage.isFullScreen() ? "Normální zobrazení" : "Plné zobrazení");
337
		fullScreen.setGraphic(new ImageView(stage.isFullScreen() ? getResource("normal.png") : getResource("full.png")));
338
	}
339
	
340
	private MenuItem createMenuItem(String name, String icon, KeyCodeCombination keyCodeComb) {
341
		Label label = new Label(name);
342
		label.setGraphicTextGap(10);
343
		label.getStyleClass().add("menu-graphics");
344
		label.setMinWidth(175);
345
		label.setMaxWidth(175);
346
		if (icon != null)
347
			label.setGraphic(new ImageView(new Image(icon)));
348
		
349
		MenuItem menuItem = new MenuItem();
350
		menuItem.setGraphic(label);
351
		menuItem.setAccelerator(keyCodeComb);
352
        return menuItem;
353
	}
354
	
355
	private void setOutputLayoutDisabled(boolean disable) {
356
		outputLayout.setDisable(disable);
357
	}
358
	
359
	private String getResource(String path) {
360
		return Deserializer.class.getResource(path).toString();
361
	}
362
	
363
	@Override
364
	public void loadingInputFileError() {
365
		Platform.runLater(()->{
366
			Report.error("Načítání souboru", null, "Při načítání souboru došlo k chybě.");
367
        });
368
	}
369

    
370
	@Override
371
	public void completed(String html, String json, String loaded) {
372
		Platform.runLater(() -> {
373
			String _html = html; String _json = json;
374
			
375
			boolean showData = html != null && html.length() > 0 && json != null && json.length() > 0;
376
			
377
			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?")) {
378
				_html = loaded.trim();
379
				_json = loaded.trim();
380
				showData = true;
381
			}
382
			
383
			if (showData) {
384
				editor.setContent(_html, _json);
385
				setOutputLayoutDisabled(false);
386
			}
387
        });
388
	}
389
	
390
}
(3-3/6)