~ubuntu-branches/debian/sid/geogebra/sid

« back to all changes in this revision

Viewing changes to geogebra/gui/app/MyFileFilter.java

  • Committer: Package Import Robot
  • Author(s): Giovanni Mascellani
  • Date: 2012-01-10 11:37:41 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20120110113741-satwohsd4de4ite1
Tags: 4.0.19.0+dfsg1-1
* New upstream version (closes: #649893).
* Update dependency: icedtea-plugin -> icedtea-netx-common (LP: #893007).
* New thumbnailer configuration compatible with Gnome 3.
* Package building is now managed by javahelper instead of upstream
  build.xml.

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
package geogebra.gui.app;
14
14
 
15
15
 
 
16
 
16
17
import java.io.File;
17
 
import java.util.Enumeration;
18
 
import java.util.Hashtable;
 
18
import java.util.ArrayList;
19
19
import java.util.Locale;
20
20
 
21
21
import javax.swing.filechooser.FileFilter;
27
27
 * Extensions are of the type ".foo", which is typically found on
28
28
 * Windows and Unix boxes, but not on Macinthosh. Case is ignored.
29
29
 *
30
 
 * Example - create a new filter that filerts out all files
 
30
 * Example - create a new filter that filters out all files
31
31
 * but gif and jpg image files:
32
32
 *
33
33
 *     JFileChooser chooser = new JFileChooser();
37
37
 *     chooser.showOpenDialog(this);
38
38
 * 
39
39
 */
40
 
public class MyFileFilter extends FileFilter {
 
40
public class MyFileFilter extends FileFilter implements java.io.FileFilter {
41
41
 
42
42
  //  private static String TYPE_UNKNOWN = "Type Unknown";
43
43
  //  private static String HIDDEN_FILE = "Hidden File";
44
44
 
45
 
    private Hashtable filters = null;
 
45
        // changed to ArrayList as we need an ordered list (want .ggb first)
 
46
        // Michael Borcherds 2010-03-04
 
47
    private ArrayList<String> filters = null; 
 
48
    
46
49
    private String description = null;
47
50
    private String fullDescription = null;
48
51
    private boolean useExtensionsInDescription = true;
54
57
     * @see #addExtension
55
58
     */
56
59
    public MyFileFilter() {
57
 
        filters = new Hashtable();
 
60
        filters = new ArrayList<String>();
58
61
    }
59
62
 
60
63
    /**
61
64
     * Creates a file filter that accepts files with the given extension.
62
65
     * Example: new MyFileFilter("jpg");
63
 
     *
 
66
     * @param extension either "ext" or ".ext" 
64
67
     * @see #addExtension
65
68
     */
66
69
    public MyFileFilter(String extension) {
67
 
        this(extension,null);
 
70
        this(extension,null);
68
71
    }
69
72
 
70
73
    /**
73
76
     *
74
77
     * Note that the "." before the extension is not needed. If
75
78
     * provided, it will be ignored.
 
79
     * @param extension either "ext" or ".ext"
 
80
     * @param description 
76
81
     *
77
82
     * @see #addExtension
78
83
     */
79
84
    public MyFileFilter(String extension, String description) {
80
 
        this();
81
 
        if(extension!=null) addExtension(extension);
82
 
        if(description!=null) setDescription(description);
 
85
                this();
 
86
                if(extension!=null) addExtension(extension);
 
87
                if(description!=null) setDescription(description);
83
88
    }
84
89
 
85
90
    /**
88
93
     *
89
94
     * Note that the "." before the extension is not needed adn
90
95
     * will be ignored.
 
96
     * @param filters array of either "ext" or ".ext" strings
91
97
     *
92
98
     * @see #addExtension
93
99
     */
94
100
    public MyFileFilter(String[] filters) {
95
 
        this(filters, null);
 
101
        this(filters, null);
96
102
    }
97
103
 
98
104
    /**
100
106
     * Example: new MyFileFilter(String {"gif", "jpg"}, "Gif and JPG Images");
101
107
     *
102
108
     * Note that the "." before the extension is not needed and will be ignored.
 
109
     * @param filters array of either "ext" or ".ext" strings
 
110
     * @param description 
103
111
     *
104
112
     * @see #addExtension
105
113
     */
106
114
    public MyFileFilter(String[] filters, String description) {
107
 
        this();
108
 
        for (int i = 0; i < filters.length; i++) {
109
 
            // add filters one by one
110
 
            addExtension(filters[i]);
111
 
        }
112
 
        if(description!=null) setDescription(description);
 
115
                this();
 
116
                for (int i = 0; i < filters.length; i++) {
 
117
                    // add filters one by one
 
118
                    addExtension(filters[i]);
 
119
                }
 
120
                if(description!=null) setDescription(description);
113
121
    }
114
122
 
115
123
    /**
119
127
     * Files that begin with "." are ignored.
120
128
     *
121
129
     * @see #getExtension
122
 
     * @see FileFilter#accepts
 
130
     * @see FileFilter#accept
123
131
     */
124
132
    public boolean accept(File f) {
125
 
        if(f != null) {
126
 
            if(f.isDirectory())
127
 
                        return true;
128
 
            String extension = getExtension(f);
129
 
            if(extension != null && filters.get(getExtension(f)) != null)
130
 
                        return true;;
131
 
        }
132
 
        return false;
 
133
                if(f != null) {
 
134
                    if(f.isDirectory())
 
135
                                return true;
 
136
                    String extension = getExtension(f);
 
137
                    if(extension != null && filters.contains(getExtension(f)))
 
138
                                return true;
 
139
                }
 
140
                return false;
133
141
    }
134
142
 
135
143
    /**
136
144
     * Return the extension portion of the file's name .
 
145
     * @param f 
 
146
     * @return "ext" for file "filename.ext"
137
147
     *
138
148
     * @see #getExtension
139
149
     * @see FileFilter#accept
140
150
     */
141
151
     public String getExtension(File f) {
142
 
        if(f != null) {
143
 
            String filename = f.getName();
144
 
            int i = filename.lastIndexOf('.');
145
 
            if(i>0 && i<filename.length()-1)
146
 
                        return filename.substring(i+1).toLowerCase(Locale.US);
147
 
        }
148
 
        return null;
 
152
                if(f != null) {
 
153
                    String filename = f.getName();
 
154
                    int i = filename.lastIndexOf('.');
 
155
                    if(i>0 && i<filename.length()-1)
 
156
                        // Modified for Intergeo File Format (Yves Kreis) -->
 
157
        //                      return filename.substring(i+1).toLowerCase(Locale.US);;
 
158
                                return filename.substring(i+1).toLowerCase(Locale.US);
 
159
                                // <-- Modified for Intergeo File Format (Yves Kreis)
 
160
                }
 
161
                return null;
149
162
    }
150
163
 
151
164
    /**
159
172
     *   filter.addExtension("tif");
160
173
     *
161
174
     * Note that the "." before the extension is not needed and will be ignored.
 
175
     * @param extension either ".ext" or "ext"
162
176
     */
163
177
    public void addExtension(String extension) {
164
 
        if(filters == null) {
165
 
            filters = new Hashtable(5);
166
 
        }
167
 
        if (extension.indexOf(".") > -1) {
168
 
                extension = extension.substring(0, extension.lastIndexOf("."));
169
 
        }
170
 
        filters.put(extension.toLowerCase(Locale.US), this);
171
 
        fullDescription = null;
 
178
                if(filters == null) {
 
179
                    filters = new ArrayList<String>(5);
 
180
                }
 
181
                // Added for Intergeo File Format (Yves Kreis) -->
 
182
                if (extension.indexOf(".") > -1) {
 
183
                        extension = extension.substring(0, extension.lastIndexOf("."));
 
184
                }
 
185
                // <-- Added for Intergeo File Format (Yves Kreis)
 
186
                filters.add(extension.toLowerCase(Locale.US));
 
187
                fullDescription = null;
172
188
    }
173
189
 
174
190
    public String toString() {
179
195
     * Returns the human readable description of this filter. For
180
196
     * example: "JPEG and GIF Image Files (*.jpg, *.gif)"
181
197
     *
182
 
     * @see setDescription
183
 
     * @see setExtensionListInDescription
184
 
     * @see isExtensionListInDescription
 
198
     * @see #setDescription
 
199
     * @see #setExtensionListInDescription
 
200
     * @see #isExtensionListInDescription
185
201
     * @see FileFilter#getDescription
186
202
     */
187
203
    public String getDescription() {
188
 
        if(fullDescription == null) {
189
 
            if(description == null || isExtensionListInDescription()) {
190
 
                fullDescription = description==null ? "(" : description + " (";
191
 
                // build the description from the extension list
192
 
                Enumeration extensions = filters.keys();
193
 
                if(extensions != null) {
194
 
                    fullDescription += "." + (String) extensions.nextElement();
195
 
                    while (extensions.hasMoreElements()) {
196
 
                        fullDescription += ", ." + (String) extensions.nextElement();
 
204
                if(fullDescription == null) {
 
205
                    if(description == null || isExtensionListInDescription()) {
 
206
                        fullDescription = description==null ? "(" : description + " (";
 
207
                        // build the description from the extension list
 
208
                        
 
209
                        if (filters.size() > 0) fullDescription += "." + (String) filters.get(0);
 
210
                        
 
211
                                for (int i = 1 ; i < filters.size() ; i++) {
 
212
                                        fullDescription += ", ." + (String) (String) filters.get(i);
 
213
                                }
 
214
                
 
215
                                fullDescription += ")";
 
216
                    } else {
 
217
                        fullDescription = description;
197
218
                    }
198
219
                }
199
 
                fullDescription += ")";
200
 
            } else {
201
 
                fullDescription = description;
202
 
            }
203
 
        }
204
 
        return fullDescription;
 
220
                return fullDescription;
205
221
    }
206
222
 
207
223
    /**
208
224
     * Sets the human readable description of this filter. For
209
225
     * example: filter.setDescription("Gif and JPG Images");
210
 
     *
211
 
     * @see setDescription
212
 
     * @see setExtensionListInDescription
213
 
     * @see isExtensionListInDescription
 
226
     * @param description
 
227
     * @see #setDescription
 
228
     * @see #setExtensionListInDescription
 
229
     * @see #isExtensionListInDescription
214
230
     */
215
231
    public void setDescription(String description) {
216
 
        this.description = description;
217
 
        fullDescription = null;
 
232
        this.description = description;
 
233
                fullDescription = null;
218
234
    }
219
235
 
220
236
    /**
223
239
     *
224
240
     * Only relevent if a description was provided in the constructor
225
241
     * or using setDescription();
 
242
     * @param b true to show the list in description
226
243
     *
227
 
     * @see getDescription
228
 
     * @see setDescription
229
 
     * @see isExtensionListInDescription
 
244
     * @see #getDescription
 
245
     * @see #setDescription
 
246
     * @see #isExtensionListInDescription
230
247
     */
231
248
    public void setExtensionListInDescription(boolean b) {
232
 
        useExtensionsInDescription = b;
233
 
        fullDescription = null;
 
249
        useExtensionsInDescription = b;
 
250
        fullDescription = null;
234
251
    }
235
252
 
236
253
    /**
239
256
     *
240
257
     * Only relevent if a description was provided in the constructor
241
258
     * or using setDescription();
 
259
     * @return true iff showing the list in description
242
260
     *
243
 
     * @see getDescription
244
 
     * @see setDescription
245
 
     * @see setExtensionListInDescription
 
261
     * @see #getDescription
 
262
     * @see #setDescription
 
263
     * @see #setExtensionListInDescription
246
264
     */
247
265
    public boolean isExtensionListInDescription() {
248
 
        return useExtensionsInDescription;
 
266
        return useExtensionsInDescription;
249
267
    }
250
268
    
251
269
    /**
252
270
     * Returns the first extension contained in the extension list.
 
271
     * 
 
272
     * Added for Intergeo File Format (Yves Kreis)
 
273
     * @return first extension (without ".")
253
274
     */
254
275
    public String getExtension() {
255
 
        Enumeration keys = filters.keys();
256
 
        if (keys.hasMoreElements()) {
257
 
                return (String) keys.nextElement();
258
 
        } else {
259
 
                return "";
260
 
        }
 
276
        return (String)filters.get(0);
261
277
    }
262
278
}
263
279