001: /*
002: * $Id: JGraphpadFileFilter.java,v 1.2 2005/10/09 12:00:04 gaudenz Exp $
003: * Copyright (c) 2001-2005, Gaudenz Alder
004: *
005: * All rights reserved.
006: *
007: * See LICENSE file for license details. If you are unable to locate
008: * this file please contact info (at) jgraph (dot) com.
009: */
010: package com.jgraph.pad.util;
011:
012: import java.io.File;
013:
014: import javax.imageio.ImageIO;
015: import javax.swing.JFileChooser;
016:
017: /**
018: * Filter for use in a {@link JFileChooser}.
019: */
020: public class JGraphpadFileFilter extends
021: javax.swing.filechooser.FileFilter {
022:
023: /**
024: * Extension of accepted files.
025: */
026: protected String ext;
027:
028: /**
029: * Description of accepted files.
030: */
031: protected String desc;
032:
033: /**
034: * Constructs a new filter for the specified extension and descpription.
035: *
036: * @param extension
037: * The extension to accept files with.
038: * @param description
039: * The description of the file format.
040: */
041: public JGraphpadFileFilter(String extension, String description) {
042: ext = extension.toLowerCase();
043: desc = description;
044: }
045:
046: /**
047: * Returns true if <code>file</code> is a directory or ends with
048: * {@link #ext}.
049: *
050: * @param file
051: * The file to be checked.
052: * @return Returns true if the file is accepted.
053: */
054: public boolean accept(File file) {
055: return file.isDirectory()
056: || file.getName().toLowerCase().endsWith(ext);
057: }
058:
059: /**
060: * Returns the description for accepted files.
061: *
062: * @return Returns the description.
063: */
064: public String getDescription() {
065: return desc;
066: }
067:
068: /**
069: * Returns the extension for accepted files.
070: *
071: * @return Returns the extension.
072: */
073: public String getExtension() {
074: return ext;
075: }
076:
077: /**
078: * Sets the extension for accepted files.
079: *
080: * @param extension
081: * The extension to set.
082: */
083: public void setExtension(String extension) {
084: this .ext = extension;
085: }
086:
087: /**
088: * Utility file filter to accept all image formats supported by image io.
089: *
090: * @see ImageIO#getReaderFormatNames()
091: */
092: public static class ImageFileFilter extends
093: javax.swing.filechooser.FileFilter {
094:
095: /**
096: * Holds the accepted file format extensions for images.
097: */
098: protected static String[] imageFormats = ImageIO
099: .getReaderFormatNames();
100:
101: /**
102: * Description of the filter.
103: */
104: protected String desc;
105:
106: /**
107: * Constructs a new file filter for all supported image formats using
108: * the specified description.
109: *
110: * @param description
111: * The description to use for the file filter.
112: */
113: public ImageFileFilter(String description) {
114: desc = description;
115: }
116:
117: /**
118: * Returns true if the file is a directory or ends with a known image
119: * extension.
120: *
121: * @param file
122: * The file to be checked.
123: * @return Returns true if the file is accepted.
124: */
125: public boolean accept(File file) {
126: if (file.isDirectory())
127: return true;
128: String filename = file.toString().toLowerCase();
129: for (int j = 0; j < imageFormats.length; j++)
130: if (filename.endsWith("."
131: + imageFormats[j].toLowerCase()))
132: return true;
133: return false;
134: }
135:
136: /**
137: * Returns the description.
138: *
139: * @return Returns the description.
140: */
141: public String getDescription() {
142: return desc;
143: }
144:
145: }
146:
147: /**
148: * Utility file filter to accept editor files, namely .xml and .xml.gz
149: * extensions.
150: *
151: * @see ImageIO#getReaderFormatNames()
152: */
153: public static class EditorFileFilter extends
154: javax.swing.filechooser.FileFilter {
155:
156: /**
157: * Description of the File format
158: */
159: protected String desc;
160:
161: /**
162: * Constructs a new editor file filter using the specified description.
163: *
164: * @param description
165: * The description to use for the filter.
166: */
167: public EditorFileFilter(String description) {
168: desc = description;
169: }
170:
171: /**
172: * Returns true if the file is a directory or has a .xml or .xml.gz
173: * extension.
174: *
175: * @return Returns true if the file is accepted.
176: */
177: public boolean accept(File file) {
178: if (file.isDirectory())
179: return true;
180: String filename = file.getName().toLowerCase();
181: return filename.endsWith(".xml")
182: || filename.endsWith(".xml.gz");
183: }
184:
185: /**
186: * Returns the description.
187: *
188: * @return Returns the description.
189: */
190: public String getDescription() {
191: return desc;
192: }
193:
194: }
195: }
|