001: package com.xoetrope.carousel.langed;
002:
003: import java.sql.Driver;
004: import java.sql.DriverManager;
005: import java.util.Enumeration;
006: import java.util.prefs.Preferences;
007:
008: import java.awt.AWTEvent;
009: import java.awt.BorderLayout;
010: import java.awt.Color;
011: import java.awt.FlowLayout;
012: import java.awt.GridLayout;
013: import java.awt.event.ActionEvent;
014: import java.awt.event.ActionListener;
015: import java.awt.event.ItemEvent;
016: import java.awt.event.ItemListener;
017: import java.awt.event.WindowEvent;
018: import javax.swing.BorderFactory;
019: import javax.swing.JButton;
020: import javax.swing.JComboBox;
021: import javax.swing.JDialog;
022: import javax.swing.JLabel;
023: import javax.swing.JPanel;
024: import javax.swing.JPasswordField;
025: import javax.swing.JTextField;
026: import javax.swing.SwingConstants;
027: import javax.swing.border.Border;
028:
029: /**
030: * <p>Title: LanguageEditor</p>
031: * <p>Description: Language Resource Translation Utility</p>
032: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
033: * the GNU Public License (GPL), please see license.txt for more details. If
034: * you make commercial use of this software you must purchase a commercial
035: * license from Xoetrope.</p>
036: * <p> $Revision: 1.5 $</p>
037: */
038: public class ConnParamsDlg extends JDialog implements ActionListener,
039: ItemListener {
040: private JPanel jPanel2 = new JPanel();
041: private JButton okBtn = new JButton();
042: private FlowLayout flowLayout1 = new FlowLayout();
043: private JButton cancelBtn = new JButton();
044: private Preferences prefs = Preferences
045: .userNodeForPackage(LangEdDesktop.class);
046:
047: private boolean isOk = false;
048: private Border border1;
049: private JPanel jPanel5 = new JPanel();
050: private JPanel jPanel4 = new JPanel();
051: private JPanel jPanel3 = new JPanel();
052: private JPanel jPanel1 = new JPanel();
053: private JLabel connStrLabel = new JLabel();
054: private JTextField translationsEdit = new JTextField();
055: private JLabel tableLabel = new JLabel();
056: private JPasswordField passwordEdit = new JPasswordField();
057: private JComboBox dsnCombo = new JComboBox();
058: private GridLayout gridLayout3 = new GridLayout();
059: private GridLayout gridLayout2 = new GridLayout();
060: private JLabel jLabel4 = new JLabel();
061: private JLabel jLabel3 = new JLabel();
062: private JLabel jLabel2 = new JLabel();
063: private JTextField languagesEdit = new JTextField();
064: private JLabel jLabel1 = new JLabel();
065: private JTextField userNameEdit = new JTextField();
066: private BorderLayout borderLayout1 = new BorderLayout();
067: private JComboBox driverCombo = new JComboBox();
068: private BorderLayout borderLayout2 = new BorderLayout();
069: private Border border2;
070:
071: public ConnParamsDlg() {
072: super ();
073: enableEvents(AWTEvent.WINDOW_EVENT_MASK);
074: setModal(true);
075:
076: try {
077: jbInit();
078: LanguageEditorApplication.setDefaultFont(this );
079: } catch (Exception e) {
080: e.printStackTrace();
081: }
082: }
083:
084: private void jbInit() throws Exception {
085: isOk = false;
086: border1 = BorderFactory.createCompoundBorder(BorderFactory
087: .createEtchedBorder(Color.white, new Color(165, 163,
088: 151)), BorderFactory.createEmptyBorder(6, 6, 6,
089: 6));
090:
091: border2 = BorderFactory.createEmptyBorder(6, 6, 6, 6);
092: okBtn.setMnemonic('O');
093: okBtn.setText("OK");
094: okBtn.addActionListener(this );
095:
096: jPanel2.setLayout(flowLayout1);
097: cancelBtn.setMnemonic('C');
098: cancelBtn.setText("Cancel");
099: cancelBtn.addActionListener(this );
100:
101: this .setTitle("Connection Parameters");
102: jPanel4.setLayout(gridLayout3);
103: jPanel3.setLayout(gridLayout2);
104: jPanel1.setLayout(borderLayout1);
105: jPanel1.setBorder(border1);
106: connStrLabel.setHorizontalAlignment(SwingConstants.RIGHT);
107: connStrLabel.setText("Driver");
108: tableLabel.setHorizontalAlignment(SwingConstants.RIGHT);
109: tableLabel.setText("Languages table");
110: dsnCombo.setEditable(true);
111: gridLayout3.setHgap(2);
112: gridLayout3.setRows(6);
113: gridLayout3.setVgap(2);
114: gridLayout2.setHgap(2);
115: gridLayout2.setRows(6);
116: gridLayout2.setVgap(2);
117: jLabel4.setHorizontalAlignment(SwingConstants.RIGHT);
118: jLabel4.setText("Database (DSN)");
119: jLabel3.setHorizontalAlignment(SwingConstants.RIGHT);
120: jLabel3.setText("Translations table");
121: jLabel2.setHorizontalAlignment(SwingConstants.RIGHT);
122: jLabel2.setText("Password");
123: jLabel1.setHorizontalAlignment(SwingConstants.RIGHT);
124: jLabel1.setText("Username");
125: borderLayout1.setHgap(2);
126: borderLayout1.setVgap(2);
127: jPanel5.setLayout(borderLayout2);
128: jPanel5.setBorder(border2);
129: jPanel2.add(cancelBtn, null);
130: this .getContentPane().add(jPanel2, BorderLayout.SOUTH);
131: jPanel2.add(okBtn, null);
132: this .getContentPane().add(jPanel5, BorderLayout.NORTH);
133: jPanel5.add(jPanel1, BorderLayout.CENTER);
134: jPanel3.add(connStrLabel, null);
135: jPanel3.add(jLabel4, null);
136: jPanel3.add(jLabel1, null);
137: jPanel3.add(jLabel2, null);
138: jPanel3.add(tableLabel, null);
139: jPanel3.add(jLabel3, null);
140: jPanel1.add(jPanel4, BorderLayout.CENTER);
141: jPanel1.add(jPanel3, BorderLayout.WEST);
142: jPanel4.add(driverCombo, null);
143: jPanel4.add(dsnCombo, null);
144: jPanel4.add(userNameEdit, null);
145: jPanel4.add(passwordEdit, null);
146: jPanel4.add(languagesEdit, null);
147: jPanel4.add(translationsEdit, null);
148:
149: driverCombo.addItemListener(this );
150: dsnCombo.addItemListener(this );
151:
152: fillDriverList();
153: fillDsnList();
154:
155: languagesEdit.setText(prefs.get("languagesTable",
156: "CS_LANGUAGES"));
157: translationsEdit.setText(prefs.get("translationTable",
158: "CS_TRANSLATION"));
159: userNameEdit.setText(prefs.get("userName", "sa"));
160: passwordEdit.setText(prefs.get("password", ""));
161:
162: if (dsnCombo.getItemCount() > 2)
163: dsnCombo.setSelectedIndex(2);
164: }
165:
166: private void fillDriverList() {
167: driverCombo.removeAllItems();
168: driverCombo.addItem("sun.jdbc.odbc.JdbcOdbcDriver");
169: driverCombo.addItem("org.objectweb.rmijdbc.Driver");
170: driverCombo
171: .addItem("com.microsoft.jdbc.sqlserver.SQLServerDriver");
172:
173: Enumeration e = DriverManager.getDrivers();
174: while (e.hasMoreElements()) {
175: driverCombo.addItem(((Driver) e.nextElement()).toString());
176: }
177: }
178:
179: private void fillDsnList() {
180: }
181:
182: //Overridden so we can exit when window is closed
183: protected void processWindowEvent(WindowEvent e) {
184: if (e.getID() == WindowEvent.WINDOW_CLOSING)
185: cancel();
186:
187: super .processWindowEvent(e);
188: }
189:
190: //Close the dialog
191: void cancel() {
192: dispose();
193: }
194:
195: //Close the dialog on a button event
196: public void actionPerformed(ActionEvent e) {
197: if (e.getSource() == cancelBtn)
198: cancel();
199: else if (e.getSource() == okBtn) {
200: // Save the parameters
201: // String the table name out of the dsn
202: String dsn = dsnCombo.getSelectedItem().toString();
203: prefs.put("lastDsn", dsn
204: .substring(dsn.lastIndexOf(':') + 1));
205:
206: prefs.put("userName", userNameEdit.getText());
207: prefs.put("password",
208: new String(passwordEdit.getPassword()));
209: prefs.put("languagesTable", languagesEdit.getText());
210: prefs.put("translationTable", translationsEdit.getText());
211:
212: setVisible(false);
213: isOk = true;
214: }
215: }
216:
217: public void itemStateChanged(ItemEvent e) {
218: String fileName = prefs.get("lastDsn", "<<DSN>>");
219: if (e.getSource() == driverCombo) {
220: if (driverCombo.getSelectedItem().toString()
221: .indexOf("odbc") >= 0) {
222: dsnCombo.removeAllItems();
223: dsnCombo.addItem("jdbc:odbc:CoolSelector");
224: dsnCombo.addItem("jdbc:odbc:CoolCat");
225: if ((fileName.compareTo("CoolSelector") != 0)
226: && (fileName.compareTo("CoolCat") != 0))
227: dsnCombo.addItem("jdbc:odbc:" + fileName);
228: } else if (driverCombo.getSelectedItem().toString()
229: .indexOf("SQLServerDriver") >= 0) {
230: dsnCombo.removeAllItems();
231: dsnCombo
232: .addItem("jdbc:microsoft:sqlserver://server1:1433;DatabaseName=CoolSelector");
233: dsnCombo
234: .addItem("jdbc:microsoft:sqlserver://server1:1433;DatabaseName=CoolCat");
235: if ((fileName.compareTo("CoolSelector") != 0)
236: && (fileName.compareTo("CoolCat") != 0))
237: dsnCombo
238: .addItem("jdbc:microsoft:sqlserver://server1:1433;DatabaseName="
239: + fileName);
240: } else {
241: dsnCombo.removeAllItems();
242: dsnCombo.addItem("jdbc:rmi:CoolSelector");
243: dsnCombo.addItem("jdbc:rmi:CoolCat");
244: if ((fileName.compareTo("CoolSelector") != 0)
245: && (fileName.compareTo("CoolCat") != 0))
246: dsnCombo.addItem("jdbc:rmi:" + fileName);
247: }
248: }
249: }
250:
251: public String getDriver() {
252: return driverCombo.getSelectedItem().toString();
253: }
254:
255: public String getDsn() {
256: return dsnCombo.getSelectedItem().toString();
257: }
258:
259: public String getUserName() {
260: return userNameEdit.getText();
261: }
262:
263: public String getPassword() {
264: return new String(passwordEdit.getPassword());
265: }
266:
267: public String getLanguagesTable() {
268: return languagesEdit.getText();
269: }
270:
271: public String getTranslationsTable() {
272: return translationsEdit.getText();
273: }
274:
275: public boolean isOk() {
276: return isOk;
277: }
278: }
|