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 ConfirmUpdateService 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: ServiceReference reference = (ServiceReference) context
092: .getAttribute(ServiceWizard.ATTRIBUTE_SERVICE_REFERENCE);
093:
094: String safeName = BasicUtilities.createSafeFilename(reference
095: .getName());
096: File existingFile = new File(IsqlToolkit.getBaseDirectory(),
097: safeName + ".service");
098: boolean deleted = false;
099: if (existingFile.canWrite()) {
100: deleted = existingFile.delete();
101: }
102:
103: if (!deleted) {
104: context
105: .warn(messages
106: .format("ConfirmUpdateService.failed_to_remove_old_service_file"));
107: }
108:
109: String stringVersion = versionCompatability.getSelection()
110: .getActionCommand();
111: int version = Integer.parseInt(stringVersion);
112: safeName = BasicUtilities.createSafeFilename(service.getName());
113: File file = new File(IsqlToolkit.getBaseDirectory(), safeName
114: + ".service");
115: FileOutputStream outputStream;
116: try {
117: outputStream = new FileOutputStream(file, false);
118: ServiceDigester
119: .writeService(version, outputStream, service);
120: } catch (FileNotFoundException error) {
121: String message = messages.format(
122: "ConfirmUpdateService.service_file_not_found", file
123: .getAbsolutePath());
124: context.showErrorDialog(error, message);
125: return false;
126: }
127:
128: EmbeddedDatabase database = EmbeddedDatabase
129: .getSharedInstance();
130: reference.setName(service.getName());
131: reference.setOrder(0);
132: try {
133: reference.setResourceURL(file.toURL());
134: } catch (MalformedURLException ignored) {
135: context.debug("", ignored);
136: }
137:
138: SwingEventManager eventManager = context.getEventManager();
139: try {
140: database.updateService(reference);
141: eventManager.actionPerformed(new ActionEvent(this ,
142: SharedActions.SERVICE_UPDATED, service.getName()));
143: if (connectOnFinish.isSelected()) {
144: reference.setLastUsed(new Date());
145: eventManager.actionPerformed(new ActionEvent(this ,
146: SharedActions.SERVICE_CONNECT, service
147: .getName()));
148: }
149: } catch (SQLException error) {
150: String message = messages.format(
151: "ConfirmUpdateService.internal_sql_error", service
152: .getName());
153: context.showErrorDialog(error, message);
154: return false;
155: }
156: return true;
157: }
158:
159: @Override
160: public void activate(WizardContext context) {
161:
162: JdbcService service = (JdbcService) context
163: .getAttribute(ServiceWizard.ATTRIBUTE_SERVICE);
164: if (service != null) {
165: SwingUtilities.replaceDocumentContent(nameDocument, service
166: .getName());
167: SwingUtilities.replaceDocumentContent(urlDocument, service
168: .getUrl());
169: SwingUtilities.replaceDocumentContent(classDocument,
170: service.getDriverClass());
171: }
172:
173: }
174:
175: @Override
176: public void init(WizardContext context) {
177:
178: setTitle(messages.getMessage("ConfirmServiceUpdate.title"));
179: setComment(messages.getMessage("ConfirmServiceUpdate.comment"));
180: setImage(SwingUtilities.loadIconResource(ServiceWizard.class,
181: "button_ok", 22));
182:
183: JPanel panel = new JPanel(new GridBagLayout());
184: setView(panel);
185:
186: Object constraint = null;
187: JComponent component = null;
188: JLabel label = null;
189: String title = null;
190: String tip = null;
191:
192: title = messages.format("newservicewizard.servicename.title");
193: tip = messages.format("newservicewizard.servicename.tip");
194: component = new JTextField(32);
195: component.setToolTipText(tip);
196: component.setFont(component.getFont().deriveFont(Font.BOLD));
197: ((JTextField) component).setEditable(false);
198: nameDocument = ((JTextField) component).getDocument();
199: constraint = ServiceWizard.constrain(0, 0, 1, 1, 0.0, 0.0,
200: GridBagConstraints.WEST, GridBagConstraints.NONE);
201: label = new JLabel(title);
202: label.setLabelFor(component);
203: panel.add(label, constraint);
204: constraint = ServiceWizard.constrain(1, 0, 1, 1, 1.0, 0.0,
205: GridBagConstraints.CENTER,
206: GridBagConstraints.HORIZONTAL);
207: panel.add(component, constraint);
208:
209: title = messages.format("newservicewizard.jdbcdriver.title");
210: tip = messages.format("newservicewizard.jdbcdriver.tip");
211: component = new JTextField(32);
212: component.setToolTipText(tip);
213: component.setFont(component.getFont().deriveFont(Font.BOLD));
214: ((JTextField) component).setEditable(false);
215: classDocument = ((JTextField) component).getDocument();
216: constraint = ServiceWizard.constrain(0, 1, 1, 1, 0.0, 0.0,
217: GridBagConstraints.WEST, GridBagConstraints.NONE);
218: label = new JLabel(title);
219: label.setLabelFor(component);
220: panel.add(label, constraint);
221: constraint = ServiceWizard.constrain(1, 1, 1, 1, 1.0, 0.0,
222: GridBagConstraints.CENTER,
223: GridBagConstraints.HORIZONTAL);
224: panel.add(component, constraint);
225:
226: title = messages.format("newservicewizard.jdbcurl.title");
227: tip = messages.format("newservicewizard.jdbcurl.tip");
228: component = new JTextField(32);
229: component.setToolTipText(tip);
230: component.setFont(component.getFont().deriveFont(Font.BOLD));
231: ((JTextField) component).setEditable(false);
232: urlDocument = ((JTextField) component).getDocument();
233: constraint = ServiceWizard.constrain(0, 2, 1, 1, 0.0, 0.0,
234: GridBagConstraints.WEST, GridBagConstraints.NONE);
235: label = new JLabel(title);
236: label.setLabelFor(component);
237: panel.add(label, constraint);
238: constraint = ServiceWizard.constrain(1, 2, 1, 1, 1.0, 0.0,
239: GridBagConstraints.CENTER,
240: GridBagConstraints.HORIZONTAL);
241: panel.add(component, constraint);
242:
243: constraint = ServiceWizard.constrain(0, 3, 2, 1, 1.0d, 0.0d,
244: GridBagConstraints.CENTER,
245: GridBagConstraints.HORIZONTAL);
246: panel
247: .add(new JSeparator(SwingConstants.HORIZONTAL),
248: constraint);
249: panel.add(Box.createVerticalStrut(32), constraint);
250:
251: title = messages.format("newservicewizard.compatability_info");
252: component = new JLabel(title);
253: constraint = ServiceWizard.constrain(0, 4, 2, 1, 0.0, 0.0,
254: GridBagConstraints.WEST, GridBagConstraints.NONE);
255: panel.add(component, constraint);
256:
257: title = messages.format("newservicewizard.compatability");
258: tip = messages.format("newservicewizard.compatability.tip");
259: component = new JRadioButton();
260: versionCompatability.add((JRadioButton) component);
261: ((JRadioButton) component).setText(title);
262: ((JRadioButton) component).setActionCommand(Integer
263: .toString(ServiceDigester.VERSION_2));
264: component.setToolTipText(tip);
265: constraint = ServiceWizard.constrain(0, 5, 2, 1, 0.0, 0.0,
266: GridBagConstraints.WEST, GridBagConstraints.NONE);
267: panel.add(component, constraint);
268:
269: title = messages.format("newservicewizard.no-compatability");
270: tip = messages.format("newservicewizard.no-compatability.tip");
271: component = new JRadioButton();
272: versionCompatability.add((JRadioButton) component);
273: ((JRadioButton) component).setText(title);
274: ((JRadioButton) component).setSelected(true);
275: ((JRadioButton) component).setActionCommand(Integer
276: .toString(ServiceDigester.VERSION_3));
277: component.setToolTipText(tip);
278: constraint = ServiceWizard.constrain(0, 6, 2, 1, 0.0, 0.0,
279: GridBagConstraints.WEST, GridBagConstraints.NONE);
280: panel.add(component, constraint);
281:
282: title = messages.format("newservicewizard.connect_on_finish");
283: tip = messages.format("newservicewizard.connect_on_finish.tip");
284: connectOnFinish = new JCheckBox(title);
285: connectOnFinish.setToolTipText(tip);
286: connectOnFinish.setSelected(true);
287: constraint = ServiceWizard.constrain(0, 7, 2, 1, 1.0, 0.0,
288: GridBagConstraints.SOUTHEAST, GridBagConstraints.NONE);
289: panel.add(connectOnFinish, constraint);
290:
291: component = (JComponent) Box.createVerticalGlue();
292: component.setName("GLUE");
293: component.setToolTipText(tip);
294: constraint = ServiceWizard.constrain(0, 7, 2, 1, 0.0, 1.0,
295: GridBagConstraints.CENTER, GridBagConstraints.VERTICAL);
296: panel.add(component, constraint);
297: }
298: }
|