Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / addlayer / fileopen / FileOpenWizard.java @ 15513

History | View | Annotate | Download (14.1 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.lang.reflect.InvocationTargetException;
27
import java.util.ArrayList;
28
import java.util.Iterator;
29
import java.util.prefs.Preferences;
30

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

    
38
import org.cresques.cts.IProjection;
39

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

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

    
73
        /**
74
   * Lista de manejadores de ficheros (extensiones)
75
   */
76
        private ArrayList         listFileOpen    = new ArrayList();
77

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

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

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

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

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

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

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

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

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

    
168
                initialize();
169
        }
170

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

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

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

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

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

    
220
                return jPanel2;
221
        }
222

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

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

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

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

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

    
266
                }
267
                return jPanelProj;
268
        }
269

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

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

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

    
293
                public String getDriverName() {
294
                        return driverName;
295
                }
296

    
297
                public IFileOpen getFileOpen() {
298
                        return fileOpen;
299
                }
300

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

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

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

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

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

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

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

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

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

    
377
                        return toAdd;
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
}