提交 4653aa5b authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Add database creation capability to GUIConsole

上级 7adea496
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
package org.h2.tools; package org.h2.tools;
import java.awt.Button; import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Font; import java.awt.Font;
import java.awt.Frame; import java.awt.Frame;
...@@ -19,8 +20,10 @@ import java.awt.MenuItem; ...@@ -19,8 +20,10 @@ import java.awt.MenuItem;
import java.awt.Panel; import java.awt.Panel;
import java.awt.PopupMenu; import java.awt.PopupMenu;
import java.awt.SystemColor; import java.awt.SystemColor;
import java.awt.TextArea;
import java.awt.TextField; import java.awt.TextField;
import java.awt.Toolkit; import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
...@@ -28,6 +31,7 @@ import java.awt.event.MouseListener; ...@@ -28,6 +31,7 @@ import java.awt.event.MouseListener;
import java.awt.event.WindowEvent; import java.awt.event.WindowEvent;
import java.awt.event.WindowListener; import java.awt.event.WindowListener;
import java.io.IOException; import java.io.IOException;
import java.sql.DriverManager;
import java.util.Locale; import java.util.Locale;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
...@@ -39,11 +43,18 @@ import org.h2.util.Utils; ...@@ -39,11 +43,18 @@ import org.h2.util.Utils;
public class GUIConsole extends Console implements ActionListener, MouseListener, WindowListener { public class GUIConsole extends Console implements ActionListener, MouseListener, WindowListener {
private long lastOpenNs; private long lastOpenNs;
private Frame frame;
private boolean trayIconUsed; private boolean trayIconUsed;
private Font font; private Font font;
private Button startBrowser;
private Frame statusFrame;
private TextField urlText; private TextField urlText;
private Button startBrowser;
private Frame createFrame;
private TextField pathField, userField, passwordField;
private Button createButton;
private TextArea errorArea;
private Object tray; private Object tray;
private Object trayIcon; private Object trayIcon;
...@@ -53,7 +64,7 @@ public class GUIConsole extends Console implements ActionListener, MouseListener ...@@ -53,7 +64,7 @@ public class GUIConsole extends Console implements ActionListener, MouseListener
loadFont(); loadFont();
try { try {
if (!createTrayIcon()) { if (!createTrayIcon()) {
showWindow(); showStatusWindow();
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
...@@ -77,9 +88,9 @@ public class GUIConsole extends Console implements ActionListener, MouseListener ...@@ -77,9 +88,9 @@ public class GUIConsole extends Console implements ActionListener, MouseListener
@Override @Override
public void shutdown() { public void shutdown() {
super.shutdown(); super.shutdown();
if (frame != null) { if (statusFrame != null) {
frame.dispose(); statusFrame.dispose();
frame = null; statusFrame = null;
} }
if (trayIconUsed) { if (trayIconUsed) {
try { try {
...@@ -129,6 +140,11 @@ public class GUIConsole extends Console implements ActionListener, MouseListener ...@@ -129,6 +140,11 @@ public class GUIConsole extends Console implements ActionListener, MouseListener
itemConsole.addActionListener(this); itemConsole.addActionListener(this);
itemConsole.setFont(font); itemConsole.setFont(font);
menuConsole.add(itemConsole); menuConsole.add(itemConsole);
MenuItem itemCreate = new MenuItem("Create a new database...");
itemCreate.setActionCommand("showCreate");
itemCreate.addActionListener(this);
itemCreate.setFont(font);
menuConsole.add(itemCreate);
MenuItem itemStatus = new MenuItem("Status"); MenuItem itemStatus = new MenuItem("Status");
itemStatus.setActionCommand("status"); itemStatus.setActionCommand("status");
itemStatus.addActionListener(this); itemStatus.addActionListener(this);
...@@ -178,21 +194,21 @@ public class GUIConsole extends Console implements ActionListener, MouseListener ...@@ -178,21 +194,21 @@ public class GUIConsole extends Console implements ActionListener, MouseListener
} }
} }
private void showWindow() { private void showStatusWindow() {
if (frame != null) { if (statusFrame != null) {
return; return;
} }
frame = new Frame("H2 Console"); statusFrame = new Frame("H2 Console");
frame.addWindowListener(this); statusFrame.addWindowListener(this);
Image image = loadImage("/org/h2/res/h2.png"); Image image = loadImage("/org/h2/res/h2.png");
if (image != null) { if (image != null) {
frame.setIconImage(image); statusFrame.setIconImage(image);
} }
frame.setResizable(false); statusFrame.setResizable(false);
frame.setBackground(SystemColor.control); statusFrame.setBackground(SystemColor.control);
GridBagLayout layout = new GridBagLayout(); GridBagLayout layout = new GridBagLayout();
frame.setLayout(layout); statusFrame.setLayout(layout);
// the main panel keeps everything together // the main panel keeps everything together
Panel mainPanel = new Panel(layout); Panel mainPanel = new Panel(layout);
...@@ -242,15 +258,15 @@ public class GUIConsole extends Console implements ActionListener, MouseListener ...@@ -242,15 +258,15 @@ public class GUIConsole extends Console implements ActionListener, MouseListener
startBrowser.addActionListener(this); startBrowser.addActionListener(this);
startBrowser.setFont(font); startBrowser.setFont(font);
mainPanel.add(startBrowser, constraintsButton); mainPanel.add(startBrowser, constraintsButton);
frame.add(mainPanel, constraintsPanel); statusFrame.add(mainPanel, constraintsPanel);
int width = 300, height = 120; int width = 300, height = 120;
frame.setSize(width, height); statusFrame.setSize(width, height);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((screenSize.width - width) / 2, statusFrame.setLocation((screenSize.width - width) / 2,
(screenSize.height - height) / 2); (screenSize.height - height) / 2);
try { try {
frame.setVisible(true); statusFrame.setVisible(true);
} catch (Throwable t) { } catch (Throwable t) {
// ignore // ignore
// some systems don't support this method, for example IKVM // some systems don't support this method, for example IKVM
...@@ -258,8 +274,8 @@ public class GUIConsole extends Console implements ActionListener, MouseListener ...@@ -258,8 +274,8 @@ public class GUIConsole extends Console implements ActionListener, MouseListener
} }
try { try {
// ensure this window is in front of the browser // ensure this window is in front of the browser
frame.setAlwaysOnTop(true); statusFrame.setAlwaysOnTop(true);
frame.setAlwaysOnTop(false); statusFrame.setAlwaysOnTop(false);
} catch (Throwable t) { } catch (Throwable t) {
// ignore // ignore
} }
...@@ -279,6 +295,161 @@ public class GUIConsole extends Console implements ActionListener, MouseListener ...@@ -279,6 +295,161 @@ public class GUIConsole extends Console implements ActionListener, MouseListener
} }
} }
private void showCreateDatabase() {
if (createFrame != null) {
return;
}
createFrame = new Frame("H2 Console");
createFrame.addWindowListener(this);
Image image = loadImage("/org/h2/res/h2.png");
if (image != null) {
createFrame.setIconImage(image);
}
createFrame.setResizable(false);
createFrame.setBackground(SystemColor.control);
GridBagLayout layout = new GridBagLayout();
createFrame.setLayout(layout);
// the main panel keeps everything together
Panel mainPanel = new Panel(layout);
GridBagConstraints constraints;
constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.gridx = 0;
constraints.gridy = 0;
Label urlLabel = new Label("Database path:", Label.LEFT);
urlLabel.setFont(font);
mainPanel.add(urlLabel, constraints);
constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.gridy = 0;
constraints.weightx = 1d;
constraints.insets = new Insets(0, 5, 0, 0);
constraints.gridx = 1;
pathField = new TextField();
pathField.setFont(font);
pathField.setText("./test");
mainPanel.add(pathField, constraints);
constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.gridx = 0;
constraints.gridy = 1;
Label userLabel = new Label("Username:", Label.LEFT);
userLabel.setFont(font);
mainPanel.add(userLabel, constraints);
constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.gridy = 1;
constraints.weightx = 1d;
constraints.insets = new Insets(0, 5, 0, 0);
constraints.gridx = 1;
userField = new TextField();
userField.setFont(font);
userField.setText("sa");
mainPanel.add(userField, constraints);
constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.gridx = 0;
constraints.gridy = 2;
Label passwordLabel = new Label("Password:", Label.LEFT);
passwordLabel.setFont(font);
mainPanel.add(passwordLabel, constraints);
constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.gridy = 2;
constraints.weightx = 1d;
constraints.insets = new Insets(0, 5, 0, 0);
constraints.gridx = 1;
passwordField = new TextField();
passwordField.setFont(font);
mainPanel.add(passwordField, constraints);
constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridwidth = 2;
constraints.insets = new Insets(10, 0, 0, 0);
constraints.gridy = 3;
constraints.anchor = GridBagConstraints.EAST;
createButton = new Button("Create");
createButton.setFocusable(false);
createButton.setActionCommand("create");
createButton.addActionListener(this);
createButton.setFont(font);
mainPanel.add(createButton, constraints);
constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.gridy = 4;
constraints.weightx = 1d;
constraints.insets = new Insets(10, 0, 0, 0);
constraints.gridx = 0;
constraints.gridwidth = 2;
errorArea = new TextArea();
errorArea.setFont(font);
errorArea.setEditable(false);
mainPanel.add(errorArea, constraints);
constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.weightx = 1d;
constraints.weighty = 1d;
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(0, 10, 0, 10);
constraints.gridy = 0;
createFrame.add(mainPanel, constraints);
int width = 300, height = 300;
createFrame.setSize(width, height);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
createFrame.setLocation((screenSize.width - width) / 2,
(screenSize.height - height) / 2);
try {
createFrame.setVisible(true);
} catch (Throwable t) {
// ignore
// some systems don't support this method, for example IKVM
// however it still works
}
try {
// ensure this window is in front of the browser
createFrame.setAlwaysOnTop(true);
createFrame.setAlwaysOnTop(false);
} catch (Throwable t) {
// ignore
}
}
private void createDatabase() {
if (web == null || createFrame == null) {
return;
}
String path = pathField.getText(), user = userField.getText(), password = passwordField.getText();
if (password.isEmpty()) {
errorArea.setForeground(Color.RED);
errorArea.setText("Specify a password");
return;
}
String url = "jdbc:h2:" + path;
try {
DriverManager.getConnection(url, user, password).close();
errorArea.setForeground(new Color(0, 0x99, 0));
errorArea.setText("Database was created successfully.\n\n"
+ "JDBC URL for H2 Console:\n"
+ url);
} catch (Exception ex) {
errorArea.setForeground(Color.RED);
errorArea.setText(ex.getMessage());
}
}
/** /**
* INTERNAL * INTERNAL
*/ */
...@@ -289,11 +460,17 @@ public class GUIConsole extends Console implements ActionListener, MouseListener ...@@ -289,11 +460,17 @@ public class GUIConsole extends Console implements ActionListener, MouseListener
shutdown(); shutdown();
} else if ("console".equals(command)) { } else if ("console".equals(command)) {
startBrowser(); startBrowser();
} else if ("showCreate".equals(command)) {
showCreateDatabase();
} else if ("status".equals(command)) { } else if ("status".equals(command)) {
showWindow(); showStatusWindow();
} else if (startBrowser == e.getSource()) { } else {
// for some reason, IKVM ignores setActionCommand // for some reason, IKVM ignores setActionCommand
startBrowser(); if (startBrowser == e.getSource()) {
startBrowser();
} else if (createButton == e.getSource()) {
createDatabase();
}
} }
} }
...@@ -345,8 +522,14 @@ public class GUIConsole extends Console implements ActionListener, MouseListener ...@@ -345,8 +522,14 @@ public class GUIConsole extends Console implements ActionListener, MouseListener
@Override @Override
public void windowClosing(WindowEvent e) { public void windowClosing(WindowEvent e) {
if (trayIconUsed) { if (trayIconUsed) {
frame.dispose(); Window window = e.getWindow();
frame = null; if (window == statusFrame) {
statusFrame.dispose();
statusFrame = null;
} else if (window == createFrame) {
createFrame.dispose();
createFrame = null;
}
} else { } else {
shutdown(); shutdown();
} }
......
...@@ -806,4 +806,4 @@ econd irst bcef ordinality nord unnest ...@@ -806,4 +806,4 @@ econd irst bcef ordinality nord unnest
analyst occupation distributive josaph aor engineer sajeewa isuru randil kevin doctor businessman artist ashan analyst occupation distributive josaph aor engineer sajeewa isuru randil kevin doctor businessman artist ashan
corrupts splitted disruption unintentional octets preconditions predicates subq objectweb insn opcodes corrupts splitted disruption unintentional octets preconditions predicates subq objectweb insn opcodes
preserves masking holder unboxing avert iae transformed subtle reevaluate exclusions subclause ftbl rgr preserves masking holder unboxing avert iae transformed subtle reevaluate exclusions subclause ftbl rgr
presorted inclusion contexts aax mwd percentile cont interpolate mwa hypothetical regproc childed listagg presorted inclusion contexts aax mwd percentile cont interpolate mwa hypothetical regproc childed listagg foreground
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论