KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > gui > sql > DbConnWin


1 package jimm.datavision.gui.sql;
2 import jimm.datavision.*;
3 import jimm.datavision.gui.*;
4 import jimm.datavision.source.sql.Database;
5 import jimm.datavision.gui.cmd.DbConnCommand;
6 import jimm.util.StringUtils;
7 import jimm.util.I18N;
8 import java.awt.BorderLayout JavaDoc;
9 import java.awt.event.*;
10 import javax.swing.*;
11
12 /**
13  * A database connection editing dialog box. The user can either enter
14  * separate values or copy values from an existing report XML file.
15  *
16  * @see DbConnReader
17  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
18  */

19 public class DbConnWin extends EditWin implements ActionListener {
20
21 protected static final int TEXT_FIELD_COLS = 32;
22
23 protected Report report;
24 protected JTextField driverClassNameField;
25 protected JTextField connInfoField;
26 protected JTextField dbNameField;
27 protected JTextField userNameField;
28 protected JPasswordField passwordField;
29
30 /**
31  * Constructor.
32  *
33  * @param designer the window to which this dialog belongs
34  * @param report the report
35  * @param modal if <code>true</code>, this window is modal
36  */

37 public DbConnWin(Designer designer, Report report, boolean modal) {
38     super(designer, I18N.get("DbConnWin.title"), "DbConnCommand.name", modal);
39
40     this.report = report;
41
42     buildWindow();
43     pack();
44     setVisible(true);
45 }
46
47 /**
48  * Builds the window contents.
49  */

50 protected void buildWindow() {
51     JPanel editorPanel = buildEditor();
52
53     // OK, Apply, Revert, and Cancel Buttons
54
JPanel buttonPanel = closeButtonPanel();
55
56     // Add values and buttons to window
57
getContentPane().add(editorPanel, BorderLayout.CENTER);
58     getContentPane().add(buttonPanel, BorderLayout.SOUTH);
59
60     new FocusSetter(driverClassNameField);
61 }
62
63 protected JPanel buildEditor() {
64     Database db = (Database)report.getDataSource();
65     EditFieldLayout efl = new EditFieldLayout();
66     efl.setBorder(20);
67
68     driverClassNameField =
69     efl.addTextField(I18N.get("DbConnWin.driver_class_name"), db == null
70              ? "" : db.getDriverClassName(), TEXT_FIELD_COLS);
71     connInfoField = efl.addTextField(I18N.get("DbConnWin.connection_info"),
72                      db == null ? "" : db.getConnectionInfo(),
73                      TEXT_FIELD_COLS);
74     dbNameField = efl.addTextField(I18N.get("DbConnWin.database_name"),
75                    db == null ? "" : db.getName(),
76                    TEXT_FIELD_COLS);
77     userNameField = efl.addTextField(I18N.get("DbConnWin.user_name"),
78                      db == null ? "" : db.getUserName(),
79                      TEXT_FIELD_COLS);
80     String JavaDoc password = (db == null ? "" : db.getPassword());
81     if (password == null) password = "";
82     passwordField = efl.addPasswordField(I18N.get("DbConnWin.password"),
83                      password, TEXT_FIELD_COLS);
84
85     // Click to copy info from another report
86
JButton copyButton = new JButton(I18N.get("DbConnWin.copy_settings"));
87     copyButton.addActionListener(this);
88     JPanel copyPanel = new JPanel();
89     copyPanel.add(copyButton);
90     efl.add(null, copyPanel);
91
92     return efl.getPanel();
93 }
94
95 protected void fillEditFields() {
96     Database db = (Database)report.getDataSource();
97     if (db == null) {
98     driverClassNameField.setText("");
99     connInfoField.setText("");
100     dbNameField.setText("");
101     userNameField.setText("");
102     }
103     else {
104     driverClassNameField.setText(db.getDriverClassName());
105     connInfoField.setText(db.getConnectionInfo());
106     dbNameField.setText(db.getName());
107     userNameField.setText(db.getUserName());
108     }
109     passwordField.setText("");
110 }
111
112 /**
113  * Handles the "Copy Settings..." button.
114  *
115  * @param e action event
116  */

117 public void actionPerformed(ActionEvent e) {
118     String JavaDoc cmd = e.getActionCommand();
119     if (I18N.get("DbConnWin.copy_settings").equals(cmd)) {
120     JFileChooser chooser = Designer.getChooser();
121     int returnVal = chooser.showOpenDialog(this);
122     if (returnVal == JFileChooser.APPROVE_OPTION) {
123         DbConnReader reader = new DbConnReader();
124         try {
125         reader.read(chooser.getSelectedFile());
126         driverClassNameField.setText(reader.getDriverClassName());
127         connInfoField.setText(reader.getConnectionInfo());
128         dbNameField.setText(reader.getDbName());
129         userNameField.setText(reader.getUserName());
130         }
131         catch (Exception JavaDoc ex) {
132         ErrorHandler.error(I18N.get("DbConnWin.copy_error"), ex);
133         }
134     }
135     }
136     else
137     super.actionPerformed(e);
138 }
139
140 protected void doSave() {
141     DbConnCommand cmd =
142     new DbConnCommand(report,
143               StringUtils.nullOrTrimmed(driverClassNameField.getText()),
144               StringUtils.nullOrTrimmed(connInfoField.getText()),
145               StringUtils.nullOrTrimmed(dbNameField.getText()),
146               StringUtils.nullOrTrimmed(userNameField.getText()),
147               StringUtils.nullOrTrimmed(new String JavaDoc(passwordField.getPassword())));
148     cmd.perform();
149     commands.add(cmd);
150 }
151
152 protected void doRevert() {
153     fillEditFields();
154 }
155
156 }
157
Popular Tags