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.Component;
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.net.MalformedURLException;
032: import java.net.URL;
033: import java.net.URLClassLoader;
034: import java.util.ArrayList;
035: import java.util.Enumeration;
036:
037: import javax.swing.Box;
038: import javax.swing.DefaultListModel;
039: import javax.swing.JButton;
040: import javax.swing.JComponent;
041: import javax.swing.JFileChooser;
042: import javax.swing.JList;
043: import javax.swing.JPanel;
044: import javax.swing.JScrollPane;
045: import javax.swing.JToolBar;
046:
047: import org.isqlviewer.sql.ConnectionProfile;
048: import org.isqlviewer.sql.JdbcService;
049: import org.isqlviewer.swing.SwingUtilities;
050: import org.isqlviewer.ui.renderer.FileListCellRenderer;
051: import org.isqlviewer.ui.wizards.AbstractWizardStep;
052: import org.isqlviewer.ui.wizards.WizardContext;
053: import org.isqlviewer.util.LocalMessages;
054:
055: public class ConfigureServiceResources extends AbstractWizardStep
056: implements ActionListener {
057:
058: private LocalMessages messages = new LocalMessages(
059: ServiceWizard.BUNDLE_NAME);
060: private DefaultListModel fileListModel = new DefaultListModel();
061: private JList fileList = new JList(fileListModel);
062: private static final int ACTION_ADD_FILE = 0;
063: private static final int ACTION_REMOVE_FILE = 1;
064:
065: public void actionPerformed(ActionEvent event) {
066:
067: String command = event.getActionCommand();
068: Component source = (Component) event.getSource();
069: int action = -1;
070: try {
071: action = Integer.parseInt(command);
072: } catch (Exception error) {
073: error.printStackTrace();
074: }
075: switch (action) {
076: case ACTION_REMOVE_FILE:
077: int[] idxs = fileList.getSelectedIndices();
078: for (int i = idxs.length - 1; i >= 0; i--) {
079: fileListModel.removeElementAt(idxs[i]);
080: }
081: break;
082: case ACTION_ADD_FILE:
083: File[] f = SwingUtilities.getSystemFiles(source,
084: JFileChooser.FILES_AND_DIRECTORIES);
085: if (f != null) {
086: for (int i = 0; i < f.length; i++) {
087: fileListModel.addElement(f[i]);
088: }
089: }
090: break;
091: default:
092: break;
093: }
094: }
095:
096: public boolean isFirst() {
097:
098: return false;
099: }
100:
101: public boolean isLast() {
102:
103: return false;
104: }
105:
106: public boolean isValid(WizardContext context) {
107:
108: JdbcService service = (JdbcService) context
109: .getAttribute(ServiceWizard.ATTRIBUTE_SERVICE);
110: ArrayList<URL> classPath = new ArrayList<URL>();
111: int count = fileListModel.getSize();
112: for (int i = 0; i < count; i++) {
113: File file = (File) fileListModel.get(i);
114: try {
115: classPath.add(file.toURL());
116: } catch (MalformedURLException e) {
117: context.error("file.toURL()", e);
118: }
119: }
120:
121: URLClassLoader classLoader = new URLClassLoader(classPath
122: .toArray(new URL[count]));
123: ClassLoader contextClassLoader = Thread.currentThread()
124: .getContextClassLoader();
125: Thread.currentThread().setContextClassLoader(classLoader);
126: try {
127: Class.forName(service.getDriverClass(), false, classLoader);
128: } catch (ClassNotFoundException error) {
129: String localMessage = messages.format(
130: "newservicewizard.class-not-found", service
131: .getDriverClass());
132: context.showErrorDialog(null, localMessage);
133: return false;
134: } catch (Error error) {
135: String localMessage = messages.format(
136: "newservicewizard.class-init-error", service
137: .getDriverClass());
138: context.showErrorDialog(null, localMessage);
139: return false;
140: } finally {
141: Thread.currentThread().setContextClassLoader(
142: contextClassLoader);
143: classLoader = null;
144: System.gc();
145: }
146:
147: ConnectionProfile profile = service.getProfile();
148: if (profile != null) {
149: Enumeration<File> existingElements = profile
150: .resourceElements();
151: while (existingElements.hasMoreElements()) {
152: profile.removeResource(existingElements.nextElement());
153: }
154: } else {
155: profile = new ConnectionProfile();
156: service.setProfile(profile);
157: }
158: for (int i = 0; i < count; i++) {
159: profile.addResource((File) fileListModel.get(i));
160: }
161: return true;
162: }
163:
164: @Override
165: public void init(WizardContext context) {
166:
167: setTitle(messages
168: .getMessage("newservicewizard.configure-resources.title"));
169: setComment(messages
170: .getMessage("newservicewizard.configure-resources.tip"));
171: setImage(SwingUtilities.loadIconResource(ServiceWizard.class,
172: "database", 22));
173:
174: GridBagConstraints constraint = null;
175: String tip = null;
176:
177: JPanel panel = new JPanel(new GridBagLayout());
178: setView(panel);
179:
180: JComponent component = new JToolBar(JToolBar.VERTICAL);
181: ((JToolBar) component).setFloatable(false);
182: constraint = ServiceWizard.constrain(0, 0, 1, 1, 0.0, 1.0,
183: GridBagConstraints.NORTHWEST, GridBagConstraints.NONE);
184: panel.add(component, constraint);
185:
186: tip = messages.format("newservicewizard.jdbcurlexample.tip");
187: fileList.setToolTipText(tip);
188: fileList.setCellRenderer(new FileListCellRenderer());
189: // fileList.setDropTarget(null);
190: constraint = ServiceWizard.constrain(1, 0, 1, 1, 1.0, 1.0,
191: GridBagConstraints.CENTER, GridBagConstraints.BOTH);
192: panel.add(new JScrollPane(fileList), constraint);
193:
194: JButton actionButton = null;
195:
196: actionButton = new JButton(SwingUtilities.loadIconResource(
197: ServiceWizard.class, "add", 16));
198: actionButton.addActionListener(this );
199: actionButton
200: .setActionCommand(Integer.toString(ACTION_ADD_FILE));
201: actionButton.setToolTipText(messages
202: .getMessage("newservicewizard.add-resources.tip"));
203: component.add(actionButton);
204:
205: actionButton = new JButton(SwingUtilities.loadIconResource(
206: ServiceWizard.class, "delete", 16));
207: actionButton.addActionListener(this );
208: actionButton.setActionCommand(Integer
209: .toString(ACTION_REMOVE_FILE));
210: actionButton.setToolTipText(messages
211: .getMessage("newservicewizard.delete-resources.tip"));
212: component.add(actionButton);
213:
214: component = (JComponent) Box.createVerticalGlue();
215: component.setName("GLUE");
216: component.setToolTipText(tip);
217: constraint = ServiceWizard.constrain(0, 7, 2, 1, 0.0, 1.0,
218: GridBagConstraints.CENTER, GridBagConstraints.VERTICAL);
219: panel.add(component, constraint);
220: }
221:
222: @Override
223: public void activate(WizardContext context) {
224:
225: super .activate(context);
226: JdbcService service = (JdbcService) context
227: .getAttribute(ServiceWizard.ATTRIBUTE_SERVICE);
228: if (fileListModel.isEmpty()) {
229: ConnectionProfile profile = service.getProfile();
230: Enumeration<File> files = profile.resourceElements();
231: while (files.hasMoreElements()) {
232: fileListModel.addElement(files.nextElement());
233: }
234: }
235: }
236: }
|