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.io.File;
031: import java.io.FileNotFoundException;
032: import java.io.FileOutputStream;
033: import java.net.MalformedURLException;
034: import java.sql.SQLException;
035: import java.util.Date;
036:
037: import javax.swing.Box;
038: import javax.swing.ButtonGroup;
039: import javax.swing.JCheckBox;
040: import javax.swing.JComponent;
041: import javax.swing.JLabel;
042: import javax.swing.JPanel;
043: import javax.swing.JRadioButton;
044: import javax.swing.JSeparator;
045: import javax.swing.JTextField;
046: import javax.swing.SwingConstants;
047: import javax.swing.text.Document;
048:
049: import org.isqlviewer.ServiceReference;
050: import org.isqlviewer.sql.JdbcService;
051: import org.isqlviewer.sql.embedded.EmbeddedDatabase;
052: import org.isqlviewer.swing.SwingUtilities;
053: import org.isqlviewer.swing.action.SharedActions;
054: import org.isqlviewer.swing.action.SwingEventManager;
055: import org.isqlviewer.ui.wizards.AbstractWizardStep;
056: import org.isqlviewer.ui.wizards.WizardContext;
057: import org.isqlviewer.util.BasicUtilities;
058: import org.isqlviewer.util.IsqlToolkit;
059: import org.isqlviewer.util.LocalMessages;
060: import org.isqlviewer.xml.ServiceDigester;
061:
062: public class ConfirmNewService extends AbstractWizardStep implements
063: ActionListener {
064:
065: private LocalMessages messages = new LocalMessages(
066: ServiceWizard.BUNDLE_NAME);
067: private Document nameDocument = null;
068: private Document urlDocument = null;
069: private Document classDocument = null;
070: private ButtonGroup versionCompatability = new ButtonGroup();
071: private JCheckBox connectOnFinish = null;
072:
073: public void actionPerformed(ActionEvent event) {
074:
075: }
076:
077: public boolean isFirst() {
078:
079: return false;
080: }
081:
082: public boolean isLast() {
083:
084: return true;
085: }
086:
087: public boolean isValid(WizardContext context) {
088:
089: JdbcService service = (JdbcService) context
090: .getAttribute(ServiceWizard.ATTRIBUTE_SERVICE);
091: String stringVersion = versionCompatability.getSelection()
092: .getActionCommand();
093: int version = Integer.parseInt(stringVersion);
094:
095: String safeName = BasicUtilities.createSafeFilename(service
096: .getName());
097: File file = new File(IsqlToolkit.getBaseDirectory(), safeName
098: + ".service");
099: FileOutputStream outputStream;
100: try {
101: outputStream = new FileOutputStream(file, false);
102: context.info("saving service:" + service.getName()
103: + " to location:" + file.getAbsolutePath());
104: ServiceDigester
105: .writeService(version, outputStream, service);
106: } catch (FileNotFoundException e) {
107:
108: }
109:
110: EmbeddedDatabase database = EmbeddedDatabase
111: .getSharedInstance();
112: ServiceReference serviceReference = new ServiceReference();
113: serviceReference.setName(service.getName());
114: serviceReference.setOrder(0);
115: try {
116: serviceReference.setResourceURL(file.toURL());
117: } catch (MalformedURLException error) {
118: }
119:
120: SwingEventManager eventManager = context.getEventManager();
121: if (connectOnFinish.isSelected()) {
122: serviceReference.setLastUsed(new Date());
123: context.info("Connnecting to new service:"
124: + service.getName());
125: eventManager.actionPerformed(new ActionEvent(this ,
126: SharedActions.SERVICE_CONNECT, service.getName()));
127: }
128:
129: try {
130: database.addService(serviceReference);
131: eventManager.actionPerformed(new ActionEvent(this ,
132: SharedActions.SERVICE_ADDED, service.getName()));
133: } catch (SQLException sqle) {
134: error("", sqle);
135: }
136:
137: return true;
138: }
139:
140: @Override
141: public void activate(WizardContext context) {
142:
143: JdbcService service = (JdbcService) context
144: .getAttribute(ServiceWizard.ATTRIBUTE_SERVICE);
145: if (service != null) {
146: SwingUtilities.replaceDocumentContent(nameDocument, service
147: .getName());
148: SwingUtilities.replaceDocumentContent(urlDocument, service
149: .getUrl());
150: SwingUtilities.replaceDocumentContent(classDocument,
151: service.getDriverClass());
152: }
153: }
154:
155: @Override
156: public void init(WizardContext context) {
157:
158: setTitle(messages
159: .getMessage("newservicewizard.finalize-service.title"));
160: setComment(messages
161: .getMessage("newservicewizard.finalize-service.tip"));
162: setImage(SwingUtilities.loadIconResource(ServiceWizard.class,
163: "ok", 22));
164:
165: JPanel panel = new JPanel(new GridBagLayout());
166: setView(panel);
167:
168: Object constraint = null;
169: JComponent component = null;
170: JLabel label = null;
171: String title = null;
172: String tip = null;
173:
174: title = messages.format("newservicewizard.servicename.title");
175: tip = messages.format("newservicewizard.servicename.tip");
176: component = new JTextField(32);
177: component.setToolTipText(tip);
178: component.setFont(component.getFont().deriveFont(Font.BOLD));
179: ((JTextField) component).setEditable(false);
180: nameDocument = ((JTextField) component).getDocument();
181: constraint = ServiceWizard.constrain(0, 0, 1, 1, 0.0, 0.0,
182: GridBagConstraints.WEST, GridBagConstraints.NONE);
183: label = new JLabel(title);
184: label.setLabelFor(component);
185: panel.add(label, constraint);
186: constraint = ServiceWizard.constrain(1, 0, 1, 1, 1.0, 0.0,
187: GridBagConstraints.CENTER,
188: GridBagConstraints.HORIZONTAL);
189: panel.add(component, constraint);
190:
191: title = messages.format("newservicewizard.jdbcdriver.title");
192: tip = messages.format("newservicewizard.jdbcdriver.tip");
193: component = new JTextField(32);
194: component.setToolTipText(tip);
195: component.setFont(component.getFont().deriveFont(Font.BOLD));
196: ((JTextField) component).setEditable(false);
197: classDocument = ((JTextField) component).getDocument();
198: constraint = ServiceWizard.constrain(0, 1, 1, 1, 0.0, 0.0,
199: GridBagConstraints.WEST, GridBagConstraints.NONE);
200: label = new JLabel(title);
201: label.setLabelFor(component);
202: panel.add(label, constraint);
203: constraint = ServiceWizard.constrain(1, 1, 1, 1, 1.0, 0.0,
204: GridBagConstraints.CENTER,
205: GridBagConstraints.HORIZONTAL);
206: panel.add(component, constraint);
207:
208: title = messages.format("newservicewizard.jdbcurl.title");
209: tip = messages.format("newservicewizard.jdbcurl.tip");
210: component = new JTextField(32);
211: component.setToolTipText(tip);
212: component.setFont(component.getFont().deriveFont(Font.BOLD));
213: ((JTextField) component).setEditable(false);
214: urlDocument = ((JTextField) component).getDocument();
215: constraint = ServiceWizard.constrain(0, 2, 1, 1, 0.0, 0.0,
216: GridBagConstraints.WEST, GridBagConstraints.NONE);
217: label = new JLabel(title);
218: label.setLabelFor(component);
219: panel.add(label, constraint);
220: constraint = ServiceWizard.constrain(1, 2, 1, 1, 1.0, 0.0,
221: GridBagConstraints.CENTER,
222: GridBagConstraints.HORIZONTAL);
223: panel.add(component, constraint);
224:
225: constraint = ServiceWizard.constrain(0, 3, 2, 1, 1.0d, 0.0d,
226: GridBagConstraints.CENTER,
227: GridBagConstraints.HORIZONTAL);
228: panel
229: .add(new JSeparator(SwingConstants.HORIZONTAL),
230: constraint);
231: panel.add(Box.createVerticalStrut(32), constraint);
232:
233: title = messages.format("newservicewizard.compatability_info");
234: component = new JLabel(title);
235: constraint = ServiceWizard.constrain(0, 4, 2, 1, 0.0, 0.0,
236: GridBagConstraints.WEST, GridBagConstraints.NONE);
237: panel.add(component, constraint);
238:
239: title = messages.format("newservicewizard.compatability");
240: tip = messages.format("newservicewizard.compatability.tip");
241: component = new JRadioButton();
242: versionCompatability.add((JRadioButton) component);
243: ((JRadioButton) component).setText(title);
244: ((JRadioButton) component).setSelected(true);
245: ((JRadioButton) component).setActionCommand(Integer
246: .toString(ServiceDigester.VERSION_2));
247: component.setToolTipText(tip);
248: constraint = ServiceWizard.constrain(0, 5, 2, 1, 0.0, 0.0,
249: GridBagConstraints.WEST, GridBagConstraints.NONE);
250: panel.add(component, constraint);
251:
252: title = messages.format("newservicewizard.no-compatability");
253: tip = messages.format("newservicewizard.no-compatability.tip");
254: component = new JRadioButton();
255: versionCompatability.add((JRadioButton) component);
256: ((JRadioButton) component).setText(title);
257: ((JRadioButton) component).setActionCommand(Integer
258: .toString(ServiceDigester.VERSION_3));
259: component.setToolTipText(tip);
260: constraint = ServiceWizard.constrain(0, 6, 2, 1, 0.0, 0.0,
261: GridBagConstraints.WEST, GridBagConstraints.NONE);
262: panel.add(component, constraint);
263:
264: title = messages.format("newservicewizard.connect_on_finish");
265: tip = messages.format("newservicewizard.connect_on_finish.tip");
266: connectOnFinish = new JCheckBox(title);
267: connectOnFinish.setToolTipText(tip);
268: connectOnFinish.setSelected(true);
269: constraint = ServiceWizard.constrain(0, 7, 2, 1, 1.0, 0.0,
270: GridBagConstraints.SOUTHEAST, GridBagConstraints.NONE);
271: panel.add(connectOnFinish, constraint);
272:
273: component = (JComponent) Box.createVerticalGlue();
274: component.setName("GLUE");
275: component.setToolTipText(tip);
276: constraint = ServiceWizard.constrain(0, 7, 2, 1, 0.0, 1.0,
277: GridBagConstraints.CENTER, GridBagConstraints.VERTICAL);
278: panel.add(component, constraint);
279: }
280: }
|