Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extRasterTools-SE / src / org / gvsig / raster / util / ExtendedFileFilter.java @ 18014

History | View | Annotate | Download (5.59 KB)

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