Showing the changes to java/pkg/com/sun/tools/example/debug/gui/JDBFileFilter.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,269 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved. | |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
| 4 | + * | |
| 5 | + * This code is free software; you can redistribute it and/or modify it | |
| 6 | + * under the terms of the GNU General Public License version 2 only, as | |
| 7 | + * published by the Free Software Foundation. Oracle designates this | |
| 8 | + * particular file as subject to the "Classpath" exception as provided | |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. | |
| 10 | + * | |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT | |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that | |
| 15 | + * accompanied this code). | |
| 16 | + * | |
| 17 | + * You should have received a copy of the GNU General Public License version | |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, | |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 20 | + * | |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
| 22 | + * or visit www.oracle.com if you need additional information or have any | |
| 23 | + * questions. | |
| 24 | + */ | |
| 25 | + | |
| 26 | +package com.sun.tools.example.debug.gui; | |
| 27 | + | |
| 28 | +import java.io.File; | |
| 29 | +import java.util.Hashtable; | |
| 30 | +import java.util.Enumeration; | |
| 31 | +import javax.swing.filechooser.*; | |
| 32 | + | |
| 33 | +//### Renamed from 'ExampleFileFilter.java' provided with Swing demos. | |
| 34 | + | |
| 35 | +/** | |
| 36 | + * A convenience implementation of FileFilter that filters out | |
| 37 | + * all files except for those type extensions that it knows about. | |
| 38 | + * | |
| 39 | + * Extensions are of the type ".foo", which is typically found on | |
| 40 | + * Windows and Unix boxes, but not on Macinthosh. Case is ignored. | |
| 41 | + * | |
| 42 | + * Example - create a new filter that filerts out all files | |
| 43 | + * but gif and jpg image files: | |
| 44 | + * | |
| 45 | + * JFileChooser chooser = new JFileChooser(); | |
| 46 | + * ExampleFileFilter filter = new ExampleFileFilter( | |
| 47 | + * new String{"gif", "jpg"}, "JPEG & GIF Images") | |
| 48 | + * chooser.addChoosableFileFilter(filter); | |
| 49 | + * chooser.showOpenDialog(this); | |
| 50 | + * | |
| 51 | + * @author Jeff Dinkins | |
| 52 | + */ | |
| 53 | + | |
| 54 | +public class JDBFileFilter extends FileFilter { | |
| 55 | + | |
| 56 | + private static String TYPE_UNKNOWN = "Type Unknown"; | |
| 57 | + private static String HIDDEN_FILE = "Hidden File"; | |
| 58 | + | |
| 59 | + private Hashtable<String, JDBFileFilter> filters = null; | |
| 60 | + private String description = null; | |
| 61 | + private String fullDescription = null; | |
| 62 | + private boolean useExtensionsInDescription = true; | |
| 63 | + | |
| 64 | + /** | |
| 65 | + * Creates a file filter. If no filters are added, then all | |
| 66 | + * files are accepted. | |
| 67 | + * | |
| 68 | + * @see #addExtension | |
| 69 | + */ | |
| 70 | + public JDBFileFilter() { | |
| 71 | + this.filters = new Hashtable<String, JDBFileFilter>(); | |
| 72 | + } | |
| 73 | + | |
| 74 | + /** | |
| 75 | + * Creates a file filter that accepts files with the given extension. | |
| 76 | + * Example: new JDBFileFilter("jpg"); | |
| 77 | + * | |
| 78 | + * @see #addExtension | |
| 79 | + */ | |
| 80 | + public JDBFileFilter(String extension) { | |
| 81 | + this(extension,null); | |
| 82 | + } | |
| 83 | + | |
| 84 | + /** | |
| 85 | + * Creates a file filter that accepts the given file type. | |
| 86 | + * Example: new JDBFileFilter("jpg", "JPEG Image Images"); | |
| 87 | + * | |
| 88 | + * Note that the "." before the extension is not needed. If | |
| 89 | + * provided, it will be ignored. | |
| 90 | + * | |
| 91 | + * @see #addExtension | |
| 92 | + */ | |
| 93 | + public JDBFileFilter(String extension, String description) { | |
| 94 | + this(); | |
| 95 | + if(extension!=null) { | |
| 96 | + addExtension(extension); | |
| 97 | + } | |
| 98 | + if(description!=null) { | |
| 99 | + setDescription(description); | |
| 100 | + } | |
| 101 | + } | |
| 102 | + | |
| 103 | + /** | |
| 104 | + * Creates a file filter from the given string array. | |
| 105 | + * Example: new JDBFileFilter(String {"gif", "jpg"}); | |
| 106 | + * | |
| 107 | + * Note that the "." before the extension is not needed adn | |
| 108 | + * will be ignored. | |
| 109 | + * | |
| 110 | + * @see #addExtension | |
| 111 | + */ | |
| 112 | + public JDBFileFilter(String[] filters) { | |
| 113 | + this(filters, null); | |
| 114 | + } | |
| 115 | + | |
| 116 | + /** | |
| 117 | + * Creates a file filter from the given string array and description. | |
| 118 | + * Example: new JDBFileFilter(String {"gif", "jpg"}, "Gif and JPG Images"); | |
| 119 | + * | |
| 120 | + * Note that the "." before the extension is not needed and will be ignored. | |
| 121 | + * | |
| 122 | + * @see #addExtension | |
| 123 | + */ | |
| 124 | + public JDBFileFilter(String[] filters, String description) { | |
| 125 | + this(); | |
| 126 | + for (String filter : filters) { | |
| 127 | + // add filters one by one | |
| 128 | + addExtension(filter); | |
| 129 | + } | |
| 130 | + if(description!=null) { | |
| 131 | + setDescription(description); | |
| 132 | + } | |
| 133 | + } | |
| 134 | + | |
| 135 | + /** | |
| 136 | + * Return true if this file should be shown in the directory pane, | |
| 137 | + * false if it shouldn't. | |
| 138 | + * | |
| 139 | + * Files that begin with "." are ignored. | |
| 140 | + * | |
| 141 | + * @see #getExtension | |
| 142 | + * @see FileFilter#accepts | |
| 143 | + */ | |
| 144 | + @Override | |
| 145 | + public boolean accept(File f) { | |
| 146 | + if(f != null) { | |
| 147 | + if(f.isDirectory()) { | |
| 148 | + return true; | |
| 149 | + } | |
| 150 | + String extension = getExtension(f); | |
| 151 | + if(extension != null && filters.get(getExtension(f)) != null) { | |
| 152 | + return true; | |
| 153 | + }; | |
| 154 | + } | |
| 155 | + return false; | |
| 156 | + } | |
| 157 | + | |
| 158 | + /** | |
| 159 | + * Return the extension portion of the file's name . | |
| 160 | + * | |
| 161 | + * @see #getExtension | |
| 162 | + * @see FileFilter#accept | |
| 163 | + */ | |
| 164 | + public String getExtension(File f) { | |
| 165 | + if(f != null) { | |
| 166 | + String filename = f.getName(); | |
| 167 | + int i = filename.lastIndexOf('.'); | |
| 168 | + if(i>0 && i<filename.length()-1) { | |
| 169 | + return filename.substring(i+1).toLowerCase(); | |
| 170 | + }; | |
| 171 | + } | |
| 172 | + return null; | |
| 173 | + } | |
| 174 | + | |
| 175 | + /** | |
| 176 | + * Adds a filetype "dot" extension to filter against. | |
| 177 | + * | |
| 178 | + * For example: the following code will create a filter that filters | |
| 179 | + * out all files except those that end in ".jpg" and ".tif": | |
| 180 | + * | |
| 181 | + * JDBFileFilter filter = new JDBFileFilter(); | |
| 182 | + * filter.addExtension("jpg"); | |
| 183 | + * filter.addExtension("tif"); | |
| 184 | + * | |
| 185 | + * Note that the "." before the extension is not needed and will be ignored. | |
| 186 | + */ | |
| 187 | + public void addExtension(String extension) { | |
| 188 | + if(filters == null) { | |
| 189 | + filters = new Hashtable<String, JDBFileFilter>(5); | |
| 190 | + } | |
| 191 | + filters.put(extension.toLowerCase(), this); | |
| 192 | + fullDescription = null; | |
| 193 | + } | |
| 194 | + | |
| 195 | + | |
| 196 | + /** | |
| 197 | + * Returns the human readable description of this filter. For | |
| 198 | + * example: "JPEG and GIF Image Files (*.jpg, *.gif)" | |
| 199 | + * | |
| 200 | + * @see setDescription | |
| 201 | + * @see setExtensionListInDescription | |
| 202 | + * @see isExtensionListInDescription | |
| 203 | + * @see FileFilter#getDescription | |
| 204 | + */ | |
| 205 | + @Override | |
| 206 | + public String getDescription() { | |
| 207 | + if(fullDescription == null) { | |
| 208 | + if(description == null || isExtensionListInDescription()) { | |
| 209 | + fullDescription = description==null ? "(" : description + " ("; | |
| 210 | + // build the description from the extension list | |
| 211 | + Enumeration<String> extensions = filters.keys(); | |
| 212 | + if(extensions != null) { | |
| 213 | + fullDescription += "." + extensions.nextElement(); | |
| 214 | + while (extensions.hasMoreElements()) { | |
| 215 | + fullDescription += ", " + extensions.nextElement(); | |
| 216 | + } | |
| 217 | + } | |
| 218 | + fullDescription += ")"; | |
| 219 | + } else { | |
| 220 | + fullDescription = description; | |
| 221 | + } | |
| 222 | + } | |
| 223 | + return fullDescription; | |
| 224 | + } | |
| 225 | + | |
| 226 | + /** | |
| 227 | + * Sets the human readable description of this filter. For | |
| 228 | + * example: filter.setDescription("Gif and JPG Images"); | |
| 229 | + * | |
| 230 | + * @see setDescription | |
| 231 | + * @see setExtensionListInDescription | |
| 232 | + * @see isExtensionListInDescription | |
| 233 | + */ | |
| 234 | + public void setDescription(String description) { | |
| 235 | + this.description = description; | |
| 236 | + fullDescription = null; | |
| 237 | + } | |
| 238 | + | |
| 239 | + /** | |
| 240 | + * Determines whether the extension list (.jpg, .gif, etc) should | |
| 241 | + * show up in the human readable description. | |
| 242 | + * | |
| 243 | + * Only relevent if a description was provided in the constructor | |
| 244 | + * or using setDescription(); | |
| 245 | + * | |
| 246 | + * @see getDescription | |
| 247 | + * @see setDescription | |
| 248 | + * @see isExtensionListInDescription | |
| 249 | + */ | |
| 250 | + public void setExtensionListInDescription(boolean b) { | |
| 251 | + useExtensionsInDescription = b; | |
| 252 | + fullDescription = null; | |
| 253 | + } | |
| 254 | + | |
| 255 | + /** | |
| 256 | + * Returns whether the extension list (.jpg, .gif, etc) should | |
| 257 | + * show up in the human readable description. | |
| 258 | + * | |
| 259 | + * Only relevent if a description was provided in the constructor | |
| 260 | + * or using setDescription(); | |
| 261 | + * | |
| 262 | + * @see getDescription | |
| 263 | + * @see setDescription | |
| 264 | + * @see setExtensionListInDescription | |
| 265 | + */ | |
| 266 | + public boolean isExtensionListInDescription() { | |
| 267 | + return useExtensionsInDescription; | |
| 268 | + } | |
| 269 | +} | |