Projekt

Obecné

Profil

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

    
4
import io.Database.DB_Messenger;
5
import io.FileWorker;
6
import javafx.application.Application;
7
import javafx.application.Platform;
8
import javafx.collections.FXCollections;
9
import javafx.collections.ObservableList;
10
import javafx.geometry.Insets;
11
import javafx.geometry.Pos;
12
import javafx.scene.Node;
13
import javafx.scene.Parent;
14
import javafx.scene.Scene;
15
import javafx.scene.control.Button;
16
import javafx.scene.control.ComboBox;
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.PasswordField;
22
import javafx.scene.control.Separator;
23
import javafx.scene.control.SeparatorMenuItem;
24
import javafx.scene.control.Tab;
25
import javafx.scene.control.TabPane;
26
import javafx.scene.control.TabPane.TabClosingPolicy;
27
import javafx.scene.control.TextField;
28
import javafx.scene.image.Image;
29
import javafx.scene.image.ImageView;
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.VBox;
37
import javafx.stage.FileChooser;
38
import javafx.stage.Stage;
39

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

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

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

    
397
	@Override
398
	public void completed(String html, String json, String loaded) {
399
		Platform.runLater(() -> {
400
			String _html = html; String _json = json;
401
			
402
			boolean showData = html != null && html.length() > 0 && json != null && json.length() > 0;
403
			
404
			if (!showData && loaded != null && loaded.length() > 0 &&
405
					Report.confirm("Výsledek deserializace", null, "Při deserializaci došlo k chybě. "
406
							+ "Je ale možné, že nejde o serializovaná data. Chcete zobrazit, co bylo načteno?")) {
407
				_html = loaded.trim();
408
				_json = loaded.trim();
409
				showData = true;
410
			}
411
			
412
			if (showData) {
413
				editor.setContent(_html, _json);
414
				setOutputLayoutDisabled(false);
415
			}
416
        });
417
	}
418
	
419
}
(6-6/6)