001: /*
002: * Created on 07/03/2002
003: *
004: * Swing Components - visit http://sf.net/projects/gfd
005: *
006: * Copyright (C) 2004 Igor Regis da Silva Simões
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or (at your option) any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: *
022: */package br.com.gfpshare.util;
023:
024: import java.io.File;
025: import java.util.Enumeration;
026: import java.util.Hashtable;
027:
028: import javax.swing.JFileChooser;
029: import javax.swing.filechooser.FileFilter;
030:
031: /**
032: * Esta classe representa um filtro de arquivos, que pode ser usado em um JFileChooser para filtrar os tipos de
033: * arquivos que serão exibidos de acordo com suas extensões.
034: *
035: * @see JFileChooser
036: * @created 18/01/2004
037: * @author Igor Regis da Silva Simões
038: */
039: public class MyFileFilter extends FileFilter implements
040: java.io.FileFilter {
041:
042: private Hashtable filters = null;
043: private String description = null;
044: private String fullDescription = null;
045: private boolean useExtensionsInDescription = true;
046:
047: /**
048: * Cria um novo Filtro de Arquivos, com filtros padrões
049: */
050: public MyFileFilter() {
051: this .filters = new Hashtable();
052: }
053:
054: /**
055: * Cria um novo Filtro de Arquivos, adicionando uma nova extensão
056: * @param extension extensão a ser usada no filtro
057: */
058: public MyFileFilter(String extension) {
059: this (extension, null);
060: }
061:
062: /** Cria um novo Filtro de Arquivos, adicionando uma nova extensão e sua descrição
063: * @param extension extensão a ser usada no filtro
064: * @param description descrição para o tipo de arquivo filtrado
065: */
066: public MyFileFilter(String extension, String description) {
067: this ();
068: if (extension != null)
069: addExtension(extension);
070: if (description != null)
071: setDescription(description);
072: }
073:
074: /** Cria um novo Filtro de Arquivos, adicionando um array de extensões
075: * @param filters array de extensões a erem usadas no filtro
076: */
077: public MyFileFilter(String[] filters) {
078: this (filters, null);
079: }
080:
081: /** Cria um novo Filtro de Arquivos, adicionando um array de extensões e suas descrições
082: * @param filters array de extensões a erem usadas no filtro
083: * @param description descrição para o tipo de arquivo filtrado
084: */
085: public MyFileFilter(String[] filters, String description) {
086: this ();
087: for (int i = 0; i < filters.length; i++) {
088: // add filters one by one
089: addExtension(filters[i]);
090: }
091: if (description != null)
092: setDescription(description);
093: }
094:
095: /** Determina quais arquivos serão exibidos de acordo com usas extensões
096: * @return boolean aceitando ou não o arquivo para exibição conforme sua extensão
097: * @param f Arquivo a ser testado
098: */
099: @Override
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
107: && filters.get(getExtension(f)) != null) {
108: return true;
109: }
110: }
111: return false;
112: }
113:
114: /** Retorna a extensão de um arquivo
115: * @return A Extensão do arquivo
116: * @param f Arquivo do qual deseja-se extrair a extensão
117: */
118: public String getExtension(File f) {
119: if (f != null) {
120: String filename = f.getName();
121: int i = filename.lastIndexOf('.');
122: if (i > 0 && i < filename.length() - 1) {
123: return filename.substring(i + 1).toLowerCase();
124: }
125: }
126: return null;
127: }
128:
129: /** Adiciona uma nova extensão ao filtro
130: * @param extension Extensão a ser usada no filtro
131: */
132: public void addExtension(String extension) {
133: if (filters == null)
134: filters = new Hashtable(5);
135:
136: filters.put(extension.toLowerCase(), this );
137: fullDescription = null;
138: }
139:
140: /** Retorna a descrção
141: * @return A Decrição do filtro
142: */
143: @Override
144: public String getDescription() {
145: if (fullDescription == null) {
146: if (description == null || isExtensionListInDescription()) {
147: fullDescription = description == null ? "("
148: : description + " (";
149: // build the description from the extension list
150: Enumeration extensions = filters.keys();
151: if (extensions != null) {
152: fullDescription += "."
153: + (String) extensions.nextElement();
154: while (extensions.hasMoreElements())
155: fullDescription += ", "
156: + (String) extensions.nextElement();
157: }
158: fullDescription += ")";
159: } else
160: fullDescription = description;
161: }
162: return fullDescription;
163: }
164:
165: /** Atrtibui uma nova descrição
166: * @param description A descrição do filtro
167: */
168: public void setDescription(String description) {
169: this .description = description;
170: fullDescription = null;
171: }
172:
173: /** Determina se a descrição será exibida junta a lista de extenções
174: * @param b Determina se a descrição será exibida junta a lista de extenções
175: */
176: public void setExtensionListInDescription(boolean b) {
177: useExtensionsInDescription = b;
178: fullDescription = null;
179: }
180:
181: /** Determina se a descrição será exibida junta a lista de extenções
182: * @return boolean
183: */
184: public boolean isExtensionListInDescription() {
185: return useExtensionsInDescription;
186: }
187: }
|