Statistics
| Revision:

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

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.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.JPanel;
33
import javax.swing.border.TitledBorder;
34
import javax.swing.filechooser.FileFilter;
35

    
36
import org.cresques.cts.IProjection;
37
import org.gvsig.gui.beans.swing.JFileChooser;
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.exceptions.layers.LoadLayerException;
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 String OPEN_LAYER_FILE_CHOOSER_ID = "OPEN_LAYER_FILE_CHOOSER_ID";
64
        private static final long serialVersionUID = 335310147513197564L;
65
        private static String     lastPath        = null;
66
        private JPanel            jPanel2         = null;
67
        private JFileChooser      fileChooser     = null;
68
        private CRSSelectPanel    jPanelProj      = null;
69
        private ListManagerSkin   listManagerSkin = null;
70
        private boolean           projection      = false;
71
        static private FileFilter lastFileFilter  = null;
72
        private TitledBorder      titledBorder    = null;
73

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

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

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

    
106
                Iterator iterator = extensionPoint.keySet().iterator();
107
                while (iterator.hasNext()) {
108
                        String key = (String) iterator.next();
109
                        Object obj = extensionPoint.get(key);
110
                        if (obj instanceof Class) {
111
                                try {
112
                                        obj = ((Class) obj).getConstructor(null).newInstance(null);
113
                                } catch (Exception e) {
114
                                        e.printStackTrace();
115
                                }
116
                        }
117
                        if (obj instanceof IFileOpen) {
118
                                listFileOpen.add((IFileOpen) 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].getFile();
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, "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
        /**
272
         * This method initializes listManagerDemoSkin
273
         * @return ListManagerSkin
274
         */
275
        private ListManagerSkin getListManagerSkin() {
276
                if (listManagerSkin == null) {
277
                        listManagerSkin = new ListManagerSkin(false);
278
                        listManagerSkin.setBounds(11, 21, 491, 363);
279
                        listManagerSkin.getListManager().setListener(this);
280
                }
281
                return listManagerSkin;
282
        }
283

    
284
        /*
285
         * (non-Javadoc)
286
         * @see com.iver.utiles.listManager.ListManagerListener#addObjects()
287
         */
288
        public Object[] addObjects() {
289
                this.callStateChanged(true);
290
                fileChooser = new JFileChooser(OPEN_LAYER_FILE_CHOOSER_ID, lastPath);
291
                fileChooser.setMultiSelectionEnabled(true);
292
                fileChooser.setAcceptAllFileFilterUsed(false);
293

    
294
                boolean finded = false;
295
                FileFilter auxFilter=null;
296
                for (int i = 0; i < listFileOpen.size(); i++) {
297
                        IFileOpen fileOpen = listFileOpen.get(i);
298
                        fileOpen.pre();
299
                        ArrayList<FileFilter> aux = fileOpen.getFileFilter();
300

    
301
                        for (int j = 0; j < aux.size(); j++) {
302
                                FileFilter fileFilter = aux.get(j);
303
                                fileChooser.addChoosableFileFilter(fileFilter);
304
                                if (lastFileFilter!=null && lastFileFilter.getDescription().equals(fileFilter.getDescription())){
305
                                        auxFilter=fileFilter;
306
                                        finded = true;
307
                                }
308
                        }
309
                }
310
                if (finded && (lastFileFilter != null))
311
                        fileChooser.setFileFilter(auxFilter);
312

    
313
                int result = fileChooser.showOpenDialog(this);
314

    
315
                File[] newFiles = null;
316
                ArrayList<MyFile> toAdd = new ArrayList<MyFile>();
317
                if (result == JFileChooser.APPROVE_OPTION) {
318
                        lastPath = fileChooser.getCurrentDirectory().getAbsolutePath();
319
                        lastFileFilter = (FileFilter) fileChooser.getFileFilter();
320
                        newFiles = fileChooser.getSelectedFiles();
321

    
322
                        IFileOpen lastFileOpen = null;
323
                        for (int i = 0; i < listFileOpen.size(); i++) {
324
                                IFileOpen fileOpen = listFileOpen.get(i);
325
                                ArrayList<FileFilter> aux = fileOpen.getFileFilter();
326
                                for (int j = 0; j < aux.size(); j++) {
327
                                        if (fileChooser.getFileFilter() == aux.get(j)) {
328
                                                for (int iFile = 0; iFile < newFiles.length; iFile++) {
329
                                                        try {
330
                                                                newFiles[iFile] = fileOpen.post(newFiles[iFile]);
331
                                                        } catch (LoadLayerException e) {
332
                                                                newFiles[iFile] = null;
333
                                                        }
334
                                                }
335
                                                lastFileOpen = fileOpen;
336
                                                break;
337
                                        }
338
                                }
339
                        }
340

    
341
                        for (int ind = 0; ind < newFiles.length; ind++) {
342
                                if (newFiles[ind] == null)
343
                                        continue;
344
                                String driverName = ((FileFilter) fileChooser.getFileFilter()).getDescription();
345
                                toAdd.add(new MyFile(newFiles[ind], driverName, 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

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

    
395
                if (mapControl == null)
396
                        return;
397

    
398
                Rectangle2D[] rects = new Rectangle2D[getFiles().length];
399
                boolean first = false;
400

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

    
407
                if (first && (rects.length > 1)) {
408
                        Rectangle2D rect = new Rectangle2D.Double();
409
                        rect.setRect(rects[0]);
410
                        for (int i = 0; i < rects.length; i++)
411
                                if (rects[i] != null)
412
                                        rect.add(rects[i]);
413
                        mapControl.getMapContext().getViewPort().setExtent(rect);
414
                }
415
                mapControl.getMapContext().endAtomicEvent();
416
        }
417

    
418
        public void setTitle(String title) {
419
                getTitledBorder().setTitle(title);
420
        }
421

    
422
        /*
423
         * (non-Javadoc)
424
         * @see com.iver.utiles.listManager.ListManagerListener#getProperties(java.lang.Object)
425
         */
426
        public Object getProperties(Object selected) {
427
                return null;
428
        }
429

    
430
        /*
431
         * (non-Javadoc)
432
         * @see com.iver.cit.gvsig.gui.WizardPanel#getLayer()
433
         */
434
        public FLayer getLayer() {
435
                return null;
436
        }
437

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

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