Projekt

Obecné

Profil

Stáhnout (14.5 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
        VBox.setVgrow(editor.getView(), Priority.ALWAYS);
148
        editorLayout.getChildren().addAll(editor.getView(), aceSettings);
149
        
150
        
151
		Label forOutputFile = new Label("Výstupní soubor:");
152
		Label outputFile = new Label("    Ještě nebyl vybrán žádný soubor...");
153
		outputFile.setStyle("-fx-font-style: italic;");
154
		
155
		Button save = new Button("Uložit");
156
		save.setOnAction(event -> {
157
			File jsonFile = null;
158
			
159
			FileChooser fCh = new FileChooser();
160
			fCh.setInitialDirectory(new File("."));
161
			fCh.getExtensionFilters().add(new FileChooser.ExtensionFilter("JSON Files", "*.json"));
162
			File file = fCh.showSaveDialog(stage);
163
			if (file != null) {
164
				jsonFile = file;
165
				outputFile.setText(file.getAbsolutePath());
166
				outputFile.setStyle("-fx-font-weight: bold;");
167
			}
168
			
169
			if (jsonFile != null) {
170
				String title = "Uložení JSON";
171
				try {
172
					FileWorker.saveJson(jsonFile, editor.getJson());
173
					Report.info(title, null, "Uložení JSON souboru proběhlo v pořádku.");
174
				} catch (Exception e) {
175
					Report.error(title, null, "Při ukládání JSON souboru nastala chyba.");
176
				}
177
			}
178
		});
179
		
180
		HBox bOF = new HBox();
181
		bOF.setAlignment(Pos.CENTER_RIGHT);
182
		bOF.getChildren().add(save);
183
		
184
		outputLayout = new VBox();
185
		outputLayout.setPadding(new Insets(0, 10, 10, 10));
186
		outputLayout.setSpacing(5.0);
187
		outputLayout.getChildren().add(forOutputFile);
188
		outputLayout.getChildren().add(outputFile);
189
		outputLayout.getChildren().add(bOF);
190
		setOutputLayoutDisabled(true);
191
		
192
		
193
		VBox layout = new VBox();
194
		layout.setSpacing(10.0);
195
		VBox.setVgrow(editorLayout, Priority.ALWAYS);
196
		layout.getChildren().addAll(header, new Separator(), editorLayout, new Separator(), outputLayout);
197
		
198
		return layout;
199
	}
200
	
201
	private Tab createQueryTab() {
202
		String propType = properties.getProperty(FileWorker.DB_TYPE);
203
		String propUrl = properties.getProperty(FileWorker.DB_URL);
204
		String propUser = properties.getProperty(FileWorker.DB_USER);
205
        
206
		
207
		String promptTexts[] = new String[] { "jdbc:oracle:thin:@server:port:nazevDB", "jdbc:sqlserver://server:port;databaseName=nazevDB;", "jdbc:mysql://server:port/nazevDB" };
208
		String dbTypeTexts[] = new String[] { "ORACLE", "MS-SQL", "MySQL" };
209
		String dbDriverClasses[] = new String[] { "oracle.jdbc.driver.OracleDriver", "com.microsoft.sqlserver.jdbc.SQLServerDriver", "com.mysql.cj.jdbc.Driver" };
210
		
211
		Label rdbmsLabel = new Label("Typ DB");
212
		Label urlLabel = new Label("URL");
213
		ComboBox<String> rdbms = new ComboBox<String>(FXCollections.observableArrayList(dbTypeTexts));
214
		TextField url = new TextField(propUrl == null ? "" : propUrl);
215
		
216
		if (propType == null) {
217
			rdbms.getSelectionModel().selectFirst();
218
			url.setPromptText(promptTexts[0]);
219
		} else {
220
			rdbms.getSelectionModel().select(propType);
221
			url.setPromptText(promptTexts[rdbms.getItems().indexOf(propType)]);
222
		}
223
		rdbms.setOnAction(event -> {
224
			url.setPromptText(promptTexts[rdbms.getSelectionModel().getSelectedIndex()]);
225
		});
226
		
227
		
228
		Label userLabel = new Label("Uživatel");
229
		Label passwordLabel = new Label("Heslo");
230
		TextField user = new TextField(propUser == null ? "" : propUser);
231
		PasswordField password = new PasswordField();
232
		
233
		VBox rdbmsLayout = new VBox();
234
		rdbmsLayout.getChildren().addAll(rdbmsLabel, rdbms);
235
		VBox urlLayout = new VBox();
236
		urlLayout.getChildren().addAll(urlLabel, url);
237
		VBox userLayout = new VBox();
238
		userLayout.getChildren().addAll(userLabel, user);
239
		VBox passwordLayout = new VBox();
240
		passwordLayout.getChildren().addAll(passwordLabel, password);
241
		
242
		rdbmsLabel.setStyle("-fx-font-weight: bold;");
243
		urlLabel.setStyle("-fx-font-weight: bold;");
244
		userLabel.setStyle("-fx-font-weight: bold;");
245
		passwordLabel.setStyle("-fx-font-weight: bold;");
246
		
247
		url.setMinWidth(400);
248
		user.setMinWidth(100);
249
		user.setPrefWidth(200);
250
		user.setMaxWidth(300);
251
		password.setMinWidth(100);
252
		password.setPrefWidth(200);
253
		password.setMaxWidth(300);
254
	
255
		HBox.setHgrow(urlLayout, Priority.ALWAYS);
256
		
257
		HBox connLayout = new HBox();
258
		connLayout.setSpacing(10);
259
		connLayout.getChildren().addAll(rdbmsLayout, urlLayout, userLayout, passwordLayout);
260
		
261
		
262
		Label select = new Label("SELECT"), from = new Label("FROM"), where = new Label("WHERE"), queryEnd = new Label(";");
263
		TextField column = new TextField(), table = new TextField(), cond = new TextField();
264
		
265
		select.setStyle("-fx-font-weight: bold;");
266
		from.setStyle("-fx-font-weight: bold;");
267
		where.setStyle("-fx-font-weight: bold;");
268
		queryEnd.setStyle("-fx-font-weight: bold;");
269
		
270
		column.setPromptText("sloupec");
271
		table.setPromptText("tabulka");
272
		cond.setPromptText("podminka");
273
		
274
		HBox.setHgrow(column, Priority.ALWAYS);
275
		HBox.setHgrow(table, Priority.ALWAYS);
276
		HBox.setHgrow(cond, Priority.ALWAYS);
277
		
278
		HBox queryLayout = new HBox();
279
		queryLayout.setAlignment(Pos.CENTER);
280
		queryLayout.setSpacing(5.0);
281
		queryLayout.getChildren().addAll(select, column, from, table, where, cond, queryEnd);
282
		
283
		Button setQuery = new Button("Spustit");
284
		setQuery.setOnAction(event -> {
285
			String urlStr = url.getText().trim();
286
			String userStr = user.getText().trim();
287
			properties.setProperty(FileWorker.DB_TYPE, rdbms.getValue());
288
			properties.setProperty(FileWorker.DB_URL, urlStr);
289
			properties.setProperty(FileWorker.DB_USER, userStr);
290
			
291
			String driver = dbDriverClasses[rdbms.getItems().indexOf(rdbms.getValue())];
292
			converter.setInput(null, new DB_Messenger(driver, urlStr, userStr, password.getText().trim(),
293
					table.getText().trim(), cond.getText().trim(), column.getText().trim()));
294
		});
295
		
296
		HBox bIF = new HBox();
297
		bIF.setAlignment(Pos.CENTER_RIGHT);
298
		bIF.getChildren().add(setQuery);
299
		
300
		VBox forInput = new VBox();
301
		forInput.setPadding(new Insets(10, 10, 0, 10));
302
		forInput.setSpacing(5.0);
303
		forInput.getChildren().addAll(connLayout, queryLayout, bIF);
304
		
305
		return new Tab("SQL", forInput);
306
	}
307
	
308
	private Tab createInputFileTab() {
309
		Label forInputFile = new Label("Soubor k deserializaci:");
310
		Label inputFile = new Label("    Ještě nebyl vybrán žádný soubor...");
311
		inputFile.setStyle("-fx-font-style: italic;");
312
		inputFile.setPrefWidth(500); // Value to cut the text.
313
		inputFile.setMaxWidth(Double.MAX_VALUE);
314
		
315
		Button setInputFile = new Button("Vstupní soubor");
316
		setInputFile.setOnAction(event -> {
317
			FileChooser fCh = new FileChooser();
318
			fCh.setInitialDirectory(new File("."));
319
			// The input can be a zip file or file without a specific extension -> file chooser without extension filters...
320
			// fCh.getExtensionFilters().add(new FileChooser.ExtensionFilter("Binary Files", "*.out"));
321
			
322
			String inputDir = properties.getProperty(FileWorker.INPUT_DIR);
323
			if (inputDir != null) {
324
				File initDir = new File(inputDir);
325
				if (initDir.exists()) {
326
					fCh.setInitialDirectory(initDir);
327
				}
328
			}
329
			
330
			File file = fCh.showOpenDialog(stage);
331
			if (file != null) {
332
				properties.setProperty(FileWorker.INPUT_DIR, file.getAbsoluteFile().getParent());
333
				setOutputLayoutDisabled(true);
334
				
335
				inputFile.setText(file.getAbsolutePath());
336
				inputFile.setStyle("-fx-font-weight: bold;");
337
				
338
				converter.setInput(file, null);
339
			}
340
		});
341
		
342
		HBox bIF = new HBox();
343
		bIF.setAlignment(Pos.CENTER_RIGHT);
344
		bIF.getChildren().add(setInputFile);
345
		
346
		VBox forInput = new VBox();
347
		forInput.setAlignment(Pos.CENTER_LEFT);
348
		forInput.setPadding(new Insets(10, 10, 0, 10));
349
		forInput.setSpacing(5.0);
350
		forInput.getChildren().addAll(forInputFile, inputFile, bIF);
351
		
352
		return new Tab("Soubor", forInput);
353
	}
354
	
355
	private Node createMenu() {
356
		Menu menu = new Menu("Aplikace");
357
		
358
		MenuItem fullScreen = createMenuItem(null, null, new KeyCodeCombination(KeyCode.F, KeyCombination.CONTROL_DOWN));
359
		fullScreen.setOnAction(event -> {
360
			stage.setFullScreen(!stage.isFullScreen());
361
		});
362
		this.fullScreen = (Label) fullScreen.getGraphic();
363
		changeFullScreenMenuItem();
364
        
365
        MenuItem close = createMenuItem("Ukončit", FileWorker.getResource("/close.png"), new KeyCodeCombination(KeyCode.E, KeyCombination.CONTROL_DOWN));
366
        close.setOnAction(event -> {
367
			Platform.exit();
368
		});
369
        
370
        menu.getItems().addAll(fullScreen, new SeparatorMenuItem(), close);
371
        
372
        MenuBar bar = new MenuBar();
373
        bar.getMenus().add(menu);
374
        return bar;
375
	}
376
	
377
	private void changeFullScreenMenuItem() {
378
		fullScreen.setText(stage.isFullScreen() ? "Normální zobrazení" : "Plné zobrazení");
379
		fullScreen.setGraphic(new ImageView(stage.isFullScreen() ? FileWorker.getResource("/normal.png") : FileWorker.getResource("/full.png")));
380
	}
381
	
382
	private MenuItem createMenuItem(String name, String icon, KeyCodeCombination keyCodeComb) {
383
		Label label = new Label(name);
384
		label.setGraphicTextGap(10);
385
		label.getStyleClass().add("menu-graphics");
386
		label.setMinWidth(175);
387
		label.setMaxWidth(175);
388
		if (icon != null)
389
			label.setGraphic(new ImageView(new Image(icon)));
390
		
391
		MenuItem menuItem = new MenuItem();
392
		menuItem.setGraphic(label);
393
		menuItem.setAccelerator(keyCodeComb);
394
        return menuItem;
395
	}
396
	
397
	private void setOutputLayoutDisabled(boolean disable) {
398
		outputLayout.setDisable(disable);
399
	}
400
	
401
	@Override
402
	public void loadingInputFileError() {
403
		Platform.runLater(()->{
404
			Report.error("Načítání souboru", null, "Při načítání souboru došlo k chybě.");
405
        });
406
	}
407

    
408
	@Override
409
	public void completed(String html, String json, String loaded) {
410
		Platform.runLater(() -> {
411
			String _html = html; String _json = json;
412
			
413
			boolean showData = html != null && json != null;
414
			
415
			if (!showData && loaded != null &&
416
					Report.confirm("Výsledek deserializace", null, "Při deserializaci došlo k chybě. "
417
							+ "Je ale možné, že nejde o serializovaná data. Chcete zobrazit, co bylo načteno?")) {
418
				_html = loaded.trim();
419
				_json = _html;
420
				showData = true;
421
			}
422
			
423
			if (showData) {
424
				editor.setContent(_html, _json);
425
				setOutputLayoutDisabled(false);
426
			}
427
        });
428
	}
429
	
430
}
(6-6/6)