Debian Codegraph @

src/main/org/h2/tools/GUIConsole.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.tools;
7
8 import java.awt.Button;
9 import java.awt.Color;
10 import java.awt.Dimension;
11 import java.awt.Font;
12 import java.awt.Frame;
13 import java.awt.GraphicsEnvironment;
14 import java.awt.GridBagConstraints;
15 import java.awt.GridBagLayout;
16 import java.awt.Image;
17 import java.awt.Insets;
18 import java.awt.Label;
19 import java.awt.MenuItem;
20 import java.awt.Panel;
21 import java.awt.PopupMenu;
22 import java.awt.SystemColor;
23 import java.awt.TextArea;
24 import java.awt.TextField;
25 import java.awt.Toolkit;
26 import java.awt.Window;
27 import java.awt.event.ActionEvent;
28 import java.awt.event.ActionListener;
29 import java.awt.event.MouseEvent;
30 import java.awt.event.MouseListener;
31 import java.awt.event.WindowEvent;
32 import java.awt.event.WindowListener;
33 import java.io.IOException;
34 import java.util.Locale;
35
36 import org.h2.jdbc.JdbcConnection;
37 import org.h2.util.Utils;
38
39 /**
40 * Console for environments with AWT support.
41 */
42 public class GUIConsole extends Console implements ActionListener, MouseListener, WindowListener {
43 private long lastOpenNs;
44
45 private boolean trayIconUsed;
46 private Font font;
47
48 private Frame statusFrame;
49 private TextField urlText;
50 private Button startBrowser;
51
52 private Frame createFrame;
53 private TextField pathField, userField, passwordField, passwordConfirmationField;
54 private Button createButton;
55 private TextArea errorArea;
56
57 private Object tray;
58 private Object trayIcon;
59
60 @Override
61 void show() {
62 if (!GraphicsEnvironment.isHeadless()) {
63 loadFont();
64 try {
65 if (!createTrayIcon()) {
66 showStatusWindow();
67 }
68 } catch (Exception e) {
69 e.printStackTrace();
70 }
71 }
72 }
73
74 private static Image loadImage(String name) {
75 try {
76 byte[] imageData = Utils.getResource(name);
77 if (imageData == null) {
78 return null;
79 }
80 return Toolkit.getDefaultToolkit().createImage(imageData);
81 } catch (IOException e) {
82 e.printStackTrace();
83 return null;
84 }
85 }
86
87 @Override
88 public void shutdown() {
89 super.shutdown();
90 if (statusFrame != null) {
91 statusFrame.dispose();
92 statusFrame = null;
93 }
94 if (trayIconUsed) {
95 try {
96 // tray.remove(trayIcon);
97 Utils.callMethod(tray, "remove", trayIcon);
98 } catch (Exception e) {
99 // ignore
100 } finally {
101 trayIcon = null;
102 tray = null;
103 trayIconUsed = false;
104 }
105 System.gc();
106 // Mac OS X: Console tool process did not stop on exit
107 String os = System.getProperty("os.name", "generic").toLowerCase(Locale.ENGLISH);
108 if (os.contains("mac")) {
109 for (Thread t : Thread.getAllStackTraces().keySet()) {
110 if (t.getName().startsWith("AWT-")) {
111 t.interrupt();
112 }
113 }
114 }
115 Thread.currentThread().interrupt();
116 // throw new ThreadDeath();
117 }
118 }
119
120 private void loadFont() {
121 if (isWindows) {
122 font = new Font("Dialog", Font.PLAIN, 11);
123 } else {
124 font = new Font("Dialog", Font.PLAIN, 12);
125 }
126 }
127
128 private boolean createTrayIcon() {
129 try {
130 // SystemTray.isSupported();
131 boolean supported = (Boolean) Utils.callStaticMethod(
132 "java.awt.SystemTray.isSupported");
133 if (!supported) {
134 return false;
135 }
136 PopupMenu menuConsole = new PopupMenu();
137 MenuItem itemConsole = new MenuItem("H2 Console");
138 itemConsole.setActionCommand("console");
139 itemConsole.addActionListener(this);
140 itemConsole.setFont(font);
141 menuConsole.add(itemConsole);
142 MenuItem itemCreate = new MenuItem("Create a new database...");
143 itemCreate.setActionCommand("showCreate");
144 itemCreate.addActionListener(this);
145 itemCreate.setFont(font);
146 menuConsole.add(itemCreate);
147 MenuItem itemStatus = new MenuItem("Status");
148 itemStatus.setActionCommand("status");
149 itemStatus.addActionListener(this);
150 itemStatus.setFont(font);
151 menuConsole.add(itemStatus);
152 MenuItem itemExit = new MenuItem("Exit");
153 itemExit.setFont(font);
154 itemExit.setActionCommand("exit");
155 itemExit.addActionListener(this);
156 menuConsole.add(itemExit);
157
158 // tray = SystemTray.getSystemTray();
159 tray = Utils.callStaticMethod("java.awt.SystemTray.getSystemTray");
160
161 // Dimension d = tray.getTrayIconSize();
162 Dimension d = (Dimension) Utils.callMethod(tray, "getTrayIconSize");
163 String iconFile;
164 if (d.width >= 24 && d.height >= 24) {
165 iconFile = "/org/h2/res/h2-24.png";
166 } else if (d.width >= 22 && d.height >= 22) {
167 // for Mac OS X 10.8.1 with retina display:
168 // the reported resolution is 22 x 22, but the image
169 // is scaled and the real resolution is 44 x 44
170 iconFile = "/org/h2/res/h2-64-t.png";
171 // iconFile = "/org/h2/res/h2-22-t.png";
172 } else {
173 iconFile = "/org/h2/res/h2.png";
174 }
175 Image icon = loadImage(iconFile);
176
177 // trayIcon = new TrayIcon(image, "H2 Database Engine",
178 // menuConsole);
179 trayIcon = Utils.newInstance("java.awt.TrayIcon",
180 icon, "H2 Database Engine", menuConsole);
181
182 // trayIcon.addMouseListener(this);
183 Utils.callMethod(trayIcon, "addMouseListener", this);
184
185 // tray.add(trayIcon);
186 Utils.callMethod(tray, "add", trayIcon);
187
188 this.trayIconUsed = true;
189
190 return true;
191 } catch (Exception e) {
192 return false;
193 }
194 }
195
196 private void showStatusWindow() {
197 if (statusFrame != null) {
198 return;
199 }
200 statusFrame = new Frame("H2 Console");
201 statusFrame.addWindowListener(this);
202 Image image = loadImage("/org/h2/res/h2.png");
203 if (image != null) {
204 statusFrame.setIconImage(image);
205 }
206 statusFrame.setResizable(false);
207 statusFrame.setBackground(SystemColor.control);
208
209 GridBagLayout layout = new GridBagLayout();
210 statusFrame.setLayout(layout);
211
212 // the main panel keeps everything together
213 Panel mainPanel = new Panel(layout);
214
215 GridBagConstraints constraintsPanel = new GridBagConstraints();
216 constraintsPanel.gridx = 0;
217 constraintsPanel.weightx = 1.0D;
218 constraintsPanel.weighty = 1.0D;
219 constraintsPanel.fill = GridBagConstraints.BOTH;
220 constraintsPanel.insets = new Insets(0, 10, 0, 10);
221 constraintsPanel.gridy = 0;
222
223 GridBagConstraints constraintsButton = new GridBagConstraints();
224 constraintsButton.gridx = 0;
225 constraintsButton.gridwidth = 2;
226 constraintsButton.insets = new Insets(10, 0, 0, 0);
227 constraintsButton.gridy = 1;
228 constraintsButton.anchor = GridBagConstraints.EAST;
229
230 GridBagConstraints constraintsTextField = new GridBagConstraints();
231 constraintsTextField.fill = GridBagConstraints.HORIZONTAL;
232 constraintsTextField.gridy = 0;
233 constraintsTextField.weightx = 1.0;
234 constraintsTextField.insets = new Insets(0, 5, 0, 0);
235 constraintsTextField.gridx = 1;
236
237 GridBagConstraints constraintsLabel = new GridBagConstraints();
238 constraintsLabel.gridx = 0;
239 constraintsLabel.gridy = 0;
240
241 Label label = new Label("H2 Console URL:", Label.LEFT);
242 label.setFont(font);
243 mainPanel.add(label, constraintsLabel);
244
245 urlText = new TextField();
246 urlText.setEditable(false);
247 urlText.setFont(font);
248 urlText.setText(web.getURL());
249 if (isWindows) {
250 urlText.setFocusable(false);
251 }
252 mainPanel.add(urlText, constraintsTextField);
253
254 startBrowser = new Button("Start Browser");
255 startBrowser.setFocusable(false);
256 startBrowser.setActionCommand("console");
257 startBrowser.addActionListener(this);
258 startBrowser.setFont(font);
259 mainPanel.add(startBrowser, constraintsButton);
260 statusFrame.add(mainPanel, constraintsPanel);
261
262 int width = 300, height = 120;
263 statusFrame.setSize(width, height);
264 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
265 statusFrame.setLocation((screenSize.width - width) / 2,
266 (screenSize.height - height) / 2);
267 try {
268 statusFrame.setVisible(true);
269 } catch (Throwable t) {
270 // ignore
271 // some systems don't support this method, for example IKVM
272 // however it still works
273 }
274 try {
275 // ensure this window is in front of the browser
276 statusFrame.setAlwaysOnTop(true);
277 statusFrame.setAlwaysOnTop(false);
278 } catch (Throwable t) {
279 // ignore
280 }
281 }
282
283 private void startBrowser() {
284 if (web != null) {
285 String url = web.getURL();
286 if (urlText != null) {
287 urlText.setText(url);
288 }
289 long now = Utils.currentNanoTime();
290 if (lastOpenNs == 0 || now - lastOpenNs > 100_000_000L) {
291 lastOpenNs = now;
292 openBrowser(url);
293 }
294 }
295 }
296
297 private void showCreateDatabase() {
298 if (createFrame != null) {
299 return;
300 }
301 createFrame = new Frame("H2 Console");
302 createFrame.addWindowListener(this);
303 Image image = loadImage("/org/h2/res/h2.png");
304 if (image != null) {
305 createFrame.setIconImage(image);
306 }
307 createFrame.setResizable(false);
308 createFrame.setBackground(SystemColor.control);
309
310 GridBagLayout layout = new GridBagLayout();
311 createFrame.setLayout(layout);
312
313 // the main panel keeps everything together
314 Panel mainPanel = new Panel(layout);
315
316 GridBagConstraints constraints;
317
318 constraints = new GridBagConstraints();
319 constraints.fill = GridBagConstraints.HORIZONTAL;
320 constraints.insets = new Insets(10, 0, 0, 0);
321 constraints.gridx = 0;
322 constraints.gridy = 0;
323 Label urlLabel = new Label("Database path:", Label.LEFT);
324 urlLabel.setFont(font);
325 mainPanel.add(urlLabel, constraints);
326
327 constraints = new GridBagConstraints();
328 constraints.fill = GridBagConstraints.HORIZONTAL;
329 constraints.gridy = 0;
330 constraints.weightx = 1d;
331 constraints.insets = new Insets(10, 5, 0, 0);
332 constraints.gridx = 1;
333 pathField = new TextField();
334 pathField.setFont(font);
335 pathField.setText("./test");
336 mainPanel.add(pathField, constraints);
337
338 constraints = new GridBagConstraints();
339 constraints.fill = GridBagConstraints.HORIZONTAL;
340 constraints.gridx = 0;
341 constraints.gridy = 1;
342 Label userLabel = new Label("Username:", Label.LEFT);
343 userLabel.setFont(font);
344 mainPanel.add(userLabel, constraints);
345
346 constraints = new GridBagConstraints();
347 constraints.fill = GridBagConstraints.HORIZONTAL;
348 constraints.gridy = 1;
349 constraints.weightx = 1d;
350 constraints.insets = new Insets(0, 5, 0, 0);
351 constraints.gridx = 1;
352 userField = new TextField();
353 userField.setFont(font);
354 userField.setText("sa");
355 mainPanel.add(userField, constraints);
356
357 constraints = new GridBagConstraints();
358 constraints.fill = GridBagConstraints.HORIZONTAL;
359 constraints.gridx = 0;
360 constraints.gridy = 2;
361 Label passwordLabel = new Label("Password:", Label.LEFT);
362 passwordLabel.setFont(font);
363 mainPanel.add(passwordLabel, constraints);
364
365 constraints = new GridBagConstraints();
366 constraints.fill = GridBagConstraints.HORIZONTAL;
367 constraints.gridy = 2;
368 constraints.weightx = 1d;
369 constraints.insets = new Insets(0, 5, 0, 0);
370 constraints.gridx = 1;
371 passwordField = new TextField();
372 passwordField.setFont(font);
373 passwordField.setEchoChar('*');
374 mainPanel.add(passwordField, constraints);
375
376 constraints = new GridBagConstraints();
377 constraints.fill = GridBagConstraints.HORIZONTAL;
378 constraints.gridx = 0;
379 constraints.gridy = 3;
380 Label passwordConfirmationLabel = new Label("Password confirmation:", Label.LEFT);
381 passwordConfirmationLabel.setFont(font);
382 mainPanel.add(passwordConfirmationLabel, constraints);
383
384 constraints = new GridBagConstraints();
385 constraints.fill = GridBagConstraints.HORIZONTAL;
386 constraints.gridy = 3;
387 constraints.weightx = 1d;
388 constraints.insets = new Insets(0, 5, 0, 0);
389 constraints.gridx = 1;
390 passwordConfirmationField = new TextField();
391 passwordConfirmationField.setFont(font);
392 passwordConfirmationField.setEchoChar('*');
393 mainPanel.add(passwordConfirmationField, constraints);
394
395 constraints = new GridBagConstraints();
396 constraints.gridx = 0;
397 constraints.gridwidth = 2;
398 constraints.insets = new Insets(10, 0, 0, 0);
399 constraints.gridy = 4;
400 constraints.anchor = GridBagConstraints.EAST;
401 createButton = new Button("Create");
402 createButton.setFocusable(false);
403 createButton.setActionCommand("create");
404 createButton.addActionListener(this);
405 createButton.setFont(font);
406 mainPanel.add(createButton, constraints);
407
408 constraints = new GridBagConstraints();
409 constraints.fill = GridBagConstraints.HORIZONTAL;
410 constraints.gridy = 5;
411 constraints.weightx = 1d;
412 constraints.insets = new Insets(10, 0, 0, 0);
413 constraints.gridx = 0;
414 constraints.gridwidth = 2;
415 errorArea = new TextArea();
416 errorArea.setFont(font);
417 errorArea.setEditable(false);
418 mainPanel.add(errorArea, constraints);
419
420 constraints = new GridBagConstraints();
421 constraints.gridx = 0;
422 constraints.weightx = 1d;
423 constraints.weighty = 1d;
424 constraints.fill = GridBagConstraints.BOTH;
425 constraints.insets = new Insets(0, 10, 0, 10);
426 constraints.gridy = 0;
427 createFrame.add(mainPanel, constraints);
428
429 int width = 400, height = 400;
430 createFrame.setSize(width, height);
431 createFrame.pack();
432 createFrame.setLocationRelativeTo(null);
433 try {
434 createFrame.setVisible(true);
435 } catch (Throwable t) {
436 // ignore
437 // some systems don't support this method, for example IKVM
438 // however it still works
439 }
440 try {
441 // ensure this window is in front of the browser
442 createFrame.setAlwaysOnTop(true);
443 createFrame.setAlwaysOnTop(false);
444 } catch (Throwable t) {
445 // ignore
446 }
447 }
448
449 private void createDatabase() {
450 if (web == null || createFrame == null) {
451 return;
452 }
453 String path = pathField.getText(), user = userField.getText(), password = passwordField.getText(),
454 passwordConfirmation = passwordConfirmationField.getText();
455 if (!password.equals(passwordConfirmation)) {
456 errorArea.setForeground(Color.RED);
457 errorArea.setText("Passwords don't match");
458 return;
459 }
460 if (password.isEmpty()) {
461 errorArea.setForeground(Color.RED);
462 errorArea.setText("Specify a password");
463 return;
464 }
465 String url = "jdbc:h2:" + path;
466 try {
467 new JdbcConnection(url, null, user, password, false).close();
468 errorArea.setForeground(new Color(0, 0x99, 0));
469 errorArea.setText("Database was created successfully.\n\n"
470 + "JDBC URL for H2 Console:\n"
471 + url);
472 } catch (Exception ex) {
473 errorArea.setForeground(Color.RED);
474 errorArea.setText(ex.getMessage());
475 }
476 }
477
478 /**
479 * INTERNAL
480 */
481 @Override
482 public void actionPerformed(ActionEvent e) {
483 String command = e.getActionCommand();
484 if ("exit".equals(command)) {
485 shutdown();
486 } else if ("console".equals(command)) {
487 startBrowser();
488 } else if ("showCreate".equals(command)) {
489 showCreateDatabase();
490 } else if ("status".equals(command)) {
491 showStatusWindow();
492 } else {
493 // for some reason, IKVM ignores setActionCommand
494 if (startBrowser == e.getSource()) {
495 startBrowser();
496 } else if (createButton == e.getSource()) {
497 createDatabase();
498 }
499 }
500 }
501
502 /**
503 * INTERNAL
504 */
505 @Override
506 public void mouseClicked(MouseEvent e) {
507 if (e.getButton() == MouseEvent.BUTTON1) {
508 startBrowser();
509 }
510 }
511
512 /**
513 * INTERNAL
514 */
515 @Override
516 public void mouseEntered(MouseEvent e) {
517 // nothing to do
518 }
519
520 /**
521 * INTERNAL
522 */
523 @Override
524 public void mouseExited(MouseEvent e) {
525 // nothing to do
526 }
527
528 /**
529 * INTERNAL
530 */
531 @Override
532 public void mousePressed(MouseEvent e) {
533 // nothing to do
534 }
535
536 /**
537 * INTERNAL
538 */
539 @Override
540 public void mouseReleased(MouseEvent e) {
541 // nothing to do
542 }
543
544 /**
545 * INTERNAL
546 */
547 @Override
548 public void windowClosing(WindowEvent e) {
549 if (trayIconUsed) {
550 Window window = e.getWindow();
551 if (window == statusFrame) {
552 statusFrame.dispose();
553 statusFrame = null;
554 } else if (window == createFrame) {
555 createFrame.dispose();
556 createFrame = null;
557 }
558 } else {
559 shutdown();
560 }
561 }
562
563 /**
564 * INTERNAL
565 */
566 @Override
567 public void windowActivated(WindowEvent e) {
568 // nothing to do
569 }
570
571 /**
572 * INTERNAL
573 */
574 @Override
575 public void windowClosed(WindowEvent e) {
576 // nothing to do
577 }
578
579 /**
580 * INTERNAL
581 */
582 @Override
583 public void windowDeactivated(WindowEvent e) {
584 // nothing to do
585 }
586
587 /**
588 * INTERNAL
589 */
590 @Override
591 public void windowDeiconified(WindowEvent e) {
592 // nothing to do
593 }
594
595 /**
596 * INTERNAL
597 */
598 @Override
599 public void windowIconified(WindowEvent e) {
600 // nothing to do
601 }
602
603 /**
604 * INTERNAL
605 */
606 @Override
607 public void windowOpened(WindowEvent e) {
608 // nothing to do
609 }
610
611 }