001: package com.pk;
002:
003: import java.awt.Container;
004: import java.awt.Dimension;
005: import java.awt.GridBagConstraints;
006: import java.awt.GridBagLayout;
007: import java.awt.Insets;
008: import java.awt.Toolkit;
009: import java.awt.event.ActionEvent;
010: import java.awt.event.ActionListener;
011: import java.awt.event.FocusEvent;
012: import java.awt.event.FocusListener;
013: import java.sql.Connection;
014: import java.sql.DriverManager;
015: import java.util.List;
016:
017: import javax.swing.JButton;
018: import javax.swing.JComboBox;
019: import javax.swing.JDialog;
020: import javax.swing.JLabel;
021: import javax.swing.JOptionPane;
022: import javax.swing.JPanel;
023: import javax.swing.JPasswordField;
024: import javax.swing.JTextField;
025:
026: public class ConnectionDialog extends JDialog implements FocusListener {
027:
028: /**
029: *
030: */
031: private static final long serialVersionUID = 3075853931225627975L;
032:
033: protected boolean canceled;
034:
035: protected JTextField passwordField;
036:
037: protected ConnectionInformation connectionInformation;
038:
039: protected JComboBox urlField;
040:
041: protected JTextField useridField;
042:
043: private Connection connection;
044:
045: private Config config;
046:
047: public ConnectionDialog(Config argConfig) {
048: config = argConfig;
049: setTitle("Connect To A Database");
050: Toolkit dKit = getToolkit(); // Get the toolkit of window
051: Dimension wSize = dKit.getScreenSize(); // Get the size of window
052: setBounds(wSize.width / 2 - 200, wSize.height / 2 - 200, // location
053: wSize.width / 2, wSize.height / 2); // size
054: buildDialogLayout();
055: setSize(300, 200);
056: }
057:
058: protected boolean attemptConnection() {
059: try {
060: connection = null;
061: connectionInformation = new ConnectionInformation();
062:
063: ConnectionConfig tmpConnectionConfig = (ConnectionConfig) urlField
064: .getSelectedItem();
065:
066: connectionInformation.setUrl(tmpConnectionConfig.getUrl());
067: connectionInformation.setConnectionListKey(String
068: .valueOf(System.currentTimeMillis()));
069: connectionInformation.setPassword(passwordField.getText());
070: connectionInformation.setUserID(useridField.getText());
071: connectionInformation.setNumberOfWindows(1);
072: connectionInformation
073: .setDatabaseDialect(tmpConnectionConfig
074: .getDatabaseDialect());
075: connectionInformation.setConnectionName(tmpConnectionConfig
076: .getName());
077: connection = DriverManager.getConnection(
078: connectionInformation.getUrl(),
079: connectionInformation.getUserID(),
080: connectionInformation.getPassword());
081: connection.setAutoCommit(false);
082: return true;
083: } catch (Exception e) {
084: e.printStackTrace();
085: JOptionPane.showMessageDialog(this , "Error connecting to "
086: + "database: " + e.getMessage());
087: }
088: return false;
089: }
090:
091: protected void buildDialogLayout() {
092: JLabel label;
093: urlField = new JComboBox();
094: java.util.List connectionConfigs = config.getConnections();
095: int size = connectionConfigs.size();
096: for (int x = 0; x < size; x++) {
097: urlField.addItem(connectionConfigs.get(x));
098: }
099: Container pane = getContentPane();
100: pane.setLayout(new GridBagLayout());
101: GridBagConstraints gbc = new GridBagConstraints();
102: gbc.anchor = GridBagConstraints.WEST;
103: gbc.insets = new Insets(6, 15, 6, 15);
104:
105: gbc.gridx = 0;
106: gbc.gridy = 0;
107: label = new JLabel("Userid:", JLabel.LEFT);
108: pane.add(label, gbc);
109:
110: gbc.gridy++;
111: label = new JLabel("Password:", JLabel.LEFT);
112: pane.add(label, gbc);
113:
114: gbc.gridy++;
115: label = new JLabel("Url:", JLabel.LEFT);
116: pane.add(label, gbc);
117:
118: gbc.gridy++;
119: //label = new JLabel("Oracle:", JLabel.LEFT);
120:
121: gbc.gridx = 1;
122: gbc.gridy = 0;
123:
124: useridField = new JTextField(10);
125: pane.add(useridField, gbc);
126:
127: gbc.gridy++;
128: passwordField = new JPasswordField(10);
129: pane.add(passwordField, gbc);
130:
131: gbc.gridy++;
132: //urlField = new JTextField("",15);
133:
134: pane.add(urlField, gbc);
135:
136: gbc.gridx = 0;
137: gbc.gridy = 4;
138: gbc.gridwidth = GridBagConstraints.REMAINDER;
139: gbc.anchor = GridBagConstraints.CENTER;
140: pane.add(getButtonPanel(), gbc);
141: }
142:
143: public void focusGained(FocusEvent e) {
144: }
145:
146: public void focusLost(FocusEvent e) {
147: }
148:
149: protected JPanel getButtonPanel() {
150: JPanel panel = new JPanel();
151: JButton btn1 = new JButton("Ok");
152: getRootPane().setDefaultButton(btn1);
153: btn1.addActionListener(new ActionListener() {
154: public void actionPerformed(ActionEvent event) {
155: onDialogOk();
156: }
157: });
158: panel.add(btn1);
159: JButton btn2 = new JButton("Cancel");
160: btn2.addActionListener(new ActionListener() {
161: public void actionPerformed(ActionEvent event) {
162: onDialogCancel();
163: }
164: });
165: panel.add(btn2);
166: return panel;
167: }
168:
169: public Connection getConnection() {
170: return connection;
171: }
172:
173: protected void onDialogCancel() {
174: useridField.setText("");
175: passwordField.setText("");
176: setVisible(false);
177: }
178:
179: protected void onDialogOk() {
180:
181: if (attemptConnection()) {
182: useridField.setText("");
183: passwordField.setText("");
184: setVisible(false);
185: }
186: }
187:
188: public List getKeywords() {
189: return connectionInformation.getDatabaseDialect().getKeywords();
190: }
191:
192: /**
193: * @return Returns the connectionInformation.
194: */
195: public ConnectionInformation getConnectionInformation() {
196: return connectionInformation;
197: }
198: }
|