001: /*
002: * Copyright 2007 Madhav Pulipaka
003: *
004: * This file is part of Vela.
005:
006: * Vela is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * Vela is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with Vela; if not, write to the Free Software
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
019: */
020: package vela.view;
021:
022: import java.awt.Color;
023: import java.awt.Component;
024: import java.awt.Container;
025: import java.awt.Dimension;
026: import java.awt.FlowLayout;
027: import java.awt.BorderLayout;
028: import java.awt.GridLayout;
029: import java.awt.Font;
030: import java.awt.Frame;
031: import java.awt.Toolkit;
032: import java.awt.event.ActionEvent;
033: import java.awt.event.ActionListener;
034: import java.awt.event.ItemEvent;
035: import java.awt.event.ItemListener;
036:
037: import javax.swing.BorderFactory;
038: import javax.swing.JButton;
039: import javax.swing.JComboBox;
040: import javax.swing.JDialog;
041: import javax.swing.JEditorPane;
042: import javax.swing.JLabel;
043: import javax.swing.JPanel;
044: import javax.swing.JScrollPane;
045: import javax.swing.JTextArea;
046: import javax.swing.JTextPane;
047: import javax.swing.JToolTip;
048: import javax.swing.border.Border;
049: import javax.swing.event.DocumentEvent;
050: import javax.swing.event.DocumentListener;
051: import javax.swing.text.Position;
052: import javax.swing.JTextField;
053: import javax.swing.JOptionPane;
054: import java.util.Properties;
055: import java.util.TreeMap;
056: import java.util.Enumeration;
057: import java.util.Iterator;
058: import java.sql.SQLException;
059:
060: import vela.model.*;
061: import vela.common.Constants;
062: import vela.db.OraDBManager;
063: import vela.db.DBManager;
064:
065: public class ConnectionParams extends JDialog implements
066: ActionListener, ItemListener, Constants {
067:
068: private static final int WIDTH = 400;
069: private static final int HEIGHT = 225;
070: TreeMap tmConnections;
071: JComboBox databaseConns = new JComboBox();
072: JTextField tUserId = new JTextField();
073: JTextField tPassword = new JTextField();
074: JTextField tHostName = new JTextField();
075: JTextField tSid = new JTextField();
076: JTextField tPort = new JTextField();
077: int retStatus = 1;
078: ConnectionProperties selectedConnProps;
079: Properties connProperties;
080: DBManager dbManager = null;
081:
082: public ConnectionParams(Frame frame, Properties properties,
083: DBManager dbManager) {
084: super (frame, "Connection Parameters", true);
085: connProperties = properties;
086: this .dbManager = dbManager;
087: readProperties(properties);
088: Container container = getContentPane();
089: JButton btnConnect = new JButton("Connect");
090: JButton btnCancel = new JButton("Cancel");
091: btnConnect.setActionCommand("Connect");
092: btnCancel.setActionCommand("Cancel");
093: Dimension screenSize = Toolkit.getDefaultToolkit()
094: .getScreenSize();
095: setBounds((int) (screenSize.getWidth() / 2) - (WIDTH / 2),
096: (int) (screenSize.getHeight() / 2) - (HEIGHT / 2),
097: WIDTH, HEIGHT);
098: btnConnect.addActionListener(this );
099: btnCancel.addActionListener(this );
100: JPanel southPanel = new JPanel(
101: new FlowLayout(FlowLayout.CENTER));
102: southPanel.add(btnConnect);
103: southPanel.add(btnCancel);
104: JLabel label = null;
105: label = new JLabel("Please input your connection parameters.");
106: label.setFont(new Font(fontFamily, Font.BOLD, fontBig));
107: container.add(southPanel, BorderLayout.SOUTH);
108: southPanel.setBorder(BorderFactory.createEtchedBorder());
109: JPanel northPanel = new JPanel(new BorderLayout());
110: JPanel centerPanel = new JPanel(new GridLayout());
111: centerPanel.setBorder(BorderFactory.createEtchedBorder());
112: northPanel.setBorder(BorderFactory.createEtchedBorder());
113: northPanel.add(label);
114: container.add(northPanel, BorderLayout.NORTH);
115: container.add(centerPanel);
116: btnConnect.setFont(new Font("Verdana", Font.BOLD, 12));
117: btnCancel.setFont(new Font("Verdana", Font.BOLD, 12));
118: centerPanel.setLayout(new GridLayout(6, 2));
119: if (tmConnections != null && !tmConnections.isEmpty()) {
120: Iterator iter = tmConnections.keySet().iterator();
121: while (iter.hasNext()) {
122: databaseConns.addItem(iter.next());
123: }
124: }
125: JLabel lConnName = new JLabel(" Connection Name");
126: JLabel lUserId = new JLabel(" User Id");
127: JLabel lPassword = new JLabel(" Password");
128: JLabel lHostName = new JLabel(" Host Name/IP Address");
129: JLabel lSid = new JLabel(" SID");
130: JLabel lPort = new JLabel(" Port");
131:
132: centerPanel.add(lConnName);
133: centerPanel.add(databaseConns);
134: centerPanel.add(lUserId);
135: centerPanel.add(tUserId);
136: centerPanel.add(lPassword);
137: centerPanel.add(tPassword);
138: centerPanel.add(lHostName);
139: centerPanel.add(tHostName);
140: centerPanel.add(lSid);
141: centerPanel.add(tSid);
142: centerPanel.add(lPort);
143: centerPanel.add(tPort);
144: lUserId.setFont(new Font(fontFamily, Font.BOLD, fontSmall));
145: tUserId.setFont(new Font(fontFamily, Font.PLAIN, fontSmall));
146: lPassword.setFont(new Font(fontFamily, Font.BOLD, fontSmall));
147: tPassword.setFont(new Font(fontFamily, Font.PLAIN, fontSmall));
148: lHostName.setFont(new Font(fontFamily, Font.BOLD, fontSmall));
149: tHostName.setFont(new Font(fontFamily, Font.PLAIN, fontSmall));
150: lSid.setFont(new Font(fontFamily, Font.BOLD, fontSmall));
151: tSid.setFont(new Font(fontFamily, Font.PLAIN, fontSmall));
152: lPort.setFont(new Font(fontFamily, Font.BOLD, fontSmall));
153: tPort.setFont(new Font(fontFamily, Font.PLAIN, fontSmall));
154: lConnName.setFont(new Font(fontFamily, Font.BOLD, fontSmall));
155: databaseConns.setFont(new Font(fontFamily, Font.PLAIN,
156: fontSmall));
157: databaseConns.setSelectedIndex(-1);
158: databaseConns.setEditable(true);
159: databaseConns.addItemListener(this );
160: retStatus = 1;
161: }
162:
163: public void actionPerformed(ActionEvent ae) {
164: if (ae.getActionCommand().equalsIgnoreCase("Connect")) {
165: if (!validateParams())
166: return;
167: selectedConnProps.setPassword(tPassword.getText());
168: try {
169: dbManager.init(selectedConnProps);
170: } catch (SQLException sqlEx) {
171: JOptionPane.showMessageDialog(this , sqlEx.getMessage());
172: } catch (Exception ex) {
173: JOptionPane.showMessageDialog(this ,
174: "Failed to connect to the database.");
175: }
176: if (dbManager.isConnectionOpen()) {
177: retStatus = 0;
178: setVisible(false);
179: dispose();
180: }
181: } else if (ae.getActionCommand().equalsIgnoreCase("Cancel")) {
182: retStatus = 1;
183: setVisible(false);
184: dispose();
185: }
186: }
187:
188: public void itemStateChanged(ItemEvent e) {
189: if (e.getStateChange() == ItemEvent.SELECTED) {
190: String connName = (String) databaseConns.getSelectedItem();
191: ConnectionProperties connProps = (ConnectionProperties) tmConnections
192: .get(connName);
193: tUserId.setText(connProps.getUserName());
194: tHostName.setText(connProps.getHost());
195: tSid.setText(connProps.getSid());
196: tPort.setText(connProps.getPort());
197: selectedConnProps = connProps;
198: }
199: }
200:
201: public void readProperties(Properties props)
202: {
203: tmConnections = new TreeMap();
204: Enumeration enum = props.keys();
205: while(enum.hasMoreElements())
206: {
207: String key = (String)enum.nextElement();
208: if(key.indexOf(".")==-1)
209: continue;
210: String connName = key.substring(0, key.indexOf("."));
211: ConnectionProperties connProps;
212: if(tmConnections.containsKey(connName))
213: connProps = (ConnectionProperties)tmConnections.get(connName);
214: else
215: {
216: connProps = new ConnectionProperties();
217: tmConnections.put(connName, connProps);
218: }
219: connProps.setDbName(connName);
220: String connParam = key.substring(key.indexOf(".")+1);
221: if(connParam.equalsIgnoreCase("USERID"))
222: connProps.setUserName(props.getProperty(key));
223: else if(connParam.equalsIgnoreCase("HOST"))
224: connProps.setHost(props.getProperty(key));
225: else if(connParam.equalsIgnoreCase("SID"))
226: connProps.setSid(props.getProperty(key));
227: else if(connParam.equalsIgnoreCase("PORT"))
228: connProps.setPort(props.getProperty(key));
229: }
230: }
231:
232: public int getRetStatus() {
233: return retStatus;
234: }
235:
236: public void setRetStatus(int retStatus) {
237: this .retStatus = retStatus;
238: }
239:
240: public ConnectionProperties getSelectedConnProps() {
241: return selectedConnProps;
242: }
243:
244: public void setSelectedConnProps(
245: ConnectionProperties selectedConnProps) {
246: this .selectedConnProps = selectedConnProps;
247: }
248:
249: public boolean validateParams() {
250: String dbName = (String) databaseConns.getSelectedItem();
251: String user = tUserId.getText();
252: String passwd = tPassword.getText();
253: String host = tHostName.getText();
254: String sid = tSid.getText();
255: String port = tPort.getText();
256: if (dbName == null || dbName.trim().length() == 0) {
257: JOptionPane
258: .showMessageDialog(this ,
259: "Please select a database name or enter database name.");
260: databaseConns.requestFocus();
261: return false;
262: }
263: return true;
264: }
265:
266: }
|