001: /*
002: * The contents of this file are subject to the Mozilla Public License
003: * Version 1.1 (the "License"); you may not use this file except in
004: * compliance with the License. You may obtain a copy of the License at
005: * http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
009: * License for the specific language governing rights and limitations
010: * under the License.
011: *
012: * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
013: *
014: * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
015: * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
016: *
017: * Contributor(s):
018: * Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
019: *
020: * If you didn't download this code from the following link, you should check
021: * if you aren't using an obsolete version: http://www.isqlviewer.com
022: */
023: package org.isqlviewer.ui.wizards.service;
024:
025: import java.awt.Font;
026: import java.awt.GridBagConstraints;
027: import java.awt.GridBagLayout;
028: import java.awt.event.ActionEvent;
029: import java.awt.event.ActionListener;
030: import java.sql.SQLException;
031: import java.util.Properties;
032: import java.util.Set;
033:
034: import javax.swing.Box;
035: import javax.swing.DefaultComboBoxModel;
036: import javax.swing.JCheckBox;
037: import javax.swing.JComboBox;
038: import javax.swing.JComponent;
039: import javax.swing.JLabel;
040: import javax.swing.JPanel;
041: import javax.swing.JPasswordField;
042: import javax.swing.JTextField;
043:
044: import org.isqlviewer.sql.ConnectionProfile;
045: import org.isqlviewer.sql.JdbcService;
046: import org.isqlviewer.sql.embedded.EmbeddedDatabase;
047: import org.isqlviewer.swing.SwingUtilities;
048: import org.isqlviewer.ui.wizards.AbstractWizardStep;
049: import org.isqlviewer.ui.wizards.WizardContext;
050: import org.isqlviewer.util.IsqlToolkit;
051: import org.isqlviewer.util.LocalMessages;
052:
053: public class ConfigureBasicServiceOptions extends AbstractWizardStep
054: implements ActionListener {
055:
056: private LocalMessages messages = new LocalMessages(
057: ServiceWizard.BUNDLE_NAME);
058: private JTextField serviceName = null;
059: private JTextField serviceDriver = null;
060: private JTextField serviceURL = null;
061: private JTextField serviceExampleURL = null;
062: private JTextField servicePrincipal = null;
063: private JTextField serviceCredentials = null;
064: private JCheckBox useAuthenticationPrompt = null;
065: private Properties driverMappings = IsqlToolkit
066: .getDefaultDriverDefinitions();
067:
068: public void actionPerformed(ActionEvent event) {
069:
070: String command = event.getActionCommand();
071: if ("1".equals(command)) {
072: boolean flag = servicePrincipal.isEnabled();
073: servicePrincipal.setEnabled(!flag);
074: servicePrincipal.setEditable(!flag);
075: serviceCredentials.setEnabled(!flag);
076: serviceCredentials.setEditable(!flag);
077: } else {
078: String className = serviceDriver.getText();
079: String defaultExample = messages
080: .getMessage("newservicewizard.jdbcurlexample.notavailable");
081: String exampleURL = driverMappings.getProperty(className,
082: defaultExample);
083: serviceExampleURL.setText(exampleURL);
084: }
085: }
086:
087: @Override
088: public void activate(WizardContext context) {
089:
090: super .activate(context);
091: JdbcService service = (JdbcService) context
092: .getAttribute(ServiceWizard.ATTRIBUTE_SERVICE);
093: if (service == null) {
094: return;
095: }
096: serviceURL.setText(service.getUrl());
097: serviceName.setText(service.getName());
098:
099: String principal = service.getPrincipal();
100: String credentials = service.getCredentials();
101: boolean usePrompt = principal == null && credentials == null;
102: if (!usePrompt) {
103: servicePrincipal.setText(principal);
104: serviceCredentials.setText(credentials);
105: } else {
106: servicePrincipal.setText("");
107: serviceCredentials.setText("");
108: }
109: useAuthenticationPrompt.setSelected(usePrompt);
110: servicePrincipal.setEnabled(!usePrompt);
111: servicePrincipal.setEditable(!usePrompt);
112: serviceCredentials.setEnabled(!usePrompt);
113: serviceCredentials.setEditable(!usePrompt);
114:
115: String className = service.getDriverClass();
116: if (className != null) {
117: serviceDriver.setText(className);
118: String defaultExample = messages
119: .getMessage("newservicewizard.jdbcurlexample.notavailable");
120: String exampleURL = driverMappings.getProperty(className,
121: defaultExample);
122: serviceExampleURL.setText(exampleURL);
123: }
124: }
125:
126: public boolean isFirst() {
127:
128: return false;
129: }
130:
131: public boolean isLast() {
132:
133: return false;
134: }
135:
136: @Override
137: public void init(WizardContext context) {
138:
139: super .init(context);
140: setTitle(messages
141: .getMessage("newservicewizard.configure-service-details.title"));
142: setComment(messages
143: .getMessage("newservicewizard.configure-service-details.tip"));
144: setImage(SwingUtilities.loadIconResource(ServiceWizard.class,
145: "database", 22));
146:
147: GridBagConstraints constraint = null;
148: String title = null;
149: String tip = null;
150: JLabel label = null;
151:
152: JPanel panel = new JPanel(new GridBagLayout());
153: setView(panel);
154:
155: title = messages.format("newservicewizard.servicename.title");
156: tip = messages.format("newservicewizard.servicename.tip");
157: JComponent component = new JTextField(32);
158: serviceName = ((JTextField) component);
159: component.setToolTipText(tip);
160: constraint = ServiceWizard.constrain(0, 0, 1, 1, 0.0, 0.0,
161: GridBagConstraints.WEST, GridBagConstraints.NONE);
162: label = new JLabel(title);
163: label.setLabelFor(component);
164: panel.add(label, constraint);
165: constraint = ServiceWizard.constrain(1, 0, 1, 1, 1.0, 0.0,
166: GridBagConstraints.CENTER,
167: GridBagConstraints.HORIZONTAL);
168: panel.add(component, constraint);
169:
170: title = messages.format("newservicewizard.jdbcdriver.title");
171: tip = messages.format("newservicewizard.jdbcdriver.tip");
172: DefaultComboBoxModel model = new DefaultComboBoxModel();
173: JComboBox comboSupport = new JComboBox();
174: comboSupport.setModel(model);
175: component = (JTextField) comboSupport.getEditor()
176: .getEditorComponent();
177:
178: Set<Object> driverClasses = driverMappings.keySet();
179: for (Object className : driverClasses) {
180: model.addElement(className);
181: }
182: comboSupport.addActionListener(this );
183: comboSupport.setMaximumRowCount(5);
184: comboSupport.setEditable(true);
185: serviceDriver = ((JTextField) component);
186: component.setToolTipText(tip);
187: constraint = ServiceWizard.constrain(0, 1, 1, 1, 0.0, 0.0,
188: GridBagConstraints.WEST, GridBagConstraints.NONE);
189: label = new JLabel(title);
190: label.setLabelFor(comboSupport);
191: panel.add(label, constraint);
192: constraint = ServiceWizard.constrain(1, 1, 1, 1, 1.0, 0.0,
193: GridBagConstraints.CENTER,
194: GridBagConstraints.HORIZONTAL);
195: panel.add(comboSupport, constraint);
196:
197: title = messages
198: .format("newservicewizard.jdbcurlexample.title");
199: tip = messages.format("newservicewizard.jdbcurlexample.tip");
200: component = new JTextField();
201: ((JTextField) component).setEditable(false);
202:
203: serviceExampleURL = ((JTextField) component);
204: component.setToolTipText(tip);
205: component.setFont(component.getFont().deriveFont(Font.ITALIC));
206: constraint = ServiceWizard.constrain(0, 2, 1, 1, 1.0, 0.0,
207: GridBagConstraints.WEST, GridBagConstraints.NONE);
208: label = new JLabel(title);
209: label.setLabelFor(component);
210: panel.add(label, constraint);
211: constraint = ServiceWizard.constrain(1, 2, 1, 1, 1.0, 0.0,
212: GridBagConstraints.CENTER,
213: GridBagConstraints.HORIZONTAL);
214: panel.add(component, constraint);
215:
216: String defaultExample = messages
217: .getMessage("newservicewizard.jdbcurlexample.notavailable");
218: String exampleURL = driverMappings.getProperty(serviceDriver
219: .getText(), defaultExample);
220: serviceExampleURL.setText(exampleURL);
221:
222: title = messages.format("newservicewizard.jdbcurl.title");
223: tip = messages.format("newservicewizard.jdbcurl.tip");
224: component = new JTextField();
225: serviceURL = ((JTextField) component);
226: component.setToolTipText(tip);
227: constraint = ServiceWizard.constrain(0, 3, 1, 1, 0.0, 0.0,
228: GridBagConstraints.WEST, GridBagConstraints.NONE);
229: label = new JLabel(title);
230: label.setLabelFor(component);
231: panel.add(label, constraint);
232: constraint = ServiceWizard.constrain(1, 3, 1, 1, 1.0, 0.0,
233: GridBagConstraints.CENTER,
234: GridBagConstraints.HORIZONTAL);
235: panel.add(component, constraint);
236:
237: title = messages.format("newservicewizard.principal.title");
238: tip = messages.format("newservicewizard.principal.tip");
239: component = new JTextField();
240: servicePrincipal = ((JTextField) component);
241: component.setToolTipText(tip);
242: constraint = ServiceWizard.constrain(0, 5, 1, 1, 0.0, 0.0,
243: GridBagConstraints.WEST, GridBagConstraints.NONE);
244: label = new JLabel(title);
245: label.setLabelFor(component);
246: panel.add(label, constraint);
247: constraint = ServiceWizard.constrain(1, 5, 1, 1, 1.0, 0.0,
248: GridBagConstraints.CENTER,
249: GridBagConstraints.HORIZONTAL);
250: panel.add(component, constraint);
251:
252: title = messages.format("newservicewizard.credentials.title");
253: tip = messages.format("newservicewizard.credentials.tip");
254: component = new JPasswordField();
255: serviceCredentials = ((JTextField) component);
256: component.setToolTipText(tip);
257: constraint = ServiceWizard.constrain(0, 6, 1, 1, 0.0, 0.0,
258: GridBagConstraints.WEST, GridBagConstraints.NONE);
259: label = new JLabel(title);
260: label.setLabelFor(component);
261: panel.add(label, constraint);
262: constraint = ServiceWizard.constrain(1, 6, 1, 1, 1.0, 0.0,
263: GridBagConstraints.CENTER,
264: GridBagConstraints.HORIZONTAL);
265: panel.add(component, constraint);
266:
267: title = messages
268: .format("newservicewizard.use_authentication_prompt.title");
269: tip = messages
270: .format("newservicewizard.use_authentication_prompt.tip");
271:
272: component = new JCheckBox(title);
273: useAuthenticationPrompt = (JCheckBox) component;
274: ((JCheckBox) component).setActionCommand(Integer.toString(1));
275: ((JCheckBox) component).addActionListener(this );
276: ((JCheckBox) component).doClick();
277: component.setToolTipText(tip);
278: constraint = ServiceWizard.constrain(0, 4, 2, 1, 1.0, 0.0,
279: GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL);
280: panel.add(component, constraint);
281:
282: component = (JComponent) Box.createVerticalGlue();
283: component.setName("GLUE");
284: component.setToolTipText(tip);
285: constraint = ServiceWizard.constrain(0, 7, 2, 1, 0.0, 1.0,
286: GridBagConstraints.CENTER, GridBagConstraints.VERTICAL);
287: panel.add(component, constraint);
288: }
289:
290: public boolean isValid(WizardContext context) {
291:
292: JdbcService service = (JdbcService) context
293: .getAttribute(ServiceWizard.ATTRIBUTE_SERVICE);
294: EmbeddedDatabase edb = EmbeddedDatabase.getSharedInstance();
295: boolean isValid = true;
296: String text = serviceName.getText().trim();
297: boolean exists = true;
298: try {
299: exists = edb.getServiceIDForName(text) < 0;
300: } catch (SQLException e) {
301: }
302:
303: if (text.length() == 0 || exists) {
304: if (text.length() == 0) {
305: context
306: .showInformationDialog(messages
307: .getMessage("newservicewizard.name-blank.error"));
308: } else {
309: context
310: .showInformationDialog(messages
311: .getMessage("newservicewizard.name-registerd.error"));
312: }
313: context.markTextComponentIncorrect(serviceName);
314: isValid = false;
315: } else {
316: context.unmarkTextComponentIncorrect(serviceName);
317: }
318:
319: text = serviceURL.getText().trim();
320: if (text.length() == 0) {
321: context.showInformationDialog(messages
322: .getMessage("newservicewizard.url-blank.error"));
323: context.markTextComponentIncorrect(serviceURL);
324: isValid = false;
325: } else {
326: context.unmarkTextComponentIncorrect(serviceURL);
327: }
328:
329: text = serviceDriver.getText().trim();
330: if (text.length() == 0) {
331: context.showInformationDialog(messages
332: .getMessage("newservicewizard.driver-blank.error"));
333: context.markTextComponentIncorrect(serviceDriver);
334: isValid = false;
335: } else {
336: context.unmarkTextComponentIncorrect(serviceDriver);
337: }
338:
339: if (isValid) {
340: service.setName(serviceName.getText());
341: service.setUrl(serviceURL.getText());
342: service.setDriverClass(serviceDriver.getText());
343: ConnectionProfile profile = service.getProfile();
344: profile.setCredentialsPersistent(false);
345: if (serviceCredentials.isEnabled()) {
346: String credentials = new String(
347: ((JPasswordField) serviceCredentials)
348: .getPassword());
349: service.setCredentials(credentials);
350: service.setPrincipal(servicePrincipal.getText());
351: profile.setCredentialsPersistent(true);
352: }
353: }
354: return isValid;
355: }
356: }
|