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: * ImportExportFileChooser.java
043: *
044: * Created on September 1, 2004, 2:37 PM
045: */
046:
047: package org.netbeans.modules.visualweb.ejb.ui;
048:
049: import java.awt.Component;
050: import java.io.File;
051: import javax.swing.JFileChooser;
052: import javax.swing.filechooser.FileFilter;
053: import org.openide.util.NbBundle;
054:
055: /**
056: * A file chooser for choosing a file to import from or export to
057: *
058: * @author cao
059: */
060: public class ImportExportFileChooser {
061:
062: public static String defaultFilePath = System
063: .getProperty("user.home")
064: + File.separator + "exported_ejb_datasources.jar";
065:
066: private JFileChooser fileChooser = org.netbeans.modules.visualweb.extension.openide.awt.JFileChooser_RAVE
067: .getJFileChooser();
068:
069: private Component parent;
070:
071: public ImportExportFileChooser(Component parent) {
072: this .parent = parent;
073:
074: // Set current dir or default dir
075: File curDir = null;
076: File curSelection = new File(defaultFilePath);
077: if (curSelection.exists()) {
078: if (curSelection.isDirectory()) {
079: curDir = curSelection;
080: } else {
081: curDir = curSelection.getParentFile();
082: }
083: }
084:
085: if (curDir == null)
086: curDir = new File(System.getProperty("user.home")); //NOI18N
087:
088: if (curSelection != null && curSelection.exists())
089: fileChooser.setSelectedFile(curSelection);
090:
091: fileChooser.setCurrentDirectory(curDir);
092: fileChooser.addChoosableFileFilter(new JarFilter());
093: }
094:
095: public static void setCurrentFilePath(String path) {
096: defaultFilePath = path;
097: }
098:
099: // Returns the selected file
100: public String getExportFile() {
101: if (fileChooser.showSaveDialog(parent) == JFileChooser.APPROVE_OPTION) {
102: String selectedFile = fileChooser.getSelectedFile()
103: .getAbsolutePath();
104:
105: return selectedFile;
106: } else
107: return null;
108: }
109:
110: // Returns the selected file
111: public String getImportFile() {
112: if (fileChooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) {
113: String selectedFile = fileChooser.getSelectedFile()
114: .getAbsolutePath();
115:
116: return selectedFile;
117: } else
118: return null;
119: }
120:
121: public class JarFilter extends FileFilter {
122:
123: //Accept all directories and all ".jar" files.
124: public boolean accept(File f) {
125: if (f.isDirectory()) {
126: return true;
127: }
128: String extension = null;
129: String s = f.getName();
130: int i = s.lastIndexOf('.');
131:
132: if (i > 0 && i < s.length() - 1) {
133: extension = s.substring(i + 1).toLowerCase();
134: }
135:
136: if (extension != null) {
137: if (extension.equals("jar")) { //NOI18N
138: return true;
139: } else {
140: return false;
141: }
142: }
143: return false;
144: }
145:
146: //The description of this filter
147: public String getDescription() {
148: return NbBundle.getMessage(ExportEjbDataSourcesPanel.class,
149: "JAR_FILE_FILTER_DESCRIPTION");
150: }
151: }
152: }
|