Statistics
| Revision:

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

History | View | Annotate | Download (15.8 KB)

1
/* 
2
 * Controles que aparacen una vez pulsado el bot�n del grupo animaci�n,"control de animaci�n". 
3
 * 
4
 */
5

    
6
package com.iver.ai2.animationgui.gui;
7

    
8
import java.awt.Checkbox;
9
import java.awt.Component;
10
import java.awt.Dimension;
11
import java.awt.GridBagConstraints;
12
import java.awt.Insets;
13
import java.awt.event.ActionEvent;
14
import java.awt.event.ActionListener;
15
import java.awt.event.ItemEvent;
16
import java.awt.event.ItemListener;
17
import java.awt.event.KeyEvent;
18
import java.awt.event.KeyListener;
19
import java.net.URL;
20
import java.util.Iterator;
21
import java.util.List;
22

    
23
import javax.swing.ImageIcon;
24
import javax.swing.JButton;
25
import javax.swing.JComboBox;
26
import javax.swing.JLabel;
27
import javax.swing.JOptionPane;
28
import javax.swing.JTextField;
29

    
30
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
31
import org.gvsig.gvsig3d.navigation.NavigationMode;
32
import org.gvsig.gvsig3dgui.view.View3D;
33
import org.gvsig.osgvp.viewer.IViewerContainer;
34

    
35
import com.iver.ai2.gvsig3d.resources.ResourcesFactory;
36
import com.iver.andami.PluginServices;
37
import com.iver.andami.ui.mdiManager.SingletonWindow;
38
import com.iver.andami.ui.mdiManager.WindowInfo;
39
import com.iver.cit.gvsig.animation.AnimationContainer;
40
import com.iver.cit.gvsig.animation.AnimationPlayer;
41
import com.iver.cit.gvsig.animation.animatedObject.IAnimatedObject;
42
import com.iver.cit.gvsig.animation.traks.IAnimationTrack;
43

    
44
/**
45
 * @author
46
 * @since 1.1
47
 * 
48
 * Class to create and to process the event's panel animation.
49
 */
50

    
51
public class AnimationContol3D extends GridBagLayoutPanel implements
52
                SingletonWindow, ActionListener, KeyListener, ItemListener {
53

    
54
        /**
55
         * default static final serial version ID
56
         */
57
        private static final long serialVersionUID = 1L;
58

    
59
        private int width = 240;
60

    
61
        private int height = 85;
62

    
63
        private int operation; // Used in choice's functions
64

    
65
        private JLabel label = null;
66

    
67
        private JLabel label2 = null;
68

    
69
        private JTextField textosegs = null;
70

    
71
        private JComboBox choice = null;
72

    
73
        private JButton buttonIcon = null;
74

    
75
        private JButton buttonIcon1 = null;
76

    
77
        private JButton buttonIcon2 = null;
78

    
79
        private JButton buttonIcon3 = null;
80

    
81
        private ImageIcon image = null;
82

    
83
        private ImageIcon image1 = null;
84

    
85
        private ImageIcon image2 = null;
86

    
87
        private ImageIcon image3 = null;
88

    
89
        private Checkbox checkbox = null;
90

    
91
        private String buttonPath;
92

    
93
        private double globalTime;
94

    
95
        private AnimationPlayer animationPlayer;
96

    
97
        private int animationMode;
98

    
99
        private int animationState;
100

    
101
        private boolean playFlag = false;
102

    
103
        private boolean pauseFlag = false;
104

    
105
        private StateUpdate runnable;
106

    
107
        /**
108
         * Default constructor.
109
         * 
110
         * @param control
111
         */
112
        public AnimationContol3D(AnimationPlayer animationPlayer) {
113

    
114
                this.animationPlayer = animationPlayer;
115

    
116
                // Setting up the button path
117
                String oldPath = ResourcesFactory.getExtPath();// Save the path.
118
                ResourcesFactory
119
                                .setExtPath("/gvSIG/extensiones/com.iver.ai2.animationgui.gui/images/reproductoranim/");// my
120
                // new
121
                // path
122
                ResourcesFactory.setExtPath("/images/reproductoranim/");// my new path
123
                buttonPath = ResourcesFactory.getResourcesPath();
124
                URL path;
125
                path = this.getClass().getClassLoader().getResource("images/reproductoranim/");
126
                buttonPath = path.getPath(); 
127
                ResourcesFactory.setExtPath(oldPath);// Restore the old path.
128
                System.out.println(oldPath);
129
                System.out.println(buttonPath);
130

    
131
                initialize();
132
        }
133
        
134
        
135
        private void updateAnimatedObjects(AnimationPlayer animationPlayer) {
136
                IViewerContainer canvas = null;
137
                AnimationContainer ac = animationPlayer.getAnimationContainer();
138
                List<IAnimationTrack> atl = ac.getAnimationTrackList();
139
                for (Iterator iterator = atl.iterator(); iterator.hasNext();) {
140
                        IAnimationTrack track = (IAnimationTrack) iterator.next();
141
                        IAnimatedObject animatedObject = (IAnimatedObject) track.getAnimatedObject();
142
                        try {
143
                                //if persistence is loaded, the canvas is null in this step
144
                                canvas = (IViewerContainer) animatedObject.getAnimatedObject("canvas");
145
                        }
146
                        catch (Exception e) {
147
                                //if canvas is null, do nothing
148
                        }
149
                        
150
                        if (canvas == null) {
151
                                try {
152
                                                //Getting the name of the view
153
                                                View3D view = (View3D) animatedObject.getAnimatedObject("view");
154
                                                //Getting the canvas and insert this canvas in the animated object
155
                                                animatedObject.addAnimatedObject("canvas", view.getCanvas3d());
156
                                }
157
                                catch(Exception e) {
158
                                        //if canvas is null, do nothing. The view is 2D
159
                                }
160
                        }
161
                }
162
                
163
        }
164

    
165
        /*
166
         * Panel creation.
167
         * 
168
         */
169
        private void initialize() {
170
                this.setPreferredSize(new Dimension(width, height));
171
                this.setSize(new Dimension(width, height));
172

    
173
                // Bag where have declared the characteristic of a Component in the
174
                // panel.
175
                GridBagConstraints c = new GridBagConstraints();
176

    
177
                /*
178
                 * gridx: column position in grid gridy: row position in grid anchor:
179
                 * bottom of space
180
                 */
181

    
182
                // Creation of four buttons: play, stop, pause y record.
183
                c.gridx = 1;
184
                c.gridy = 0;
185
                this.add(getPlayButton(), c);
186

    
187
                c.gridx = 2;
188
                c.gridy = 0;
189
                this.add(getPauseButton(), c);
190

    
191
                c.gridx = 3;
192
                c.gridy = 0;
193
                this.add(getStopButton(), c);
194

    
195
                c.gridx = 4;
196
                c.gridy = 0;
197
                this.add(getRecButton(), c);
198

    
199
                // Space between rows.
200
                c.insets = new Insets(10, 0, 0, 0);
201

    
202
                // Creation of two labels in the panel.
203
                c.gridx = 1;
204
                c.gridy = 1;
205
                c.anchor = GridBagConstraints.WEST;
206
                this.add(getLabelDuration(), c);
207

    
208
                c.gridx = 1;
209
                c.gridy = 3;
210
                c.anchor = GridBagConstraints.WEST;
211
                this.add(getLabelMode(), c);
212

    
213
                // textField to introduce the duration of the movie in seconds or
214
                // frames.
215
                c.gridx = 2;
216
                c.gridy = 1;
217
                c.anchor = GridBagConstraints.WEST;
218
                this.add(getSeconds(), c);
219

    
220
                // checkbox for the provisional selection of video in seconds or frames.
221
                // c.gridx = 3;
222
                // c.gridy = 1;
223
                // c.gridwidth = 2;
224
                // c.anchor = GridBagConstraints.EAST;
225
                // this.add(getJCheckBox(), c);
226

    
227
                // choice for mode player selection.
228
                c.gridx = 2;
229
                c.gridy = 3;
230
                c.anchor = GridBagConstraints.WEST;
231
                c.gridwidth = 3;
232
                this.add(getChoiceValue(), c);
233
                choice.setSelectedIndex(this.animationPlayer.getAnimationMode());
234

    
235
                this.setPreferredSize(new Dimension(width, height));
236
                this.setSize(new Dimension(width, height));
237
                setGlobalTime(this.animationPlayer.getGlobalTime() / 1000);
238

    
239
                // textosegs.setText(Double.toString(globalTime));
240
        }
241

    
242
        private JLabel getLabelMode() {
243
                label2 = new JLabel(PluginServices.getText(this, "Label_Mode"));
244
                return label2;
245
        }
246

    
247
        private JLabel getLabelDuration() {
248
                label = new JLabel(PluginServices.getText(this, "Label_Duration"));
249
                return label;
250
        }
251

    
252
        /**
253
         * @return Checkbox. Return the checkbox of selecion of frames.
254
         */
255
        private Checkbox getJCheckBox() {
256
                if (checkbox == null) {
257
                        checkbox = new Checkbox(PluginServices.getText(this, "Check_Text"),
258
                                        null, false);
259
                        checkbox.addItemListener(this);
260
                }
261
                return checkbox;
262
        }
263

    
264
        /**
265
         * @return Choice. Return the choice with the diferents modes of display.
266
         */
267
        private JComboBox getChoiceValue() {
268
                if (choice == null) {
269
                        choice = new JComboBox();
270
                        choice.addItem("Loop mode");
271
                        choice.addItem("Ping pong loop");
272
                        choice.addItem("Play once");
273
                        choice.addItem("Loop once backwards");
274
                        choice.addItemListener(this);
275
                }
276
                return choice;
277
        }
278

    
279
        /**
280
         * @return TextField Return textfield where the user put the seconds of the
281
         *         movie.
282
         */
283
        private JTextField getSeconds() {
284
                if (textosegs == null) {
285
                        textosegs = new JTextField(PluginServices
286
                                        .getText(this, "Text_Secs"), 4);
287
                        textosegs.setName("SECONDS");
288
                        textosegs.setHorizontalAlignment(JTextField.RIGHT);
289
                        textosegs.addKeyListener(this);
290
                }
291
                return textosegs;
292
        }
293

    
294
        /**
295
         * @return JButton. Return record button.
296
         */
297
        private JButton getRecButton() {
298
                if (buttonIcon3 == null) {
299
                        buttonIcon3 = new JButton();
300
                        // path where are the image to load in the button.
301
                        image3 = new ImageIcon(buttonPath + "/record.png");
302
                        buttonIcon3.setIcon(image3);
303
                        buttonIcon3.setActionCommand("REC");
304
                        buttonIcon3.addActionListener(this);
305
                }
306
                return buttonIcon3;
307
        }
308

    
309
        /**
310
         * @return JButton. Return stop button.
311
         */
312
        private JButton getStopButton() {
313
                if (buttonIcon2 == null) {
314
                        buttonIcon2 = new JButton();
315
                        image2 = new ImageIcon(buttonPath + "/stop.png");
316
                        buttonIcon2.setIcon(image2);
317
                        buttonIcon2.setActionCommand("STOP");
318
                        buttonIcon2.addActionListener(this);
319
                }
320
                return buttonIcon2;
321
        }
322

    
323
        /**
324
         * @return JButton. Return pause button.
325
         */
326
        private JButton getPauseButton() {
327
                if (buttonIcon1 == null) {
328
                        buttonIcon1 = new JButton();
329
                        image1 = new ImageIcon(buttonPath + "/pause.png");
330
                        buttonIcon1.setIcon(image1);
331
                        buttonIcon1.setActionCommand("PAUSE");
332
                        buttonIcon1.addActionListener(this);
333
                }
334
                return buttonIcon1;
335
        }
336

    
337
        /**
338
         * @return JButton. Return play button.
339
         */
340
        public JButton getPlayButton() {
341
                if (buttonIcon == null) {
342
                        buttonIcon = new JButton();
343
                        image = new ImageIcon(buttonPath + "/play.png");
344
                        buttonIcon.setIcon(image);
345
                        buttonIcon.setActionCommand("PLAY");
346
                        buttonIcon.addActionListener(this);
347
                        buttonIcon.setIcon(image);
348
                
349
                }
350
                return buttonIcon;
351
        }
352

    
353
        /**
354
         * @see com.iver.andami.ui.mdiManager.SingletonWindow#getWindowModel()
355
         * @return Object
356
         */
357
        public Object getWindowModel() {
358
                // TODO Auto-generated method stub
359
                return null;
360
        }
361

    
362
        /**
363
         * @see com.iver.andami.ui.mdiManager.IWindow#getWindowInfo()
364
         * @return WindowInfo
365
         */
366
        public WindowInfo getWindowInfo() {
367
                WindowInfo m_viewinfo = new WindowInfo();
368
                m_viewinfo.setTitle(PluginServices.getText(this, "Window_Title"));
369
                m_viewinfo.setHeight(height);
370
                m_viewinfo.setWidth(width);
371
                return m_viewinfo;
372
        }
373

    
374
        /**
375
         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
376
         *      logic events of pressed buttons.
377
         */
378
        public void actionPerformed(ActionEvent arg0) {
379
                String actionCommand = arg0.getActionCommand();
380

    
381
                this.animationPlayer
382
                                .setAnimationPlayerState(AnimationPlayer.ANIMATION_INITIAL);
383

    
384
                if (actionCommand.equals("PLAY")) { // play pressed
385
                        if (runnable == null) {
386
                                runnable = new StateUpdate();
387
                                // Create the thread supplying it with the runnable object
388
                                Thread thread = new Thread(runnable);
389
                                // Start the thread
390
                                thread.start();
391
                        }
392

    
393
                        // this.animationPlayer.setGlobalTime(globalTime);
394
                        // this.animationPlayer.setAnimationFrequency(this.animationPlayer.getAnimationFrequency());//frequency
395
                        // in the AnimationPlayer.
396
                        
397
                        updateAnimatedObjects(this.animationPlayer);
398
                        
399
                        this.animationPlayer.play();
400
                        playFlag = true;
401
                        if (this.animationPlayer.getAnimationPlayerState() == AnimationPlayer.ANIMATION_PLAY) {
402
                                image = new ImageIcon(buttonPath + "/playon.png");
403
                                buttonIcon.setIcon(image);
404
                                com.iver.andami.ui.mdiManager.IWindow view = PluginServices
405
                            .getMDIManager().getActiveWindow();
406
                          if (!(view instanceof View3D))
407
                           return;
408
                          // Casting to View3D
409
                          View3D vista3D = (View3D) view;
410
                          IViewerContainer viewer = vista3D.getCanvas3d();
411
                          viewer.getOSGViewer().setCameraManipulator(null);
412
                                if (pauseFlag) {
413
                                        image1 = new ImageIcon(buttonPath + "/pause.png");
414
                                        buttonIcon1.setIcon(image1);
415
                                }
416

    
417
                        }
418

    
419
                } else if (actionCommand.equals("PAUSE")) { // pause pressed
420

    
421
                        this.animationPlayer.pause();
422
                        if (runnable != null)
423
                                runnable.end();
424
                        if (this.animationPlayer.getAnimationPlayerState() == AnimationPlayer.ANIMATION_PAUSE
425
                                        && playFlag) {
426
                                playFlag = false;
427
                                pauseFlag = true;
428
                                image = new ImageIcon(buttonPath + "/play.png");
429
                                buttonIcon.setIcon(image);
430
                                image1 = new ImageIcon(buttonPath + "/pauseon.png");
431
                                buttonIcon1.setIcon(image1);
432
                        }
433
                } else if (actionCommand.equals("STOP")) {
434

    
435
                        animationPlayer.stop();
436
                        if (runnable != null)
437
                                runnable.end();
438
                        if (this.animationPlayer.getAnimationPlayerState() == AnimationPlayer.ANIMATION_STOP
439
                                        && (playFlag || pauseFlag)) {
440

    
441
                                if (playFlag) {
442
                                        image = new ImageIcon(buttonPath + "/play.png");
443
                                        buttonIcon.setIcon(image);
444
                                        playFlag = false;
445
                                } else {
446
                                        image1 = new ImageIcon(buttonPath + "/pause.png");
447
                                        buttonIcon1.setIcon(image1);
448
                                        pauseFlag = false;
449
                                }
450
                        }
451

    
452
                } else if (actionCommand.equals("REC")) {
453
                        animationPlayer.record();
454
                        JOptionPane.showMessageDialog(null, "Grabandooooooo",
455
                                        "Alertas de prueba", JOptionPane.WARNING_MESSAGE);
456

    
457
                }// if
458
        }// public void
459

    
460
        /*
461
         * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
462
         * 
463
         */
464
        public void keyPressed(KeyEvent arg0) {
465
                // TODO Auto-generated method stub
466
        }
467

    
468
        /*
469
         * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
470
         *      Events when you put the film�s seconds in the animation panel.
471
         */
472
        public void keyReleased(KeyEvent arg0) {
473
                // TODO Auto-generated method stub
474

    
475
                System.out.println("valor del campo segundos: "
476
                                + this.textosegs.getText());
477
                globalTime = new Double(this.textosegs.getText()).doubleValue();
478
                // if (AP == null)
479
                // AP = new AnimationPlayer();
480
                this.animationPlayer.setGlobalTime(globalTime);
481
        }
482

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

    
490
        /**
491
         * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
492
         *      ItemListener methods.
493
         */
494
        public void itemStateChanged(ItemEvent event) {
495
                // TODO Auto-generated method stub
496
                Object obj = event.getSource();
497
                if (obj == choice) {
498
                        operation = choice.getSelectedIndex(); // get select item.
499
                        setMode(operation);
500
                } else if (obj == checkbox) {
501
                        System.out.println("opcion seleccionada checkbox");
502
                }
503
        }
504

    
505
        /*
506
         * funci�n que seg�n la opci�n del choice ejecuta una operaci�n definida.
507
         * esta luego no se usar� 'creo'.
508
         */
509
        private void setMode(int option) {
510
                // TODO Auto-generated method stub
511
                String mode = String.valueOf(choice.getItemAt(option));
512
                if (mode.equals("Play once")) {
513
                        animationMode = AnimationPlayer.PLAY_ONCE;
514
                } else if (mode.equals("Loop mode")) {
515
                        animationMode = AnimationPlayer.LOOP;
516
                } else if (mode.equals("Loop once backwards")) {
517
                        animationMode = AnimationPlayer.PLAY_ONCE_BACKWARDS;
518
                } else if (mode.equals("Ping pong loop"))
519
                        animationMode = AnimationPlayer.PING_PONG_LOOP;
520

    
521
                this.animationPlayer.setAnimationMode(animationMode);
522

    
523
                System.out.println("opcion: " + mode);
524
        }
525

    
526
        public void setGlobalTime(double time) {
527
                textosegs.setText(Double.toString(time));
528
        }
529

    
530
        /*
531
         * Class to control the automatically stop state of the actual animation.
532
         */
533

    
534
        public class StateUpdate implements Runnable {
535

    
536
                private boolean finish = false;
537

    
538
                // private int ani
539
                // private AnimationPlayer animationPlayer;
540

    
541
                public void run() {
542
                        // TODO Auto-generated method stub
543

    
544
                        while (true) {
545
                                try {
546
                                        Thread.sleep(800);
547
                                        synchronized (this) {
548
                                                if (finish) {
549
                                                        break;
550
                                                }
551
                                        }
552
                                } catch (InterruptedException e) {
553

    
554
                                        e.printStackTrace();
555
                                }
556
                                animationState = animationPlayer.getAnimationPlayerState();
557
                                if (animationPlayer.getAnimationPlayerState() == AnimationPlayer.ANIMATION_STOP
558
                                                && (playFlag || pauseFlag)) {
559

    
560
                                        if (playFlag) {
561
                                                image = new ImageIcon(buttonPath + "/play.png");
562
                                                buttonIcon.setIcon(image);
563
                                                playFlag = false;
564
                                        } else {
565
                                                image1 = new ImageIcon(buttonPath + "/pause.png");
566
                                                buttonIcon1.setIcon(image1);
567
                                                pauseFlag = false;
568
                                        }
569
                                        runnable.end();
570
                                }// long if
571
                        }
572
                }
573

    
574
                /*
575
                 * function to stop the thread.
576
                 */
577

    
578
                public synchronized void end() {
579
                        finish = true;
580
                        runnable = null;
581
                }
582
        }// end class StateUpdate.
583

    
584
}// end class AnimationContol3D.