gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.fmap / src / main / java / org / gvsig / raster / util / ExtendedFileFilter.java @ 463
History | View | Annotate | Download (5.56 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
package org.gvsig.raster.util; |
23 |
|
24 |
import java.io.File; |
25 |
import java.util.ArrayList; |
26 |
|
27 |
import javax.swing.filechooser.FileFilter; |
28 |
|
29 |
|
30 |
/**
|
31 |
* ExtendedFileFilter es una clase para usarla junto a los JFileChooser.
|
32 |
* Ofrece una funcionalidad simple para poder agregar extensiones de manera
|
33 |
* comoda y rapida. Las descripciones ya las pone con un formato, asi que esto
|
34 |
* es opcional poner una descripci?n especifica.
|
35 |
*
|
36 |
* Un ejemplo t?pico de como se usaria:
|
37 |
* <pre>
|
38 |
* // Usamos el JFileChooser de libUIComponents
|
39 |
* JFileChooser chooser = new JFileChooser(this.getClass().toString(), (File) null);
|
40 |
* // Desactivamos el modo de ver todos los ficheros
|
41 |
* chooser.setAcceptAllFileFilterUsed(false);
|
42 |
* // Activamos la multiseleccion
|
43 |
* chooser.setMultiSelectionEnabled(true);
|
44 |
* // Nos guardamos cada tipo de fichero en uno que contenga todos
|
45 |
* ExtendedFileFilter allFilters = new ExtendedFileFilter();
|
46 |
* for (int i = 0; i < formats.length; i++) {
|
47 |
* ExtendedFileFilter fileFilter = new ExtendedFileFilter();
|
48 |
* fileFilter.addExtension(formats[i]);
|
49 |
* // Agregamos el filefilter al JFileChooser
|
50 |
* chooser.addChoosableFileFilter(fileFilter);
|
51 |
* // Agregamos el mismo filtro a un ExtendedFileFilter global
|
52 |
* allFilters.addExtension(formats[i]);
|
53 |
* }
|
54 |
* // Poner una descripcion (OPCIONAL) para todos los ficheros.
|
55 |
* allFilters.setDescription(PluginServices.getText(this, "todos_soportados"));
|
56 |
* // Lo a?adimos
|
57 |
* chooser.addChoosableFileFilter(allFilters);
|
58 |
* // Y lo dejamos seleccionado por defecto
|
59 |
* chooser.setFileFilter(allFilters);
|
60 |
* </pre>
|
61 |
*
|
62 |
* @version 21/11/2007
|
63 |
* @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
|
64 |
*/
|
65 |
public class ExtendedFileFilter extends FileFilter { |
66 |
String description = null; |
67 |
|
68 |
ArrayList extensions = new ArrayList(); |
69 |
|
70 |
/**
|
71 |
* Constructor de un ExtendedFileFilter
|
72 |
*/
|
73 |
public ExtendedFileFilter() {
|
74 |
} |
75 |
|
76 |
/**
|
77 |
* Construye un ExtendedFileFilter con una extensi?n ya agregada
|
78 |
* @param extension
|
79 |
*/
|
80 |
public ExtendedFileFilter(String extension) { |
81 |
addExtension(extension); |
82 |
} |
83 |
|
84 |
/**
|
85 |
* A?ade una extensi?n a la lista de extensiones soportadas
|
86 |
* @param extension
|
87 |
*/
|
88 |
public void addExtension(String extension) { |
89 |
if (extension == null) |
90 |
return;
|
91 |
|
92 |
extensions.add(extension); |
93 |
} |
94 |
|
95 |
/*
|
96 |
* (non-Javadoc)
|
97 |
* @see javax.swing.filechooser.FileFilter#accept(java.io.File)
|
98 |
*/
|
99 |
public boolean accept(File f) { |
100 |
if (f.isDirectory())
|
101 |
return true; |
102 |
|
103 |
String s = f.getName();
|
104 |
int i = s.lastIndexOf('.'); |
105 |
|
106 |
if (i > 0 && i < s.length() - 1) { |
107 |
String extension = s.substring(i + 1).toLowerCase(); |
108 |
for (int j = 0; j < extensions.size(); j++) { |
109 |
if (extensions.get(j).toString().toLowerCase().equals(extension))
|
110 |
return true; |
111 |
} |
112 |
} |
113 |
|
114 |
return false; |
115 |
} |
116 |
|
117 |
/**
|
118 |
* Normaliza el nombre de un fichero, a?adiendo la extension si fuera
|
119 |
* necesario
|
120 |
* @param file
|
121 |
* @return
|
122 |
*/
|
123 |
public String getNormalizedFilename(File file) { |
124 |
String s = file.getName();
|
125 |
int i = s.lastIndexOf('.'); |
126 |
|
127 |
if (i > 0 && i < s.length() - 1) { |
128 |
String extension = s.substring(i + 1).toLowerCase(); |
129 |
for (int j = 0; j < extensions.size(); j++) { |
130 |
if (extensions.get(j).toString().toLowerCase().equals(extension))
|
131 |
return file.toString();
|
132 |
} |
133 |
} |
134 |
|
135 |
return file.toString() + "." + extensions.get(0).toString().toLowerCase(); |
136 |
} |
137 |
|
138 |
/*
|
139 |
* (non-Javadoc)
|
140 |
* @see javax.swing.filechooser.FileFilter#getDescription()
|
141 |
*/
|
142 |
public String getDescription() { |
143 |
String format1 = ""; |
144 |
String format2 = ""; |
145 |
for (int j = 0; j < extensions.size(); j++) { |
146 |
if (format1.length() != 0) { |
147 |
format1 = format1 + ", ";
|
148 |
format2 = format2 + "; ";
|
149 |
} |
150 |
// Files JPG, GIF, ... (*.jpg; *.gif ...)
|
151 |
if (j >= 4) { |
152 |
format1 = "...";
|
153 |
format2 = "...";
|
154 |
break;
|
155 |
} |
156 |
format1 = format1 + extensions.get(j).toString().toUpperCase(); |
157 |
format2 = format2 + "*." + extensions.get(j).toString().toLowerCase();
|
158 |
} |
159 |
if (description == null) |
160 |
return "files" + " " + format1 + " (" + format2 + ")"; |
161 |
|
162 |
return description + " (" + format2 + ")"; |
163 |
} |
164 |
|
165 |
/**
|
166 |
* Especifica la descripcion del item
|
167 |
* @param description the description to set
|
168 |
*/
|
169 |
public void setDescription(String description) { |
170 |
this.description = description;
|
171 |
} |
172 |
|
173 |
/**
|
174 |
* Borra una extension de la lista de extensiones
|
175 |
* @param extension
|
176 |
*/
|
177 |
public void removeExtension(String extension){ |
178 |
extensions.remove(extension); |
179 |
} |
180 |
|
181 |
/**
|
182 |
* Borra todas las extensiones existentes
|
183 |
*/
|
184 |
public void clearExtensions(){ |
185 |
extensions.clear(); |
186 |
} |
187 |
|
188 |
/**
|
189 |
* Devuelve una lista con las extensiones disponibles
|
190 |
* @return
|
191 |
*/
|
192 |
public ArrayList getExtensions(){ |
193 |
return extensions;
|
194 |
} |
195 |
} |