001: /*
002: * DriverEditorDialog.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.profiles;
013:
014: import java.awt.BorderLayout;
015: import java.awt.Dimension;
016: import java.awt.EventQueue;
017: import java.awt.FlowLayout;
018: import java.awt.Frame;
019: import java.awt.event.ActionEvent;
020: import java.awt.event.ActionListener;
021: import java.awt.event.WindowAdapter;
022: import java.awt.event.WindowEvent;
023:
024: import javax.swing.ActionMap;
025: import javax.swing.InputMap;
026: import javax.swing.JButton;
027: import javax.swing.JComponent;
028: import javax.swing.JDialog;
029: import javax.swing.JPanel;
030: import javax.swing.border.EtchedBorder;
031:
032: import workbench.gui.actions.EscAction;
033: import workbench.gui.components.WbButton;
034: import workbench.resource.ResourceMgr;
035: import workbench.resource.Settings;
036:
037: public class DriverEditorDialog extends JDialog implements
038: ActionListener {
039: private JPanel dummyPanel;
040: private JPanel buttonPanel;
041: private JButton okButton;
042: private DriverlistEditorPanel driverListPanel;
043: private JButton cancelButton;
044: private boolean cancelled = true;
045: private EscAction escAction;
046:
047: public DriverEditorDialog(Frame parent) {
048: super (parent, true);
049: initComponents();
050:
051: this .getRootPane().setDefaultButton(this .okButton);
052: InputMap im = this .getRootPane().getInputMap(
053: JComponent.WHEN_IN_FOCUSED_WINDOW);
054: ActionMap am = this .getRootPane().getActionMap();
055: escAction = new EscAction(this );
056: escAction.addToInputMap(im, am);
057:
058: if (!Settings.getInstance().restoreWindowSize(this )) {
059: this .setSize(600, 400);
060: }
061: driverListPanel.restoreSettings();
062:
063: // when invoked from the connection dialog, it seems that under
064: // Linux the dialog is not visible (because it's behind the connection
065: // dialog), so we're trying to make this window visible
066: EventQueue.invokeLater(new Runnable() {
067: public void run() {
068: toFront();
069: requestFocus();
070: }
071: });
072: }
073:
074: private void initComponents() {
075: driverListPanel = new workbench.gui.profiles.DriverlistEditorPanel();
076: buttonPanel = new JPanel();
077: okButton = new WbButton(ResourceMgr
078: .getString(ResourceMgr.TXT_OK));
079: cancelButton = new WbButton(ResourceMgr
080: .getString(ResourceMgr.TXT_CANCEL));
081: dummyPanel = new JPanel();
082:
083: setTitle(ResourceMgr.getString("TxtDriverEditorWindowTitle"));
084: setModal(true);
085: setName("DriverEditorDialog");
086: addWindowListener(new WindowAdapter() {
087: public void windowClosing(WindowEvent evt) {
088: closeDialog(evt);
089: }
090: });
091:
092: driverListPanel.setBorder(new EtchedBorder());
093: getContentPane().add(driverListPanel, BorderLayout.CENTER);
094:
095: buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
096:
097: okButton.setFont(null);
098: okButton.addActionListener(this );
099: buttonPanel.add(okButton);
100:
101: cancelButton.addActionListener(this );
102: buttonPanel.add(cancelButton);
103:
104: getContentPane().add(buttonPanel, BorderLayout.SOUTH);
105:
106: dummyPanel.setMaximumSize(new Dimension(2, 2));
107: dummyPanel.setMinimumSize(new Dimension(1, 1));
108: dummyPanel.setPreferredSize(new Dimension(2, 2));
109: getContentPane().add(dummyPanel, BorderLayout.NORTH);
110:
111: }
112:
113: public void actionPerformed(ActionEvent e) {
114: if (e.getSource() == okButton) {
115: okButtonActionPerformed(e);
116: } else if (e.getSource() == cancelButton
117: || e.getActionCommand().equals(
118: escAction.getActionName())) {
119: cancelButtonActionPerformed(e);
120: }
121: }
122:
123: private void cancelButtonActionPerformed(ActionEvent evt) {
124: this .cancelled = true;
125: this .closeDialog();
126: }
127:
128: /**
129: * Sets the driver name to be pre-selected in the list
130: */
131: public void setDriverName(String name) {
132: if (this .driverListPanel != null && name != null) {
133: this .driverListPanel.selectDriver(name);
134: }
135: }
136:
137: private void okButtonActionPerformed(ActionEvent evt) {
138: try {
139: this .driverListPanel.saveItem();
140: this .cancelled = false;
141: this .closeDialog();
142: } catch (Exception e) {
143: e.printStackTrace();
144: }
145: }
146:
147: public boolean isCancelled() {
148: return this .cancelled;
149: }
150:
151: /** Closes the dialog */
152: private void closeDialog(WindowEvent evt) {
153: this .closeDialog();
154: }
155:
156: public void closeDialog() {
157: Settings.getInstance().storeWindowSize(this );
158: driverListPanel.saveSettings();
159: setVisible(false);
160: dispose();
161: }
162: }
|