1
/** BEGIN COPYRIGHT BLOCK
2
* Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
3
* Copyright (C) 2005 Red Hat, Inc.
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
* END COPYRIGHT BLOCK **/
20
package com.netscape.admin.dirserv;
23
import java.awt.event.*;
25
import java.util.Vector;
26
import java.util.Enumeration;
29
import javax.swing.plaf.FileChooserUI;
30
import javax.swing.plaf.ComponentUI;
31
import javax.swing.filechooser.*;
32
import com.netscape.management.client.util.*;
33
import netscape.ldap.*;
34
import netscape.ldap.util.*;
43
public class DSFileDialog {
44
public DSFileDialog() {
47
protected static String getFilePath( String path, boolean save, int mode,
50
/* Use JFileChooser, not AWT FileDialog */
52
// With JDK1.4 on Unix the first instance of JFileChooser
53
// has an incorrect layout for some of the buttons. Discard
54
// the first instance.
56
_discardFirst = false;
57
Object o = new JFileChooser();
60
JFileChooser fileChooser = new JFileChooser( path );
61
fileChooser.setFileSelectionMode( mode );
62
if ( (filters != null) && (filters.length > 0) ) {
63
for( int i = 0; i < filters.length; i++ ) {
64
fileChooser.addChoosableFileFilter( filters[i] );
67
JFrame frame = UtilConsoleGlobals.getActivatedFrame();
71
int result = save ? fileChooser.showSaveDialog( c ) :
72
fileChooser.showOpenDialog( c );
74
if ( result == fileChooser.APPROVE_OPTION ) {
75
File curFile = fileChooser.getSelectedFile();
76
if ( curFile != null ) {
77
name = curFile.getPath();// + curFile.getName();
83
protected static String getFilePath( String path, boolean save, int mode,
84
FileFilter[] filters ) {
85
return getFilePath( path, save, mode, filters, null );
88
public static String getFileName(String path, boolean save,
90
return getFilePath( path, save, JFileChooser.FILES_ONLY, null, c );
93
public static String getFileName(String path, boolean save ) {
94
return getFileName( path, save, null );
97
public static String getFileName(String path, boolean save,
99
String[] descriptions,
101
FileFilter[] filters = null;
102
if ( (extensions != null) && (descriptions != null) &&
103
(extensions.length == descriptions.length) ) {
104
filters = new FileFilter[extensions.length];
105
for( int i = 0; i < extensions.length; i++ ) {
106
filters[i] = new ExtensionFileFilter( extensions[i], descriptions[i] );
109
String returnFileName = getFilePath( path, save, JFileChooser.FILES_ONLY, filters, c );
110
if (returnFileName != null) {
111
File file = new File(returnFileName);
112
String parent = file.getParent();
113
if (parent != null) {
114
_path = parent + java.io.File.separator;
115
Debug.println("DSFileDialog.getFilePath new path "+_path);
118
return returnFileName;
122
public static String getFileName(String path, boolean save,
124
String[] descriptions ) {
125
return getFileName( path, save, extensions, descriptions,
129
public static String getDirectoryName(String path, boolean save,
131
return getFilePath( path, save, JFileChooser.DIRECTORIES_ONLY, null,
135
public static String getDirectoryName(String path, boolean save ) {
136
return getDirectoryName( path, save, null );
139
public static String getFileName(String path, Component c) {
140
return getFileName( path, true, c );
143
public static String getFileName(String path) {
144
return getFileName( path, null );
147
public static String getFileName( boolean save,
149
String[] descriptions,
151
String defaultPath ) {
152
return getFileName(save, extensions, descriptions, c, "*", defaultPath);
155
public static String getFileName( boolean save,
157
String[] descriptions,
160
String defaultPath) {
162
if (defaultPath != null) {
163
File defaultPathFile = new File(defaultPath);
164
if (defaultPathFile.exists()) {
167
_path = ".." + java.io.File.separator;
170
_path = ".." + java.io.File.separator;
173
String path = _path +extension;
174
String returnFileName = getFileName(path, save, extensions, descriptions, c);
175
if (returnFileName != null) {
176
File file = new File(returnFileName);
177
String parent = file.getParent();
178
if (parent != null) {
179
_path = parent+java.io.File.separator;
180
Debug.println("DSFileDialog.getFilePath new path "+_path);
183
return returnFileName;
186
public static String getPath() {
190
public static void main( String[] args ) {
191
if (args.length < 1) {
192
System.out.println("Please input filename");
195
String s = getFileName(args[0], true);
196
System.out.println( "Selected file: " + s );
200
private static String _path;
201
private static boolean _discardFirst = true;
204
class ExtensionFileFilter extends FileFilter {
205
ExtensionFileFilter( String extension, String description ) {
206
_extension = extension;
207
_description = description;
209
public boolean accept( File f ) {
213
/* Allow navigating through directories */
214
if ( f.isDirectory() ) {
218
/* Case-insensitive on Windows; how about Mac? Does "." indicate Mac? */
219
/* Note: this applies only for files on the local file system */
220
if ( File.separator.equals("\\") || File.separator.equals(".") ) {
221
return f.getName().toLowerCase().endsWith( "." + _extension.toLowerCase() );
223
return f.getName().endsWith( "." + _extension );
228
* The description of this filter. For example: "JPG and GIF Images"
229
* @see FileView#getName
231
public String getDescription() {
234
private String _extension;
235
private String _description;