001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.db.explorer.actions;
043:
044: import java.awt.Dialog;
045: import java.awt.event.ActionEvent;
046: import java.awt.event.ActionListener;
047: import java.io.IOException;
048: import java.net.URL;
049: import java.text.MessageFormat;
050: import java.util.List;
051: import org.netbeans.api.db.explorer.DatabaseException;
052:
053: import org.openide.DialogDescriptor;
054: import org.openide.DialogDisplayer;
055: import org.openide.NotifyDescriptor;
056: import org.openide.nodes.Node;
057:
058: import org.netbeans.modules.db.explorer.dlg.AddDriverDialog;
059: import org.netbeans.api.db.explorer.JDBCDriver;
060: import org.netbeans.api.db.explorer.JDBCDriverManager;
061: import org.netbeans.modules.db.explorer.infos.DriverListNodeInfo;
062: import org.netbeans.modules.db.explorer.infos.DriverNodeInfo;
063:
064: public class CustomizeDriverAction extends DatabaseAction {
065: static final long serialVersionUID = -109193000951395612L;
066:
067: private Dialog dialog;
068:
069: protected boolean enable(Node[] activatedNodes) {
070: if (activatedNodes == null || activatedNodes.length != 1)
071: return false;
072:
073: DriverNodeInfo info = (DriverNodeInfo) activatedNodes[0]
074: .getCookie(DriverNodeInfo.class);
075: if (info != null
076: && info.getURL().equals("sun.jdbc.odbc.JdbcOdbcDriver")) //NOI18N
077: return false;
078:
079: return true;
080: }
081:
082: public void performAction(Node[] activatedNodes) {
083: final Node[] n = activatedNodes;
084:
085: int drvIndex = 0;
086: final DriverNodeInfo info = (DriverNodeInfo) n[0]
087: .getCookie(DriverNodeInfo.class);
088: if (info == null)
089: return; //should not happen
090: JDBCDriver drv = info.getJDBCDriver();
091: if (drv == null) {
092: return;
093: }
094: final AddDriverDialog dlgPanel = new AddDriverDialog(drv);
095:
096: ActionListener actionListener = new ActionListener() {
097: public void actionPerformed(ActionEvent event) {
098: if (event.getSource() == DialogDescriptor.OK_OPTION) {
099: String displayName = dlgPanel.getDisplayName();
100: List drvLoc = dlgPanel.getDriverLocation();
101: String drvClass = dlgPanel.getDriverClass();
102:
103: StringBuffer err = new StringBuffer();
104: if (drvLoc.size() < 1)
105: err.append(bundle().getString(
106: "AddDriverDialog_MissingFile")); //NOI18N
107: if (drvClass == null || drvClass.equals("")) {
108: if (err.length() > 0)
109: err.append(", "); //NOI18N
110:
111: err.append(bundle().getString(
112: "AddDriverDialog_MissingClass")); //NOI18N
113: }
114: if (err.length() > 0) {
115: String message = MessageFormat
116: .format(
117: bundle()
118: .getString(
119: "AddDriverDialog_ErrorMessage"),
120: new String[] { err.toString() }); //NOI18N
121: DialogDisplayer
122: .getDefault()
123: .notify(
124: new NotifyDescriptor.Message(
125: message,
126: NotifyDescriptor.INFORMATION_MESSAGE));
127:
128: return;
129: }
130:
131: closeDialog();
132:
133: //create driver instance and save it in the XML format
134: if (displayName == null || displayName.equals(""))
135: displayName = drvClass;
136:
137: try {
138: String oldName = info.getJDBCDriver().getName();
139: info.delete();
140: JDBCDriverManager.getDefault().addDriver(
141: JDBCDriver.create(oldName, displayName,
142: drvClass, (URL[]) drvLoc
143: .toArray(new URL[drvLoc
144: .size()])));
145: } catch (IOException exc) {
146: //PENDING
147: } catch (DatabaseException exc) {
148: //PENDING
149: }
150:
151: javax.swing.SwingUtilities
152: .invokeLater(new Runnable() {
153: public void run() {
154: try {
155: DriverListNodeInfo info = (DriverListNodeInfo) n[0]
156: .getCookie(DriverListNodeInfo.class);
157: if (info != null)
158: info.refreshChildren();
159: } catch (Exception exc) {
160: // exc.printStackTrace();
161: }
162: }
163: });
164:
165: }
166: }
167: };
168:
169: DialogDescriptor descriptor = new DialogDescriptor(dlgPanel,
170: bundle().getString("AddDriverDialogTitle"), true,
171: actionListener); //NOI18N
172: Object[] closingOptions = { DialogDescriptor.CANCEL_OPTION };
173: descriptor.setClosingOptions(closingOptions);
174: dialog = DialogDisplayer.getDefault().createDialog(descriptor);
175: dialog.setVisible(true);
176: }
177:
178: private void closeDialog() {
179: if (dialog != null)
180: dialog.dispose();
181: }
182: }
|