Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/arranger/jarl/widget/BaseSegmentWidget.java


1   package com.arranger.jarl.widget;
2   
3   import com.arranger.jarl.util.*;
4   import com.arranger.jarl.base.IContext;
5   import com.arranger.jarl.base.Time;
6   import com.arranger.jarl.base.IJarlObjectInfo;
7   import org.w3c.dom.Element;
8   
9   import java.util.Collection;
10  
11  /**
12   * BaseSegmentWidget created on Mar 25, 2003
13   */
14  public abstract class BaseSegmentWidget extends BaseWidget implements IWidgetConfigSegmentFactory {
15  
16      protected java.util.List m_configSegments;
17  
18      protected double m_width = Double.NEGATIVE_INFINITY;
19      protected double m_height = Double.NEGATIVE_INFINITY;
20  
21      protected double m_startWidth = Double.NEGATIVE_INFINITY;
22      protected double m_startHeight = Double.NEGATIVE_INFINITY;
23      protected double m_endWidth = Double.NEGATIVE_INFINITY;
24      protected double m_endHeight = Double.NEGATIVE_INFINITY;
25  
26      /**
27       * Always remember some attrs might not be there
28       * @param context
29       */
30      protected void initAttributes(IContext context) {
31          super.initAttributes(context);
32  
33          ObjectUtil.initializeField("width", m_configElement, this, ObjectUtil.NORMALIZING_CONVERSION);
34          ObjectUtil.initializeField("height", m_configElement, this, ObjectUtil.NORMALIZING_CONVERSION);
35          ObjectUtil.initializeField("startWidth", m_configElement, this, ObjectUtil.NORMALIZING_CONVERSION);
36          ObjectUtil.initializeField("startHeight", m_configElement, this, ObjectUtil.NORMALIZING_CONVERSION);
37          ObjectUtil.initializeField("endWidth", m_configElement, this, ObjectUtil.NORMALIZING_CONVERSION);
38          ObjectUtil.initializeField("endHeight", m_configElement, this, ObjectUtil.NORMALIZING_CONVERSION);
39  
40          //look for segments
41          java.util.List seg = WidgetUtil.initWidgetConfigSegments(m_configElement, this);
42          if (seg != null) {
43              m_configSegments = seg;
44          }
45      }
46  
47      protected void addJarlObjectInfo(IJarlObjectInfo jarlObjectInfo) {
48          super.addJarlObjectInfo(jarlObjectInfo);
49          populateInfo(jarlObjectInfo, "width", "Width", JarlInfoUtil.PCT_DISPLAY);
50          populateInfo(jarlObjectInfo, "height", "Height", JarlInfoUtil.PCT_DISPLAY);
51          populateInfo(jarlObjectInfo, "startWidth", "Start Width", JarlInfoUtil.PCT_DISPLAY);
52          populateInfo(jarlObjectInfo, "startHeight", "Start Height", JarlInfoUtil.PCT_DISPLAY);
53          populateInfo(jarlObjectInfo, "endWidth", "End Width", JarlInfoUtil.PCT_DISPLAY);
54          populateInfo(jarlObjectInfo, "endHeight", "End Height", JarlInfoUtil.PCT_DISPLAY);
55      }
56  
57      /**
58       * Get the 'active' widget config segment
59       * @param context
60       * @return the 'active' widget config segment
61       */
62      protected WidgetConfigSegment getCurrentSegment(IContext context) {
63          //get how far along we are into this widget
64          double currentTimePct = InterpolateUtil.interpolate(this, context.getTime());
65  
66          //get the current motion segment
67          return WidgetUtil.getWidgetConfigSegment(m_configSegments, currentTimePct);
68      }
69  
70      /**
71       * Get the pct of time relative to this segment
72       * @param widgetConfigSegment
73       * @return relative time into this segment
74       *
75       * @see #getCurrentSegment
76       */
77      protected double getCurrentSegmentTimePct(IContext context, WidgetConfigSegment widgetConfigSegment) {
78  
79          //get relative time
80          Time relTime = WidgetUtil.getRelativeTime(this, context.getTime());
81  
82          //get current time pct relative to this segment
83          //relTime - segStart
84          //-------------------
85          //       segLength
86          double segStart = widgetConfigSegment.getStartTimePct() * getTotalTime().getFrame();
87          double segEnd = widgetConfigSegment.getEndTimePct() * getTotalTime().getFrame();
88          double segLength = segEnd - segStart;
89  
90          return (relTime.getFrame() - segStart) / segLength;
91      }
92  
93      protected double getWidth(IContext context) {
94          double width;
95          if (m_width != Double.NEGATIVE_INFINITY) {
96              width = m_width;
97          } else if (m_startWidth != Double.NEGATIVE_INFINITY &&
98                  m_endWidth != Double.NEGATIVE_INFINITY) {
99  
100             width = InterpolateUtil.interpolate(getStartTime(),
101                     getEndTime(),
102                     m_startWidth,
103                     m_endWidth,
104                     context.getTime());
105         } else {
106 
107             BaseWidgetConfigSegment baseWidgetConfigSegment = (BaseWidgetConfigSegment) getCurrentSegment(context);
108             double currentTimePct = getCurrentSegmentTimePct(context, baseWidgetConfigSegment);
109 
110             width = InterpolateUtil.interpolate(0,
111                     1,
112                     baseWidgetConfigSegment.getStartWidth(),
113                     baseWidgetConfigSegment.getEndWidth(),
114                     currentTimePct);
115         }
116         return context.getWidth() * width;
117     }
118 
119     protected double getHeight(IContext context) {
120         double height;
121         if (m_height != Double.NEGATIVE_INFINITY) {
122             height = m_height;
123         } else if (m_startHeight != Double.NEGATIVE_INFINITY &&
124                 m_endHeight != Double.NEGATIVE_INFINITY) {
125 
126             height = InterpolateUtil.interpolate(getStartTime(),
127                     getEndTime(),
128                     m_startHeight,
129                     m_endHeight,
130                     context.getTime());
131         } else {
132 
133             BaseWidgetConfigSegment baseWidgetConfigSegment = (BaseWidgetConfigSegment) getCurrentSegment(context);
134             double currentTimePct = getCurrentSegmentTimePct(context, baseWidgetConfigSegment);
135 
136             height = InterpolateUtil.interpolate(0,
137                     1,
138                     baseWidgetConfigSegment.getStartHeight(),
139                     baseWidgetConfigSegment.getEndHeight(),
140                     currentTimePct);
141         }
142         return context.getHeight() * height;
143     }
144 
145     /**
146      * Create a concrete {@link WidgetConfigSegment} based on this element
147      * @param element
148      * @return a specific {@link WidgetConfigSegment}
149      */
150     public WidgetConfigSegment createSegment(Element element) {
151         return new BaseWidgetConfigSegment(element);
152     }
153 
154     /**
155      * @return a collection of {@link WidgetConfigSegment}
156      */
157     public Collection getConfigSegments() {
158         return m_configSegments;
159     }
160 
161     public static class BaseWidgetConfigSegment extends WidgetConfigSegment {
162 
163         protected double m_startWidth = Double.NEGATIVE_INFINITY;
164         protected double m_startHeight = Double.NEGATIVE_INFINITY;
165         protected double m_endWidth = Double.NEGATIVE_INFINITY;
166         protected double m_endHeight = Double.NEGATIVE_INFINITY;
167 
168         public BaseWidgetConfigSegment(Element element) {
169             super(element);
170 
171             ObjectUtil.initializeFieldStrict("startWidth", element, this, ObjectUtil.NORMALIZING_CONVERSION);
172             ObjectUtil.initializeFieldStrict("startHeight", element, this, ObjectUtil.NORMALIZING_CONVERSION);
173             ObjectUtil.initializeFieldStrict("endWidth", element, this, ObjectUtil.NORMALIZING_CONVERSION);
174             ObjectUtil.initializeFieldStrict("endHeight", element, this, ObjectUtil.NORMALIZING_CONVERSION);
175         }
176 
177         protected void addJarlObjectInfo(IJarlObjectInfo jarlObjectInfo) {
178             super.addJarlObjectInfo(jarlObjectInfo);
179             populateInfo(jarlObjectInfo, "startWidth", "Start Width", JarlInfoUtil.PCT_DISPLAY);
180             populateInfo(jarlObjectInfo, "startHeight", "Start Height", JarlInfoUtil.PCT_DISPLAY);
181             populateInfo(jarlObjectInfo, "endWidth", "End Width", JarlInfoUtil.PCT_DISPLAY);
182             populateInfo(jarlObjectInfo, "endHeight", "End Height", JarlInfoUtil.PCT_DISPLAY);
183         }
184 
185         public double getStartWidth() {
186             return m_startWidth;
187         }
188 
189         public double getStartHeight() {
190             return m_startHeight;
191         }
192 
193         public double getEndWidth() {
194             return m_endWidth;
195         }
196 
197         public double getEndHeight() {
198             return m_endHeight;
199         }
200     }
201 }