Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / addlayer / fileopen / FileOpenWizard.java @ 25214

History | View | Annotate | Download (13.5 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.io.File;
25
import java.util.ArrayList;
26
import java.util.Iterator;
27
import java.util.List;
28
import java.util.prefs.Preferences;
29

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

    
36
import org.cresques.cts.IProjection;
37
import org.gvsig.fmap.crs.CRSFactory;
38
import org.gvsig.fmap.dal.DataStoreParameters;
39
import org.gvsig.fmap.geom.primitive.DefaultEnvelope;
40
import org.gvsig.fmap.geom.primitive.Envelope;
41
import org.gvsig.fmap.mapcontext.layers.FLayer;
42
import org.gvsig.fmap.mapcontrol.MapControl;
43
import org.gvsig.gui.beans.swing.JFileChooser;
44
import org.gvsig.tools.ToolsLocator;
45
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
46
import org.gvsig.tools.extensionpoint.ExtensionPoint.Extension;
47

    
48
import com.iver.andami.PluginServices;
49
import com.iver.andami.ui.mdiManager.IWindow;
50
import com.iver.cit.gvsig.addlayer.AddLayerDialog;
51
import com.iver.cit.gvsig.gui.WizardPanel;
52
import com.iver.cit.gvsig.gui.panels.CRSSelectPanel;
53
import com.iver.cit.gvsig.project.documents.gui.ListManagerSkin;
54
import com.iver.cit.gvsig.project.documents.view.gui.BaseView;
55
import com.iver.cit.gvsig.project.documents.view.gui.FPanelLocConfig;
56
import com.iver.utiles.listManager.ListManagerListener;
57

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

    
76
        /**
77
         * Lista de manejadores de ficheros (extensiones)
78
         */
79
        private ArrayList<IFileOpen> listFileOpen = new ArrayList<IFileOpen>();
80

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

    
89
        /**
90
         * Construye un FileOpenWizard usando el punto de extension pasado por
91
         * parametro
92
         * 
93
         * @param nameExtension
94
         * 
95
         */
96
        public FileOpenWizard(String nameExtension) {
97
                this(nameExtension, true);
98
        }
99
        /**
100
         * Construye un FileOpenWizard usando el punto de extension pasado por
101
         * parametro
102
         * @param nameExtension
103
         */
104
        public FileOpenWizard(String nameExtension, boolean proj) {
105
                ExtensionPointManager epManager = ToolsLocator
106
                                .getExtensionPointManager();
107
                if (!epManager.has(nameExtension)) {
108
                        return;
109
                }
110

    
111
                Iterator iterator = epManager.get(nameExtension).iterator();
112
                Object obj = null;
113
                while (iterator.hasNext()) {
114
                        try {
115
                                obj = ((Extension) iterator.next()).create();
116
                        } catch (Exception e) {
117
                                e.printStackTrace(); //FIXME Excepcion
118
                                continue;
119
                        }
120
                        if (obj instanceof IFileOpen) {
121
                                listFileOpen.add((IFileOpen) obj);
122
                        }
123
                }
124
                init(null, proj);
125
        }
126

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

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

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

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

    
162
                if (driverClasses != null) {
163
                        for (int i = 0; i < driverClasses.length; i++) {
164
                                listFileOpen.add(driverClasses[i]);
165
                        }
166
                }
167

    
168
                if (lastPath == null) {
169
                        Preferences prefs = Preferences.userRoot().node("gvsig.foldering");
170
                        lastPath = prefs.get("DataFolder", null);
171
                }
172

    
173
                initialize();
174
        }
175

    
176
        /*
177
         * (non-Javadoc)
178
         * @see com.iver.cit.gvsig.gui.WizardPanel#initWizard()
179
         */
180
        public void initWizard() {
181
                setTabName(PluginServices.getText(this, "Fichero"));
182
                init(null, true);
183
        }
184

    
185
        /**
186
         * This method initializes this
187
         */
188
        private void initialize() {
189
                this.setSize(514, 280);
190
                this.setLayout(null);
191
                this.add(getJPanel2(), null);
192
        }
193

    
194
        public File[] getFiles() {
195
                MyFile[] files = (MyFile[]) getListManagerSkin().getListManager().getListModel().getObjects().toArray(new MyFile[0]);
196
                File[] ret = new File[files.length];
197
                int pos = files.length - 1;
198
                for (int i = 0; i < files.length; i++) {
199
                        ret[pos] = files[i].getFile();
200
                        pos--;
201
                }
202
                return ret;
203
        }
204

    
205
        public MyFile[] getMyFiles() {
206
                return (MyFile[]) getListManagerSkin().getListManager().getListModel().getObjects().toArray(new MyFile[0]);
207
        }
208

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

    
226
                return jPanel2;
227
        }
228

    
229
        private TitledBorder getTitledBorder() {
230
                if (titledBorder == null) {
231
                        titledBorder = BorderFactory.createTitledBorder(null, PluginServices.getText(this, "Seleccionar_fichero"), TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null);
232
                        titledBorder.setTitle(PluginServices.getText(this, PluginServices.getText(this, "Capas")));
233
                }
234
                return titledBorder;
235
        }
236

    
237
        public String[] getDriverNames() {
238
                MyFile[] files = (MyFile[]) getListManagerSkin().getListManager().getListModel().getObjects().toArray(new MyFile[0]);
239
                String[] ret = new String[files.length];
240
                int pos = files.length - 1;
241

    
242
                for (int i = 0; i < files.length; i++) {
243
                        ret[pos] = files[i].getFilter().getDescription();
244
                        pos--;
245
                }
246
                return ret;
247
        }
248

    
249
        /**
250
         * This method initializes jPanel
251
         * @return javax.swing.JPanel
252
         */
253
        private CRSSelectPanel getJPanelProj() {
254
                if (jPanelProj == null) {
255
                        IProjection proj = CRSFactory.getCRS("EPSG:23030");
256
                        if (PluginServices.getMainFrame() != null) {
257
                                proj = AddLayerDialog.getLastProjection();
258
                        }
259

    
260
                        jPanelProj = CRSSelectPanel.getPanel(proj);
261
                        jPanelProj.setTransPanelActive(true);
262
                        jPanelProj.setBounds(11, 400, 448, 35);
263
                        jPanelProj.setPreferredSize(new Dimension(448, 35));
264
                        jPanelProj.addActionListener(new ActionListener() {
265
                                public void actionPerformed(ActionEvent e) {
266
                                        if (jPanelProj.isOkPressed()) {
267
                                                AddLayerDialog.setLastProjection(jPanelProj.getCurProj());
268
                                        }
269
                                }
270
                        });
271

    
272
                }
273
                return jPanelProj;
274
        }
275

    
276

    
277
        /**
278
         * This method initializes listManagerDemoSkin
279
         * @return ListManagerSkin
280
         */
281
        private ListManagerSkin getListManagerSkin() {
282
                if (listManagerSkin == null) {
283
                        listManagerSkin = new ListManagerSkin(false);
284
                        listManagerSkin.setBounds(11, 21, 491, 363);
285
                        listManagerSkin.getListManager().setListener(this);
286
                }
287
                return listManagerSkin;
288
        }
289

    
290
        /*
291
         * (non-Javadoc)
292
         * @see com.iver.utiles.listManager.ListManagerListener#addObjects()
293
         */
294
        public Object[] addObjects() {
295
                this.callStateChanged(true);
296
                fileChooser = new JFileChooser(OPEN_LAYER_FILE_CHOOSER_ID, lastPath);
297
                fileChooser.setMultiSelectionEnabled(true);
298
                fileChooser.setAcceptAllFileFilterUsed(false);
299

    
300
                boolean finded = false;
301
                FileFilter auxFilter=null;
302
                for (int i = 0; i < listFileOpen.size(); i++) {
303
                        IFileOpen fileOpen = listFileOpen.get(i);
304
                        fileOpen.pre();
305
                        List<FileFilter> aux = fileOpen.getFileFilter();
306

    
307
                        for (int j = 0; j < aux.size(); j++) {
308
                                FileFilter fileFilter = aux.get(j);
309
                                fileChooser.addChoosableFileFilter(fileFilter);
310
                                if (lastFileFilter!=null && lastFileFilter.getDescription().equals(fileFilter.getDescription())){
311
                                        auxFilter=fileFilter;
312
                                        finded = true;
313
                                }
314
                        }
315
                }
316
                if (finded && (lastFileFilter != null)) {
317
                        fileChooser.setFileFilter(auxFilter);
318
                }
319

    
320
                int result = fileChooser.showOpenDialog(this);
321

    
322
                File[] newFiles = null;
323
                ArrayList<MyFile> toAdd = new ArrayList<MyFile>();
324
                if (result == JFileChooser.APPROVE_OPTION) {
325
                        lastPath = fileChooser.getCurrentDirectory().getAbsolutePath();
326
                        lastFileFilter = fileChooser.getFileFilter();
327
                        newFiles = fileChooser.getSelectedFiles();
328

    
329
                        IFileOpen lastFileOpen = null;
330
                        for (int i = 0; i < listFileOpen.size(); i++) {
331
                                IFileOpen fileOpen = listFileOpen.get(i);
332
                                List<FileFilter> aux = fileOpen.getFileFilter();
333
                                for (int j = 0; j < aux.size(); j++) {
334
                                        if (fileChooser.getFileFilter() == aux.get(j)) {
335
                                                for (int iFile=0; iFile<newFiles.length; iFile++) {
336
                                                        newFiles[iFile] = fileOpen.post(newFiles[iFile]);
337
                                                }
338
                                                lastFileOpen = fileOpen;
339
                                                break;
340
                                        }
341
                                }
342
                        }
343

    
344
                        for (int ind = 0; ind < newFiles.length; ind++) {
345
                                if (newFiles[ind] == null) {
346
                                        break;
347
                                }
348
                                toAdd.add(new MyFile(newFiles[ind], (fileChooser
349
                                                .getFileFilter()), lastFileOpen));
350
                        }
351

    
352
                        return toAdd.toArray();
353
                } else {
354
                        return new Object[0];
355
                }
356
        }
357

    
358
        /*
359
         * (non-Javadoc)
360
         * @see com.iver.cit.gvsig.gui.WizardPanel#execute()
361
         */
362
        public void execute() {
363
                if (getFiles() == null) {
364
                        return;
365
                }
366

    
367
                MapControl mapControl = null;
368
                IWindow[] w = PluginServices.getMDIManager().getAllWindows();
369

    
370
                // Si se est? cargando la capa en el localizador se obtiene el mapcontrol de este
371
                for (int i = 0; i < w.length; i++) {
372
                        if (w[i] instanceof FPanelLocConfig) {
373
                                mapControl = ((FPanelLocConfig) w[i]).getMapCtrl();
374
                                DefaultListModel lstModel = (DefaultListModel) ((FPanelLocConfig) w[i]).getJList().getModel();
375
                                lstModel.clear();
376
                                for (int k = 0; k < getFiles().length; k++) {
377
                                        lstModel.addElement(getFiles()[k].getName());
378
                                }
379
                                for (int j = mapControl.getMapContext().getLayers().getLayersCount() - 1; j >= 0; j--) {
380
                                        FLayer lyr = mapControl.getMapContext().getLayers().getLayer(j);
381
                                        lstModel.addElement(lyr.getName());
382
                                }
383
                        }
384
                }
385

    
386
                // Obtiene la primera vista activa
387
                if (mapControl == null) {
388
                        for (int i = 0; i < w.length; i++) {
389
                                IWindow activeWindow = PluginServices.getMDIManager().getActiveWindow();
390
                                if (w[i] instanceof BaseView && w[i].equals(activeWindow)) {
391
                                        mapControl = ((BaseView) w[i]).getMapControl();
392
                                }
393
                        }
394
                }
395
                // Si no hay ninguna activa obtiene la primera vista aunque no est? activa
396
                if (mapControl == null) {
397
                        for (int i = 0; i < w.length; i++) {
398
                                if (w[i] instanceof BaseView) {
399
                                        mapControl = ((BaseView) w[i]).getMapControl();
400
                                }
401
                        }
402
                }
403

    
404
                //                if (mapControl == null) {
405
                //                        return;
406
                //                }
407

    
408
                Envelope[] rects = new Envelope[getFiles().length];
409
                boolean first = false;
410

    
411
                for (int i = getMyFiles().length - 1; i >= 0; i--) {
412
                        if (mapControl != null
413
                                        && mapControl.getMapContext().getViewPort().getExtent() == null) {
414
                                first = true;
415
                        }
416
                        rects[i] = (getMyFiles()[i]).createLayer(mapControl);
417
                }
418

    
419
                if (first && (rects.length > 1)) {
420
                        Envelope rect = new DefaultEnvelope(rects[0].getMinimum(0),rects[0].getMinimum(1),rects[0].getMaximum(0),rects[0].getMaximum(1));
421
//                        rect.setRect(rects[0]);
422
                        for (int i = 0; i < rects.length; i++) {
423
                                if (rects[i] != null) {
424
                                        rect.add(rects[i]);
425
                                }
426
                        }
427
                        if (mapControl != null) {
428
                                mapControl.getMapContext().getViewPort().setEnvelope(rect);
429
                        }
430
                }
431
                if (mapControl != null) {
432
                        mapControl.getMapContext().endAtomicEvent();// FIXME ??? y el begin
433
                }
434
        }
435

    
436
        public void setTitle(String title) {
437
                getTitledBorder().setTitle(title);
438
        }
439

    
440
        /*
441
         * (non-Javadoc)
442
         * @see com.iver.utiles.listManager.ListManagerListener#getProperties(java.lang.Object)
443
         */
444
        public Object getProperties(Object selected) {
445
                return null;
446
        }
447

    
448

    
449
        /**
450
         * Obtiene la ?ltima ruta seleccionada al a?adir ficheros.
451
         * @return Ruta del ?ltimo fichero seleccionado
452
         */
453
        public static String getLastPath() {
454
                return lastPath;
455
        }
456

    
457
        /**
458
         * Asigna la ?ltima ruta en una selecci?n de ficheros de disco. Es necesario
459
         * poder hacer esta asignaci?n desde fuera de FileOpenWizard ya que este path debe
460
         * ser com?n a toda la aplicaci?n. Hay otros puntos donde se seleccionan ficheros
461
         * de disco.
462
         * @param lastPath Ruta del ?ltimo fichero de disco seleccionado
463
         */
464
        public static void setLastPath(String path) {
465
                lastPath = path;
466
        }
467

    
468
        public DataStoreParameters[] getParameters() {
469
                // TODO Auto-generated method stub
470
                return null;
471
        }
472
}