Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extRasterTools-SE / src / org / gvsig / raster / gui / wizards / FileOpenWizard.java @ 31496

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 org.gvsig.raster.gui.wizards;
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.andami.PluginServices;
38
import org.gvsig.andami.ui.mdiManager.IWindow;
39
import org.gvsig.app.addlayer.AddLayerDialog;
40
import org.gvsig.app.gui.WizardPanel;
41
import org.gvsig.app.gui.panels.CRSSelectPanel;
42
import org.gvsig.app.project.documents.gui.ListManagerSkin;
43
import org.gvsig.app.project.documents.view.gui.AbstractViewPanel;
44
import org.gvsig.app.project.documents.view.gui.FPanelLocConfig;
45
import org.gvsig.fmap.crs.CRSFactory;
46
import org.gvsig.fmap.dal.DataStoreParameters;
47
import org.gvsig.fmap.geom.GeometryLocator;
48
import org.gvsig.fmap.geom.GeometryManager;
49
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
50
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
51
import org.gvsig.fmap.geom.primitive.Envelope;
52
import org.gvsig.fmap.mapcontext.layers.FLayer;
53
import org.gvsig.fmap.mapcontrol.MapControl;
54
import org.gvsig.gui.beans.swing.JFileChooser;
55
import org.gvsig.tools.ToolsLocator;
56
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
57
import org.gvsig.tools.extensionpoint.ExtensionPoint.Extension;
58
import org.gvsig.utils.listManager.ListManagerListener;
59
import org.slf4j.Logger;
60
import org.slf4j.LoggerFactory;
61

    
62

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

    
83
        /**
84
         * Lista de manejadores de ficheros (extensiones)
85
         */
86
        private ArrayList<IFileOpen> listFileOpen = new ArrayList<IFileOpen>();
87

    
88
        /**
89
         * Construye un FileOpenWizard usando la extension por defecto
90
         * 'FileExtendingOpenDialog'
91
         */
92
        public FileOpenWizard() {
93
                this("FileExtendingOpenDialog");
94
        }
95

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

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

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

    
141
        /**
142
         * Creates a new FileOpenWizard object.
143
         * @param driverClasses
144
         */
145
        public FileOpenWizard(IFileOpen[] driverClasses) {
146
                init(driverClasses, true);
147
        }
148

    
149
        /**
150
         * Creates a new FileOpenWizard object.
151
         * @param driverClasses
152
         * @param proj
153
         * @param title
154
         */
155
        public FileOpenWizard(IFileOpen[] driverClasses, boolean proj, String title) {
156
                setTitle(title);
157
                init(driverClasses, proj);
158
        }
159

    
160
        /**
161
         * @param driverClasses2
162
         * @param b
163
         */
164
        private void init(IFileOpen[] driverClasses, boolean projection) {
165
                this.projection = projection;
166

    
167
                if (driverClasses != null)
168
                        for (int i = 0; i < driverClasses.length; i++)
169
                                listFileOpen.add(driverClasses[i]);
170

    
171
                if (lastPath == null) {
172
                        Preferences prefs = Preferences.userRoot().node("gvsig.foldering");
173
                        lastPath = prefs.get("DataFolder", null);
174
                }
175

    
176
                initialize();
177
        }
178

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

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

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

    
208
        public MyFile[] getMyFiles() {
209
                return (MyFile[]) getListManagerSkin().getListManager().getListModel().getObjects().toArray(new MyFile[0]);
210
        }
211

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

    
228
                return jPanel2;
229
        }
230

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

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

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

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

    
261
                        jPanelProj = CRSSelectPanel.getPanel(proj);
262
                        jPanelProj.setTransPanelActive(true);
263
                        jPanelProj.setBounds(11, 400, 448, 35);
264
                        jPanelProj.setPreferredSize(new Dimension(448, 35));
265
                        jPanelProj.addActionListener(new ActionListener() {
266
                                public void actionPerformed(ActionEvent e) {
267
                                        if (jPanelProj.isOkPressed())
268
                                                AddLayerDialog.setLastProjection(jPanelProj.getCurProj());
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
                int result = fileChooser.showOpenDialog(this);
320

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

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

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

    
348
                        return toAdd.toArray();
349
                } else
350
                        return new Object[0];
351
        }
352

    
353
        /*
354
         * (non-Javadoc)
355
         * @see com.iver.cit.gvsig.gui.WizardPanel#execute()
356
         */
357
        public void execute() {
358
                if (getFiles() == null)
359
                        return;
360

    
361
                MapControl mapControl = null;
362
                IWindow[] w = PluginServices.getMDIManager().getAllWindows();
363

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

    
378
                // Obtiene la primera vista activa
379
                if (mapControl == null)
380
                        for (int i = 0; i < w.length; i++) {
381
                                IWindow activeWindow = PluginServices.getMDIManager().getActiveWindow();
382
                                if (w[i] instanceof AbstractViewPanel && w[i].equals(activeWindow))
383
                                        mapControl = ((AbstractViewPanel) w[i]).getMapControl();
384
                        }
385
                // Si no hay ninguna activa obtiene la primera vista aunque no est? activa
386
                if (mapControl == null)
387
                        for (int i = 0; i < w.length; i++)
388
                                if (w[i] instanceof AbstractViewPanel)
389
                                        mapControl = ((AbstractViewPanel) w[i]).getMapControl();
390

    
391
                //                if (mapControl == null) {
392
                //                        return;
393
                //                }
394

    
395
                Envelope[] rects = new Envelope[getFiles().length];
396
                boolean first = false;
397

    
398
                for (int i = getMyFiles().length - 1; i >= 0; i--) {
399
                        if (mapControl != null
400
                                        && mapControl.getMapContext().getViewPort().getExtent() == null)
401
                                first = true;
402
                        rects[i] = (getMyFiles()[i]).createLayer(mapControl);
403
                }
404

    
405
                if (first && (rects.length > 1)) {
406
                        Envelope rect;
407
                        try {
408
                                rect = geomManager.createEnvelope(
409
                                                rects[0].getMinimum(0), rects[0].getMinimum(1), rects[0]
410
                                                                .getMaximum(0), rects[0].getMaximum(1),
411
                                                                SUBTYPES.GEOM2D);
412
//                                rect.setRect(rects[0]);
413
                                for (int i = 0; i < rects.length; i++)
414
                                        if (rects[i] != null)
415
                                                rect.add(rects[i]);
416
                                if (mapControl != null)
417
                                        mapControl.getMapContext().getViewPort().setEnvelope(rect);
418
                        } catch (CreateEnvelopeException e) {
419
                                logger.error("Error creating the envelope", e);
420
                        }
421
                }
422
                if (mapControl != null)
423
                        mapControl.getMapContext().endAtomicEvent();// FIXME ??? y el begin
424
        }
425

    
426
        public void setTitle(String title) {
427
                getTitledBorder().setTitle(title);
428
        }
429

    
430
        /*
431
         * (non-Javadoc)
432
         * @see com.iver.utiles.listManager.ListManagerListener#getProperties(java.lang.Object)
433
         */
434
        public Object getProperties(Object selected) {
435
                return null;
436
        }
437

    
438

    
439
        /**
440
         * Obtiene la ?ltima ruta seleccionada al a?adir ficheros.
441
         * @return Ruta del ?ltimo fichero seleccionado
442
         */
443
        public static String getLastPath() {
444
                return lastPath;
445
        }
446

    
447
        /**
448
         * Asigna la ?ltima ruta en una selecci?n de ficheros de disco. Es necesario
449
         * poder hacer esta asignaci?n desde fuera de FileOpenWizard ya que este path debe
450
         * ser com?n a toda la aplicaci?n. Hay otros puntos donde se seleccionan ficheros
451
         * de disco.
452
         * @param lastPath Ruta del ?ltimo fichero de disco seleccionado
453
         */
454
        public static void setLastPath(String path) {
455
                lastPath = path;
456
        }
457

    
458
        public DataStoreParameters[] getParameters() {
459
                // TODO Auto-generated method stub
460
                return null;
461
        }
462

    
463
        @Override
464
        public void close() {
465
                // Nothing to do
466
        }
467
}