KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > openfile > FileChooser


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 2004 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.openfile;
21
22 import java.awt.GridLayout JavaDoc;
23 import java.io.File JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26 import javax.swing.JFileChooser JavaDoc;
27 import javax.swing.JLabel JavaDoc;
28 import javax.swing.JPanel JavaDoc;
29 import javax.swing.filechooser.FileFilter JavaDoc;
30 import org.openide.DialogDisplayer;
31 import org.openide.NotifyDescriptor;
32 import org.openide.filesystems.FileUtil;
33 import org.openide.util.HelpCtx;
34 import org.openide.util.NbBundle;
35
36 /**
37  *
38  * @author Jesse Glick
39  * @author Marian Petras
40  */

41 class FileChooser extends JFileChooser JavaDoc {
42
43     /** Creates a new instance of FileChooser */
44     FileChooser() {
45         setFileSelectionMode(JFileChooser.FILES_ONLY);
46         setMultiSelectionEnabled(true);
47         
48         /* initialize file filters */
49         FileFilter JavaDoc currentFilter = getFileFilter();
50         addChoosableFileFilter(new Filter JavaDoc(
51             new String JavaDoc[] {DefaultOpenFileImpl.JAVA_EXT},
52             NbBundle.getMessage(FileChooser.class, "TXT_JavaFilter"))); //NOI18N
53
addChoosableFileFilter(new Filter JavaDoc(
54             new String JavaDoc[] {DefaultOpenFileImpl.TXT_EXT},
55             NbBundle.getMessage(FileChooser.class, "TXT_TxtFilter"))); //NOI18N
56
setFileFilter(currentFilter);
57     }
58     
59     public void approveSelection() {
60         final File JavaDoc[] selectedFiles = getSelectedFiles();
61
62         /* check the files: */
63         List JavaDoc/*<String>*/ errorMsgs = null;
64         for (int i = 0; i < selectedFiles.length; i++) {
65             String JavaDoc msgPatternRef = null;
66             boolean isUNCPath = false;
67             File JavaDoc file = selectedFiles[i];
68
69             if (!file.exists()) {
70                 msgPatternRef = "MSG_FileDoesNotExist"; //NOI18N
71
} else if (OpenFile.isSpecifiedByUNCPath(file)) {
72                 msgPatternRef = "MSG_FileSpecifiedByUNC"; //NOI18N
73
isUNCPath = true;
74             } else if (file.isDirectory()) {
75                 msgPatternRef = "MSG_FileIsADirectory"; //NOI18N
76
} else if (!file.isFile()) {
77                 msgPatternRef = "MSG_FileIsNotPlainFile"; //NOI18N
78
}
79             if (msgPatternRef == null) {
80                 continue;
81             }
82
83             if (errorMsgs == null) {
84                 errorMsgs = new ArrayList JavaDoc(selectedFiles.length - i);
85             }
86             errorMsgs.add(NbBundle.getMessage(FileChooser.class,
87                                               msgPatternRef,
88                                               isUNCPath
89                                                   ? getUNCPathToDisplay(file)
90                                                   : file.getName()));
91         }
92         if (errorMsgs == null) {
93             super.approveSelection();
94         } else {
95             JPanel JavaDoc panel = new JPanel JavaDoc(new GridLayout JavaDoc(errorMsgs.size(), 0,
96                                                      0, 2)); //gaps
97
for (java.util.Iterator JavaDoc i = errorMsgs.iterator(); i.hasNext(); ) {
98                 panel.add(new JLabel JavaDoc((String JavaDoc) i.next()));
99             }
100             DialogDisplayer.getDefault().notify(
101                     new NotifyDescriptor.Message(
102                             panel, NotifyDescriptor.WARNING_MESSAGE));
103         }
104     }
105
106     /**
107      * Creates a file name to be displayed in an error message.
108      * If the file's full path is short, it is returned withouth any change.
109      * Otherwise part of the path is replaced with an ellipsis (...).
110      *
111      * @param file file whose name is to be displayed
112      * @return path of the file
113      */

114     private static String JavaDoc getUNCPathToDisplay(File JavaDoc file) {
115         String JavaDoc name = file.getName();
116         String JavaDoc path = file.getPath();
117         String JavaDoc pathToCheck = path.substring(2,
118                                             path.length() - name.length() - 1);
119
120         final char sep = File.separatorChar;
121         int slashIndex = pathToCheck.indexOf(sep);
122         if (slashIndex != -1) {
123             slashIndex = pathToCheck.indexOf(sep, slashIndex + 1);
124             if ((slashIndex != -1) && (pathToCheck.indexOf(sep, slashIndex + 1)
125                                        != -1)) {
126
127                 /* too many separators in the path - shorten with an ellipsis */
128                 return new StringBuffer JavaDoc(slashIndex + name.length() + 7)
129                        .append(path.substring(0, 2 + slashIndex))
130                        .append(sep)
131                        .append("...") //NOI18N
132
.append(sep)
133                        .append(name)
134                        .toString();
135             }
136         }
137         return path;
138     }
139         
140     /** File chooser filter that filters files by their names' suffixes. */
141     private static class Filter extends FileFilter JavaDoc {
142         
143         /** suffixes accepted by this filter */
144         private String JavaDoc[] extensions;
145         
146         /** localized description of this filter */
147         private String JavaDoc description;
148         
149         
150         /**
151          * Creates a new filter that accepts files having specified suffixes.
152          * The filter is case-insensitive.
153          * <p>
154          * The filter does not use file <em>extensions</em> but it just
155          * tests whether the file name ends with the specified string.
156          * So it is recommended to pass a file name extension including the
157          * preceding dot rather than just the extension.
158          *
159          * @param extensions list of accepted suffixes
160          * @param description name of the filter
161          */

162         public Filter(String JavaDoc[] extensions, String JavaDoc description) {
163             
164             this.extensions = new String JavaDoc[extensions.length];
165             for (int i = 0; i < extensions.length; i++) {
166                 this.extensions[i] = extensions[i].toUpperCase();
167             }
168             this.description = description;
169         }
170         
171         
172         /**
173          * @return <code>true</code> if the file's name ends with one of the
174          * strings specified by the constructor or if the file
175          * is a directory, <code>false</code> otherwise
176          */

177         public boolean accept(File JavaDoc file) {
178             if (file.isDirectory()) {
179                 return true;
180             }
181             for (int i = 0; i < extensions.length; i++) {
182                 if (file.getName().toUpperCase().endsWith(extensions[i])) {
183                     return true;
184                 }
185             }
186             
187             return false;
188         }
189         
190         /** */
191         public String JavaDoc getDescription() {
192             return description;
193         }
194     } // End of Filter class.
195

196 }
197
Popular Tags