Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / wizards / FileOpenRasterDialog.java @ 12178

History | View | Annotate | Download (10.5 KB)

1
/*
2
 * Created on 23-abr-2004
3
 *
4
 * To change the template for this generated file go to
5
 * Window>Preferences>Java>Code Generation>Code and Comments
6
 */
7
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
8
 *
9
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
10
 *
11
 * This program is free software; you can redistribute it and/or
12
 * modify it under the terms of the GNU General Public License
13
 * as published by the Free Software Foundation; either version 2
14
 * of the License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
24
 *
25
 * For more information, contact:
26
 *
27
 *  Generalitat Valenciana
28
 *   Conselleria d'Infraestructures i Transport
29
 *   Av. Blasco Ib??ez, 50
30
 *   46010 VALENCIA
31
 *   SPAIN
32
 *
33
 *      +34 963862235
34
 *   gvsig@gva.es
35
 *      www.gvsig.gva.es
36
 *
37
 *    or
38
 *
39
 *   IVER T.I. S.A
40
 *   Salamanca 50
41
 *   46005 Valencia
42
 *   Spain
43
 *
44
 *   +34 963163400
45
 *   dac@iver.es
46
 */
47
package org.gvsig.rastertools.wizards;
48

    
49
import java.awt.BorderLayout;
50
import java.awt.Component;
51
import java.awt.geom.Rectangle2D;
52
import java.io.File;
53

    
54
import javax.swing.JFileChooser;
55
import javax.swing.JOptionPane;
56
import javax.swing.filechooser.FileFilter;
57

    
58
import org.cresques.cts.IProjection;
59
import org.gvsig.fmap.layers.FLyrRasterSE;
60
import org.gvsig.rastertools.RasterModule;
61

    
62
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
63
import com.iver.andami.PluginServices;
64
import com.iver.andami.ui.mdiManager.IWindow;
65
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
66
import com.iver.cit.gvsig.exceptions.layers.LoadLayerException;
67
import com.iver.cit.gvsig.fmap.MapControl;
68
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
69
import com.iver.cit.gvsig.fmap.layers.FLayer;
70
import com.iver.cit.gvsig.gui.WizardPanel;
71
import com.iver.cit.gvsig.gui.panels.CRSSelectPanel;
72
import com.iver.cit.gvsig.project.documents.gui.FOpenDialog;
73
import com.iver.cit.gvsig.project.documents.gui.ListManagerSkin;
74
import com.iver.cit.gvsig.project.documents.view.gui.View;
75
import com.iver.utiles.listManager.ListManagerListener;
76

    
77
public class FileOpenRasterDialog extends WizardPanel implements ListManagerListener {
78
        private static final long serialVersionUID = 335310147513197564L;
79
//        private static DriverFileFilter lastFileFilter = null;
80
        private javax.swing.JPanel jPanel2 = null;
81
        private JFileChooser fileChooser;
82
        //private Class[] driverClasses;
83
        private String title = PluginServices.getText(this,"Raster");
84
        private CRSSelectPanel jPanelProj = null;
85
        private ListManagerSkin listManagerDemoSkin = null;
86
        private boolean proj;
87

    
88
        public FileOpenRasterDialog() {
89
                init(/*new Class[] {GenericRasterDriver.class }*/null, true);
90
        }
91

    
92
        public void initWizard() {
93
                setTabName("Raster");
94
                init(/*new Class[] {GenericRasterDriver.class }*/null, true);
95
        }
96

    
97
        public void execute() {
98
                if (getFiles() == null)
99
                        return;
100

    
101
                MapControl mapControl = null;
102
                IWindow[] w = PluginServices.getMDIManager().getAllWindows();
103
                //Obtiene la primera vista activa
104
                for(int i = 0 ; i < w.length; i ++){
105
                        IWindow activeWindow = PluginServices.getMDIManager().getActiveWindow();
106
                        if(w[i] instanceof View && w[i].equals(activeWindow))
107
                                mapControl = ((View)w[i]).getMapControl();
108
                }
109
                //Si no hay ninguna activa obtiene la primera vista aunque no est? activa
110
                if(mapControl == null) {
111
                        for(int i = 0 ; i < w.length; i ++){
112
                                if(w[i] instanceof View)
113
                                        mapControl = ((View)w[i]).getMapControl();
114
                        }
115
                }
116

    
117
                if(mapControl == null)
118
                        return;
119

    
120
                FLayer lyr = null;
121
                IProjection proj = FOpenDialog.getLastProjection();
122

    
123
                Rectangle2D[] rects = new Rectangle2D[getFiles().length];
124
                boolean first = false;
125

    
126
                mapControl.getMapContext().beginAtomicEvent();
127

    
128
                for (int iFile = 0; iFile < getFiles().length; iFile++) {
129
                        File fich = getFiles()[iFile];
130
                        String layerName = fich.getName();
131
                        try{
132

    
133
                                try {
134
                                        lyr = FLyrRasterSE.createLayer(layerName, fich, proj);
135
                                } catch (LoadLayerException e) {
136
                                        JOptionPane.showMessageDialog(
137
                                                        (Component) PluginServices.getMainFrame(), PluginServices.getText(
138
                                                                        this, "coordenadas_erroneas"));
139
                                }
140

    
141
                                if (lyr != null) {
142
                                        lyr.setVisible(true);
143
                                        if (mapControl.getMapContext().getViewPort().getExtent() == null)
144
                                                first = true;
145
                                        //checkProjection(lyr, mapControl.getViewPort());
146
                                        mapControl.getMapContext().getLayers().addLayer(lyr);
147
                                        rects[iFile] = lyr.getFullExtent();
148
                                }
149
                        }catch(ReadDriverException e){
150

    
151
                        }catch(ExpansionFileReadException e1){
152

    
153
                        }
154
                }
155

    
156
                if (rects.length > 1) {
157
                        Rectangle2D rect = new Rectangle2D.Double();
158
                        rect.setRect(rects[0]);
159

    
160
                        if (first) {
161
                                for (int i = 0; i < rects.length; i++) {
162
                                        if (rects[i] != null) {
163
                                                rect.add(rects[i]);
164
                                        }
165
                                }
166
                                mapControl.getMapContext().getViewPort().setExtent(rect);
167
                        }
168
                }
169
                mapControl.getMapContext().endAtomicEvent();
170
        }
171

    
172
        public FLayer getLayer() {
173
                return null;
174
        }
175

    
176
        /**
177
         * @param driverClasses2
178
         * @param b
179
         */
180
        private void init(Class[] driverClasses, boolean proj) {
181
                this.proj = proj;
182

    
183
                initialize();
184
        }
185

    
186
        /**
187
         * Creates a new FOpenDialog object.
188
         *
189
         * @param view Vista que vamos a refrescar
190
         * @param mapControl MapControl que recibe la capa (te puede interesar
191
         *        a?adirla al principal o al Overview.
192
         */
193
        public FileOpenRasterDialog(Class[] driverClasses, boolean proj) {
194
                init(driverClasses, proj);
195
        }
196

    
197
        public FileOpenRasterDialog(Class[] driverClasses, boolean proj, String title) {
198
                this.title= title;
199
                init(driverClasses, proj);
200
        }
201

    
202
        /**
203
         * This method initializes this
204
         */
205
        private void initialize() {
206
                this.setLayout(new BorderLayout());
207
                this.setSize(514, 280);
208
                this.setLayout(null);
209
                this.add(getJPanel2(), null);
210
        }
211

    
212
        /**
213
         * DOCUMENT ME!
214
         *
215
         * @return DOCUMENT ME!
216
         */
217
        public File[] getFiles() {
218
                MyFile[] files = (MyFile[]) getListManagerDemoSkin().getListManager().getListModel().getObjects().toArray(new MyFile[0]);
219
                File[] ret = new File[files.length];
220
                int pos=files.length-1;
221
                for (int i=0; i < files.length; i++){
222
                        ret[pos] = files[i].f;
223
                        pos--;
224
                }
225
                return ret;
226
        }
227

    
228
        public boolean accept(File f){
229
                return fileChooser.getFileFilter().accept(f);
230
        }
231

    
232
        /**
233
         * This method initializes jPanel2
234
         *
235
         * @return javax.swing.JPanel
236
         */
237
        private javax.swing.JPanel getJPanel2() {
238
                if (jPanel2 == null) {
239
                        javax.swing.border.TitledBorder titledBorder1 = javax.swing.BorderFactory.createTitledBorder(null,
240
                                PluginServices.getText(this,"Seleccionar_fichero"),
241
                                javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
242
                                javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null);
243
                        jPanel2 = new javax.swing.JPanel();
244
                        jPanel2.setLayout(null);
245
                        jPanel2.setBorder(titledBorder1);
246
                        jPanel2.setPreferredSize(new java.awt.Dimension(380,200));
247
                        jPanel2.setBounds(2, 2, 506, 472);
248
                        titledBorder1.setTitle(this.title);
249
                        if (proj) {
250
                                jPanel2.add(getJPanelProj(), null);
251
                        }
252
                        jPanel2.add(getListManagerDemoSkin(), null);
253
                }
254

    
255
                return jPanel2;
256
        }
257

    
258
        public String[] getDriverNames(){
259
                MyFile[] files = (MyFile[]) getListManagerDemoSkin().getListManager().getListModel().getObjects().toArray(new MyFile[0]);
260
                String[] ret = new String[files.length];
261
                int pos=files.length-1;
262

    
263
                for (int i=0; i < files.length; i++)
264
                {
265
                        ret[pos] = files[i].getDriverName();
266
                        pos--;
267
                }
268
                return ret;
269
        }
270

    
271
        public class DriverFileFilter extends FileFilter {
272

    
273
                public DriverFileFilter() {
274
                }
275

    
276
                /**
277
                 * @see javax.swing.filechooser.FileFilter#accept(java.io.File)
278
                 */
279
                public synchronized boolean accept(File f) {
280
                        if (f.isDirectory())
281
                                return true;
282
                        return FLyrRasterSE.isFileAccepted(f);
283
                }
284

    
285
                /**
286
                 * @see javax.swing.filechooser.FileFilter#getDescription()
287
                 */
288
                public String getDescription() {
289
                        return "gvSIG Raster Driver";
290
                }
291
        }
292

    
293
        /**
294
         * This method initializes jPanel
295
         *
296
         * @return javax.swing.JPanel
297
         */
298
        private CRSSelectPanel getJPanelProj() {
299
                if (jPanelProj == null) {
300
                        IProjection proj = CRSFactory.getCRS("EPSG:23030");
301
                        if (PluginServices.getMainFrame() != null) {
302
                                proj = FOpenDialog.getLastProjection();
303
                        }
304

    
305
                        jPanelProj = CRSSelectPanel.getPanel(proj);
306
                        jPanelProj.setTransPanelActive(true);
307
                        jPanelProj.setBounds(11, 400, 448, 35);
308
                        jPanelProj.setPreferredSize(new java.awt.Dimension(448,35));
309
                        jPanelProj.addActionListener(new java.awt.event.ActionListener() {
310
                                public void actionPerformed(java.awt.event.ActionEvent e) {
311
                                                        if (jPanelProj.isOkPressed()) {
312
                                                                FOpenDialog.setLastProjection(jPanelProj.getCurProj());
313
                                                        }
314
                                }
315
                        });
316

    
317
                }
318
                return jPanelProj;
319
        }
320

    
321
        public class MyFile {
322
                private File f;
323
                private String driverName;
324

    
325
                public MyFile(File f, String driverName){
326
                        this.f = f;
327
                        this.driverName = driverName;
328
                }
329

    
330
                public String toString(){
331
                        return f.getName();
332
                }
333

    
334
                public String getDriverName(){
335
                        return driverName;
336
                }
337
        }
338

    
339
        /**
340
         * This method initializes listManagerDemoSkin
341
         *
342
         * @return com.iver.utiles.listManager.ListManagerDemoSkin
343
         */
344
        private ListManagerSkin getListManagerDemoSkin() {
345
                if (listManagerDemoSkin == null) {
346
                        listManagerDemoSkin = new ListManagerSkin(false);
347
                        listManagerDemoSkin.setBounds(11, 21, 491, 363);
348
                        listManagerDemoSkin.getListManager().setListener(this);
349
                }
350
                return listManagerDemoSkin;
351
        }
352

    
353
        public Object getProperties(Object selected) {
354
                return null;
355
        }
356

    
357
        public Object[] addObjects() {
358
                this.callStateChanged(true);
359
                FLyrRasterSE.class.getClass();
360
                fileChooser = new JFileChooser(RasterModule.getPath());
361
                fileChooser.setMultiSelectionEnabled(true);
362
                fileChooser.setAcceptAllFileFilterUsed(false);
363

    
364
                DriverFileFilter element = new DriverFileFilter();
365
                fileChooser.addChoosableFileFilter(element);
366

    
367
                int result = fileChooser.showOpenDialog(FileOpenRasterDialog.this);
368

    
369
                File[] newFiles = null;
370
                MyFile[] toAdd;
371
                if (result == JFileChooser.APPROVE_OPTION) {
372
                        // lastFileFilter = (DriverFileFilter) fileChooser.getFileFilter();
373
                        RasterModule.setPath(fileChooser.getCurrentDirectory().getAbsolutePath());
374
                        newFiles = fileChooser.getSelectedFiles();
375
                        toAdd = new MyFile[newFiles.length];
376
                        for (int ind = 0; ind < newFiles.length; ind++) {
377
                                String driverName = ((DriverFileFilter) fileChooser.getFileFilter()).getDescription();
378
                                toAdd[ind] = new MyFile(newFiles[ind], driverName);
379
                        }
380

    
381
                        return toAdd;
382
                } else
383
                        return new Object[0];
384
        }
385
}