001: package jimm.datavision.gui.sql;
002:
003: import jimm.datavision.*;
004: import jimm.datavision.gui.*;
005: import jimm.datavision.source.sql.Database;
006: import jimm.datavision.gui.cmd.DbConnCommand;
007: import jimm.util.StringUtils;
008: import jimm.util.I18N;
009: import java.awt.BorderLayout;
010: import java.awt.event.*;
011: import javax.swing.*;
012:
013: /**
014: * A database connection editing dialog box. The user can either enter
015: * separate values or copy values from an existing report XML file.
016: *
017: * @see DbConnReader
018: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
019: */
020: public class DbConnWin extends EditWin implements ActionListener {
021:
022: protected static final int TEXT_FIELD_COLS = 32;
023:
024: protected Report report;
025: protected JTextField driverClassNameField;
026: protected JTextField connInfoField;
027: protected JTextField dbNameField;
028: protected JTextField userNameField;
029: protected JPasswordField passwordField;
030:
031: /**
032: * Constructor.
033: *
034: * @param designer the window to which this dialog belongs
035: * @param report the report
036: * @param modal if <code>true</code>, this window is modal
037: */
038: public DbConnWin(Designer designer, Report report, boolean modal) {
039: super (designer, I18N.get("DbConnWin.title"),
040: "DbConnCommand.name", modal);
041:
042: this .report = report;
043:
044: buildWindow();
045: pack();
046: setVisible(true);
047: }
048:
049: /**
050: * Builds the window contents.
051: */
052: protected void buildWindow() {
053: JPanel editorPanel = buildEditor();
054:
055: // OK, Apply, Revert, and Cancel Buttons
056: JPanel buttonPanel = closeButtonPanel();
057:
058: // Add values and buttons to window
059: getContentPane().add(editorPanel, BorderLayout.CENTER);
060: getContentPane().add(buttonPanel, BorderLayout.SOUTH);
061:
062: new FocusSetter(driverClassNameField);
063: }
064:
065: protected JPanel buildEditor() {
066: Database db = (Database) report.getDataSource();
067: EditFieldLayout efl = new EditFieldLayout();
068: efl.setBorder(20);
069:
070: driverClassNameField = efl.addTextField(I18N
071: .get("DbConnWin.driver_class_name"), db == null ? ""
072: : db.getDriverClassName(), TEXT_FIELD_COLS);
073: connInfoField = efl.addTextField(I18N
074: .get("DbConnWin.connection_info"), db == null ? "" : db
075: .getConnectionInfo(), TEXT_FIELD_COLS);
076: dbNameField = efl.addTextField(I18N
077: .get("DbConnWin.database_name"), db == null ? "" : db
078: .getName(), TEXT_FIELD_COLS);
079: userNameField = efl.addTextField(I18N
080: .get("DbConnWin.user_name"), db == null ? "" : db
081: .getUserName(), TEXT_FIELD_COLS);
082: String password = (db == null ? "" : db.getPassword());
083: if (password == null)
084: password = "";
085: passwordField = efl.addPasswordField(I18N
086: .get("DbConnWin.password"), password, TEXT_FIELD_COLS);
087:
088: // Click to copy info from another report
089: JButton copyButton = new JButton(I18N
090: .get("DbConnWin.copy_settings"));
091: copyButton.addActionListener(this );
092: JPanel copyPanel = new JPanel();
093: copyPanel.add(copyButton);
094: efl.add(null, copyPanel);
095:
096: return efl.getPanel();
097: }
098:
099: protected void fillEditFields() {
100: Database db = (Database) report.getDataSource();
101: if (db == null) {
102: driverClassNameField.setText("");
103: connInfoField.setText("");
104: dbNameField.setText("");
105: userNameField.setText("");
106: } else {
107: driverClassNameField.setText(db.getDriverClassName());
108: connInfoField.setText(db.getConnectionInfo());
109: dbNameField.setText(db.getName());
110: userNameField.setText(db.getUserName());
111: }
112: passwordField.setText("");
113: }
114:
115: /**
116: * Handles the "Copy Settings..." button.
117: *
118: * @param e action event
119: */
120: public void actionPerformed(ActionEvent e) {
121: String cmd = e.getActionCommand();
122: if (I18N.get("DbConnWin.copy_settings").equals(cmd)) {
123: JFileChooser chooser = Designer.getChooser();
124: Designer.setPrefsDir(chooser, null);
125: int returnVal = chooser.showOpenDialog(this );
126: if (returnVal == JFileChooser.APPROVE_OPTION) {
127: DbConnReader reader = new DbConnReader();
128: try {
129: reader.read(chooser.getSelectedFile());
130: driverClassNameField.setText(reader
131: .getDriverClassName());
132: connInfoField.setText(reader.getConnectionInfo());
133: dbNameField.setText(reader.getDbName());
134: userNameField.setText(reader.getUserName());
135: } catch (Exception ex) {
136: ErrorHandler.error(
137: I18N.get("DbConnWin.copy_error"), ex);
138: }
139: }
140: } else
141: super .actionPerformed(e);
142: }
143:
144: protected void doSave() {
145: DbConnCommand cmd = new DbConnCommand(report, StringUtils
146: .nullOrTrimmed(driverClassNameField.getText()),
147: StringUtils.nullOrTrimmed(connInfoField.getText()),
148: StringUtils.nullOrTrimmed(dbNameField.getText()),
149: StringUtils.nullOrTrimmed(userNameField.getText()),
150: StringUtils.nullOrTrimmed(new String(passwordField
151: .getPassword())));
152: cmd.perform();
153: commands.add(cmd);
154: }
155:
156: protected void doRevert() {
157: fillEditFields();
158: }
159:
160: }
|