1
+ /*
2
+ * To change this template, choose Tools | Templates
3
+ * and open the template in the editor.
4
+ */
5
+
6
+ package googletranslate ;
7
+
8
+
9
+
10
+ import java .io .File ;
11
+ import java .util .Hashtable ;
12
+ import java .util .Enumeration ;
13
+ import javax .swing .*;
14
+ import javax .swing .filechooser .*;
15
+
16
+ class ExampleFileFilter extends FileFilter {
17
+
18
+ private static String TYPE_UNKNOWN = "Type Unknown" ;
19
+ private static String HIDDEN_FILE = "Hidden File" ;
20
+
21
+ private Hashtable filters = null ;
22
+ private String description = null ;
23
+ private String fullDescription = null ;
24
+ private boolean useExtensionsInDescription = true ;
25
+
26
+ /**
27
+ * Creates a file filter. If no filters are added, then all
28
+ * files are accepted.
29
+ *
30
+ * @see #addExtension
31
+ */
32
+ public ExampleFileFilter () {
33
+ this .filters = new Hashtable ();
34
+ }
35
+
36
+ /**
37
+ * Creates a file filter that accepts files with the given extension.
38
+ * Example: new ExampleFileFilter("jpg");
39
+ *
40
+ * @see #addExtension
41
+ */
42
+ public ExampleFileFilter (String extension ) {
43
+ this (extension ,null );
44
+ }
45
+
46
+ /**
47
+ * Creates a file filter that accepts the given file type.
48
+ * Example: new ExampleFileFilter("jpg", "JPEG Image Images");
49
+ *
50
+ * Note that the "." before the extension is not needed. If
51
+ * provided, it will be ignored.
52
+ *
53
+ * @see #addExtension
54
+ */
55
+ public ExampleFileFilter (String extension , String description ) {
56
+ this ();
57
+ if (extension !=null ) addExtension (extension );
58
+ if (description !=null ) setDescription (description );
59
+ }
60
+
61
+ /**
62
+ * Creates a file filter from the given string array.
63
+ * Example: new ExampleFileFilter(String {"gif", "jpg"});
64
+ *
65
+ * Note that the "." before the extension is not needed adn
66
+ * will be ignored.
67
+ *
68
+ * @see #addExtension
69
+ */
70
+ public ExampleFileFilter (String [] filters ) {
71
+ this (filters , null );
72
+ }
73
+
74
+ /**
75
+ * Creates a file filter from the given string array and description.
76
+ * Example: new ExampleFileFilter(String {"gif", "jpg"}, "Gif and JPG Images");
77
+ *
78
+ * Note that the "." before the extension is not needed and will be ignored.
79
+ *
80
+ * @see #addExtension
81
+ */
82
+ public ExampleFileFilter (String [] filters , String description ) {
83
+ this ();
84
+ for (int i = 0 ; i < filters .length ; i ++) {
85
+ // add filters one by one
86
+ addExtension (filters [i ]);
87
+ }
88
+ if (description !=null ) setDescription (description );
89
+ }
90
+
91
+ /**
92
+ * Return true if this file should be shown in the directory pane,
93
+ * false if it shouldn't.
94
+ *
95
+ * Files that begin with "." are ignored.
96
+ *
97
+ * @see #getExtension
98
+ * @see FileFilter#accepts
99
+ */
100
+ public boolean accept (File f ) {
101
+ if (f != null ) {
102
+ if (f .isDirectory ()) {
103
+ return true ;
104
+ }
105
+ String extension = getExtension (f );
106
+ if (extension != null && filters .get (getExtension (f )) != null ) {
107
+ return true ;
108
+ };
109
+ }
110
+ return false ;
111
+ }
112
+
113
+ /**
114
+ * Return the extension portion of the file's name .
115
+ *
116
+ * @see #getExtension
117
+ * @see FileFilter#accept
118
+ */
119
+ public String getExtension (File f ) {
120
+ if (f != null ) {
121
+ String filename = f .getName ();
122
+ int i = filename .lastIndexOf ('.' );
123
+ if (i >0 && i <filename .length ()-1 ) {
124
+ return filename .substring (i +1 ).toLowerCase ();
125
+ };
126
+ }
127
+ return null ;
128
+ }
129
+
130
+ /**
131
+ * Adds a filetype "dot" extension to filter against.
132
+ *
133
+ * For example: the following code will create a filter that filters
134
+ * out all files except those that end in ".jpg" and ".tif":
135
+ *
136
+ * ExampleFileFilter filter = new ExampleFileFilter();
137
+ * filter.addExtension("jpg");
138
+ * filter.addExtension("tif");
139
+ *
140
+ * Note that the "." before the extension is not needed and will be ignored.
141
+ */
142
+ public void addExtension (String extension ) {
143
+ if (filters == null ) {
144
+ filters = new Hashtable (5 );
145
+ }
146
+ filters .put (extension .toLowerCase (), this );
147
+ fullDescription = null ;
148
+ }
149
+
150
+
151
+ /**
152
+ * Returns the human readable description of this filter. For
153
+ * example: "JPEG and GIF Image Files (*.jpg, *.gif)"
154
+ *
155
+ * @see setDescription
156
+ * @see setExtensionListInDescription
157
+ * @see isExtensionListInDescription
158
+ * @see FileFilter#getDescription
159
+ */
160
+ public String getDescription () {
161
+ if (fullDescription == null ) {
162
+ if (description == null || isExtensionListInDescription ()) {
163
+ fullDescription = description ==null ? "(" : description + " (" ;
164
+ // build the description from the extension list
165
+ Enumeration extensions = filters .keys ();
166
+ if (extensions != null ) {
167
+ fullDescription += "." + (String ) extensions .nextElement ();
168
+ while (extensions .hasMoreElements ()) {
169
+ fullDescription += ", " + (String ) extensions .nextElement ();
170
+ }
171
+ }
172
+ fullDescription += ")" ;
173
+ } else {
174
+ fullDescription = description ;
175
+ }
176
+ }
177
+ return fullDescription ;
178
+ }
179
+
180
+ /**
181
+ * Sets the human readable description of this filter. For
182
+ * example: filter.setDescription("Gif and JPG Images");
183
+ *
184
+ * @see setDescription
185
+ * @see setExtensionListInDescription
186
+ * @see isExtensionListInDescription
187
+ */
188
+ public void setDescription (String description ) {
189
+ this .description = description ;
190
+ fullDescription = null ;
191
+ }
192
+
193
+ /**
194
+ * Determines whether the extension list (.jpg, .gif, etc) should
195
+ * show up in the human readable description.
196
+ *
197
+ * Only relevent if a description was provided in the constructor
198
+ * or using setDescription();
199
+ *
200
+ * @see getDescription
201
+ * @see setDescription
202
+ * @see isExtensionListInDescription
203
+ */
204
+ public void setExtensionListInDescription (boolean b ) {
205
+ useExtensionsInDescription = b ;
206
+ fullDescription = null ;
207
+ }
208
+
209
+ /**
210
+ * Returns whether the extension list (.jpg, .gif, etc) should
211
+ * show up in the human readable description.
212
+ *
213
+ * Only relevent if a description was provided in the constructor
214
+ * or using setDescription();
215
+ *
216
+ * @see getDescription
217
+ * @see setDescription
218
+ * @see setExtensionListInDescription
219
+ */
220
+ public boolean isExtensionListInDescription () {
221
+ return useExtensionsInDescription ;
222
+ }
223
+ }
0 commit comments