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-2007 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: * ImportEjbDataSourcesDialog.java
043: *
044: * Created on September 1, 2004, 10:42 AM
045: */
046:
047: package org.netbeans.modules.visualweb.ejb.ui;
048:
049: import org.netbeans.modules.visualweb.ejb.datamodel.EjbDataModel;
050: import org.netbeans.modules.visualweb.ejb.datamodel.EjbGroup;
051: import java.awt.Dialog;
052: import java.awt.event.ActionListener;
053: import java.io.File;
054: import java.io.IOException;
055: import java.util.*;
056: import javax.swing.JButton;
057: import org.netbeans.modules.visualweb.ejb.util.Util;
058: import org.openide.DialogDescriptor;
059: import org.openide.DialogDisplayer;
060: import org.openide.ErrorManager;
061: import org.openide.NotifyDescriptor;
062: import org.openide.util.HelpCtx;
063: import org.openide.util.NbBundle;
064:
065: import org.netbeans.modules.visualweb.extension.openide.io.RaveFileCopy;
066:
067: /**
068: * A dialog for import EJB datasources
069: *
070: * @author cao
071: */
072: public class ImportEjbDataSourcesDialog implements ActionListener {
073:
074: private Dialog dialog;
075:
076: private JButton okButton;
077: private JButton cancelButton;
078:
079: // The panel for importing EJB datasources
080: private ImportEjbDataSourcesPanel importPanel;
081:
082: public ImportEjbDataSourcesDialog() {
083:
084: importPanel = new ImportEjbDataSourcesPanel();
085:
086: DialogDescriptor dialogDescriptor = new DialogDescriptor(
087: importPanel, NbBundle.getMessage(
088: ImportEjbDataSourcesDialog.class,
089: "IMPORT_EJB_DATASOURCES"), true,
090: (ActionListener) this );
091:
092: okButton = new JButton(NbBundle.getMessage(
093: ImportEjbDataSourcesDialog.class, "OK_BUTTON_LABEL"));
094:
095: okButton.getAccessibleContext().setAccessibleDescription(
096: java.util.ResourceBundle.getBundle(
097: "org/netbeans/modules/visualweb/ejb/ui/Bundle")
098: .getString("OK_BUTTON_DESC"));
099: okButton.setMnemonic(NbBundle.getMessage(
100: ImportEjbDataSourcesDialog.class, "OK_BUTTON_MNEMONIC")
101: .charAt(0));
102: cancelButton = new JButton(NbBundle
103: .getMessage(ImportEjbDataSourcesDialog.class,
104: "CANCEL_BUTTON_LABEL"));
105: cancelButton.getAccessibleContext().setAccessibleDescription(
106: java.util.ResourceBundle.getBundle(
107: "org/netbeans/modules/visualweb/ejb/ui/Bundle")
108: .getString("CANCEL_BUTTON_DESC"));
109: cancelButton.setMnemonic(NbBundle.getMessage(
110: ImportEjbDataSourcesDialog.class,
111: "CANCEL_BUTTON_MNEMONIC").charAt(0));
112: dialogDescriptor.setOptions(new Object[] { okButton,
113: cancelButton });
114: dialogDescriptor
115: .setClosingOptions(new Object[] { cancelButton });
116:
117: // TODO: no help for preview feature
118: dialogDescriptor.setHelpCtx(new HelpCtx(
119: "projrave_ui_elements_server_nav_import_ejb_db"));
120:
121: dialog = DialogDisplayer.getDefault().createDialog(
122: dialogDescriptor);
123: dialog.setResizable(true);
124: }
125:
126: public void showDialog() {
127: // First, the user has to choose a file to import from
128:
129: ImportExportFileChooser fileChooser = new ImportExportFileChooser(
130: importPanel);
131: String selectedFile = fileChooser.getImportFile();
132: if (selectedFile != null) {
133: // First, need to check the file existence here
134: if (!(new File(selectedFile)).exists()) {
135: String msg = NbBundle.getMessage(
136: ImportEjbDataSourcesPanel.class,
137: "IMPORT_FILE_NOT_FOUND", selectedFile);
138: NotifyDescriptor d = new NotifyDescriptor.Message(msg,
139: NotifyDescriptor.ERROR_MESSAGE);
140: DialogDisplayer.getDefault().notify(d);
141: return;
142: }
143:
144: fileChooser.setCurrentFilePath(selectedFile);
145: importPanel.setImportFilePath(selectedFile);
146:
147: // Read in the data from the import file
148:
149: PortableEjbDataSource[] ejbDataSources = ImportEjbDataSourcesHelper
150: .readDataSourceImports(importPanel
151: .getImportFilePath());
152: if (ejbDataSources != null && ejbDataSources.length != 0)
153: importPanel.setEjbDataSources(ejbDataSources);
154: else {
155: String msg = NbBundle.getMessage(
156: ImportEjbDataSourcesPanel.class,
157: "NO_EJB_SET_FOR_IMPORT", selectedFile);
158: NotifyDescriptor d = new NotifyDescriptor.Message(msg,
159: NotifyDescriptor.ERROR_MESSAGE);
160: DialogDisplayer.getDefault().notify(d);
161: return;
162: }
163:
164: // Bring up the dialog with the to-be-imported EJB datasources
165: dialog.pack();
166: dialog.setVisible(true);
167: }
168: }
169:
170: public void actionPerformed(java.awt.event.ActionEvent e) {
171: if (e.getSource() == okButton) {
172: // Before anything, save the changes the user made if any
173: if (!importPanel.saveChange())
174: return;
175:
176: PortableEjbDataSource[] ejbDataSources = importPanel
177: .getEjbDataSources();
178:
179: // Added the selected groups to the EjbDataModel
180: // and copy over the client jars and wrapper jars
181:
182: boolean noneSelected = true;
183: for (int i = 0; i < ejbDataSources.length; i++) {
184: if (ejbDataSources[i].isPortable()) {
185: // Copy the client jars and wrapper jar to the user dir
186: copyJars(ejbDataSources[i].getEjbGroup());
187: EjbDataModel.getInstance().addEjbGroup(
188: ejbDataSources[i].getEjbGroup());
189:
190: noneSelected = false;
191: }
192: }
193:
194: if (noneSelected) {
195: String msg = NbBundle.getMessage(
196: ImportEjbDataSourcesDialog.class,
197: "NO_IMPORT_SELECTION");
198: NotifyDescriptor d = new NotifyDescriptor.Message(msg,
199: NotifyDescriptor.INFORMATION_MESSAGE);
200: DialogDisplayer.getDefault().notify(d);
201: return;
202: }
203:
204: dialog.dispose();
205: }
206: }
207:
208: private void copyJars(EjbGroup ejbGroup) {
209: try {
210: String ejbDir = Util.getEjbStateDir().getAbsolutePath()
211: + File.separator + "ejb-datasource"; // NOI18N
212:
213: // Client jars
214: for (Iterator iter = ejbGroup.getClientJarFiles()
215: .iterator(); iter.hasNext();) {
216: String jarPath = (String) iter.next();
217: String jarName = org.netbeans.modules.visualweb.ejb.util.Util
218: .getFileName(jarPath);
219: RaveFileCopy.fileCopy(new File(jarPath), new File(
220: ejbDir, jarName));
221: }
222:
223: // Client Wrapper bean jar
224: String wrapperJarPath = ejbGroup.getClientWrapperBeanJar();
225: String wrapperJarName = org.netbeans.modules.visualweb.ejb.util.Util
226: .getFileName(wrapperJarPath);
227: RaveFileCopy.fileCopy(new File(wrapperJarPath), new File(
228: ejbDir, wrapperJarName));
229:
230: // DesignInfo jar
231: String designInfoJarPath = ejbGroup.getDesignInfoJar();
232: String designInfoJarName = org.netbeans.modules.visualweb.ejb.util.Util
233: .getFileName(designInfoJarPath);
234: RaveFileCopy.fileCopy(new File(designInfoJarPath),
235: new File(ejbDir, designInfoJarName));
236:
237: // Fix the jar locations
238: ejbGroup.fixJarDir(ejbDir);
239:
240: } catch (IOException ioe) {
241: ErrorManager.getDefault().notify(ioe);
242: }
243:
244: }
245: }
|