Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extAnimationGUI / src / com / iver / ai2 / animationgui / gui / AnimationContol3D.java @ 23594

History | View | Annotate | Download (17.2 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.ai2.animationgui.gui;
20

    
21
import java.awt.Checkbox;
22
import java.awt.GridBagConstraints;
23
import java.awt.Insets;
24
import java.awt.event.ActionEvent;
25
import java.awt.event.ActionListener;
26
import java.awt.event.FocusEvent;
27
import java.awt.event.FocusListener;
28
import java.awt.event.ItemEvent;
29
import java.awt.event.ItemListener;
30
import java.awt.event.KeyEvent;
31
import java.awt.event.KeyListener;
32
import java.net.URL;
33
import java.util.ArrayList;
34
import java.util.Iterator;
35
import java.util.List;
36

    
37
import javax.swing.ImageIcon;
38
import javax.swing.JButton;
39
import javax.swing.JComboBox;
40
import javax.swing.JLabel;
41
import javax.swing.JOptionPane;
42
import javax.swing.JPanel;
43
import javax.swing.JTextField;
44

    
45
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
46
import org.gvsig.gvsig3dgui.view.View3D;
47
import org.gvsig.osgvp.planets.PlanetViewer;
48
import org.gvsig.osgvp.viewer.IViewerContainer;
49

    
50
import com.iver.ai2.gvsig3d.resources.ResourcesFactory;
51
import com.iver.andami.PluginServices;
52
import com.iver.cit.gvsig.animation.AnimationContainer;
53
import com.iver.cit.gvsig.animation.AnimationPlayer;
54
import com.iver.cit.gvsig.animation.animatedObject.IAnimatedObject;
55
import com.iver.cit.gvsig.animation.traks.IAnimationTrack;
56
import com.iver.cit.gvsig.project.documents.view.gui.BaseView;
57

    
58
/**
59
 * @author
60
 * @since 1.1
61
 * 
62
 * Class to create and to process the event's panel animation.
63
 */
64

    
65
public class AnimationContol3D extends GridBagLayoutPanel implements
66
                ActionListener, KeyListener, ItemListener, FocusListener {
67

    
68
        /**
69
         * default static final serial version ID
70
         */
71
        private static final long serialVersionUID = 1L;
72

    
73
        private int operation; // Used in choice's functions
74

    
75
        private JLabel label = null;
76

    
77
        private JLabel label2 = null;
78

    
79
        private JTextField textosegs = null;
80

    
81
        private JComboBox choice = null;
82

    
83
        private JButton buttonIcon = null;
84

    
85
        private JButton buttonIcon1 = null;
86

    
87
        private JButton buttonIcon2 = null;
88

    
89
        private JButton buttonIcon3 = null;
90

    
91
        private ImageIcon image = null;
92

    
93
        private ImageIcon image1 = null;
94

    
95
        private ImageIcon image2 = null;
96

    
97
        private ImageIcon image3 = null;
98

    
99
        private Checkbox checkbox = null;
100

    
101
        private String buttonPath;
102

    
103
        private double globalTime;
104

    
105
        private AnimationPlayer animationPlayer;
106

    
107
        private int animationMode;
108

    
109
        private boolean playFlag = false;
110

    
111
        private boolean pauseFlag = false;
112

    
113
        private StateUpdate runnable;
114

    
115
        private ArrayList<WindowClosedListener> actionCommandListeners = new ArrayList<WindowClosedListener>();
116

    
117
        //private ProjectAnimationDocument projectAnimationDocument;
118
        
119
        private List<BaseView> viewListAdded =  new ArrayList<BaseView>();
120

    
121

    
122
        /**
123
         * Default constructor.
124
         * 
125
         * @param control
126
         */
127
        public AnimationContol3D(AnimationPlayer animationPlayer) {
128

    
129
        //        this.projectAnimationDocument = projectAnimationDocument;
130
                
131
                
132
                this.animationPlayer = animationPlayer;
133
                this.animationPlayer.setAnimationPlayerState(AnimationPlayer.ANIMATION_INITIAL);
134

    
135
                // Setting up the button path
136
                String oldPath = ResourcesFactory.getExtPath();// Save the path.
137
                ResourcesFactory
138
                                .setExtPath("/gvSIG/extensiones/com.iver.ai2.animationgui.gui/images/reproductoranim/");// my
139
                // new
140
                // path
141
                ResourcesFactory.setExtPath("/images/reproductoranim/");// my new path
142
                buttonPath = ResourcesFactory.getResourcesPath();
143
                URL path;
144
                path = this.getClass().getClassLoader().getResource("images/reproductoranim/");
145
                buttonPath = path.getPath(); 
146
                ResourcesFactory.setExtPath(oldPath);// Restore the old path.
147
                System.out.println(oldPath);
148
                System.out.println(buttonPath);
149

    
150
                initialize();
151
        }
152
        
153
        
154
        private void updateAnimatedObjects(AnimationPlayer animationPlayer) {
155
                IViewerContainer canvas = null;
156
                AnimationContainer ac = animationPlayer.getAnimationContainer();
157
                List<IAnimationTrack> atl = ac.getAnimationTrackList();
158
                for (Iterator<IAnimationTrack> iterator = atl.iterator(); iterator.hasNext();) {
159
                        IAnimationTrack track = (IAnimationTrack) iterator.next();
160
                        IAnimatedObject animatedObject = (IAnimatedObject) track.getAnimatedObject();
161
                        try {
162
                                //if persistence is loaded, the canvas is null in this step
163
                                canvas = (IViewerContainer) animatedObject.getAnimatedObject("canvas");
164
                        }
165
                        catch (Exception e) {
166
                                //if canvas is null, do nothing
167
                        }
168
                        
169
                        if (canvas == null) {
170
                                try {
171
                                                //Getting the name of the view
172
                                                View3D view = (View3D) animatedObject.getAnimatedObject("view");
173
                                                //Getting the canvas and insert this canvas in the animated object
174
                                                animatedObject.addAnimatedObject("canvas", view.getCanvas3d());
175
                                }
176
                                catch(Exception e) {
177
                                        //if canvas is null, do nothing. The view is 2D
178
                                }
179
                        }
180
                }
181
                
182
        }
183

    
184
        /*
185
         * Panel creation.
186
         * 
187
         */
188
        private void initialize() {
189
                // Bag where have declared the characteristic of a Component in the
190
                // panel.
191
                GridBagConstraints c;
192

    
193
                /*
194
                 * gridx: column position in grid gridy: row position in grid anchor:
195
                 * bottom of space
196
                 */
197

    
198
                // Creation of four buttons: play, stop, pause y record.
199
                c = new GridBagConstraints();
200
                c.gridx = 0;
201
                c.gridy = 0;
202
                c.insets = new Insets(2, 5, 2, 2);
203

    
204
                this.add(getPlayButton(), c);
205

    
206
                c = new GridBagConstraints();
207
                c.gridx = 1;
208
                c.gridy = 0;
209
                c.insets = new Insets(2, 2, 2, 2);
210
                this.add(getPauseButton(), c);
211

    
212
                c = new GridBagConstraints();
213
                c.gridx = 2;
214
                c.gridy = 0;
215
                c.insets = new Insets(2, 2, 2, 2);
216
                this.add(getStopButton(), c);
217

    
218
                c = new GridBagConstraints();
219
                c.gridx = 3;
220
                c.gridy = 0;
221
                c.gridwidth = 2;
222
                c.fill = GridBagConstraints.BOTH;
223
                c.insets = new Insets(2, 2, 2, 5);
224
                this.add(getRecButton(), c);
225

    
226
                // Space between rows.
227
                c = new GridBagConstraints();
228
                c.insets = new Insets(2, 5, 2, 2);
229
                c.gridx = 1;
230
                c.gridy = 1;
231
                c.anchor = GridBagConstraints.WEST;
232
                this.add(getLabelDuration(), c);
233

    
234
                // textField to introduce the duration of the movie in seconds or
235
                // frames.
236
                c = new GridBagConstraints();
237
                c.insets = new Insets(2, 2, 2, 5);
238
                c.gridx = 2;
239
                c.gridy = 1;
240
                c.gridwidth = 1;
241
                c.anchor = GridBagConstraints.WEST;
242
                c.fill = GridBagConstraints.BOTH;
243
                this.add(getSeconds(), c);
244

    
245
                // checkbox for the provisional selection of video in seconds or frames.
246
                // c.gridx = 3;
247
                // c.gridy = 1;
248
                // c.gridwidth = 2;
249
                // c.anchor = GridBagConstraints.EAST;
250
                // this.add(getJCheckBox(), c);
251

    
252
                c = new GridBagConstraints();
253
                c.insets = new Insets(2, 5, 2, 2);
254
                c.gridx = 1;
255
                c.gridy = 3;
256
                c.anchor = GridBagConstraints.WEST;
257
                this.add(getLabelMode(), c);
258
                
259
                
260
                // choice for mode player selection.
261
                c = new GridBagConstraints();
262
                c.insets = new Insets(2, 2, 5, 5);
263
                c.gridx = 2;
264
                c.gridy = 3;
265
                c.anchor = GridBagConstraints.WEST;
266
                c.fill = GridBagConstraints.BOTH;
267
                c.gridwidth = 3;
268
                this.add(getChoiceValue(), c);
269
                choice.setSelectedIndex(this.animationPlayer.getAnimationMode());
270
                
271
                c = new GridBagConstraints();
272
                c.insets = new Insets(0, 0, 0, 0);
273
                c.gridx = 0;
274
                c.gridy = 3;
275
                c.gridwidth = 5;
276
                c.weightx = 1.0;
277
                c.weighty = 1.0;
278
                this.add(new JPanel(), c);
279

    
280
                setGlobalTime(this.animationPlayer.getGlobalTime() / 1000);
281

    
282
        }
283

    
284
        private JLabel getLabelMode() {
285
                label2 = new JLabel(PluginServices.getText(this, "Label_Mode"));
286
                return label2;
287
        }
288

    
289
        private JLabel getLabelDuration() {
290
                label = new JLabel(PluginServices.getText(this, "Label_Duration"));
291
                return label;
292
        }
293

    
294
        /**
295
         * @return Choice. Return the choice with the different modes of display.
296
         */
297
        private JComboBox getChoiceValue() {
298
                if (choice == null) {
299
                        choice = new JComboBox();
300
                        choice.addItem("Loop mode");
301
                        choice.addItem("Ping pong loop");
302
                        choice.addItem("Play once");
303
                        choice.addItem("Loop once backwards");
304
                        choice.addItemListener(this);
305
                }
306
                return choice;
307
        }
308

    
309
        /**
310
         * @return TextField Return textfield where the user put the seconds of the
311
         *         movie.
312
         */
313
        private JTextField getSeconds() {
314
                if (textosegs == null) {
315
                        textosegs = new JTextField(PluginServices
316
                                        .getText(this, "Text_Secs"), 4);
317
                        textosegs.setName("SECONDS");
318
                        textosegs.setHorizontalAlignment(JTextField.RIGHT);
319
                        textosegs.addKeyListener(this);
320
                        textosegs.addFocusListener(this);
321
                }
322
                return textosegs;
323
        }
324

    
325
        /**
326
         * @return JButton. Return record button.
327
         */
328
        private JButton getRecButton() {
329
                if (buttonIcon3 == null) {
330
                        buttonIcon3 = new JButton();
331
                        // path where are the image to load in the button.
332
                        image3 = new ImageIcon(buttonPath + "/record.png");
333
                        buttonIcon3.setIcon(image3);
334
                        buttonIcon3.setActionCommand("REC");
335
                        buttonIcon3.addActionListener(this);
336
                }
337
                return buttonIcon3;
338
        }
339

    
340
        /**
341
         * @return JButton. Return stop button.
342
         */
343
        private JButton getStopButton() {
344
                if (buttonIcon2 == null) {
345
                        buttonIcon2 = new JButton();
346
                        image2 = new ImageIcon(buttonPath + "/stop.png");
347
                        buttonIcon2.setIcon(image2);
348
                        buttonIcon2.setActionCommand("STOP");
349
                        buttonIcon2.addActionListener(this);
350
                }
351
                return buttonIcon2;
352
        }
353

    
354
        /**
355
         * @return JButton. Return pause button.
356
         */
357
        private JButton getPauseButton() {
358
                if (buttonIcon1 == null) {
359
                        buttonIcon1 = new JButton();
360
                        image1 = new ImageIcon(buttonPath + "/pause.png");
361
                        buttonIcon1.setIcon(image1);
362
                        buttonIcon1.setActionCommand("PAUSE");
363
                        buttonIcon1.addActionListener(this);
364
                }
365
                return buttonIcon1;
366
        }
367

    
368
        /**
369
         * @return JButton. Return play button.
370
         */
371
        public JButton getPlayButton() {
372
                if (buttonIcon == null) {
373
                        buttonIcon = new JButton();
374
                        image = new ImageIcon(buttonPath + "/play.png");
375
                        buttonIcon.setIcon(image);
376
                        buttonIcon.setActionCommand("PLAY");
377
                        buttonIcon.addActionListener(this);
378
                        buttonIcon.setIcon(image);
379
                
380
                }
381
                return buttonIcon;
382
        }
383
        
384
        public List<BaseView> getViewListAdded() {
385
                return viewListAdded;
386
        }
387

    
388

    
389
        public void setViewListAdded(List<BaseView> viewListAdded) {
390
                this.viewListAdded = viewListAdded;
391
        }
392

    
393

    
394
        /**
395
         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
396
         *      logic events of pressed buttons.
397
         */
398
        public void actionPerformed(ActionEvent arg0) {
399
                String actionCommand = arg0.getActionCommand();
400

    
401
                if (actionCommand.equals("PLAY")) { // play pressed
402
                        if (runnable == null) {
403
                                runnable = new StateUpdate();
404
                                // Create the thread supplying it with the runnable object
405
                                Thread thread = new Thread(runnable);
406
                                // Start the thread
407
                                thread.start();
408
                        }
409

    
410
                        updateAnimatedObjects(this.animationPlayer);
411
                        
412
                        Iterator<BaseView> iter = viewListAdded.iterator();
413
                        while (iter.hasNext()) {
414
                                Object object = iter.next();
415
                                if (object instanceof View3D){
416
                                        ((View3D) object).getCanvas3d().getOSGViewer().setCameraManipulator(null);
417
                                }
418
                        }
419
                        
420
                        this.animationPlayer.play();
421
                        playFlag = true;
422
                        if (this.animationPlayer.getAnimationPlayerState() == AnimationPlayer.ANIMATION_PLAY) {
423
                                image = new ImageIcon(buttonPath + "/playon.png");
424
                                buttonIcon.setIcon(image);
425
                        
426
                                if (pauseFlag) {
427
                                        image1 = new ImageIcon(buttonPath + "/pause.png");
428
                                        buttonIcon1.setIcon(image1);
429
                                }
430

    
431
                        }
432

    
433
                } else if (actionCommand.equals("PAUSE")) { // pause pressed
434

    
435
                        this.animationPlayer.pause();
436
                        if (this.animationPlayer.getAnimationPlayerState() == AnimationPlayer.ANIMATION_PAUSE
437
                                        && playFlag) {
438
                                playFlag = false;
439
                                pauseFlag = true;
440
                                image = new ImageIcon(buttonPath + "/play.png");
441
                                buttonIcon.setIcon(image);
442
                                image1 = new ImageIcon(buttonPath + "/pauseon.png");
443
                                buttonIcon1.setIcon(image1);
444
                        }
445
                } else if (actionCommand.equals("STOP")) {
446

    
447
                        Iterator<BaseView> iter = viewListAdded.iterator();
448
                        while (iter.hasNext()) {
449
                                Object object = iter.next();
450
                                if (object instanceof View3D){
451
                                        ((PlanetViewer) ((View3D) object).getCanvas3d().getOSGViewer()).restoreCustomTerrainManipulator();
452
                                }
453
                        }
454
                        
455
                        animationPlayer.stop();
456
                        if (runnable != null)
457
                                runnable.end();
458
                        if (this.animationPlayer.getAnimationPlayerState() == AnimationPlayer.ANIMATION_STOP
459
                                        && (playFlag || pauseFlag)) {
460

    
461
                                if (playFlag) {
462
                                        image = new ImageIcon(buttonPath + "/play.png");
463
                                        buttonIcon.setIcon(image);
464
                                        playFlag = false;
465
                                } else {
466
                                        image1 = new ImageIcon(buttonPath + "/pause.png");
467
                                        buttonIcon1.setIcon(image1);
468
                                        pauseFlag = false;
469
                                }
470
                        }
471
                
472
                } else if (actionCommand.equals("REC")) {
473
                        animationPlayer.record();
474
                        JOptionPane.showMessageDialog(null, "Opci?n no disponible",
475
                                        "Alertas de prueba", JOptionPane.WARNING_MESSAGE);
476

    
477
                }// if
478
        }// public void
479

    
480
        /*
481
         * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
482
         * 
483
         */
484
        public void keyPressed(KeyEvent arg0) {
485
                // TODO Auto-generated method stub
486
        }
487

    
488
        /*
489
         * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
490
         *      Events when you put the film?s seconds in the animation panel.
491
         */
492
        public void keyReleased(KeyEvent arg0) {
493
                System.out.println("valor del campo segundos: "
494
                                + this.textosegs.getText());
495
                try {
496
                        globalTime = new Double(this.textosegs.getText()).doubleValue();
497
                }
498
                catch (Exception e) {
499
                }
500
                this.animationPlayer.setGlobalTime(globalTime);
501
        }
502

    
503
        /*
504
         * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
505
         */
506
        public void keyTyped(KeyEvent arg0) {
507
                // TODO Auto-generated method stub
508
        }
509

    
510
        /**
511
         * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
512
         *      ItemListener methods.
513
         */
514
        public void itemStateChanged(ItemEvent event) {
515
                Object obj = event.getSource();
516
                if (obj == choice) {
517
                        operation = choice.getSelectedIndex(); // get select item.
518
                        setMode(operation);
519
                } else if (obj == checkbox) {
520
                        System.out.println("opcion seleccionada checkbox");
521
                }
522
        }
523

    
524
        /*
525
         * Funci?n que seg?n la opci?n del choice ejecuta una operaci?n definida.
526
         * 
527
         */
528
        private void setMode(int option) {
529
                String mode = String.valueOf(choice.getItemAt(option));
530
                if (mode.equals("Play once")) {
531
                        animationMode = AnimationPlayer.PLAY_ONCE;
532
                } else if (mode.equals("Loop mode")) {
533
                        animationMode = AnimationPlayer.LOOP;
534
                } else if (mode.equals("Loop once backwards")) {
535
                        animationMode = AnimationPlayer.PLAY_ONCE_BACKWARDS;
536
                } else if (mode.equals("Ping pong loop"))
537
                        animationMode = AnimationPlayer.PING_PONG_LOOP;
538

    
539
                this.animationPlayer.setAnimationMode(animationMode);
540

    
541
                System.out.println("opcion: " + mode);
542
        }
543

    
544
        public void setGlobalTime(double time) {
545
                textosegs.setText(Double.toString(time));
546
        }
547

    
548
        /*
549
         * Class to control the automatically stop state of the actual animation.
550
         */
551

    
552
        public class StateUpdate implements Runnable {
553

    
554
                private boolean finish = false;
555

    
556
                public void run() {
557

    
558
                        while (true) {
559
                                try {
560
                                        Thread.sleep(800);
561
                                        synchronized (this) {
562
                                                if (finish) {
563
                                                        Iterator<BaseView> iter = viewListAdded.iterator();
564
                                                        while (iter.hasNext()) {
565
                                                                Object object = iter.next();
566
                                                                if (object instanceof View3D){
567
                                                                        ((PlanetViewer) ((View3D) object).getCanvas3d().getOSGViewer()).restoreCustomTerrainManipulator();
568
                                                                }
569
                                                        }
570
                                                        break;
571
                                                }
572
                                        }
573
                                } catch (InterruptedException e) {
574

    
575
                                        e.printStackTrace();
576
                                }
577
                                
578
                                if (animationPlayer.getAnimationPlayerState() == AnimationPlayer.ANIMATION_STOP
579
                                                && (playFlag || pauseFlag)) {
580

    
581
                                        if (playFlag) {
582
                                                image = new ImageIcon(buttonPath + "/play.png");
583
                                                buttonIcon.setIcon(image);
584
                                                playFlag = false;
585
                                        } else {
586
                                                image1 = new ImageIcon(buttonPath + "/pause.png");
587
                                                buttonIcon1.setIcon(image1);
588
                                                pauseFlag = false;
589
                                        }
590
                                        runnable.end();
591
                                }// long if
592
                        }
593
                }
594

    
595
                /*
596
                 * function to stop the thread.
597
                 */
598

    
599
                public synchronized void end() {
600
                        finish = true;
601
                        runnable = null;
602
                }
603
        }// end class StateUpdate.
604

    
605
        /**
606
         * A?adir un listener a la lista de eventos
607
         * @param listener
608
         */
609
        public void addWindowClosedListener(WindowClosedListener listener) {
610
                if (!actionCommandListeners.contains(listener))
611
                        actionCommandListeners.add(listener);
612
        }
613
        
614
        /**
615
         * Borrar un listener de la lista de eventos
616
         * @param listener
617
         */
618
        public void removeWindowClosedListener(WindowClosedListener listener) {
619
                actionCommandListeners.remove(listener);
620
        }
621

    
622
        public void focusGained(FocusEvent e) {
623
                // TODO Auto-generated method stub
624
        }
625

    
626
        public void focusLost(FocusEvent e) {
627
                textosegs.setText(Double.toString(globalTime));
628
        }
629
}