root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / gui / FileOpenDialog.java @ 8120
History | View | Annotate | Download (13.1 KB)
1 | 7304 | caballero | /*
|
---|---|---|---|
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 | 7619 | fjp | import org.cresques.cts.IProjection; |
63 | |||
64 | 7304 | caballero | 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.ProjectExtension; |
||
70 | 7619 | fjp | import com.iver.cit.gvsig.fmap.crs.CRSFactory; |
71 | 7304 | caballero | import com.iver.cit.gvsig.fmap.drivers.RasterDriver; |
72 | import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver; |
||
73 | import com.iver.cit.gvsig.fmap.layers.LayerFactory; |
||
74 | 8120 | jmvivo | import com.iver.cit.gvsig.gui.panels.CRSSelectPanel; |
75 | 7304 | caballero | 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 | 8120 | jmvivo | private CRSSelectPanel jPanelProj = null; |
93 | 7304 | caballero | 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 | public void setDriverClasses(Class[] driverClasses) { |
||
238 | this.driverClasses = driverClasses;
|
||
239 | } |
||
240 | /**
|
||
241 | * This method initializes jPanel
|
||
242 | *
|
||
243 | * @return javax.swing.JPanel
|
||
244 | */
|
||
245 | 8120 | jmvivo | private CRSSelectPanel getJPanelProj() {
|
246 | 7304 | caballero | if (jPanelProj == null) { |
247 | 7619 | fjp | IProjection proj = CRSFactory.getCRS("EPSG:23030");
|
248 | if (PluginServices.getMainFrame() != null) |
||
249 | { |
||
250 | ProjectExtension pe = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class); |
||
251 | proj = pe.getProject().getProjection(); |
||
252 | } |
||
253 | |||
254 | 8120 | jmvivo | jPanelProj = CRSSelectPanel.getPanel(proj); |
255 | jPanelProj.setTransPanelActive(true);
|
||
256 | 7304 | caballero | jPanelProj.setBounds(11, 400, 448, 35); |
257 | jPanelProj.setPreferredSize(new java.awt.Dimension(448,35)); |
||
258 | jPanelProj.addActionListener(new java.awt.event.ActionListener() {
|
||
259 | public void actionPerformed(java.awt.event.ActionEvent e) { |
||
260 | if (jPanelProj.isOkPressed()) {
|
||
261 | FOpenDialog.setLastProjection(jPanelProj.getCurProj()); |
||
262 | } |
||
263 | } |
||
264 | }); |
||
265 | |||
266 | } |
||
267 | return jPanelProj;
|
||
268 | } |
||
269 | |||
270 | public class MyFile { |
||
271 | private File f; |
||
272 | private String driverName; |
||
273 | |||
274 | public MyFile(File f, String driverName){ |
||
275 | this.f = f;
|
||
276 | this.driverName = driverName;
|
||
277 | } |
||
278 | |||
279 | public String toString(){ |
||
280 | return f.getName();
|
||
281 | } |
||
282 | public String getDriverName(){ |
||
283 | return driverName;
|
||
284 | } |
||
285 | |||
286 | } |
||
287 | |||
288 | /**
|
||
289 | * This method initializes listManagerDemoSkin
|
||
290 | *
|
||
291 | * @return com.iver.utiles.listManager.ListManagerDemoSkin
|
||
292 | */
|
||
293 | private ListManagerSkin getListManagerDemoSkin() {
|
||
294 | if (listManagerDemoSkin == null) { |
||
295 | listManagerDemoSkin = new ListManagerSkin(false); |
||
296 | listManagerDemoSkin.setBounds(11, 21, 491, 363); |
||
297 | listManagerDemoSkin.getListManager().setListener(new ListManagerListener() {
|
||
298 | public Object[] addObjects() { |
||
299 | TreeSet filters = new TreeSet(new Comparator() { |
||
300 | public int compare(Object o1, Object o2) { |
||
301 | DriverFileFilter dff1 = (DriverFileFilter) o1; |
||
302 | DriverFileFilter dff2 = (DriverFileFilter) o2; |
||
303 | |||
304 | return dff1.driver.getName().compareTo(dff2.driver.getName());
|
||
305 | } |
||
306 | }); |
||
307 | fileChooser = new JFileChooser(lastPath); |
||
308 | fileChooser.setMultiSelectionEnabled(true);
|
||
309 | fileChooser.setAcceptAllFileFilterUsed(false);
|
||
310 | |||
311 | try {
|
||
312 | boolean last = false; |
||
313 | String[] driverNames = LayerFactory.getDM().getDriverNames(); |
||
314 | DriverFileFilter defaultFileFilter = null;
|
||
315 | DriverFileFilter auxF; |
||
316 | for (int i = 0; i < driverNames.length; i++) { |
||
317 | System.err.println("DRIVER "+i+" : "+driverNames[i]); |
||
318 | boolean is = false; |
||
319 | for (int j = 0; j < driverClasses.length; j++) { |
||
320 | if (i == 0) System.err.println("DRIVER CLASS "+j+" : "+driverClasses[j].toString()); |
||
321 | if (LayerFactory.getDM().isA(driverNames[i], driverClasses[j]))
|
||
322 | is = true;
|
||
323 | } |
||
324 | if (is){
|
||
325 | auxF = new DriverFileFilter(driverNames[i]);
|
||
326 | filters.add(auxF); |
||
327 | if (driverNames[i].indexOf("shp") != -1) { |
||
328 | defaultFileFilter = auxF; |
||
329 | } |
||
330 | if (lastFileFilter != null) { |
||
331 | if (driverNames[i].equals(lastFileFilter.driver.getName())){
|
||
332 | last = true;
|
||
333 | } |
||
334 | } |
||
335 | } |
||
336 | } |
||
337 | DriverFileFilter def = (DriverFileFilter) fileChooser.getFileFilter(); |
||
338 | if (last) {
|
||
339 | def = lastFileFilter; |
||
340 | } else {
|
||
341 | if (lastFileFilter != null){ |
||
342 | if (filters.contains(lastFileFilter)) {
|
||
343 | def = defaultFileFilter; |
||
344 | } |
||
345 | } |
||
346 | } |
||
347 | Iterator i = filters.iterator();
|
||
348 | while (i.hasNext()) {
|
||
349 | DriverFileFilter element = (DriverFileFilter) i.next(); |
||
350 | if (def != null){ |
||
351 | if (!element.driver.getName().equals(def.driver.getName())){
|
||
352 | fileChooser.addChoosableFileFilter(element); |
||
353 | } |
||
354 | } else {
|
||
355 | fileChooser.addChoosableFileFilter(element); |
||
356 | } |
||
357 | } |
||
358 | if (def != null){ |
||
359 | fileChooser.setFileFilter(def); |
||
360 | } |
||
361 | |||
362 | } catch (DriverLoadException e1) {
|
||
363 | NotificationManager.addError("No se pudo acceder a los drivers", e1);
|
||
364 | } |
||
365 | |||
366 | int result = fileChooser.showOpenDialog(FileOpenDialog.this);
|
||
367 | |||
368 | File[] newFiles = null; |
||
369 | MyFile[] toAdd;
|
||
370 | if (result == JFileChooser.APPROVE_OPTION) { |
||
371 | lastFileFilter = (DriverFileFilter) fileChooser.getFileFilter(); |
||
372 | lastPath = fileChooser.getCurrentDirectory().getAbsolutePath(); |
||
373 | newFiles = fileChooser.getSelectedFiles(); |
||
374 | toAdd = new MyFile[newFiles.length];
|
||
375 | for (int ind=0; ind < newFiles.length; ind++) |
||
376 | { |
||
377 | String driverName = ((DriverFileFilter) fileChooser.getFileFilter()).getDescription();
|
||
378 | toAdd[ind] = new MyFile(newFiles[ind], driverName);
|
||
379 | } |
||
380 | |||
381 | |||
382 | return toAdd;
|
||
383 | } |
||
384 | else
|
||
385 | return new Object[0]; |
||
386 | } |
||
387 | |||
388 | public Object getProperties(Object selected) { |
||
389 | return null; |
||
390 | } |
||
391 | }); |
||
392 | } |
||
393 | return listManagerDemoSkin;
|
||
394 | } |
||
395 | } // @jve:decl-index=0:visual-constraint="10,10"
|
||
396 | // @jve:visual-info decl-index=0 visual-constraint="10,10"
|
||
397 | // @jve:visual-info decl-index=0 visual-constraint="10,10" |