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 static java.awt.GridBagConstraints.HORIZONTAL;
026: import static java.awt.GridBagConstraints.NONE;
027: import static java.awt.GridBagConstraints.WEST;
028:
029: import java.awt.BorderLayout;
030: import java.awt.Container;
031: import java.awt.GridBagLayout;
032: import java.awt.event.ActionEvent;
033: import java.awt.event.ActionListener;
034: import java.sql.Driver;
035: import java.sql.DriverPropertyInfo;
036: import java.sql.SQLException;
037: import java.util.Hashtable;
038: import java.util.Map;
039: import java.util.Properties;
040: import java.util.Set;
041:
042: import javax.swing.Box;
043: import javax.swing.JButton;
044: import javax.swing.JCheckBox;
045: import javax.swing.JComboBox;
046: import javax.swing.JComponent;
047: import javax.swing.JLabel;
048: import javax.swing.JPanel;
049: import javax.swing.JScrollPane;
050: import javax.swing.JTextField;
051: import javax.swing.JToolBar;
052: import javax.swing.text.JTextComponent;
053:
054: import org.isqlviewer.sql.JdbcService;
055: import org.isqlviewer.swing.SwingUtilities;
056: import org.isqlviewer.ui.wizards.AbstractWizardStep;
057: import org.isqlviewer.ui.wizards.WizardContext;
058: import org.isqlviewer.util.LocalMessages;
059:
060: /**
061: * Wizard step for configuring JDBC driver options.
062: * <p>
063: * Options that are configured with this step are usually platform specific, and depends on what the driver is built to
064: * support. Things like connecting to the database over SSL can *usually* be configured here as in PostgreSQL.
065: * <p>
066: * Often Drivers will allow all of these options provided in this step to be embedded in the JDBC URL itself.
067: *
068: * @author Mark A. Kobold <mkobold at isqlviewer dot com>
069: * @version 1.0
070: */
071: public class DriverPropertyOptions extends AbstractWizardStep implements
072: ActionListener {
073:
074: private LocalMessages messages = new LocalMessages(
075: ServiceWizard.BUNDLE_NAME);
076: private DriverPropertyInfo[] currentOptions = new DriverPropertyInfo[0];
077: private Container uiContainer = null;
078: private Hashtable<String, JComponent> componentMap = new Hashtable<String, JComponent>();
079: private JCheckBox enableOptions = new JCheckBox();
080: private Driver driverRefrence;
081:
082: private static final String JDBC_ENV_PRINCIPAL = "user";
083: private static final String JDBC_ENV_PASSWORD = "password";
084:
085: public void actionPerformed(ActionEvent event) {
086:
087: Object src = event.getSource();
088: String command = event.getActionCommand();
089: WizardContext context = getContext();
090:
091: if (src == enableOptions) {
092: activate(context);
093: } else if ("refresh".equals(command)) {
094: Properties properties = (Properties) context
095: .getAttribute(ServiceWizard.ATTRIBUTE_PROPERTIES);
096: Set<Map.Entry<String, JComponent>> entries = componentMap
097: .entrySet();
098: for (Map.Entry<String, JComponent> entry : entries) {
099: String newValue = getPropertyValue(entry.getValue());
100: if (newValue != null) {
101: properties.setProperty(entry.getKey(), newValue);
102: } else {
103: properties.remove(entry.getKey());
104: }
105: }
106: activate(context);
107: } else if ("revert".equals(command)) {
108: context.removeAttribute(ServiceWizard.ATTRIBUTE_PROPERTIES);
109: context.setAttribute(ServiceWizard.ATTRIBUTE_PROPERTIES,
110: new Properties());
111: componentMap.clear();
112: activate(context);
113: }
114: }
115:
116: @Override
117: public void activate(WizardContext context) {
118:
119: super .activate(context);
120: JdbcService service = (JdbcService) context
121: .getAttribute(ServiceWizard.ATTRIBUTE_SERVICE);
122: if (service == null) {
123: return;
124: }
125: Properties environment = (Properties) context
126: .getAttribute(ServiceWizard.ATTRIBUTE_PROPERTIES);
127: if (environment == null) {
128: environment = service.getEnvironment();
129: context.setAttribute(ServiceWizard.ATTRIBUTE_PROPERTIES,
130: environment);
131: }
132: enableOptions.setSelected(!environment.isEmpty());
133: if (enableOptions.isSelected()) {
134: if (driverRefrence == null) {
135: driverRefrence = service.getDriver();
136: }
137: try {
138: currentOptions = driverRefrence.getPropertyInfo(service
139: .getUrl(), environment);
140: propertyChange(currentOptions);
141: } catch (SQLException e) {
142: context.showErrorDialog(e, "");
143: }
144: } else {
145: uiContainer.removeAll();
146: uiContainer.invalidate();
147: }
148: }
149:
150: public boolean isFirst() {
151:
152: return false;
153: }
154:
155: public boolean isLast() {
156:
157: return false;
158: }
159:
160: @Override
161: public void init(WizardContext context) {
162:
163: super .init(context);
164: setTitle(messages.getMessage("DriverPropertyOptions.title"));
165: setComment(messages.getMessage("DriverPropertyOptions.tip"));
166: setImage(SwingUtilities.loadIconResource("options", 22));
167:
168: String tip = null;
169: JButton button = null;
170:
171: JPanel panel = new JPanel(new BorderLayout());
172: JToolBar toolbar = new JToolBar(JToolBar.HORIZONTAL);
173: setView(panel);
174:
175: enableOptions.setText(messages
176: .format("DriverPropertyOptions.enable-options"));
177: enableOptions.addActionListener(this );
178: toolbar.add(enableOptions);
179: toolbar.add(Box.createHorizontalGlue());
180:
181: toolbar.setFloatable(false);
182: tip = messages
183: .format("DriverPropertyOptions.refresh-properties.tip");
184: button = new JButton(SwingUtilities.loadIconResource("reload",
185: 16));
186: button.setActionCommand("refresh");
187: button.addActionListener(this );
188: button.setToolTipText(tip);
189: toolbar.add(button);
190:
191: tip = messages
192: .format("DriverPropertyOptions.revert-properties.tip");
193: button = new JButton(SwingUtilities
194: .loadIconResource("undo", 16));
195: button.setActionCommand("revert");
196: button.addActionListener(this );
197: button.setToolTipText(tip);
198: toolbar.add(button);
199:
200: uiContainer = new JPanel(new GridBagLayout());
201:
202: panel.add(BorderLayout.CENTER, new JScrollPane(uiContainer));
203: panel.add(BorderLayout.NORTH, toolbar);
204: }
205:
206: public boolean isValid(WizardContext context) {
207:
208: if (!enableOptions.isSelected()) {
209:
210: } else {
211: JdbcService service = (JdbcService) context
212: .getAttribute(ServiceWizard.ATTRIBUTE_SERVICE);
213: Properties environment = (Properties) context
214: .getAttribute(ServiceWizard.ATTRIBUTE_PROPERTIES);
215: environment.clear();
216: Set<Map.Entry<String, JComponent>> entries = componentMap
217: .entrySet();
218: for (Map.Entry<String, JComponent> entry : entries) {
219: environment.put(entry.getKey(), getPropertyValue(entry
220: .getValue()));
221: }
222: service.setEnvironment(environment);
223: }
224: return true;
225: }
226:
227: private synchronized void propertyChange(
228: DriverPropertyInfo[] newOptions) {
229:
230: Object constraint = null;
231: uiContainer.removeAll();
232: int row = 0;
233: for (int i = 0; i < newOptions.length; i++) {
234: DriverPropertyInfo dpi = newOptions[i];
235: if (JDBC_ENV_PASSWORD.equals(dpi.name)
236: || JDBC_ENV_PRINCIPAL.equals(dpi.name)) {
237: // we already have these pre-defined so skip as to not confuse the user //
238: continue;
239: }
240:
241: JComponent component = componentMap.get(dpi.name);
242: if (component != null) {
243: updateComponent(component, dpi);
244: } else {
245: component = createNewEditor(dpi);
246: componentMap.put(dpi.name, component);
247: }
248: JLabel label = new JLabel(dpi.name);
249: label.setLabelFor(component);
250: if (dpi.required) {
251: label.setIcon(SwingUtilities.loadIconResource(
252: "button_ok", 16));
253: }
254: label.setToolTipText(dpi.description);
255: constraint = ServiceWizard.constrain(0, row, 1, 1, 0.0,
256: 0.0, WEST, NONE);
257: uiContainer.add(label, constraint);
258: component.setToolTipText(dpi.description);
259: constraint = ServiceWizard.constrain(1, row, 1, 1, 0.0,
260: 1.0, WEST, HORIZONTAL);
261: uiContainer.add(component, constraint);
262: row++;
263: }
264: uiContainer.validate();
265: }
266:
267: private JComponent createNewEditor(DriverPropertyInfo dpi) {
268:
269: String[] options = dpi.choices;
270:
271: if (options == null) {
272: JTextField textField = new JTextField(dpi.value);
273: return textField;
274: }
275:
276: if (options.length == 2) {
277: boolean isBooleanProperty = true;
278: for (int i = 0; i < 2; i++) {
279: String boolString = options[i];
280: if ("true".equalsIgnoreCase(boolString)
281: || "false".equalsIgnoreCase(boolString)) {
282: continue;
283: }
284: isBooleanProperty = false;
285: break;
286: }
287: if (isBooleanProperty) {
288: JCheckBox checkBox = new JCheckBox();
289: checkBox.setSelected(Boolean.valueOf(dpi.value)
290: .booleanValue());
291: return checkBox;
292: }
293: }
294: JComboBox choices = new JComboBox(options);
295: choices.setSelectedItem(dpi.value);
296: return choices;
297: }
298:
299: private void updateComponent(JComponent component,
300: DriverPropertyInfo newOption) {
301:
302: if (component instanceof JTextComponent) {
303: JTextComponent textField = (JTextComponent) component;
304: textField.setText(newOption.value);
305: } else if (component instanceof JCheckBox) {
306: JCheckBox checkBox = (JCheckBox) component;
307: checkBox.setSelected(Boolean.valueOf(newOption.value)
308: .booleanValue());
309: } else if (component instanceof JComboBox) {
310: JComboBox comboBox = (JComboBox) component;
311: comboBox.setSelectedItem(newOption.value);
312: }
313: }
314:
315: private String getPropertyValue(JComponent value) {
316:
317: if (value instanceof JTextComponent) {
318: return ((JTextComponent) value).getText();
319: } else if (value instanceof JCheckBox) {
320: return Boolean.toString(((JCheckBox) value).isSelected());
321: } else if (value instanceof JComboBox) {
322: JComboBox comboBox = (JComboBox) value;
323: Object selected = comboBox.getSelectedItem();
324: return selected == null ? null : selected.toString();
325: }
326: return null;
327: }
328: }
|