Statistics
| Revision:

root / trunk / extensions / ext3Dgui / src / org / gvsig / gvsig3dgui / import3D / ControlImport3D.java @ 24995

History | View | Annotate | Download (17.9 KB)

1
package org.gvsig.gvsig3dgui.import3D;
2

    
3
import java.awt.BorderLayout;
4
import java.awt.Component;
5
import java.awt.GridLayout;
6
import java.awt.Point;
7
import java.awt.event.ActionEvent;
8
import java.awt.event.ActionListener;
9
import java.awt.event.MouseEvent;
10
import java.awt.event.MouseListener;
11
import java.awt.geom.Point2D;
12
import java.io.File;
13
import java.io.FileInputStream;
14
import java.io.FileNotFoundException;
15
import java.util.Hashtable;
16
import java.util.Iterator;
17

    
18
import javax.swing.JButton;
19
import javax.swing.JFileChooser;
20
import javax.swing.JLabel;
21
import javax.swing.JOptionPane;
22
import javax.swing.JPanel;
23
import javax.swing.JTextField;
24
import javax.swing.border.TitledBorder;
25
import javax.swing.filechooser.FileFilter;
26

    
27
import org.gvsig.driver.OSGDriver;
28
import org.gvsig.geometries3D.MultiGeometry;
29
import org.gvsig.gpe.osg.OSGParser;
30
import org.gvsig.gui.beans.datainput.DataInputField;
31
import org.gvsig.gvsig3d.cacheservices.OSGCacheService;
32
import org.gvsig.gvsig3d.map3d.MapContext3D;
33
import org.gvsig.gvsig3dgui.view.View3D;
34
import org.gvsig.osgvp.Group;
35
import org.gvsig.osgvp.Vec3;
36
import org.gvsig.osgvp.viewer.Intersections;
37

    
38
import com.iver.ai2.gvsig3d.map3d.layers.Layer3DProps;
39
import com.iver.andami.PluginServices;
40
import com.iver.andami.ui.mdiManager.IWindow;
41
import com.iver.andami.ui.mdiManager.WindowInfo;
42
import com.iver.cit.gvsig.fmap.MapContext;
43
import com.iver.cit.gvsig.fmap.layers.FLayer;
44
import com.iver.cit.gvsig.fmap.layers.FLayers;
45
import com.iver.cit.gvsig.project.documents.view.IProjectView;
46

    
47
/**
48
 * DOCUMENT ME!
49
 * 
50
 * @author
51
 */
52

    
53
public class ControlImport3D extends JPanel implements IWindow, ActionListener,
54
                MouseListener {
55

    
56
        private static final long serialVersionUID = 8557746972654979029L;
57
        private JPanel panelOpcionesObjeto;
58
        private TitledBorder borderOpcionesObjeto;
59
        private JLabel labelLatitud;
60
        private DataInputField textLatitud;
61
        private JLabel labelLongitud;
62
        private DataInputField textLongitud;
63
        private JLabel labelEscaladoX;
64
        private JLabel labelEscaladoY;
65
        private JLabel labelEscaladoZ;
66
        private DataInputField textEscaladoX;
67
        private DataInputField textEscaladoY;
68
        private DataInputField textEscaladoZ;
69
        private JLabel labelRotacionX;
70
        private JLabel labelRotacionY;
71
        private JLabel labelRotacionZ;
72
        private DataInputField textRotacionX;
73
        private DataInputField textRotacionY;
74
        private DataInputField textRotacionZ;
75
        private JPanel panelImportar;
76
        private TitledBorder borderImportar;
77
        private JButton buttonImportar;
78
        private int tipoObjectImport;
79
        private JTextField textFile;
80
        private TitledBorder border;
81
        private JPanel panelPath;
82
        private Hashtable iveExtensionsSupported = null;
83
        private Hashtable osgExtensionsSupported = null;
84
        private String lastPath = null;
85
        private JLabel labelAltura;
86
        private DataInputField textAltura;
87
        private JPanel panelButtons;
88
        private JButton buttonAccept;
89
        private JButton buttonCancel;
90
        private JPanel panelAux;
91
        private JLabel labelX;
92
        private DataInputField textX;
93
        private JLabel labelY;
94
        private DataInputField textY;
95

    
96
        private View3D vista;
97
        private File fileOSG = null;
98
        private FileInputStream fileInput = null;
99
        private Vec3 position = new Vec3(0,0,0);
100

    
101
        public ControlImport3D(int tipoObjeto, View3D vista3D) {
102

    
103
                tipoObjectImport = tipoObjeto;
104
                vista = vista3D;
105

    
106
                this.panelOpcionesObjeto = new JPanel();
107
                this.borderOpcionesObjeto = new TitledBorder(PluginServices.getText(
108
                                this, "Opciones de objeto"));
109
                this.panelOpcionesObjeto.setBorder(this.borderOpcionesObjeto);
110
                this.panelOpcionesObjeto.setLayout(new GridLayout(5, 6, 5, 5));
111

    
112
                this.textLatitud = new DataInputField();
113
                this.labelLatitud = new JLabel(
114
                                PluginServices.getText(this, "Latitude"), JLabel.RIGHT);
115

    
116
                this.textLongitud = new DataInputField();
117
                this.labelLongitud = new JLabel(PluginServices.getText(this,
118
                                "Longitude"), JLabel.RIGHT);
119

    
120
                this.textEscaladoX = new DataInputField();
121
                this.textEscaladoX.setValue("1");
122
                this.textEscaladoY = new DataInputField();
123
                this.textEscaladoY.setValue("1");
124
                this.textEscaladoZ = new DataInputField();
125
                this.textEscaladoZ.setValue("1");
126
                this.labelEscaladoX = new JLabel(PluginServices
127
                                .getText(this, "EscaladoX"), JLabel.RIGHT);
128
                this.labelEscaladoY = new JLabel(PluginServices
129
                                .getText(this, "EscaladoY"), JLabel.RIGHT);
130
                this.labelEscaladoZ = new JLabel(PluginServices
131
                                .getText(this, "EscaladoZ"), JLabel.RIGHT);
132

    
133
                this.textRotacionX = new DataInputField();
134
                this.textRotacionY = new DataInputField();
135
                this.textRotacionZ = new DataInputField();
136
                this.labelRotacionX = new JLabel(PluginServices.getText(this,
137
                                "Rotation_degreesX"), JLabel.RIGHT);
138
                this.labelRotacionY = new JLabel(PluginServices.getText(this,
139
                                "Rotation_degreesY"), JLabel.RIGHT);
140
                this.labelRotacionZ = new JLabel(PluginServices.getText(this,
141
                                "Rotation_degreesZ"), JLabel.RIGHT);
142

    
143
                this.textAltura = new DataInputField();
144
                this.labelAltura = new JLabel(PluginServices.getText(this, "Hight"),
145
                                JLabel.RIGHT);
146

    
147
                this.textX = new DataInputField();
148
                this.labelX = new JLabel(PluginServices.getText(this, "Coordenada X:"),
149
                                JLabel.RIGHT);
150

    
151
                this.textY = new DataInputField();
152
                this.labelY = new JLabel(PluginServices.getText(this, "Coordenada Y:"),
153
                                JLabel.RIGHT);
154

    
155
                this.panelOpcionesObjeto.add(this.labelLatitud);
156
                this.panelOpcionesObjeto.add(this.textLatitud);
157
                this.panelOpcionesObjeto.add(this.labelX);
158
                this.panelOpcionesObjeto.add(this.textX);
159
                this.panelOpcionesObjeto.add(new JPanel());
160
                this.panelOpcionesObjeto.add(new JPanel());
161

    
162
                this.panelOpcionesObjeto.add(this.labelLongitud);
163
                this.panelOpcionesObjeto.add(this.textLongitud);
164
                this.panelOpcionesObjeto.add(this.labelY);
165
                this.panelOpcionesObjeto.add(this.textY);
166
                this.panelOpcionesObjeto.add(new JPanel());
167
                this.panelOpcionesObjeto.add(new JPanel());
168

    
169
                this.panelOpcionesObjeto.add(this.labelAltura);
170
                this.panelOpcionesObjeto.add(this.textAltura);
171
                this.panelOpcionesObjeto.add(new JPanel());
172
                this.panelOpcionesObjeto.add(new JPanel());
173
                this.panelOpcionesObjeto.add(new JPanel());
174
                this.panelOpcionesObjeto.add(new JPanel());
175

    
176
                this.panelOpcionesObjeto.add(this.labelEscaladoX);
177
                this.panelOpcionesObjeto.add(this.textEscaladoX);
178
                this.panelOpcionesObjeto.add(this.labelEscaladoY);
179
                this.panelOpcionesObjeto.add(this.textEscaladoY);
180
                this.panelOpcionesObjeto.add(this.labelEscaladoZ);
181
                this.panelOpcionesObjeto.add(this.textEscaladoZ);
182

    
183
                this.panelOpcionesObjeto.add(this.labelRotacionX);
184
                this.panelOpcionesObjeto.add(this.textRotacionX);
185
                this.panelOpcionesObjeto.add(this.labelRotacionY);
186
                this.panelOpcionesObjeto.add(this.textRotacionY);
187
                this.panelOpcionesObjeto.add(this.labelRotacionZ);
188
                this.panelOpcionesObjeto.add(this.textRotacionZ);
189

    
190
                this.panelImportar = new JPanel();
191
                this.borderImportar = new TitledBorder(PluginServices.getText(this,
192
                                "File"));
193
                this.panelImportar.setBorder(this.borderImportar);
194
                this.panelImportar.setLayout(new GridLayout(1, 1, 5, 5));
195
                this.buttonImportar = new JButton(PluginServices.getText(this,
196
                                "Examine"));
197
                this.panelImportar.add(this.buttonImportar);
198
                this.buttonImportar.addActionListener(this);
199

    
200
                this.panelAux = new JPanel();
201
                this.panelAux.setLayout(new BorderLayout(5, 5));
202
                this.panelAux.add(this.panelOpcionesObjeto, BorderLayout.CENTER);
203
                this.panelAux.add(this.panelImportar, BorderLayout.SOUTH);
204

    
205
                this.panelPath = new JPanel();
206
                this.border = new TitledBorder(PluginServices.getText(this,
207
                                "Ruta_manual"));
208
                this.panelPath.setBorder(this.border);
209
                this.panelPath.setLayout(new GridLayout(1, 1, 5, 5));
210
                this.textFile = new JTextField();
211
                this.panelPath.add(this.textFile);
212

    
213
                this.panelButtons = new JPanel();
214
                this.panelButtons.setLayout(new GridLayout(1, 3, 5, 5));
215
                this.buttonAccept = new JButton(PluginServices.getText(this, "Accept"));
216
                this.buttonAccept.addActionListener(this);
217
                this.buttonCancel = new JButton(PluginServices.getText(this, "Cancel"));
218
                this.buttonCancel.addActionListener(this);
219
                this.panelButtons.add(new JPanel());
220
                this.panelButtons.add(this.buttonAccept);
221
                this.panelButtons.add(this.buttonCancel);
222

    
223
                this.setLayout(new BorderLayout(5, 5));
224
                this.add(this.panelAux, BorderLayout.NORTH);
225
                this.add(this.panelPath, BorderLayout.CENTER);
226
                this.add(this.panelButtons, BorderLayout.SOUTH);
227

    
228
                vista.getCanvas3d().addMouseListener(this);
229

    
230
        }
231

    
232
        /*
233
         * Window properties
234
         * 
235
         * @see com.iver.andami.ui.mdiManager.IWindow#getWindowInfo()
236
         */
237

    
238
        public WindowInfo getWindowInfo() {
239
                WindowInfo m_viewinfo = new WindowInfo();
240
                String cad = null;
241
                // java.awt.Dimension minSize = new java.awt.Dimension();
242
                // minSize.setSize(290, 400);
243

    
244
                if (tipoObjectImport == 0)
245
                        cad = "Importar_Objeto_3D";
246
                else if (tipoObjectImport == 1)
247
                        cad = "Importar_Objeto_3D_Vectorial";
248

    
249
                m_viewinfo.setTitle(PluginServices.getText(this, cad));
250
                m_viewinfo.setHeight(290);
251
                m_viewinfo.setWidth(610);
252
                // m_viewinfo.setMinimumSize(minSize);
253
                return m_viewinfo;
254
        }
255

    
256
        /*
257
         * Buttons events listener
258
         */
259

    
260
        public void actionPerformed(ActionEvent event) {
261
                if (event.getSource() == this.buttonImportar) {
262
                        actionPerformedImport();
263

    
264
                }
265
                if (event.getSource() == this.buttonAccept) {
266
                        actionPerformedAccept();
267

    
268
                }
269
                if (event.getSource() == this.buttonCancel) {
270
                        PluginServices.getMDIManager().closeWindow(this);
271
                }
272
        }
273

    
274
        /*
275
         * Accept button listener
276
         */
277

    
278
        private void actionPerformedAccept() {
279

    
280

    
281
                if (fileOSG == null) {// of the user write the file
282
                        // String fichManual = getJTextFieldFichero();
283
                        if(getJTextFieldFichero().equals("")) {
284
                                JOptionPane.showMessageDialog(null, "Fichero no introducido",
285
                                                "Introduce o selecciona un fichero", JOptionPane.WARNING_MESSAGE);
286
                                return;
287
                        }
288
                        else {
289
                                        fileOSG = new File(getJTextFieldFichero());
290
                                if(!fileOSG.exists()){
291
                                        JOptionPane.showMessageDialog(null, "Fichero no encontrado",
292
                                                        "La ruta no existe", JOptionPane.WARNING_MESSAGE);
293
                                        fileOSG = null;
294
                                        this.setTextFieldFichero();
295
                                        return;
296
                                }
297
                        }
298

    
299
                } else {
300
                        try {
301
                                if(!fileOSG.exists()){
302
                                        JOptionPane.showMessageDialog(null, "Fichero no encontrado",
303
                                                        "La ruta no existe", JOptionPane.WARNING_MESSAGE);
304
                                        return;
305
                                }
306
                                fileInput = new FileInputStream(fileOSG);
307
                                
308
                        } catch (FileNotFoundException e) {
309
                                e.printStackTrace();
310
                        }
311
                }
312

    
313
                OSGDriver _osgDriver = new OSGDriver();
314
                OSGParser parser = new OSGParser("OSG", "OSG File Formats Parser");
315
                parser.parse(_osgDriver, null, fileOSG.toURI());
316
                MultiGeometry root = _osgDriver.getRootFeature();
317
//                Group rootGroup = new Group();
318

    
319
                IProjectView model = vista.getModel();
320
                MapContext mapContext = model.getMapContext();
321
                FLayers layers = mapContext.getLayers();
322
                FLayer[] actives = layers.getActives();
323
                if (actives.length == 1
324
                                && Layer3DProps.getLayer3DProps(actives[0]).getType() == Layer3DProps.layer3DOSG) {
325
                        OSGCacheService osgCache = (OSGCacheService) Layer3DProps.getLayer3DProps(actives[0]).getCacheService();
326
                        Vec3 scale = new Vec3(Double.parseDouble(textEscaladoX.getValue()), Double.parseDouble(textEscaladoY.getValue()), Double.parseDouble(textEscaladoZ.getValue()));
327
                        Vec3 rotation = new Vec3(Double.parseDouble(textRotacionX.getValue()), Double.parseDouble(textRotacionY.getValue()), Double.parseDouble(textRotacionZ.getValue()));
328
                        osgCache.AddGeometryToLayer(root, position,rotation,scale);
329
                }
330
                else {
331
                        JOptionPane.showMessageDialog(null, "Selecciona una capa editable OSG.",
332
                                "Capa no selecionada o capa no v?lida", JOptionPane.WARNING_MESSAGE);
333
                        return;
334
                }
335
                PluginServices.getMDIManager().closeWindow(this);
336
        }
337

    
338
        /*
339
         * Import file button listener
340
         */
341

    
342
        private void actionPerformedImport() {
343
                JFileChooser jfc = new JFileChooser(lastPath);
344
                jfc.removeChoosableFileFilter(jfc.getAcceptAllFileFilter());
345
                iveExtensionsSupported = new Hashtable();
346
                osgExtensionsSupported = new Hashtable();
347
                osgExtensionsSupported.put("osg", new MyFileFilter("osg",
348
                                PluginServices.getText(this, "Ficheros *.osg"), "osg"));
349
                iveExtensionsSupported.put("ive", new MyFileFilter("ive",
350
                                PluginServices.getText(this, "Ficheros *.ive"), "ive"));
351
                iveExtensionsSupported.put("obj", new MyFileFilter("obj",
352
                                PluginServices.getText(this, "Ficheros *.obj"), "obj"));
353
                iveExtensionsSupported.put("dae", new MyFileFilter("dae",
354
                                PluginServices.getText(this, "Ficheros *.dae"), "dae"));
355
                
356
                Iterator iter = osgExtensionsSupported.values().iterator();
357
                
358
                while (iter.hasNext()) {
359
                        jfc.addChoosableFileFilter((FileFilter) iter.next());
360
                }
361

    
362
                iter = iveExtensionsSupported.values().iterator();
363
                while (iter.hasNext()) {
364
                        jfc.addChoosableFileFilter((FileFilter) iter.next());
365
                }
366

    
367
                if (jfc.showOpenDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
368

    
369
                        fileOSG = jfc.getSelectedFile();
370
                        if (fileOSG.exists()) {// file don't exists in the directory.
371

    
372
                                MyFileFilter filter = (MyFileFilter) jfc.getFileFilter();
373
                                fileOSG = filter.normalizeExtension(fileOSG);
374
                                textFile.setText(fileOSG.getAbsolutePath());
375
                        } else {
376
                                JOptionPane.showMessageDialog(null, "El fichero no existe.",
377
                                                "Fichero no encontrado", JOptionPane.WARNING_MESSAGE);
378
                                return;
379
                        }
380
                }// If aprove option.
381
        }
382

    
383
        /*
384
         * Getters and Setters of all TextFields values
385
         */
386

    
387
        public String getJTextFieldFichero() {
388
                return textFile.getText();
389
        }
390
        
391
        public void setTextFieldFichero() {
392
                textFile.setText(null);
393
        }
394
        /*
395
         * All MouseListener methods
396
         * 
397
         */
398
        public void mouseClicked(MouseEvent e) {
399
                // TODO Auto-generated method stub
400
        }
401

    
402
        public void mouseEntered(MouseEvent e) {
403
                // TODO Auto-generated method stub
404

    
405
        }
406

    
407
        public void mouseExited(MouseEvent e) {
408
                // TODO Auto-generated method stub
409

    
410
        }
411

    
412
        public void mousePressed(MouseEvent e) {
413
                // TODO Auto-generated method stub
414
        }
415

    
416
        public void mouseReleased(MouseEvent e) {
417

    
418
                Point pScreen = e.getPoint();
419

    
420
                if (e.getButton() == MouseEvent.BUTTON1) {
421
                        Vec3 intersection = null;
422

    
423
                        // System.err.println("Coordenadas de pantalla " + pScreen.getX() +
424
                        // ","+
425
                        // pScreen.getY());
426

    
427
                        textX.setValue(Double.toString(pScreen.getX()));
428
                        textY.setValue(Double.toString(pScreen.getY()));
429

    
430
                        IProjectView model = vista.getModel();
431

    
432
                        MapContext3D mcontext = (MapContext3D) model.getMapContext();
433
                        mcontext.getCanvas3d();
434

    
435
                        Intersections hits = mcontext.getCanvas3d().getOSGViewer().rayPick(
436
                                        (int) pScreen.getX(), (int) pScreen.getY());
437
                        Point2D pWorld = new Point2D.Double();
438
                        if (hits.containsIntersections()) {
439
                                Vec3 hit = hits.getFirstIntersection().getIntersectionPoint();
440
                                // convert to geo coordinates
441
                                position = hit;
442
                                // System.err.println("Interseccion de osg " + hit.x() + ","+
443
                                // hit.y());
444
                                if (mcontext.getProjection().getAbrev().compareToIgnoreCase(
445
                                                "EPSG:4326") == 0) {
446
                                        Vec3 geoPt = mcontext.getPlanet()
447
                                                        .convertXYZToLatLongHeight(hit);
448
                                        // Swap the coordinates X and Y, because they are invert.
449
                                        intersection = new Vec3(geoPt.y(), geoPt.x(), geoPt.z());
450
                                } else {
451
                                        intersection = hit;
452
                                }
453
                        } else {
454
                                if (mcontext.getProjection().getAbrev().compareToIgnoreCase(
455
                                                "EPSG:4326") == 0) {
456
                                        pWorld.setLocation(360, 120);
457
                                        intersection = new Vec3(360, 120, 0);
458
                                } else {
459
                                        intersection = new Vec3(1e100, 1e100, 0);
460
                                        // pWorld.setLocation(1e100, 1e100);
461
                                }
462
                        }
463

    
464
                        textLatitud.setValue(Double.toString(intersection.x()));
465
                        textLongitud.setValue(Double.toString(intersection.y()));
466
                        textAltura.setValue(Double.toString(intersection.z()));
467

    
468
                }// if e.getButton()
469
        }// if hits
470

    
471
        public Object getWindowProfile() {
472
                return WindowInfo.PROPERTIES_PROFILE;
473
        }
474
        
475

    
476
}
477

    
478

    
479
/**
480
 * 
481
 * @version 14/08/2007
482
 * @author Borja S?nchez Zamorano (borja.sanchez@iver.es)
483
 * 
484
 */
485
class MyFileFilter extends FileFilter {
486

    
487
        private String[] extensiones = new String[1];
488
        private String description;
489
        private boolean dirs = true;
490
        private String info = null;
491

    
492
        public MyFileFilter(String[] ext, String desc) {
493
                extensiones = ext;
494
                description = desc;
495
        }
496

    
497
        public MyFileFilter(String[] ext, String desc, String info) {
498
                extensiones = ext;
499
                description = desc;
500
                this.info = info;
501
        }
502

    
503
        public MyFileFilter(String ext, String desc) {
504
                extensiones[0] = ext;
505
                description = desc;
506
        }
507

    
508
        public MyFileFilter(String ext, String desc, String info) {
509
                extensiones[0] = ext;
510
                description = desc;
511
                this.info = info;
512
        }
513

    
514
        public MyFileFilter(String ext, String desc, boolean dirs) {
515
                extensiones[0] = ext;
516
                description = desc;
517
                this.dirs = dirs;
518
        }
519

    
520
        public MyFileFilter(String ext, String desc, boolean dirs, String info) {
521
                extensiones[0] = ext;
522
                description = desc;
523
                this.dirs = dirs;
524
                this.info = info;
525
        }
526

    
527
        public boolean accept(File f) {
528
                if (f.isDirectory()) {
529
                        if (dirs) {
530
                                return true;
531
                        } else {
532
                                return false;
533
                        }
534
                }
535
                for (int i = 0; i < extensiones.length; i++) {
536
                        if (extensiones[i].equals("")) {
537
                                continue;
538
                        }
539
                        if (getExtensionOfAFile(f).equalsIgnoreCase(extensiones[i])) {
540
                                return true;
541
                        }
542
                }
543

    
544
                return false;
545
        }
546

    
547
        /**
548
         * @see javax.swing.filechooser.FileFilter#getDescription()
549
         */
550
        public String getDescription() {
551
                return description;
552
        }
553

    
554
        public String[] getExtensions() {
555
                return extensiones;
556
        }
557

    
558
        public boolean isDirectory() {
559
                return dirs;
560
        }
561

    
562
        private String getExtensionOfAFile(File file) {
563
                String name;
564
                int dotPos;
565
                name = file.getName();
566
                dotPos = name.lastIndexOf(".");
567
                if (dotPos < 1) {
568
                        return "";
569
                }
570
                return name.substring(dotPos + 1);
571
        }
572

    
573
        public File normalizeExtension(File file) {
574
                String ext = getExtensionOfAFile(file);
575
                if (ext.equals("") || !(this.accept(file))) {
576
                        return new File(file.getAbsolutePath() + "." + extensiones[0]);
577
                }
578
                return file;
579
        }
580

    
581
        public String getInfo() {
582
                return this.info;
583
        }
584

    
585
        public void setInfo(String info) {
586
                this.info = info;
587
        }
588

    
589
}