Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / addlayer / fileopen / FileOpenWizard.java @ 16403

History | View | Annotate | Download (14 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 com.iver.cit.gvsig.addlayer.fileopen;
20

    
21
import java.awt.Dimension;
22
import java.awt.event.ActionEvent;
23
import java.awt.event.ActionListener;
24
import java.awt.geom.Rectangle2D;
25
import java.io.File;
26
import java.util.ArrayList;
27
import java.util.Iterator;
28
import java.util.prefs.Preferences;
29

    
30
import javax.swing.BorderFactory;
31
import javax.swing.DefaultListModel;
32
import javax.swing.JFileChooser;
33
import javax.swing.JPanel;
34
import javax.swing.border.TitledBorder;
35
import javax.swing.filechooser.FileFilter;
36

    
37
import org.cresques.cts.IProjection;
38

    
39
import com.iver.andami.PluginServices;
40
import com.iver.andami.ui.mdiManager.IWindow;
41
import com.iver.cit.gvsig.addlayer.AddLayerDialog;
42
import com.iver.cit.gvsig.fmap.MapControl;
43
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
44
import com.iver.cit.gvsig.fmap.layers.FLayer;
45
import com.iver.cit.gvsig.gui.WizardPanel;
46
import com.iver.cit.gvsig.gui.panels.CRSSelectPanel;
47
import com.iver.cit.gvsig.project.documents.gui.ListManagerSkin;
48
import com.iver.cit.gvsig.project.documents.view.gui.BaseView;
49
import com.iver.cit.gvsig.project.documents.view.gui.FPanelLocConfig;
50
import com.iver.utiles.extensionPoints.ExtensionPoint;
51
import com.iver.utiles.extensionPoints.ExtensionPoints;
52
import com.iver.utiles.extensionPoints.ExtensionPointsSingleton;
53
import com.iver.utiles.listManager.ListManagerListener;
54

    
55
/**
56
 * Pesta?a donde estara la apertura de ficheros
57
 *
58
 * @version 04/09/2007
59
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
60
 */
61
public class FileOpenWizard extends WizardPanel implements ListManagerListener {
62
        private static final long serialVersionUID = 335310147513197564L;
63
        private static String     lastPath        = null;
64
        private JPanel            jPanel2         = null;
65
        private JFileChooser      fileChooser     = null;
66
        private CRSSelectPanel    jPanelProj      = null;
67
        private ListManagerSkin   listManagerSkin = null;
68
        private boolean           projection      = false;
69
        static private FileFilter lastFileFilter  = null;
70
        private TitledBorder      titledBorder    = null;
71

    
72
        /**
73
         * Lista de manejadores de ficheros (extensiones)
74
         */
75
        private ArrayList<IFileOpen> listFileOpen = new ArrayList<IFileOpen>();
76

    
77
        /**
78
         * Construye un FileOpenWizard usando la extension por defecto
79
         * 'FileExtendingOpenDialog'
80
         */
81
        public FileOpenWizard() {
82
                this("FileExtendingOpenDialog");
83
        }
84

    
85
        /**
86
         * Construye un FileOpenWizard usando el punto de extension pasado por
87
         * parametro
88
         * @param nameExtension
89
         */
90
        public FileOpenWizard(String nameExtension) {
91
                this(nameExtension, true);
92
        }
93
        /**
94
         * Construye un FileOpenWizard usando el punto de extension pasado por
95
         * parametro
96
         * @param nameExtension
97
         */
98
        public FileOpenWizard(String nameExtension, boolean proj) {
99
                ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
100
                ExtensionPoint extensionPoint = (ExtensionPoint) extensionPoints.get(nameExtension);
101
                if (extensionPoint == null)
102
                        return;
103

    
104
                Iterator iterator = extensionPoint.keySet().iterator();
105
                while (iterator.hasNext()) {
106
                        String key = (String) iterator.next();
107
                        Object obj = extensionPoint.get(key);
108
                        if (obj instanceof Class) {
109
                                try {
110
                                        obj = ((Class) obj).getConstructor(null).newInstance(null);
111
                                } catch (Exception e) {
112
                                        e.printStackTrace();
113
                                }
114
                        }
115
                        if (obj instanceof IFileOpen) {
116
                                listFileOpen.add((IFileOpen) obj);
117
                        }
118
                }
119
                init(null, proj);
120
        }
121

    
122
        /**
123
         * Creates a new FileOpenWizard object.
124
         * @param driverClasses
125
         * @param proj
126
         */
127
        public FileOpenWizard(IFileOpen[] driverClasses, boolean proj) {
128
                init(driverClasses, proj);
129
        }
130

    
131
        /**
132
         * Creates a new FileOpenWizard object.
133
         * @param driverClasses
134
         */
135
        public FileOpenWizard(IFileOpen[] driverClasses) {
136
                init(driverClasses, true);
137
        }
138

    
139
        /**
140
         * Creates a new FileOpenWizard object.
141
         * @param driverClasses
142
         * @param proj
143
         * @param title
144
         */
145
        public FileOpenWizard(IFileOpen[] driverClasses, boolean proj, String title) {
146
                setTitle(title);
147
                init(driverClasses, proj);
148
        }
149

    
150
        /**
151
         * @param driverClasses2
152
         * @param b
153
         */
154
        private void init(IFileOpen[] driverClasses, boolean projection) {
155
                this.projection = projection;
156

    
157
                if (driverClasses != null)
158
                        for (int i = 0; i < driverClasses.length; i++)
159
                                listFileOpen.add(driverClasses[i]);
160

    
161
                if (lastPath == null) {
162
                        Preferences prefs = Preferences.userRoot().node("gvsig.foldering");
163
                        lastPath = prefs.get("DataFolder", null);
164
                }
165

    
166
                initialize();
167
        }
168

    
169
        /*
170
         * (non-Javadoc)
171
         * @see com.iver.cit.gvsig.gui.WizardPanel#initWizard()
172
         */
173
        public void initWizard() {
174
                setTabName(PluginServices.getText(this, "Fichero"));
175
                init(null, true);
176
        }
177

    
178
        /**
179
         * This method initializes this
180
         */
181
        private void initialize() {
182
                this.setSize(514, 280);
183
                this.setLayout(null);
184
                this.add(getJPanel2(), null);
185
        }
186

    
187
        public File[] getFiles() {
188
                MyFile[] files = (MyFile[]) getListManagerSkin().getListManager().getListModel().getObjects().toArray(new MyFile[0]);
189
                File[] ret = new File[files.length];
190
                int pos = files.length - 1;
191
                for (int i = 0; i < files.length; i++) {
192
                        ret[pos] = files[i].f;
193
                        pos--;
194
                }
195
                return ret;
196
        }
197

    
198
        public MyFile[] getMyFiles() {
199
                return (MyFile[]) getListManagerSkin().getListManager().getListModel().getObjects().toArray(new MyFile[0]);
200
        }
201

    
202
        /**
203
         * This method initializes jPanel2
204
         * @return javax.swing.JPanel
205
         */
206
        private JPanel getJPanel2() {
207
                if (jPanel2 == null) {
208
                        jPanel2 = new JPanel();
209
                        jPanel2.setLayout(null);
210
                        jPanel2.setBorder(getTitledBorder());
211
                        jPanel2.setPreferredSize(new Dimension(380, 200));
212
                        jPanel2.setBounds(2, 2, 506, 472);
213
                        if (projection)
214
                                jPanel2.add(getJPanelProj(), null);
215
                        jPanel2.add(getListManagerSkin(), null);
216
                }
217

    
218
                return jPanel2;
219
        }
220

    
221
        private TitledBorder getTitledBorder() {
222
                if (titledBorder == null) {
223
                        titledBorder = BorderFactory.createTitledBorder(null, PluginServices.getText(this, "Seleccionar_fichero"), TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null);
224
                        titledBorder.setTitle(PluginServices.getText(this, PluginServices.getText(this, "Capas")));
225
                }
226
                return titledBorder;
227
        }
228

    
229
        public String[] getDriverNames() {
230
                MyFile[] files = (MyFile[]) getListManagerSkin().getListManager().getListModel().getObjects().toArray(new MyFile[0]);
231
                String[] ret = new String[files.length];
232
                int pos = files.length - 1;
233

    
234
                for (int i = 0; i < files.length; i++) {
235
                        ret[pos] = files[i].getDriverName();
236
                        pos--;
237
                }
238
                return ret;
239
        }
240

    
241
        /**
242
         * This method initializes jPanel
243
         * @return javax.swing.JPanel
244
         */
245
        private CRSSelectPanel getJPanelProj() {
246
                if (jPanelProj == null) {
247
                        IProjection proj = CRSFactory.getCRS("EPSG:23030");
248
                        if (PluginServices.getMainFrame() != null) {
249
                                proj = AddLayerDialog.getLastProjection();
250
                        }
251

    
252
                        jPanelProj = CRSSelectPanel.getPanel(proj);
253
                        jPanelProj.setTransPanelActive(true);
254
                        jPanelProj.setBounds(11, 400, 448, 35);
255
                        jPanelProj.setPreferredSize(new Dimension(448, 35));
256
                        jPanelProj.addActionListener(new ActionListener() {
257
                                public void actionPerformed(ActionEvent e) {
258
                                        if (jPanelProj.isOkPressed()) {
259
                                                AddLayerDialog.setLastProjection(jPanelProj.getCurProj());
260
                                        }
261
                                }
262
                        });
263

    
264
                }
265
                return jPanelProj;
266
        }
267

    
268
        /**
269
   * @version 05/09/2007
270
   * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
271
   */
272
        public class MyFile {
273
                private File      f;
274
                private String    driverName;
275
                private IFileOpen fileOpen;
276

    
277
                public MyFile(File f, String driverName, IFileOpen fileOpen) {
278
                        this.f = f;
279
                        this.driverName = driverName;
280
                        this.fileOpen = fileOpen;
281
                }
282

    
283
                /*
284
                 * (non-Javadoc)
285
                 * @see java.lang.Object#toString()
286
                 */
287
                public String toString() {
288
                        return f.getName();
289
                }
290

    
291
                public String getDriverName() {
292
                        return driverName;
293
                }
294

    
295
                public IFileOpen getFileOpen() {
296
                        return fileOpen;
297
                }
298

    
299
                public Rectangle2D createLayer(MapControl mapControl) {
300
                        return fileOpen.createLayer(f, mapControl, driverName, AddLayerDialog.getLastProjection());
301
                }
302
        }
303

    
304
        /**
305
         * This method initializes listManagerDemoSkin
306
         * @return ListManagerSkin
307
         */
308
        private ListManagerSkin getListManagerSkin() {
309
                if (listManagerSkin == null) {
310
                        listManagerSkin = new ListManagerSkin(false);
311
                        listManagerSkin.setBounds(11, 21, 491, 363);
312
                        listManagerSkin.getListManager().setListener(this);
313
                }
314
                return listManagerSkin;
315
        }
316

    
317
        /*
318
         * (non-Javadoc)
319
         * @see com.iver.utiles.listManager.ListManagerListener#addObjects()
320
         */
321
        public Object[] addObjects() {
322
                this.callStateChanged(true);
323
                fileChooser = new JFileChooser(lastPath);
324
                fileChooser.setMultiSelectionEnabled(true);
325
                fileChooser.setAcceptAllFileFilterUsed(false);
326

    
327
                boolean finded = false;
328
                FileFilter auxFilter=null;
329
                for (int i = 0; i < listFileOpen.size(); i++) {
330
                        IFileOpen fileOpen = listFileOpen.get(i);
331
                        fileOpen.pre();
332
                        ArrayList<FileFilter> aux = fileOpen.getFileFilter();
333

    
334
                        for (int j = 0; j < aux.size(); j++) {
335
                                FileFilter fileFilter = aux.get(j);
336
                                fileChooser.addChoosableFileFilter(fileFilter);
337
                                if (lastFileFilter!=null && lastFileFilter.getDescription().equals(fileFilter.getDescription())){
338
                                        auxFilter=fileFilter;
339
                                        finded = true;
340
                                }
341
                        }
342
                }
343
                if (finded && (lastFileFilter != null))
344
                        fileChooser.setFileFilter(auxFilter);
345

    
346
                int result = fileChooser.showOpenDialog(this);
347

    
348
                File[] newFiles = null;
349
                ArrayList<MyFile> toAdd = new ArrayList<MyFile>();
350
                if (result == JFileChooser.APPROVE_OPTION) {
351
                        lastPath = fileChooser.getCurrentDirectory().getAbsolutePath();
352
                        lastFileFilter = (FileFilter) fileChooser.getFileFilter();
353
                        newFiles = fileChooser.getSelectedFiles();
354

    
355
                        IFileOpen lastFileOpen = null;
356
                        for (int i = 0; i < listFileOpen.size(); i++) {
357
                                IFileOpen fileOpen = listFileOpen.get(i);
358
                                ArrayList<FileFilter> aux = fileOpen.getFileFilter();
359
                                for (int j = 0; j < aux.size(); j++) {
360
                                        if (fileChooser.getFileFilter() == aux.get(j)) {
361
                                                for (int iFile=0; iFile<newFiles.length; iFile++) {
362
                                                        newFiles[iFile] = fileOpen.post(newFiles[iFile]);
363
                                                }
364
                                                lastFileOpen = fileOpen;
365
                                                break;
366
                                        }
367
                                }
368
                        }
369

    
370
                        for (int ind = 0; ind < newFiles.length; ind++) {
371
                                if (newFiles[ind] == null)
372
                                        break;
373
                                String driverName = ((FileFilter) fileChooser.getFileFilter()).getDescription();
374
                                toAdd.add(new MyFile(newFiles[ind], driverName, lastFileOpen));
375
                        }
376

    
377
                        return toAdd.toArray();
378
                } else
379
                        return new Object[0];
380
        }
381

    
382
        /*
383
         * (non-Javadoc)
384
         * @see com.iver.cit.gvsig.gui.WizardPanel#execute()
385
         */
386
        public void execute() {
387
                if (getFiles() == null)
388
                        return;
389

    
390
                MapControl mapControl = null;
391
                IWindow[] w = PluginServices.getMDIManager().getAllWindows();
392

    
393
                // Si se est? cargando la capa en el localizador se obtiene el mapcontrol de este
394
                for (int i = 0; i < w.length; i++) {
395
                        if (w[i] instanceof FPanelLocConfig) {
396
                                mapControl = ((FPanelLocConfig) w[i]).getMapCtrl();
397
                                DefaultListModel lstModel = (DefaultListModel) ((FPanelLocConfig) w[i]).getJList().getModel();
398
                                lstModel.clear();
399
                                for (int k = 0; k < getFiles().length; k++)
400
                                        lstModel.addElement(getFiles()[k].getName());
401
                                for (int j = mapControl.getMapContext().getLayers().getLayersCount() - 1; j >= 0; j--) {
402
                                        FLayer lyr = mapControl.getMapContext().getLayers().getLayer(j);
403
                                        lstModel.addElement(lyr.getName());
404
                                }
405
                        }
406
                }
407

    
408
                // Obtiene la primera vista activa
409
                if (mapControl == null) {
410
                        for (int i = 0; i < w.length; i++) {
411
                                IWindow activeWindow = PluginServices.getMDIManager().getActiveWindow();
412
                                if (w[i] instanceof BaseView && w[i].equals(activeWindow))
413
                                        mapControl = ((BaseView) w[i]).getMapControl();
414
                        }
415
                }
416
                // Si no hay ninguna activa obtiene la primera vista aunque no est? activa
417
                if (mapControl == null) {
418
                        for (int i = 0; i < w.length; i++) {
419
                                if (w[i] instanceof BaseView)
420
                                        mapControl = ((BaseView) w[i]).getMapControl();
421
                        }
422
                }
423

    
424
                if (mapControl == null)
425
                        return;
426

    
427
                Rectangle2D[] rects = new Rectangle2D[getFiles().length];
428
                boolean first = false;
429

    
430
                for (int i = getMyFiles().length - 1; i >= 0; i--) {
431
                        if (mapControl.getMapContext().getViewPort().getExtent() == null)
432
                                first = true;
433
                        rects[i] = ((MyFile) getMyFiles()[i]).createLayer(mapControl);
434
                }
435

    
436
                if (first && (rects.length > 1)) {
437
                        Rectangle2D rect = new Rectangle2D.Double();
438
                        rect.setRect(rects[0]);
439
                        for (int i = 0; i < rects.length; i++)
440
                                if (rects[i] != null)
441
                                        rect.add(rects[i]);
442
                        mapControl.getMapContext().getViewPort().setExtent(rect);
443
                }
444
                mapControl.getMapContext().endAtomicEvent();
445
        }
446

    
447
        public void setTitle(String title) {
448
                getTitledBorder().setTitle(title);
449
        }
450

    
451
        /*
452
         * (non-Javadoc)
453
         * @see com.iver.utiles.listManager.ListManagerListener#getProperties(java.lang.Object)
454
         */
455
        public Object getProperties(Object selected) {
456
                return null;
457
        }
458

    
459
        /*
460
         * (non-Javadoc)
461
         * @see com.iver.cit.gvsig.gui.WizardPanel#getLayer()
462
         */
463
        public FLayer getLayer() {
464
                return null;
465
        }
466

    
467
        /**
468
         * Obtiene la ?ltima ruta seleccionada al a?adir ficheros.
469
         * @return Ruta del ?ltimo fichero seleccionado
470
         */
471
        public static String getLastPath() {
472
                return lastPath;
473
        }
474

    
475
        /**
476
         * Asigna la ?ltima ruta en una selecci?n de ficheros de disco. Es necesario
477
         * poder hacer esta asignaci?n desde fuera de FileOpenWizard ya que este path debe
478
         * ser com?n a toda la aplicaci?n. Hay otros puntos donde se seleccionan ficheros
479
         * de disco.
480
         * @param lastPath Ruta del ?ltimo fichero de disco seleccionado
481
         */
482
        public static void setLastPath(String path) {
483
                lastPath = path;
484
        }
485
}