Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.serv / org.gvsig.tools.swing.serv.field / src / main / java / org / gvsig / tools / swing / components / file / JFileChooser.java @ 270

History | View | Annotate | Download (6.98 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
/*
23
 * AUTHORS (In addition to CIT):
24
 * 2010 Institute of New Imaging Technologies (INIT): 
25
 *   http://www.init.uji.es
26
 * Geographic Information research group: 
27
 *   http://www.geoinfo.uji.es
28
 * Universitat Jaume I, Spain
29
 */
30

    
31
/**
32
 * 
33
 */
34
package org.gvsig.tools.swing.components.file;
35

    
36
import java.io.File;
37
import java.util.ArrayList;
38
import java.util.List;
39

    
40
import javax.swing.ImageIcon;
41
import javax.swing.filechooser.FileFilter;
42

    
43
import org.gvsig.tools.dataTypes.DataTypes;
44
import org.gvsig.tools.swing.api.ComponentService;
45
import org.gvsig.tools.swing.components.DefaultResultListenersHandler;
46
import org.gvsig.tools.swing.components.ResultListener;
47
import org.gvsig.tools.swing.components.ResultProvider;
48
import org.gvsig.tools.swing.serv.field.ToolsSwingDefaultFieldImplLibrary;
49
import org.gvsig.tools.swing.serv.field.listener.AddItem;
50

    
51
/**
52
 * @author <a href="mailto:reinhold@uji.es">cmartin</a>
53
 * 
54
 */
55
public class JFileChooser implements ResultProvider, AddItem {
56

    
57
    private class JFileFilter extends FileFilter {
58

    
59
        public JFileFilter(String[] extensions) {
60
            this.seExtensiontList(extensions);
61
        }
62

    
63
        /*
64
         * (non-Javadoc)
65
         * 
66
         * @see javax.swing.filechooser.FileFilter#accept(java.io.File)
67
         */
68
        @Override
69
        public boolean accept(File f) {
70
            // int type =fc.getFileSelectionMode();
71
            //
72
            // if ((type==DIRECTORIES_ONLY)||(type==FILES_AND_DIRECTORIES))
73
            // if (f.isDirectory())
74
            // return true;
75
            if (f.isDirectory())
76
                return true;
77

    
78
            String extension = this.getExtension(f);
79
            if ((extension != null) && (!extensions.isEmpty())) {
80
                for (String ext : extensions)
81
                    if (extension.equals(ext))
82
                        return true;
83
            }
84
            return false;
85
        }
86

    
87
        /*
88
         * (non-Javadoc)
89
         * 
90
         * @see javax.swing.filechooser.FileFilter#getDescription()
91
         */
92
        @Override
93
        public String getDescription() {
94
            // TODO Auto-generated method stub
95
            return null;
96
        }
97

    
98
        /*
99
         * Get the extension of a file.
100
         */
101
        private String getExtension(File f) {
102
            String ext = null;
103
            String s = f.getName();
104
            int i = s.lastIndexOf('.');
105

    
106
            if ((i > 0) && (i < s.length() - 1)) {
107
                ext = s.substring(i + 1).toLowerCase();
108
            }
109
            return ext;
110
        }
111

    
112
        private void seExtensiontList(String[] extensions) {
113
            if (extensions == null)
114
                return;
115
            for (String extension : extensions) {
116
                JFileChooser.this.getExtensions().add(extension);
117
            }
118
        }
119
    }
120

    
121
    public static final int DIRECTORIES_ONLY =
122
        javax.swing.JFileChooser.DIRECTORIES_ONLY;
123
    public static final int FILES_AND_DIRECTORIES =
124
        javax.swing.JFileChooser.FILES_AND_DIRECTORIES;
125

    
126
    private static final String ACCEPT_BUTTON = null;
127

    
128
    /** Returns an ImageIcon, or null if the path was invalid. */
129
    protected static ImageIcon createImageIcon(String path) {
130
        java.net.URL imgURL =
131
            ToolsSwingDefaultFieldImplLibrary.class.getResource(path);
132
        if (imgURL != null) {
133
            return new ImageIcon(imgURL);
134
        } else {
135
            System.err.println("Couldn't find file: " + path);
136
            return null;
137
        }
138
    }
139

    
140
    // Create a file chooser
141
    private javax.swing.JFileChooser fc = null;
142
    private javax.swing.JComponent panel = null;
143

    
144
    protected List<String> extensions;
145

    
146
    private final DefaultResultListenersHandler listener;
147

    
148
    public JFileChooser(int type) {
149
        this(type, null);
150
    }
151

    
152
    public JFileChooser(int type, String[] extensionsFilter) {
153
        fc = new javax.swing.JFileChooser();
154
        listener = new DefaultResultListenersHandler();
155

    
156
        extensions = new ArrayList<String>();
157
        // By default FILES_ONLY.
158
        if (type == DataTypes.FOLDER)
159
            fc.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);
160
        // else
161
        // fc.setFileSelectionMode(javax.swing.JFileChooser.FILES_AND_DIRECTORIES);
162

    
163
        if (extensionsFilter != null)
164
            fc.addChoosableFileFilter(new JFileFilter(extensionsFilter));
165
    }
166

    
167
    public AddItem add(String extension) {
168
        if (extension != null)
169
            extensions.add(extension);
170
        return this;
171
    }
172

    
173
    public void addResultListener(ResultListener listener) {
174
        this.listener.add(listener);
175
    }
176

    
177
    public void alignToField(ComponentService comp) {
178
        panel = (javax.swing.JComponent) comp.asJComponent();
179
        // this.field = comp.getDynField();
180
    }
181

    
182
    /*
183
     * (non-Javadoc)
184
     * 
185
     * @see org.gvsig.tools.swing.components.ResultProvider#fireResultEvent(int,
186
     * java.lang.Object)
187
     */
188
    public void fireResultEvent(int resultVal, Object value) {
189
        listener.fireResultEvent(resultVal, value);
190
    }
191

    
192
    /**
193
     * @return
194
     */
195
    public List<String> getExtensions() {
196
        return extensions;
197
    }
198

    
199
    /**
200
     * @return
201
     */
202
    private int getReturnVal(int returnVal) {
203
        switch (returnVal) {
204
        case javax.swing.JFileChooser.APPROVE_OPTION:
205
            return RETURN_OK;
206
        case javax.swing.JFileChooser.CANCEL_OPTION:
207
            return RETURN_CANCEL;
208
        default:
209
            return RETURN_ERROR;
210
        }
211
    }
212

    
213
    /**
214
     * @return
215
     */
216
    private Object getReturnValue() {
217
        if (fc != null)
218
            return fc.getSelectedFile();
219
        return null;
220
    }
221

    
222
    public void setApproveButtonText() {
223
        fc.setApproveButtonText(ACCEPT_BUTTON);
224
    }
225

    
226
    public void showDialog() {
227
        int returnVal = fc.showOpenDialog(panel);
228
        if (listener != null) {
229
            this.fireResultEvent(this.getReturnVal(returnVal), this
230
                .getReturnValue());
231
        }
232
    }
233

    
234
}