Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libAnimation / src / com / iver / cit / gvsig / animation / AnimationContainer.java @ 24050

History | View | Annotate | Download (7.78 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

    
20
package com.iver.cit.gvsig.animation;
21

    
22
import java.util.ArrayList;
23
import java.util.List;
24

    
25
import com.iver.cit.gvsig.animation.traks.AnimationDatedTrack;
26
import com.iver.cit.gvsig.animation.traks.AnimationTimeTrack;
27
import com.iver.cit.gvsig.animation.traks.IAnimationTrack;
28
import com.iver.utiles.IPersistence;
29
import com.iver.utiles.XMLEntity;
30

    
31
public class AnimationContainer implements IPersistence {
32

    
33
        // IAnimationTrakc List.
34
        public List<IAnimationTrack>  AnimationTrackList;
35

    
36
        private int trackListSize=0;
37

    
38
        private  AnimationPlayer animationPlayer = new AnimationPlayer();
39

    
40
        public AnimationContainer() {
41
                List<IAnimationTrack> aniTrackList = this.getAnimationTrackList();
42
                aniTrackList = new ArrayList<IAnimationTrack>();
43
                this.setAnimationTrackList(aniTrackList);
44
        }
45

    
46
        /**
47
         * @param animationType
48
         * @return
49
         */
50
        public List<IAnimationTrack> getTackListOfType(IAnimationType animationType) {
51
                List<IAnimationTrack> typeList = new ArrayList<IAnimationTrack>();
52
                for (int i = 0; i < this.AnimationTrackList.size(); i++) {
53
                        IAnimationTrack trackElement = (IAnimationTrack) this.AnimationTrackList
54
                                        .get(i);
55
                        if (animationType.getClassName().equals(
56
                                        trackElement.getAnimationType().getClassName())) {
57
                                typeList.add(trackElement);
58
                        }
59

    
60
                }
61
                return typeList;
62
        }
63

    
64
        /**
65
         * @param animationType
66
         * @return
67
         */
68
        public IAnimationTrack CreateDatedTrack(IAnimationType animationType) {
69

    
70
                List<IAnimationTrack> aniTrackList = this.getAnimationTrackList();
71
                if (aniTrackList == null) {
72
                        aniTrackList = new ArrayList<IAnimationTrack>();
73
                }
74
                AnimationDatedTrack ADTrack = new AnimationDatedTrack(animationType);
75
                aniTrackList.add(ADTrack);
76
                this.setAnimationTrackList(aniTrackList);
77
                return ADTrack;
78
        }
79

    
80
        public IAnimationTrack CreateTimeTrack(IAnimationType animationType) {
81

    
82
                List<IAnimationTrack> aniTrackList = this.getAnimationTrackList();
83
                if (aniTrackList == null) {
84
                        aniTrackList = new ArrayList<IAnimationTrack>();
85
                }
86
                AnimationTimeTrack ADTrack = new AnimationTimeTrack(animationType);
87
                aniTrackList.add(ADTrack);
88
                this.setAnimationTrackList(aniTrackList);
89
                return ADTrack;
90
        }
91

    
92
        /**
93
         * @param animationTrack
94
         */
95
        public void addTrack(IAnimationTrack animationTrack) {
96
                IAnimationTrack track = findTrack(animationTrack.getName());
97
                if (track == null) {
98
                        this.AnimationTrackList.add(animationTrack);
99
                }
100
        }
101

    
102
        /**
103
         * @param name
104
         * @return
105
         */
106
        public IAnimationTrack findTrack(String name) {
107

    
108
                IAnimationTrack IAT = null;
109
                for (int i = 0; i < this.AnimationTrackList.size(); i++) {
110
                        IAnimationTrack trackElement = (IAnimationTrack) this.AnimationTrackList
111
                                        .get(i);
112
                        if (trackElement.getName().equals(name)) {
113
                                IAT = trackElement;
114
                        }
115

    
116
                }
117
                return IAT;
118
        }
119

    
120
        /**
121
         * @param animationTrack
122
         */
123
        public void removeTrack(IAnimationTrack animationTrack) {
124
                animationTrack.removeAllIntervals();
125
                this.AnimationTrackList.remove(animationTrack);
126
        }
127

    
128
        /**
129
         * 
130
         */
131
        public void removeAllTrack() {
132
                for (int i=AnimationTrackList.size() - 1; i>=0; i--) {
133
                        removeTrack(AnimationTrackList.get(i));
134
                }
135
                
136
//                for (Iterator<IAnimationTrack> iterator = AnimationTrackList.iterator(); iterator.hasNext();) {
137
//                        ((IAnimationTrack) iterator.next()).removeAllIntervals();
138
////                        removeTrack(iterator.next());
139
//                }
140
//                AnimationTrackList.clear();
141
        }
142

    
143
        /**
144
         * @return
145
         */
146
        public List<IAnimationTrack> getAnimationTrackList() {
147
                return AnimationTrackList;
148
        }
149

    
150
        
151
        public boolean isEmpty() {
152
                
153
                if(this.AnimationTrackList.size() == 0)
154
                        return true;
155
                else
156
                        return false;
157
                
158
        }
159
        
160
        /**
161
         * @param animationTrackList
162
         */
163
        public void setAnimationTrackList(List<IAnimationTrack> animationTrackList) {
164
                AnimationTrackList = animationTrackList;
165
        }
166
        
167
        public String toString() {
168
                String result = "";
169
                List<IAnimationTrack> ATL = this.AnimationTrackList;
170
                result += "Mostrando lista de tracks:";
171
                if ((ATL == null) || ATL.isEmpty()) {
172
                        result += "\nLista vacia";
173
                } else {
174
                        for (int i = 0; i < ATL.size(); i++) {
175
                                Object element = ATL.get(i);
176
                                result += "\n" + element;
177
                        }
178
                }
179
                return result;
180

    
181
        }
182

    
183
        public void apply(double Tini, double Tend) {
184
                List<IAnimationTrack> ATL = this.AnimationTrackList;
185
//                System.out.println("Tiempo de inicio: " + Tini + " tiempo final "
186
//                                + Tend);
187
                if ((ATL != null) && !ATL.isEmpty()) {
188
                        for (int i = 0; i < ATL.size(); i++) {
189
                                IAnimationTrack element = (IAnimationTrack) ATL.get(i);
190
//                                System.out.println("track: " + element.getName() + "OBJETO" + element.getAnimatedObject());
191
                                element.apply(Tini, Tend);
192
                                if (element.isEnable()){}
193
                        }
194
                }
195
        }
196
        
197
        
198
        /**
199
         * Return the name of this class.
200
         * @return name.
201
         */
202
        
203
        public String getClassName() {
204
                return this.getClass().getName();
205
        }
206
        
207
        /**
208
         * Return the player associated with the container.
209
         * @return animationPlayer.
210
         */
211
        
212
        public AnimationPlayer getAnimationPlayer(){
213
                return this.animationPlayer;
214
        }
215
        
216
        /*
217
         * IPersistence methods.
218
         */
219
        
220
        public XMLEntity getXMLEntity() {
221
                XMLEntity xml = new XMLEntity();
222
                
223
                xml.putProperty("className", this.getClassName());
224
                xml.putProperty("animationContainer", "true");
225
                xml.putProperty("trackListSize", this.AnimationTrackList.size());
226
                
227
                xml.addChild(((IPersistence)animationPlayer).getXMLEntity());
228
                
229
                for (int i = 0; i < this.AnimationTrackList.size(); i++) {                        
230
                        IAnimationTrack trackElement = (IAnimationTrack) this.AnimationTrackList.get(i);                        
231
                        xml.addChild(((IPersistence)trackElement).getXMLEntity());
232
                }
233
                return xml;
234
        }
235

    
236
        
237
        
238

    
239
        /**
240
         * @param xml
241
         */
242
        public void setXMLEntity(XMLEntity xml) {
243
                
244
                String class_name;
245
                
246
                if (xml.contains("trackListSize"))
247
                        this.trackListSize = xml.getIntProperty("trackListSize");
248
                 
249
                // Node AnimationPlayer in the animation tree. Calling setXmlEntity() methods.
250
                // Reconstruyendo el framework. Paso: accediendo a la configuracion del player.
251
                
252
                this.animationPlayer.setXMLEntity(xml.getChild(0));
253
                
254
                
255
                // Node IAnimationTrack in the animation tree. Calling setXmlEntity() methods.
256
                // Reconstruyendo el framework. Paso: nuevo IAnimationTrack, AnimationDateTrack o AnimationTimeTrack.
257
                
258
                List<IAnimationTrack> trackList = new ArrayList<IAnimationTrack>();
259
                for (int i = 1; i <= this.trackListSize; i++) {//desde el hijo 1 que es el primer track hasta el ?ltimo track.
260
                        XMLEntity xmlTrack = xml.getChild(i);
261
                        class_name = xmlTrack.getStringProperty("className");//Accedemos al tipo de track.
262
                                try {
263
                                        Class<?> classTrack = Class.forName(class_name);
264
                                        Object obj = classTrack .newInstance();
265
                                        IPersistence objPersist = (IPersistence) obj;
266
                                        objPersist.setXMLEntity(xmlTrack);
267
                                        IAnimationTrack trackElement = (IAnimationTrack) obj;
268
                                        trackElement.setEnable(true);
269
                                        trackList.add(trackElement);
270
                                } catch (Exception e) {
271
                                        e.printStackTrace();
272
                        }
273
                }
274
                this.setAnimationTrackList(trackList);
275

    
276
        }
277

    
278
}