svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / gui / FileOpenDialog.java @ 13068
History | View | Annotate | Download (12.9 KB)
1 |
/*
|
---|---|
2 |
* Created on 23-abr-2004
|
3 |
*
|
4 |
* To change the template for this generated file go to
|
5 |
* Window>Preferences>Java>Code Generation>Code and Comments
|
6 |
*/
|
7 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
8 |
*
|
9 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
10 |
*
|
11 |
* This program is free software; you can redistribute it and/or
|
12 |
* modify it under the terms of the GNU General Public License
|
13 |
* as published by the Free Software Foundation; either version 2
|
14 |
* of the License, or (at your option) any later version.
|
15 |
*
|
16 |
* This program is distributed in the hope that it will be useful,
|
17 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
19 |
* GNU General Public License for more details.
|
20 |
*
|
21 |
* You should have received a copy of the GNU General Public License
|
22 |
* along with this program; if not, write to the Free Software
|
23 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
24 |
*
|
25 |
* For more information, contact:
|
26 |
*
|
27 |
* Generalitat Valenciana
|
28 |
* Conselleria d'Infraestructures i Transport
|
29 |
* Av. Blasco Ib??ez, 50
|
30 |
* 46010 VALENCIA
|
31 |
* SPAIN
|
32 |
*
|
33 |
* +34 963862235
|
34 |
* gvsig@gva.es
|
35 |
* www.gvsig.gva.es
|
36 |
*
|
37 |
* or
|
38 |
*
|
39 |
* IVER T.I. S.A
|
40 |
* Salamanca 50
|
41 |
* 46005 Valencia
|
42 |
* Spain
|
43 |
*
|
44 |
* +34 963163400
|
45 |
* dac@iver.es
|
46 |
*/
|
47 |
package com.iver.cit.gvsig.project.documents.gui; |
48 |
|
49 |
import java.awt.BorderLayout; |
50 |
import java.io.File; |
51 |
import java.util.ArrayList; |
52 |
import java.util.Comparator; |
53 |
import java.util.Iterator; |
54 |
import java.util.TreeSet; |
55 |
import java.util.prefs.Preferences; |
56 |
|
57 |
import javax.swing.DefaultListModel; |
58 |
import javax.swing.JFileChooser; |
59 |
import javax.swing.JPanel; |
60 |
import javax.swing.filechooser.FileFilter; |
61 |
|
62 |
import org.cresques.cts.IProjection; |
63 |
|
64 |
import com.hardcode.driverManager.Driver; |
65 |
import com.hardcode.driverManager.DriverLoadException; |
66 |
import com.hardcode.gdbms.engine.data.driver.FileDriver; |
67 |
import com.iver.andami.PluginServices; |
68 |
import com.iver.andami.messages.NotificationManager; |
69 |
import com.iver.cit.gvsig.fmap.crs.CRSFactory; |
70 |
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver; |
71 |
import com.iver.cit.gvsig.fmap.layers.LayerFactory; |
72 |
import com.iver.cit.gvsig.gui.panels.CRSSelectPanel; |
73 |
import com.iver.utiles.listManager.ListManagerListener; |
74 |
/**
|
75 |
* Di?logo para cargar las dieferentes capas a la vista.
|
76 |
*
|
77 |
* @author Vicente Caballero Navarro
|
78 |
*/
|
79 |
public class FileOpenDialog extends JPanel{ |
80 |
// TODO LWS ?los m_Files ... ?tendr?an que ser GeoFiles (Projected y dem?s)?
|
81 |
private static String lastPath = null; |
82 |
private static DriverFileFilter lastFileFilter = null; |
83 |
private ArrayList m_Files; |
84 |
private javax.swing.JPanel jPanel2 = null; |
85 |
private DefaultListModel m_lstModel; |
86 |
private JFileChooser fileChooser; |
87 |
private Class[] driverClasses; |
88 |
private String title = PluginServices.getText(this,"Capas"); |
89 |
|
90 |
private CRSSelectPanel jPanelProj = null; |
91 |
private ListManagerSkin listManagerDemoSkin = null; |
92 |
private boolean proj; |
93 |
|
94 |
public FileOpenDialog(Class[] driverClasses) { |
95 |
init(driverClasses, true);
|
96 |
} |
97 |
|
98 |
/**
|
99 |
* @param driverClasses2
|
100 |
* @param b
|
101 |
*/
|
102 |
private void init(Class[] driverClasses, boolean proj) { |
103 |
this.proj = proj;
|
104 |
|
105 |
initialize(); |
106 |
if (lastPath == null) { |
107 |
Preferences prefs = Preferences.userRoot().node( "gvsig.foldering" ); |
108 |
lastPath = prefs.get("DataFolder", null); |
109 |
} |
110 |
m_Files = new ArrayList(); |
111 |
|
112 |
this.driverClasses = driverClasses;
|
113 |
|
114 |
// Cada vez que se abre, se borran las capas que pudiera haber.
|
115 |
m_lstModel = new DefaultListModel(); |
116 |
} |
117 |
|
118 |
/**
|
119 |
* Creates a new FOpenDialog object.
|
120 |
*
|
121 |
* @param view Vista que vamos a refrescar
|
122 |
* @param mapControl MapControl que recibe la capa (te puede interesar
|
123 |
* a?adirla al principal o al Overview.
|
124 |
*/
|
125 |
public FileOpenDialog(Class[] driverClasses, boolean proj) { |
126 |
init(driverClasses, proj); |
127 |
} |
128 |
|
129 |
public FileOpenDialog(Class[] driverClasses, boolean proj, String title) { |
130 |
this.title= title;
|
131 |
init(driverClasses, proj); |
132 |
} |
133 |
|
134 |
/**
|
135 |
* This method initializes this
|
136 |
*/
|
137 |
private void initialize() { |
138 |
this.setLayout(new BorderLayout()); |
139 |
this.setSize(514, 280); |
140 |
this.setLayout(null); |
141 |
this.add(getJPanel2(), null); |
142 |
} |
143 |
|
144 |
/**
|
145 |
* DOCUMENT ME!
|
146 |
*
|
147 |
* @return DOCUMENT ME!
|
148 |
*/
|
149 |
public File[] getFiles() { |
150 |
MyFile[] files = (MyFile[]) getListManagerDemoSkin().getListManager().getListModel().getObjects().toArray(new MyFile[0]); |
151 |
File[] ret = new File[files.length]; |
152 |
int pos=files.length-1; |
153 |
for (int i=0; i < files.length; i++){ |
154 |
ret[pos] = files[i].f; |
155 |
pos--; |
156 |
} |
157 |
return ret;
|
158 |
} |
159 |
public boolean accept(File f){ |
160 |
return fileChooser.getFileFilter().accept(f);
|
161 |
} |
162 |
/**
|
163 |
* This method initializes jPanel2
|
164 |
*
|
165 |
* @return javax.swing.JPanel
|
166 |
*/
|
167 |
private javax.swing.JPanel getJPanel2() {
|
168 |
if (jPanel2 == null) { |
169 |
javax.swing.border.TitledBorder titledBorder1 = javax.swing.BorderFactory.createTitledBorder(null,
|
170 |
PluginServices.getText(this,"Seleccionar_fichero"), |
171 |
javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, |
172 |
javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null); |
173 |
jPanel2 = new javax.swing.JPanel();
|
174 |
jPanel2.setLayout(null);
|
175 |
jPanel2.setBorder(titledBorder1); |
176 |
jPanel2.setPreferredSize(new java.awt.Dimension(380,200)); |
177 |
jPanel2.setBounds(2, 2, 506, 472); |
178 |
titledBorder1.setTitle(this.title);
|
179 |
if (proj) {
|
180 |
jPanel2.add(getJPanelProj(), null);
|
181 |
} |
182 |
jPanel2.add(getListManagerDemoSkin(), null);
|
183 |
} |
184 |
|
185 |
return jPanel2;
|
186 |
} |
187 |
|
188 |
public String[] getDriverNames(){ |
189 |
MyFile[] files = (MyFile[]) getListManagerDemoSkin().getListManager().getListModel().getObjects().toArray(new MyFile[0]); |
190 |
String[] ret = new String[files.length]; |
191 |
int pos=files.length-1; |
192 |
|
193 |
for (int i=0; i < files.length; i++) |
194 |
{ |
195 |
ret[pos] = files[i].getDriverName(); |
196 |
pos--; |
197 |
} |
198 |
return ret;
|
199 |
|
200 |
// return ((DriverFileFilter) fileChooser.getFileFilter()).getDescription();
|
201 |
} |
202 |
|
203 |
public class DriverFileFilter extends FileFilter{ |
204 |
|
205 |
private Driver driver; |
206 |
|
207 |
public DriverFileFilter(String driverName) throws DriverLoadException{ |
208 |
driver = LayerFactory.getDM().getDriver(driverName); |
209 |
} |
210 |
|
211 |
/**
|
212 |
* @see javax.swing.filechooser.FileFilter#accept(java.io.File)
|
213 |
*/
|
214 |
public boolean accept(File f) { |
215 |
if (f.isDirectory()) return true; |
216 |
if (driver instanceof VectorialFileDriver){ |
217 |
return ((VectorialFileDriver) driver).accept(f);
|
218 |
}else if (driver instanceof FileDriver){ |
219 |
return ((FileDriver) driver).fileAccepted(f);
|
220 |
// }else if (driver instanceof RasterDriver){
|
221 |
// return ((RasterDriver) driver).fileAccepted(f);
|
222 |
}else{
|
223 |
throw new RuntimeException("Tipo no reconocido"); |
224 |
} |
225 |
} |
226 |
|
227 |
/**
|
228 |
* @see javax.swing.filechooser.FileFilter#getDescription()
|
229 |
*/
|
230 |
public String getDescription() { |
231 |
return ((Driver) driver).getName(); |
232 |
} |
233 |
} |
234 |
|
235 |
public void setDriverClasses(Class[] driverClasses) { |
236 |
this.driverClasses = driverClasses;
|
237 |
} |
238 |
/**
|
239 |
* This method initializes jPanel
|
240 |
*
|
241 |
* @return javax.swing.JPanel
|
242 |
*/
|
243 |
private CRSSelectPanel getJPanelProj() {
|
244 |
if (jPanelProj == null) { |
245 |
IProjection proj = CRSFactory.getCRS("EPSG:23030");
|
246 |
if (PluginServices.getMainFrame() != null) { |
247 |
proj = FOpenDialog.getLastProjection(); |
248 |
} |
249 |
|
250 |
jPanelProj = CRSSelectPanel.getPanel(proj); |
251 |
jPanelProj.setTransPanelActive(true);
|
252 |
jPanelProj.setBounds(11, 400, 448, 35); |
253 |
jPanelProj.setPreferredSize(new java.awt.Dimension(448,35)); |
254 |
jPanelProj.addActionListener(new java.awt.event.ActionListener() {
|
255 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
256 |
if (jPanelProj.isOkPressed()) {
|
257 |
FOpenDialog.setLastProjection(jPanelProj.getCurProj()); |
258 |
} |
259 |
} |
260 |
}); |
261 |
|
262 |
} |
263 |
return jPanelProj;
|
264 |
} |
265 |
|
266 |
public class MyFile { |
267 |
private File f; |
268 |
private String driverName; |
269 |
|
270 |
public MyFile(File f, String driverName){ |
271 |
this.f = f;
|
272 |
this.driverName = driverName;
|
273 |
} |
274 |
|
275 |
public String toString(){ |
276 |
return f.getName();
|
277 |
} |
278 |
public String getDriverName(){ |
279 |
return driverName;
|
280 |
} |
281 |
|
282 |
} |
283 |
|
284 |
/**
|
285 |
* This method initializes listManagerDemoSkin
|
286 |
*
|
287 |
* @return com.iver.utiles.listManager.ListManagerDemoSkin
|
288 |
*/
|
289 |
private ListManagerSkin getListManagerDemoSkin() {
|
290 |
if (listManagerDemoSkin == null) { |
291 |
listManagerDemoSkin = new ListManagerSkin(false); |
292 |
listManagerDemoSkin.setBounds(11, 21, 491, 363); |
293 |
listManagerDemoSkin.getListManager().setListener(new ListManagerListener() {
|
294 |
public Object[] addObjects() { |
295 |
TreeSet filters = new TreeSet(new Comparator() { |
296 |
public int compare(Object o1, Object o2) { |
297 |
DriverFileFilter dff1 = (DriverFileFilter) o1; |
298 |
DriverFileFilter dff2 = (DriverFileFilter) o2; |
299 |
|
300 |
return dff1.driver.getName().compareTo(dff2.driver.getName());
|
301 |
} |
302 |
}); |
303 |
fileChooser = new JFileChooser(lastPath); |
304 |
fileChooser.setMultiSelectionEnabled(true);
|
305 |
fileChooser.setAcceptAllFileFilterUsed(false);
|
306 |
|
307 |
try {
|
308 |
boolean last = false; |
309 |
String[] driverNames = LayerFactory.getDM().getDriverNames(); |
310 |
DriverFileFilter defaultFileFilter = null;
|
311 |
DriverFileFilter auxF; |
312 |
for (int i = 0; i < driverNames.length; i++) { |
313 |
System.err.println("DRIVER "+i+" : "+driverNames[i]); |
314 |
boolean is = false; |
315 |
for (int j = 0; j < driverClasses.length; j++) { |
316 |
if (i == 0) System.err.println("DRIVER CLASS "+j+" : "+driverClasses[j].toString()); |
317 |
if (LayerFactory.getDM().isA(driverNames[i], driverClasses[j]))
|
318 |
is = true;
|
319 |
} |
320 |
if (is){
|
321 |
auxF = new DriverFileFilter(driverNames[i]);
|
322 |
filters.add(auxF); |
323 |
if (driverNames[i].indexOf("shp") != -1) { |
324 |
defaultFileFilter = auxF; |
325 |
} |
326 |
if (lastFileFilter != null) { |
327 |
if (driverNames[i].equals(lastFileFilter.driver.getName())){
|
328 |
last = true;
|
329 |
} |
330 |
} |
331 |
} |
332 |
} |
333 |
DriverFileFilter def = (DriverFileFilter) fileChooser.getFileFilter(); |
334 |
if (last) {
|
335 |
def = lastFileFilter; |
336 |
} else {
|
337 |
if (lastFileFilter != null){ |
338 |
if (filters.contains(lastFileFilter)) {
|
339 |
def = defaultFileFilter; |
340 |
} |
341 |
} |
342 |
} |
343 |
Iterator i = filters.iterator();
|
344 |
while (i.hasNext()) {
|
345 |
DriverFileFilter element = (DriverFileFilter) i.next(); |
346 |
if (def != null){ |
347 |
if (!element.driver.getName().equals(def.driver.getName())){
|
348 |
fileChooser.addChoosableFileFilter(element); |
349 |
} |
350 |
} else {
|
351 |
fileChooser.addChoosableFileFilter(element); |
352 |
} |
353 |
} |
354 |
if (def != null){ |
355 |
fileChooser.setFileFilter(def); |
356 |
} |
357 |
|
358 |
} catch (DriverLoadException e1) {
|
359 |
NotificationManager.addError("No se pudo acceder a los drivers", e1);
|
360 |
} |
361 |
|
362 |
int result = fileChooser.showOpenDialog(FileOpenDialog.this);
|
363 |
|
364 |
File[] newFiles = null; |
365 |
MyFile[] toAdd;
|
366 |
if (result == JFileChooser.APPROVE_OPTION) { |
367 |
lastFileFilter = (DriverFileFilter) fileChooser.getFileFilter(); |
368 |
lastPath = fileChooser.getCurrentDirectory().getAbsolutePath(); |
369 |
newFiles = fileChooser.getSelectedFiles(); |
370 |
toAdd = new MyFile[newFiles.length];
|
371 |
for (int ind=0; ind < newFiles.length; ind++) |
372 |
{ |
373 |
String driverName = ((DriverFileFilter) fileChooser.getFileFilter()).getDescription();
|
374 |
toAdd[ind] = new MyFile(newFiles[ind], driverName);
|
375 |
} |
376 |
|
377 |
|
378 |
return toAdd;
|
379 |
} |
380 |
else
|
381 |
return new Object[0]; |
382 |
} |
383 |
|
384 |
public Object getProperties(Object selected) { |
385 |
return null; |
386 |
} |
387 |
}); |
388 |
} |
389 |
return listManagerDemoSkin;
|
390 |
} |
391 |
} // @jve:decl-index=0:visual-constraint="10,10"
|
392 |
// @jve:visual-info decl-index=0 visual-constraint="10,10"
|
393 |
// @jve:visual-info decl-index=0 visual-constraint="10,10"
|