001: package com.xoetrope.carousel.survey;
002:
003: import java.awt.BorderLayout;
004: import java.awt.Dimension;
005: import java.awt.FlowLayout;
006: import java.awt.GridBagConstraints;
007: import java.awt.GridBagLayout;
008: import java.awt.Insets;
009: import java.awt.Toolkit;
010: import java.awt.event.ActionEvent;
011: import java.awt.event.ActionListener;
012: import javax.swing.*;
013: import javax.swing.border.Border;
014: import javax.swing.border.TitledBorder;
015: import net.xoetrope.xui.XProject;
016: import net.xoetrope.xui.XProjectManager;
017:
018: /**
019: * A dialog for configuring input database connection.
020: *
021: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
022: * the GNU Public License (GPL), please see license.txt for more details. If
023: * you make commercial use of this software you must purchase a commercial
024: * license from Xoetrope.</p>
025: * <p> $Revision: 1.5 $</p>
026: */
027: //@todo show the available surveys, langauges, etc. in this dialog
028: public class InputDbDialog extends JDialog {
029: private static final String DEF_SURVEYNAME = "Survey1";
030: private static final String DEF_LANGUAGE = "English";
031: private static final String DEF_URL = "jdbc:hsqldb:hsql://localhost";
032: private static final String DEF_DRIVER = "org.hsqldb.jdbcDriver";
033: private static final String DEF_USERNAME = "sa";
034: private static final String DEF_PASSWORD = "";
035:
036: public static final int LABEL_WIDTH = 80;
037: public static final int EDIT_WIDTH = 160;
038: public static final int ROW_HEIGHT = 24;
039: public static final int WIDTH = LABEL_WIDTH + EDIT_WIDTH + 30;
040: public static final int HEIGHT = 260;
041: public static final int BUTTON_WIDTH = 80;
042: public static final int BUTTON_HEIGHT = 24;
043:
044: protected JTextField surveyNameEdit;
045: protected JTextField surveyLanguageEdit;
046: protected JTextField dbUrlEdit;
047: protected JTextField dbDriverEdit;
048: protected JTextField dbUserEdit;
049: protected JPasswordField dbPasswordEdit;
050:
051: protected boolean okPressed;
052:
053: public InputDbDialog() {
054: super ();
055: setModal(true);
056: okPressed = false;
057: init();
058: }
059:
060: public boolean showDialog() {
061: okPressed = false;
062: setVisible(true);
063: return okPressed;
064: }
065:
066: private void init() {
067: Dimension labelSize = new Dimension(LABEL_WIDTH, ROW_HEIGHT);
068: Dimension editSize = new Dimension(EDIT_WIDTH, ROW_HEIGHT);
069: Dimension buttonSize = new Dimension(BUTTON_WIDTH,
070: BUTTON_HEIGHT);
071: Dimension size = new Dimension(WIDTH, HEIGHT);
072: setSize(size);
073: setPreferredSize(size);
074:
075: TitledBorder titledBorder = BorderFactory
076: .createTitledBorder(" Setup the database connection");
077:
078: JPanel mainPanel = new JPanel();
079: mainPanel.setLayout(new BorderLayout());
080: JPanel editPanel = new JPanel();
081: editPanel.setLayout(new GridBagLayout());
082: JPanel bottomPanel = new JPanel();
083: bottomPanel.setLayout(new FlowLayout());
084:
085: // survey name
086: JLabel surveyNameLabel = new JLabel("Survey name");
087: surveyNameLabel.setSize(labelSize);
088: surveyNameLabel.setPreferredSize(labelSize);
089: surveyNameLabel.setMinimumSize(labelSize);
090: surveyNameLabel.setHorizontalAlignment(SwingConstants.RIGHT);
091: surveyNameEdit = new JTextField();
092: surveyNameEdit.setSize(editSize);
093: surveyNameEdit.setMinimumSize(editSize);
094: surveyNameEdit.setPreferredSize(editSize);
095:
096: // survey language
097: JLabel surveyLanguageLabel = new JLabel("Language");
098: surveyLanguageLabel.setSize(labelSize);
099: surveyLanguageLabel.setPreferredSize(labelSize);
100: surveyLanguageLabel.setMinimumSize(labelSize);
101: surveyLanguageLabel
102: .setHorizontalAlignment(SwingConstants.RIGHT);
103: surveyLanguageEdit = new JTextField();
104: surveyLanguageEdit.setSize(editSize);
105: surveyLanguageEdit.setMinimumSize(editSize);
106: surveyLanguageEdit.setPreferredSize(editSize);
107:
108: // database url
109: JLabel dbUrlLabel = new JLabel("Database URL");
110: dbUrlLabel.setSize(labelSize);
111: dbUrlLabel.setPreferredSize(labelSize);
112: dbUrlLabel.setMinimumSize(labelSize);
113: dbUrlLabel.setHorizontalAlignment(SwingConstants.RIGHT);
114: dbUrlEdit = new JTextField();
115: dbUrlEdit.setSize(editSize);
116: dbUrlEdit.setMinimumSize(editSize);
117: dbUrlEdit.setPreferredSize(editSize);
118:
119: // database driver
120: JLabel dbDriverLabel = new JLabel("Database driver");
121: dbDriverLabel.setSize(labelSize);
122: dbDriverLabel.setPreferredSize(labelSize);
123: dbDriverLabel.setMinimumSize(labelSize);
124: dbDriverLabel.setHorizontalAlignment(SwingConstants.RIGHT);
125: dbDriverEdit = new JTextField();
126: dbDriverEdit.setSize(editSize);
127: dbDriverEdit.setMinimumSize(editSize);
128: dbDriverEdit.setPreferredSize(editSize);
129:
130: // database user
131: JLabel dbUserLabel = new JLabel("Username");
132: dbUserLabel.setSize(labelSize);
133: dbUserLabel.setPreferredSize(labelSize);
134: dbUserLabel.setMinimumSize(labelSize);
135: dbUserLabel.setHorizontalAlignment(SwingConstants.RIGHT);
136: dbUserEdit = new JTextField();
137: dbUserEdit.setSize(editSize);
138: dbUserEdit.setMinimumSize(editSize);
139: dbUserEdit.setPreferredSize(editSize);
140:
141: // database password
142: JLabel dbPasswordLabel = new JLabel("Password");
143: dbPasswordLabel.setSize(labelSize);
144: dbPasswordLabel.setPreferredSize(labelSize);
145: dbPasswordLabel.setMinimumSize(labelSize);
146: dbPasswordLabel.setHorizontalAlignment(SwingConstants.RIGHT);
147: dbPasswordEdit = new JPasswordField();
148: dbPasswordEdit.setSize(editSize);
149: dbPasswordEdit.setMinimumSize(editSize);
150: dbPasswordEdit.setPreferredSize(editSize);
151:
152: // setup the default values
153: surveyNameEdit.setText(DEF_SURVEYNAME);
154: surveyLanguageEdit.setText(DEF_LANGUAGE);
155: dbUrlEdit.setText(DEF_URL);
156: dbDriverEdit.setText(DEF_DRIVER);
157: dbUserEdit.setText(DEF_USERNAME);
158: dbPasswordEdit.setText(DEF_PASSWORD);
159:
160: JButton buttonOk = new JButton("Load");
161: buttonOk.setSize(buttonSize);
162: buttonOk.setPreferredSize(buttonSize);
163: buttonOk.setMinimumSize(buttonSize);
164: buttonOk.addActionListener(new ActionListener() {
165: public void actionPerformed(ActionEvent e) {
166: okPressed = true;
167: InputDbDialog.this .setVisible(false);
168: }
169: });
170:
171: JButton buttonCancel = new JButton("Cancel");
172: buttonCancel.setSize(buttonSize);
173: buttonCancel.setPreferredSize(buttonSize);
174: buttonCancel.setMinimumSize(buttonSize);
175: buttonCancel.addActionListener(new ActionListener() {
176: public void actionPerformed(ActionEvent e) {
177: okPressed = false;
178: InputDbDialog.this .setVisible(false);
179: }
180: });
181:
182: GridBagConstraints c = new GridBagConstraints();
183: c.insets = new Insets(1, 1, 1, 1);
184:
185: // survey name
186: c.gridx = 0;
187: c.gridy = 0;
188: editPanel.add(surveyNameLabel, c);
189: c.gridx = 1;
190: c.gridy = 0;
191: editPanel.add(surveyNameEdit, c);
192:
193: // survey langauge
194: c.gridx = 0;
195: c.gridy = 1;
196: editPanel.add(surveyLanguageLabel, c);
197: c.gridx = 1;
198: c.gridy = 1;
199: editPanel.add(surveyLanguageEdit, c);
200:
201: // db url
202: c.gridx = 0;
203: c.gridy = 2;
204: editPanel.add(dbUrlLabel, c);
205: c.gridx = 1;
206: c.gridy = 2;
207: editPanel.add(dbUrlEdit, c);
208:
209: // db driver
210: c.gridx = 0;
211: c.gridy = 3;
212: editPanel.add(dbDriverLabel, c);
213: c.gridx = 1;
214: c.gridy = 3;
215: editPanel.add(dbDriverEdit, c);
216:
217: // db user
218: c.gridx = 0;
219: c.gridy = 4;
220: editPanel.add(dbUserLabel, c);
221: c.gridx = 1;
222: c.gridy = 4;
223: editPanel.add(dbUserEdit, c);
224:
225: // db password
226: c.gridx = 0;
227: c.gridy = 5;
228: editPanel.add(dbPasswordLabel, c);
229: c.gridx = 1;
230: c.gridy = 5;
231: editPanel.add(dbPasswordEdit, c);
232:
233: bottomPanel.add(buttonOk);
234: bottomPanel.add(buttonCancel);
235: mainPanel.add(editPanel, BorderLayout.CENTER);
236: mainPanel.add(bottomPanel, BorderLayout.SOUTH);
237: editPanel.setBorder(titledBorder);
238: add(mainPanel);
239: pack();
240:
241: Dimension screenSize = Toolkit.getDefaultToolkit()
242: .getScreenSize();
243: Dimension dialogSize = getSize();
244: setLocation((screenSize.width - dialogSize.width) / 2,
245: (screenSize.height - dialogSize.height) / 2);
246: }
247:
248: public String getSurveyName() {
249: return surveyNameEdit.getText();
250: }
251:
252: public String getSurveyLanguage() {
253: return surveyLanguageEdit.getText();
254: }
255:
256: public String getDbUrl() {
257: return dbUrlEdit.getText();
258: }
259:
260: public String getDbDriver() {
261: return dbDriverEdit.getText();
262: }
263:
264: public String getDbUser() {
265: return dbUserEdit.getText();
266: }
267:
268: public String getDbPassword() {
269: return dbPasswordEdit.getText();
270: }
271:
272: }
|