svn-gvsig-desktop / branches / v10 / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / gui / FileOpenDialog.java @ 9056
History | View | Annotate | Download (13.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.RasterDriver; |
71 |
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver; |
72 |
import com.iver.cit.gvsig.fmap.drivers.raster.CmsRasterDriver; |
73 |
import com.iver.cit.gvsig.fmap.layers.LayerFactory; |
74 |
import com.iver.cit.gvsig.gui.panels.CRSSelectPanel; |
75 |
import com.iver.utiles.listManager.ListManagerListener; |
76 |
/**
|
77 |
* Di?logo para cargar las dieferentes capas a la vista.
|
78 |
*
|
79 |
* @author Vicente Caballero Navarro
|
80 |
*/
|
81 |
public class FileOpenDialog extends JPanel{ |
82 |
// TODO LWS ?los m_Files ... ?tendr?an que ser GeoFiles (Projected y dem?s)?
|
83 |
private static String lastPath = null; |
84 |
private static DriverFileFilter lastFileFilter = null; |
85 |
private ArrayList m_Files; |
86 |
private javax.swing.JPanel jPanel2 = null; |
87 |
private DefaultListModel m_lstModel; |
88 |
private JFileChooser fileChooser; |
89 |
private Class[] driverClasses; |
90 |
private String title = PluginServices.getText(this,"Capas"); |
91 |
|
92 |
private CRSSelectPanel jPanelProj = null; |
93 |
private ListManagerSkin listManagerDemoSkin = null; |
94 |
private boolean proj; |
95 |
|
96 |
public FileOpenDialog(Class[] driverClasses) { |
97 |
init(driverClasses, true);
|
98 |
} |
99 |
|
100 |
/**
|
101 |
* @param driverClasses2
|
102 |
* @param b
|
103 |
*/
|
104 |
private void init(Class[] driverClasses, boolean proj) { |
105 |
this.proj = proj;
|
106 |
|
107 |
initialize(); |
108 |
if (lastPath == null) { |
109 |
Preferences prefs = Preferences.userRoot().node( "gvsig.foldering" ); |
110 |
lastPath = prefs.get("DataFolder", null); |
111 |
} |
112 |
m_Files = new ArrayList(); |
113 |
|
114 |
this.driverClasses = driverClasses;
|
115 |
|
116 |
// Cada vez que se abre, se borran las capas que pudiera haber.
|
117 |
m_lstModel = new DefaultListModel(); |
118 |
} |
119 |
|
120 |
/**
|
121 |
* Creates a new FOpenDialog object.
|
122 |
*
|
123 |
* @param view Vista que vamos a refrescar
|
124 |
* @param mapControl MapControl que recibe la capa (te puede interesar
|
125 |
* a?adirla al principal o al Overview.
|
126 |
*/
|
127 |
public FileOpenDialog(Class[] driverClasses, boolean proj) { |
128 |
init(driverClasses, proj); |
129 |
} |
130 |
|
131 |
public FileOpenDialog(Class[] driverClasses, boolean proj, String title) { |
132 |
this.title= title;
|
133 |
init(driverClasses, proj); |
134 |
} |
135 |
|
136 |
/**
|
137 |
* This method initializes this
|
138 |
*/
|
139 |
private void initialize() { |
140 |
this.setLayout(new BorderLayout()); |
141 |
this.setSize(514, 280); |
142 |
this.setLayout(null); |
143 |
this.add(getJPanel2(), null); |
144 |
} |
145 |
|
146 |
/**
|
147 |
* DOCUMENT ME!
|
148 |
*
|
149 |
* @return DOCUMENT ME!
|
150 |
*/
|
151 |
public File[] getFiles() { |
152 |
MyFile[] files = (MyFile[]) getListManagerDemoSkin().getListManager().getListModel().getObjects().toArray(new MyFile[0]); |
153 |
File[] ret = new File[files.length]; |
154 |
int pos=files.length-1; |
155 |
for (int i=0; i < files.length; i++){ |
156 |
ret[pos] = files[i].f; |
157 |
pos--; |
158 |
} |
159 |
return ret;
|
160 |
} |
161 |
public boolean accept(File f){ |
162 |
return fileChooser.getFileFilter().accept(f);
|
163 |
} |
164 |
/**
|
165 |
* This method initializes jPanel2
|
166 |
*
|
167 |
* @return javax.swing.JPanel
|
168 |
*/
|
169 |
private javax.swing.JPanel getJPanel2() {
|
170 |
if (jPanel2 == null) { |
171 |
javax.swing.border.TitledBorder titledBorder1 = javax.swing.BorderFactory.createTitledBorder(null,
|
172 |
PluginServices.getText(this,"Seleccionar_fichero"), |
173 |
javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, |
174 |
javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null); |
175 |
jPanel2 = new javax.swing.JPanel();
|
176 |
jPanel2.setLayout(null);
|
177 |
jPanel2.setBorder(titledBorder1); |
178 |
jPanel2.setPreferredSize(new java.awt.Dimension(380,200)); |
179 |
jPanel2.setBounds(2, 2, 506, 472); |
180 |
titledBorder1.setTitle(this.title);
|
181 |
if (proj) {
|
182 |
jPanel2.add(getJPanelProj(), null);
|
183 |
} |
184 |
jPanel2.add(getListManagerDemoSkin(), null);
|
185 |
} |
186 |
|
187 |
return jPanel2;
|
188 |
} |
189 |
|
190 |
public String[] getDriverNames(){ |
191 |
MyFile[] files = (MyFile[]) getListManagerDemoSkin().getListManager().getListModel().getObjects().toArray(new MyFile[0]); |
192 |
String[] ret = new String[files.length]; |
193 |
int pos=files.length-1; |
194 |
|
195 |
for (int i=0; i < files.length; i++) |
196 |
{ |
197 |
ret[pos] = files[i].getDriverName(); |
198 |
pos--; |
199 |
} |
200 |
return ret;
|
201 |
|
202 |
// return ((DriverFileFilter) fileChooser.getFileFilter()).getDescription();
|
203 |
} |
204 |
|
205 |
public class DriverFileFilter extends FileFilter{ |
206 |
|
207 |
private Driver driver; |
208 |
|
209 |
public DriverFileFilter(String driverName) throws DriverLoadException{ |
210 |
driver = LayerFactory.getDM().getDriver(driverName); |
211 |
} |
212 |
|
213 |
/**
|
214 |
* @see javax.swing.filechooser.FileFilter#accept(java.io.File)
|
215 |
*/
|
216 |
public boolean accept(File f) { |
217 |
if (f.isDirectory()) return true; |
218 |
if (driver instanceof VectorialFileDriver){ |
219 |
return ((VectorialFileDriver) driver).accept(f);
|
220 |
}else if (driver instanceof FileDriver){ |
221 |
return ((FileDriver) driver).fileAccepted(f);
|
222 |
}else if (driver instanceof RasterDriver){ |
223 |
return ((RasterDriver) driver).fileAccepted(f);
|
224 |
}else{
|
225 |
throw new RuntimeException("Tipo no reconocido"); |
226 |
} |
227 |
} |
228 |
|
229 |
/**
|
230 |
* @see javax.swing.filechooser.FileFilter#getDescription()
|
231 |
*/
|
232 |
public String getDescription() { |
233 |
return ((Driver) driver).getName(); |
234 |
} |
235 |
|
236 |
/**
|
237 |
* Sometimes it is necessary to create a file
|
238 |
* pre-procces to load a file. This method realizes
|
239 |
* it and return the new file name.
|
240 |
* @return
|
241 |
* The new file to load
|
242 |
*/
|
243 |
public File getTransformedFile(File file){ |
244 |
if (driver instanceof CmsRasterDriver){ |
245 |
return ((CmsRasterDriver)driver).getTransformedFile(file);
|
246 |
} |
247 |
return null; |
248 |
} |
249 |
} |
250 |
|
251 |
public void setDriverClasses(Class[] driverClasses) { |
252 |
this.driverClasses = driverClasses;
|
253 |
} |
254 |
/**
|
255 |
* This method initializes jPanel
|
256 |
*
|
257 |
* @return javax.swing.JPanel
|
258 |
*/
|
259 |
private CRSSelectPanel getJPanelProj() {
|
260 |
if (jPanelProj == null) { |
261 |
IProjection proj = CRSFactory.getCRS("EPSG:23030");
|
262 |
if (PluginServices.getMainFrame() != null) { |
263 |
proj = FOpenDialog.getLastProjection(); |
264 |
} |
265 |
|
266 |
jPanelProj = CRSSelectPanel.getPanel(proj); |
267 |
jPanelProj.setTransPanelActive(true);
|
268 |
jPanelProj.setBounds(11, 400, 448, 35); |
269 |
jPanelProj.setPreferredSize(new java.awt.Dimension(448,35)); |
270 |
jPanelProj.addActionListener(new java.awt.event.ActionListener() {
|
271 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
272 |
if (jPanelProj.isOkPressed()) {
|
273 |
FOpenDialog.setLastProjection(jPanelProj.getCurProj()); |
274 |
} |
275 |
} |
276 |
}); |
277 |
|
278 |
} |
279 |
return jPanelProj;
|
280 |
} |
281 |
|
282 |
public class MyFile { |
283 |
private File f; |
284 |
private String driverName; |
285 |
|
286 |
public MyFile(File f, String driverName){ |
287 |
this.f = f;
|
288 |
this.driverName = driverName;
|
289 |
} |
290 |
|
291 |
public String toString(){ |
292 |
return f.getName();
|
293 |
} |
294 |
public String getDriverName(){ |
295 |
return driverName;
|
296 |
} |
297 |
|
298 |
} |
299 |
|
300 |
/**
|
301 |
* This method initializes listManagerDemoSkin
|
302 |
*
|
303 |
* @return com.iver.utiles.listManager.ListManagerDemoSkin
|
304 |
*/
|
305 |
private ListManagerSkin getListManagerDemoSkin() {
|
306 |
if (listManagerDemoSkin == null) { |
307 |
listManagerDemoSkin = new ListManagerSkin(false); |
308 |
listManagerDemoSkin.setBounds(11, 21, 491, 363); |
309 |
listManagerDemoSkin.getListManager().setListener(new ListManagerListener() {
|
310 |
public Object[] addObjects() { |
311 |
TreeSet filters = new TreeSet(new Comparator() { |
312 |
public int compare(Object o1, Object o2) { |
313 |
DriverFileFilter dff1 = (DriverFileFilter) o1; |
314 |
DriverFileFilter dff2 = (DriverFileFilter) o2; |
315 |
|
316 |
return dff1.driver.getName().compareTo(dff2.driver.getName());
|
317 |
} |
318 |
}); |
319 |
fileChooser = new JFileChooser(lastPath); |
320 |
fileChooser.setMultiSelectionEnabled(true);
|
321 |
fileChooser.setAcceptAllFileFilterUsed(false);
|
322 |
|
323 |
try {
|
324 |
boolean last = false; |
325 |
String[] driverNames = LayerFactory.getDM().getDriverNames(); |
326 |
DriverFileFilter defaultFileFilter = null;
|
327 |
DriverFileFilter auxF; |
328 |
for (int i = 0; i < driverNames.length; i++) { |
329 |
System.err.println("DRIVER "+i+" : "+driverNames[i]); |
330 |
boolean is = false; |
331 |
for (int j = 0; j < driverClasses.length; j++) { |
332 |
if (i == 0) System.err.println("DRIVER CLASS "+j+" : "+driverClasses[j].toString()); |
333 |
if (LayerFactory.getDM().isA(driverNames[i], driverClasses[j]))
|
334 |
is = true;
|
335 |
} |
336 |
if (is){
|
337 |
auxF = new DriverFileFilter(driverNames[i]);
|
338 |
filters.add(auxF); |
339 |
if (driverNames[i].indexOf("shp") != -1) { |
340 |
defaultFileFilter = auxF; |
341 |
} |
342 |
if (lastFileFilter != null) { |
343 |
if (driverNames[i].equals(lastFileFilter.driver.getName())){
|
344 |
last = true;
|
345 |
} |
346 |
} |
347 |
} |
348 |
} |
349 |
DriverFileFilter def = (DriverFileFilter) fileChooser.getFileFilter(); |
350 |
if (last) {
|
351 |
def = lastFileFilter; |
352 |
} else {
|
353 |
if (lastFileFilter != null){ |
354 |
if (filters.contains(lastFileFilter)) {
|
355 |
def = defaultFileFilter; |
356 |
} |
357 |
} |
358 |
} |
359 |
Iterator i = filters.iterator();
|
360 |
while (i.hasNext()) {
|
361 |
DriverFileFilter element = (DriverFileFilter) i.next(); |
362 |
if (def != null){ |
363 |
if (!element.driver.getName().equals(def.driver.getName())){
|
364 |
fileChooser.addChoosableFileFilter(element); |
365 |
} |
366 |
} else {
|
367 |
fileChooser.addChoosableFileFilter(element); |
368 |
} |
369 |
} |
370 |
if (def != null){ |
371 |
fileChooser.setFileFilter(def); |
372 |
} |
373 |
|
374 |
} catch (DriverLoadException e1) {
|
375 |
NotificationManager.addError("No se pudo acceder a los drivers", e1);
|
376 |
} |
377 |
|
378 |
int result = fileChooser.showOpenDialog(FileOpenDialog.this);
|
379 |
|
380 |
File[] newFiles = null; |
381 |
MyFile[] toAdd;
|
382 |
if (result == JFileChooser.APPROVE_OPTION) { |
383 |
lastFileFilter = (DriverFileFilter) fileChooser.getFileFilter(); |
384 |
lastPath = fileChooser.getCurrentDirectory().getAbsolutePath(); |
385 |
newFiles = fileChooser.getSelectedFiles(); |
386 |
toAdd = new MyFile[newFiles.length];
|
387 |
for (int ind=0; ind < newFiles.length; ind++){ |
388 |
DriverFileFilter driverFileFilter = (DriverFileFilter) fileChooser.getFileFilter(); |
389 |
String driverName = driverFileFilter.getDescription();
|
390 |
|
391 |
if (driverFileFilter.driver instanceof CmsRasterDriver){ |
392 |
if (driverFileFilter.driver.getClass() != CmsRasterDriver.class){
|
393 |
newFiles[ind] = driverFileFilter.getTransformedFile(newFiles[ind]); |
394 |
if (newFiles[ind] == null){ |
395 |
return new Object[0]; |
396 |
} |
397 |
} |
398 |
} |
399 |
toAdd[ind] = new MyFile(newFiles[ind], driverName);
|
400 |
} |
401 |
|
402 |
|
403 |
return toAdd;
|
404 |
} |
405 |
else
|
406 |
return new Object[0]; |
407 |
} |
408 |
|
409 |
public Object getProperties(Object selected) { |
410 |
return null; |
411 |
} |
412 |
}); |
413 |
} |
414 |
return listManagerDemoSkin;
|
415 |
} |
416 |
} // @jve:decl-index=0:visual-constraint="10,10"
|
417 |
// @jve:visual-info decl-index=0 visual-constraint="10,10"
|
418 |
// @jve:visual-info decl-index=0 visual-constraint="10,10"
|