~ubuntu-branches/ubuntu/wily/389-ds-console/wily-proposed

« back to all changes in this revision

Viewing changes to src/com/netscape/admin/dirserv/DSFileDialog.java

  • Committer: Package Import Robot
  • Author(s): Timo Aaltonen
  • Date: 2012-03-15 19:58:37 UTC
  • Revision ID: package-import@ubuntu.com-20120315195837-296zyft51thld8q7
Tags: upstream-1.2.6
ImportĀ upstreamĀ versionĀ 1.2.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** BEGIN COPYRIGHT BLOCK
 
2
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 
3
 * Copyright (C) 2005 Red Hat, Inc.
 
4
 * All rights reserved.
 
5
 * 
 
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.
 
9
 * 
 
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.
 
14
 * 
 
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 **/
 
19
 
 
20
package com.netscape.admin.dirserv;
 
21
 
 
22
import java.awt.*;
 
23
import java.awt.event.*;
 
24
import java.io.File;
 
25
import java.util.Vector;
 
26
import java.util.Enumeration;
 
27
import java.net.*;
 
28
import javax.swing.*;
 
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.*;
 
35
 
 
36
/**
 
37
 * DSFileDialog
 
38
 * 
 
39
 *
 
40
 * @version 1.0
 
41
 * @author rweltman
 
42
 **/
 
43
public class DSFileDialog {
 
44
    public DSFileDialog() {
 
45
    }
 
46
 
 
47
    protected static String getFilePath( String path, boolean save, int mode,
 
48
                                                                                 FileFilter[] filters,
 
49
                                                                                 Component c ) {
 
50
                /* Use JFileChooser, not AWT FileDialog */
 
51
 
 
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.
 
55
                if (_discardFirst) {
 
56
                        _discardFirst = false;
 
57
                        Object o = new JFileChooser();
 
58
                }
 
59
 
 
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] );
 
65
                        }
 
66
                }
 
67
                JFrame frame = UtilConsoleGlobals.getActivatedFrame();
 
68
                if (c == null) {
 
69
                        c = frame;
 
70
                }
 
71
                int result = save ? fileChooser.showSaveDialog( c ) :                   
 
72
                        fileChooser.showOpenDialog( c );
 
73
                String name = null;
 
74
                if ( result == fileChooser.APPROVE_OPTION ) {
 
75
                        File curFile = fileChooser.getSelectedFile();
 
76
                        if ( curFile != null ) {
 
77
                                name = curFile.getPath();// + curFile.getName();
 
78
                        }
 
79
                }
 
80
                return name;
 
81
        }
 
82
 
 
83
    protected static String getFilePath( String path, boolean save, int mode,
 
84
                                                                                 FileFilter[] filters ) {
 
85
                return getFilePath( path, save, mode, filters, null );
 
86
        }
 
87
 
 
88
    public static String getFileName(String path, boolean save,
 
89
                                                                         Component c ) {
 
90
                return getFilePath( path, save, JFileChooser.FILES_ONLY, null, c );
 
91
        }
 
92
       
 
93
    public static String getFileName(String path, boolean save ) {
 
94
                return getFileName( path, save, null );
 
95
        }
 
96
 
 
97
    public static String getFileName(String path, boolean save,
 
98
                                                                         String[] extensions,
 
99
                                                                         String[] descriptions,
 
100
                                                                         Component c) {
 
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] );
 
107
                        }
 
108
                }
 
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);
 
116
                        }
 
117
                }
 
118
                return returnFileName;          
 
119
        }
 
120
 
 
121
 
 
122
    public static String getFileName(String path, boolean save,
 
123
                                                                         String[] extensions,
 
124
                                                                         String[] descriptions ) {
 
125
                return getFileName( path, save, extensions, descriptions,
 
126
                                                        null );
 
127
        }
 
128
 
 
129
    public static String getDirectoryName(String path, boolean save,
 
130
                                                                                  Component c) {
 
131
                return getFilePath( path, save, JFileChooser.DIRECTORIES_ONLY, null,
 
132
                                                        c );
 
133
        }
 
134
       
 
135
    public static String getDirectoryName(String path, boolean save ) {
 
136
                return getDirectoryName( path, save, null );
 
137
        }
 
138
       
 
139
    public static String getFileName(String path, Component c) {
 
140
                return getFileName( path, true, c );
 
141
    }
 
142
    
 
143
    public static String getFileName(String path) {
 
144
                return getFileName( path, null );
 
145
        }
 
146
 
 
147
        public static String getFileName( boolean save,
 
148
                                                                         String[] extensions,
 
149
                                                                         String[] descriptions,
 
150
                                                                         Component c,
 
151
                                                                         String defaultPath ) {         
 
152
                return getFileName(save, extensions, descriptions, c, "*", defaultPath);
 
153
        }
 
154
 
 
155
        public static String getFileName( boolean save,
 
156
                                                                          String[] extensions,
 
157
                                                                          String[] descriptions,
 
158
                                                                          Component c ,
 
159
                                                                          String extension,
 
160
                                                                          String defaultPath) {
 
161
                if (_path == null) {
 
162
                        if (defaultPath != null) {
 
163
                                File defaultPathFile = new File(defaultPath);
 
164
                                if (defaultPathFile.exists()) {
 
165
                                        _path = defaultPath;
 
166
                                } else {
 
167
                                        _path = ".." + java.io.File.separator;
 
168
                                }
 
169
                        } else {
 
170
                                _path = ".." + java.io.File.separator;
 
171
                        }
 
172
                }
 
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);
 
181
                        }
 
182
                }
 
183
                return returnFileName;
 
184
        }
 
185
 
 
186
        public static String getPath() {
 
187
                return _path;
 
188
        }
 
189
 
 
190
    public static void main( String[] args ) {
 
191
        if (args.length < 1) {
 
192
            System.out.println("Please input filename");
 
193
            System.exit(1);
 
194
        }
 
195
        String s = getFileName(args[0], true);
 
196
                System.out.println( "Selected file: " + s );
 
197
                System.exit( 0 );
 
198
    }
 
199
        
 
200
        private static String _path;
 
201
        private static boolean _discardFirst = true;
 
202
}
 
203
 
 
204
class ExtensionFileFilter extends FileFilter {
 
205
        ExtensionFileFilter( String extension, String description ) {
 
206
                _extension = extension;
 
207
                _description = description;
 
208
        }
 
209
    public boolean accept( File f ) {
 
210
                if ( f == null ) {
 
211
                        return false;
 
212
                }
 
213
                /* Allow navigating through directories */
 
214
                if ( f.isDirectory() ) {
 
215
                        return true;
 
216
                }
 
217
 
 
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() );
 
222
                } else {
 
223
                        return f.getName().endsWith( "." + _extension );
 
224
                }
 
225
        }
 
226
 
 
227
    /**
 
228
     * The description of this filter. For example: "JPG and GIF Images"
 
229
     * @see FileView#getName
 
230
     */
 
231
    public String getDescription() {
 
232
                return _description;
 
233
        }
 
234
        private String _extension;
 
235
        private String _description;
 
236
}
 
237
 
 
238