Statistics
| Revision:

root / trunk / extensions / extRasterTools-SE / src / org / gvsig / raster / util / ExtendedFileFilter.java @ 16554

History | View | Annotate | Download (4.26 KB)

1
/* 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
 * comoda y rapida. Las descripciones ya las pone con un formato.
31
 *
32
 * @version 21/11/2007
33
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
34
 */
35
public class ExtendedFileFilter extends FileFilter {
36
        String description = null;
37

    
38
        ArrayList extensions = new ArrayList();
39

    
40
        /**
41
         * Constructor de un ExtendedFileFilter
42
         */
43
        public ExtendedFileFilter() {
44
        }
45

    
46
        /**
47
         * Construye un ExtendedFileFilter con una extensi?n ya agregada
48
         * @param extension
49
         */
50
        public ExtendedFileFilter(String extension) {
51
                addExtension(extension);
52
        }
53

    
54
        /**
55
         * A?ade una extensi?n a la lista de extensiones soportadas
56
         * @param extension
57
         */
58
        public void addExtension(String extension) {
59
                if (extension == null)
60
                        return;
61

    
62
                extensions.add(extension);
63
        }
64

    
65
        /*
66
         * (non-Javadoc)
67
         * @see javax.swing.filechooser.FileFilter#accept(java.io.File)
68
         */
69
        public boolean accept(File f) {
70
                if (f.isDirectory())
71
                        return true;
72

    
73
                String s = f.getName();
74
                int i = s.lastIndexOf('.');
75

    
76
                if (i > 0 && i < s.length() - 1) {
77
                        String extension = s.substring(i + 1).toLowerCase();
78
                        for (int j = 0; j < extensions.size(); j++) {
79
                                if (extensions.get(j).toString().toLowerCase().equals(extension))
80
                                        return true;
81
                        }
82
                }
83

    
84
                return false;
85
        }
86

    
87
        /**
88
         * Normaliza el nombre de un fichero, a?adiendo la extension si fuera
89
         * necesario
90
         * @param file
91
         * @return
92
         */
93
        public String getNormalizedFilename(File file) {
94
                String s = file.getName();
95
                int i = s.lastIndexOf('.');
96

    
97
                if (i > 0 && i < s.length() - 1) {
98
                        String extension = s.substring(i + 1).toLowerCase();
99
                        for (int j = 0; j < extensions.size(); j++) {
100
                                if (extensions.get(j).toString().toLowerCase().equals(extension))
101
                                        return file.toString();
102
                        }
103
                }
104

    
105
                return file.toString() + "." + extensions.get(0).toString().toLowerCase();
106
        }
107

    
108
        /*
109
         * (non-Javadoc)
110
         * @see javax.swing.filechooser.FileFilter#getDescription()
111
         */
112
        public String getDescription() {
113
                String format1 = "";
114
                String format2 = "";
115
                for (int j = 0; j < extensions.size(); j++) {
116
                        if (format1.length() != 0) {
117
                                format1 = format1 + ", ";
118
                                format2 = format2 + "; ";
119
                        }
120
                        format1 = format1 + extensions.get(j).toString().toUpperCase();
121
                        format2 = format2 + "*." + extensions.get(j).toString().toLowerCase();
122
                }
123
                if (description == null)
124
                        return PluginServices.getText(this, "files") + " " + format1 + " (" + format2 + ")";
125

    
126
                return description + " (" + format2 + ")";
127
        }
128

    
129
        /**
130
         * Especifica la descripcion del item
131
         * @param description the description to set
132
         */
133
        public void setDescription(String description) {
134
                this.description = description;
135
        }
136

    
137
        /**
138
         * Borra una extension de la lista de extensiones
139
         * @param extension
140
         */
141
        public void removeExtension(String extension){
142
                extensions.remove(extension);
143
 }
144

    
145
        /**
146
         * Borra todas las extensiones existentes
147
         */
148
        public void clearExtensions(){
149
                extensions.clear();
150
        }
151

    
152
        /**
153
         * Devuelve una lista con las extensiones disponibles
154
         * @return
155
         */
156
        public ArrayList getExtensions(){
157
                return extensions;
158
        }
159
}