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.GridBagConstraints;
026: import java.awt.GridBagLayout;
027: import java.awt.event.MouseEvent;
028: import java.awt.event.MouseListener;
029: import java.io.IOException;
030: import java.io.InputStream;
031: import java.net.URL;
032: import java.sql.SQLException;
033: import java.util.Collection;
034:
035: import javax.swing.DefaultListModel;
036: import javax.swing.Icon;
037: import javax.swing.JComponent;
038: import javax.swing.JList;
039: import javax.swing.JPanel;
040: import javax.swing.JScrollPane;
041: import javax.swing.event.ListSelectionEvent;
042: import javax.swing.event.ListSelectionListener;
043:
044: import org.isqlviewer.ServiceReference;
045: import org.isqlviewer.sql.JdbcService;
046: import org.isqlviewer.sql.embedded.EmbeddedDatabase;
047: import org.isqlviewer.swing.SwingUtilities;
048: import org.isqlviewer.ui.laf.EnhancedListCellRenderer;
049: import org.isqlviewer.ui.wizards.AbstractWizardStep;
050: import org.isqlviewer.ui.wizards.WizardContext;
051: import org.isqlviewer.util.IsqlToolkit;
052: import org.isqlviewer.util.LocalMessages;
053: import org.isqlviewer.xml.ServiceDigester;
054: import org.xml.sax.InputSource;
055:
056: public class SelectServiceStep extends AbstractWizardStep implements
057: ListSelectionListener, MouseListener {
058:
059: private LocalMessages messages = new LocalMessages(
060: ServiceWizard.BUNDLE_NAME);
061: private DefaultListModel serviceList = new DefaultListModel();
062: private int serviceSelection = -1;
063:
064: public void mouseClicked(MouseEvent e) {
065:
066: if (e.getClickCount() == 2) {
067: if (serviceSelection >= 0) {
068: WizardContext context = getContext();
069: context.invokeNext();
070: }
071: }
072: }
073:
074: public void mouseEntered(MouseEvent e) {
075:
076: }
077:
078: public void mouseExited(MouseEvent e) {
079:
080: }
081:
082: public void mousePressed(MouseEvent e) {
083:
084: }
085:
086: public void mouseReleased(MouseEvent e) {
087:
088: }
089:
090: public boolean isFirst() {
091:
092: return false;
093: }
094:
095: public boolean isLast() {
096:
097: return false;
098: }
099:
100: @Override
101: public void activate(WizardContext context) {
102:
103: super .activate(context);
104: EmbeddedDatabase edb = EmbeddedDatabase.getSharedInstance();
105: try {
106: if (serviceList.isEmpty()) {
107: Collection<ServiceReference> references = edb
108: .getRegisteredServices();
109: for (ServiceReference sr : references) {
110: serviceList.addElement(sr);
111: }
112: }
113: } catch (SQLException error) {
114: String message = messages
115: .format("SelectServiceStep.failed_to_load_service_definitions");
116: context.showErrorDialog(error, message);
117: }
118: }
119:
120: public boolean isValid(WizardContext context) {
121:
122: if (serviceSelection < 0) {
123: return false;
124: }
125: ServiceReference selection = (ServiceReference) serviceList
126: .getElementAt(serviceSelection);
127: if (selection == null) {
128: return false;
129: }
130: String function = (String) context
131: .getAttribute(ServiceWizard.ATTRIBUTE_FUNCTION);
132: context.setAttribute(ServiceWizard.ATTRIBUTE_SERVICE_REFERENCE,
133: selection);
134: URL url = selection.getResourceURL();
135: InputStream inputStream = null;
136: try {
137: inputStream = url.openStream();
138: InputSource inputSource = new InputSource(inputStream);
139: JdbcService service = ServiceDigester.parseService(
140: inputSource, IsqlToolkit.getSharedEntityResolver());
141: context.setAttribute(ServiceWizard.ATTRIBUTE_SERVICE,
142: service);
143: return true;
144: } catch (IOException error) {
145: if (!ServiceWizard.FUNCTION_DELETE.equals(function)) {
146: String loc = url.toExternalForm();
147: String message = messages
148: .format(
149: "SelectServiceStep.failed_to_load_service_from_url",
150: loc);
151: context.showErrorDialog(error, message);
152: } else {
153: return true;
154: }
155: } catch (Exception error) {
156: String message = messages.format(
157: "SelectServiceStep.service_load_error", url
158: .toExternalForm());
159: context.showErrorDialog(error, message);
160: }
161: return false;
162: }
163:
164: @Override
165: public void init(WizardContext context) {
166:
167: super .init(context);
168: setTitle(messages.getMessage("SelectServiceStep.title"));
169: setComment(messages.getMessage("SelectServiceStep.comment"));
170: setImage(SwingUtilities.loadIconResource(ServiceWizard.class,
171: "service", 22));
172:
173: String tip = null;
174: JComponent component = null;
175: GridBagConstraints constraint = null;
176:
177: JPanel panel = new JPanel(new GridBagLayout());
178: setView(panel);
179:
180: tip = messages.format("SelectServiceStep.list_view.tip");
181: component = new JList(serviceList);
182: component.addMouseListener(this );
183: ((JList) component).addListSelectionListener(this );
184: ((JList) component)
185: .setCellRenderer(new ServiceReferenceRenderer());
186: component.setToolTipText(tip);
187: constraint = ServiceWizard.constrain(0, 0, 1, 1, 1.0, 1.0,
188: GridBagConstraints.CENTER, GridBagConstraints.BOTH);
189: panel.add(new JScrollPane(component), constraint);
190: }
191:
192: public void valueChanged(ListSelectionEvent e) {
193:
194: if (e == null) {
195: serviceSelection = -1;
196: } else if (!e.getValueIsAdjusting()) {
197: serviceSelection = e.getFirstIndex();
198: }
199: }
200:
201: private static class ServiceReferenceRenderer extends
202: EnhancedListCellRenderer {
203:
204: private static final long serialVersionUID = 5174787602448379313L;
205:
206: @Override
207: public String getText(Object aObject) {
208:
209: if (aObject instanceof ServiceReference) {
210: return ((ServiceReference) aObject).getName();
211: }
212: return super .getText(aObject);
213: }
214:
215: @Override
216: public Icon getIcon(Object aObject) {
217:
218: if (aObject instanceof ServiceReference) {
219: return SwingUtilities.loadIconResource("service", 22);
220: }
221: return super.getIcon(aObject);
222: }
223: }
224: }
|