Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
SuffixFilter |
|
| 2.5555555555555554;2.556 |
1 | /* $Id: SuffixFilter.java 17887 2010-01-12 21:17:18Z linus $ | |
2 | ***************************************************************************** | |
3 | * Copyright (c) 2009 Contributors - see below | |
4 | * All rights reserved. This program and the accompanying materials | |
5 | * are made available under the terms of the Eclipse Public License v1.0 | |
6 | * which accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * thn | |
11 | ***************************************************************************** | |
12 | * | |
13 | * Some portions of this file was previously release using the BSD License: | |
14 | */ | |
15 | ||
16 | // Copyright (c) 2003-2006 The Regents of the University of California. All | |
17 | // Rights Reserved. Permission to use, copy, modify, and distribute this | |
18 | // software and its documentation without fee, and without a written | |
19 | // agreement is hereby granted, provided that the above copyright notice | |
20 | // and this paragraph appear in all copies. This software program and | |
21 | // documentation are copyrighted by The Regents of the University of | |
22 | // California. The software program and documentation are supplied "AS | |
23 | // IS", without any accompanying services from The Regents. The Regents | |
24 | // does not warrant that the operation of the program will be | |
25 | // uninterrupted or error-free. The end-user understands that the program | |
26 | // was developed for research purposes and is advised not to rely | |
27 | // exclusively on the program for any reason. IN NO EVENT SHALL THE | |
28 | // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, | |
29 | // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, | |
30 | // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF | |
31 | // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF | |
32 | // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY | |
33 | // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
34 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE | |
35 | // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF | |
36 | // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, | |
37 | // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | |
38 | ||
39 | package org.argouml.util; | |
40 | ||
41 | import java.io.File; | |
42 | ||
43 | import javax.swing.filechooser.FileFilter; | |
44 | ||
45 | /** | |
46 | * This class handles file extensions. | |
47 | * | |
48 | */ | |
49 | public class SuffixFilter extends FileFilter { | |
50 | ||
51 | private final String[] suffixes; | |
52 | private final String desc; | |
53 | ||
54 | /** | |
55 | * Construct a file filter files with the given suffix and description. | |
56 | * | |
57 | * @param suffix the suffix string | |
58 | * @param d the file type description | |
59 | */ | |
60 | 900 | public SuffixFilter(String suffix, String d) { |
61 | 900 | suffixes = new String[] {suffix}; |
62 | 900 | desc = d; |
63 | 900 | } |
64 | ||
65 | /** | |
66 | * Construct a filter for an array of suffixes | |
67 | * | |
68 | * @param s the suffixes string | |
69 | * @param d the file type description | |
70 | */ | |
71 | 0 | public SuffixFilter(String[] s, String d) { |
72 | 0 | suffixes = new String[s.length]; |
73 | 0 | System.arraycopy(s, 0, suffixes, 0, s.length); |
74 | 0 | desc = d; |
75 | 0 | } |
76 | ||
77 | /* | |
78 | * @see javax.swing.filechooser.FileFilter#accept(java.io.File) | |
79 | */ | |
80 | public boolean accept(File f) { | |
81 | 575 | if (f == null) { |
82 | 0 | return false; |
83 | } | |
84 | 575 | if (f.isDirectory()) { |
85 | 375 | return true; |
86 | } | |
87 | 200 | String extension = getExtension(f); |
88 | 400 | for (String suffix : suffixes) { |
89 | 200 | if (suffix.equalsIgnoreCase(extension)) { |
90 | 0 | return true; |
91 | } | |
92 | } | |
93 | 200 | return false; |
94 | } | |
95 | ||
96 | /** | |
97 | * @param f the file to get the extension from | |
98 | * @return the extension string (without the dot) | |
99 | */ | |
100 | public static String getExtension(File f) { | |
101 | 200 | if (f == null) { |
102 | 0 | return null; |
103 | } | |
104 | 200 | return getExtension(f.getName()); |
105 | } | |
106 | ||
107 | /** | |
108 | * @param filename the name of the file to get the extension from | |
109 | * @return the extension string (without the dot) | |
110 | */ | |
111 | public static String getExtension(String filename) { | |
112 | 200 | int i = filename.lastIndexOf('.'); |
113 | 200 | if (i > 0 && i < filename.length() - 1) { |
114 | 0 | return filename.substring(i + 1).toLowerCase(); |
115 | } | |
116 | 200 | return null; |
117 | } | |
118 | ||
119 | /* | |
120 | * @see javax.swing.filechooser.FileFilter#getDescription() | |
121 | */ | |
122 | public String getDescription() { | |
123 | 3059 | StringBuffer result = new StringBuffer(desc); |
124 | 3059 | result.append(" ("); |
125 | 6118 | for (int i = 0; i < suffixes.length; i++) { |
126 | 3059 | result.append('.'); |
127 | 3059 | result.append(suffixes[i]); |
128 | 3059 | if (i < suffixes.length - 1) { |
129 | 0 | result.append(", "); |
130 | } | |
131 | } | |
132 | 3059 | result.append(')'); |
133 | 3059 | return result.toString(); |
134 | } | |
135 | ||
136 | /** | |
137 | * @return Returns the default or preferred suffix for this type of file. | |
138 | */ | |
139 | public String getSuffix() { | |
140 | 440 | return suffixes[0]; |
141 | } | |
142 | ||
143 | /** | |
144 | * @return Returns the list of all acceptable suffixes. | |
145 | */ | |
146 | public String[] getSuffixes() { | |
147 | 0 | return suffixes; |
148 | } | |
149 | ||
150 | /** | |
151 | * Adding this function enables easy selection of suffixfilters | |
152 | * e.g. in a combobox. | |
153 | * | |
154 | * {@inheritDoc} | |
155 | */ | |
156 | public String toString() { | |
157 | 1577 | return getDescription(); |
158 | } | |
159 | ||
160 | } |