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

Quick Search    Search Deep

Source code: com/arranger/jarl/trait/base/Resize.java


1   package com.arranger.jarl.trait.base;
2   
3   import com.arranger.jarl.base.IContext;
4   import com.arranger.jarl.base.IJarlObjectInfo;
5   import com.arranger.jarl.base.Time;
6   import com.arranger.jarl.trait.BaseSegmentTrait;
7   import com.arranger.jarl.util.InterpolateUtil;
8   import com.arranger.jarl.util.JarlInfoUtil;
9   import com.arranger.jarl.util.ObjectUtil;
10  import com.arranger.jarl.util.WidgetConfigSegment;
11  import com.arranger.jarl.widget.IWidget;
12  import org.w3c.dom.Element;
13  
14  import java.awt.*;
15  
16  /**
17   * Resize will resize the widget relative to the screen size.
18   *
19   * Required attributes: (should be percentages: eg. 10%)
20   *  startWidth
21   *  startHeight
22   *  endWidth
23   *  endHeight
24   *
25   * @traitAttribute width ## xs:string ## the width (relative to the screen) to resize the widget
26   * @traitAttribute height ## xs:string ## the height (relative to the screen) to resize the widget
27   * @traitAttribute startWidth ## xs:string ## the starting width
28   * @traitAttribute startHeight ## xs:string ## the starting height
29   * @traitAttribute endWidth ## xs:string ## the ending width
30   * @traitAttribute endHeight ## xs:string ## the ending height
31   */
32  public class Resize extends BaseSegmentTrait {
33  
34      protected double m_width = Double.NEGATIVE_INFINITY;
35      protected double m_height = Double.NEGATIVE_INFINITY;
36      protected double m_startWidth = Double.NEGATIVE_INFINITY;
37      protected double m_startHeight = Double.NEGATIVE_INFINITY;
38      protected double m_endWidth = Double.NEGATIVE_INFINITY;
39      protected double m_endHeight = Double.NEGATIVE_INFINITY;
40  
41  
42      /**
43       * Prior to the {@link IWidget#paint} the trait can perform some work
44       * @param widget the widget that is being painted
45       * @param context the current context
46       * @param graphics2D the current graphics object
47       */
48      public Graphics2D prePaint(IWidget widget, IContext context, Graphics2D graphics2D) {
49          //get and store transform
50          graphics2D = initGraphicsTransform(graphics2D);
51  
52          //get times
53          Time startTime = widget.getStartTime();
54          Time endTime = widget.getEndTime();
55          Time currentTime = context.getTime();
56  
57          double width, height;
58          if (m_width != Double.NEGATIVE_INFINITY && m_height != Double.NEGATIVE_INFINITY) {
59              width = m_width;
60              height = m_height;
61          } else if (m_startWidth != Double.NEGATIVE_INFINITY &&
62                  m_endWidth != Double.NEGATIVE_INFINITY &&
63                  m_startHeight != Double.NEGATIVE_INFINITY &&
64                  m_endHeight != Double.NEGATIVE_INFINITY) {
65              //get interpolated width & height pcts
66              width = InterpolateUtil.interpolate(startTime, endTime, m_startWidth, m_endWidth, currentTime);
67              height = InterpolateUtil.interpolate(startTime, endTime, m_startHeight, m_endHeight, currentTime);
68          } else {
69              //get the current position segment and the pct relative to that segment
70              ResizeConfigSegment resizeConfigSegment = (ResizeConfigSegment) getCurrentSegment(widget, context);
71              double currentTimePct = getCurrentSegmentTimePct(widget, context, resizeConfigSegment);
72  
73              width = InterpolateUtil.interpolate(0,
74                      1,
75                      resizeConfigSegment.getStartWidth(),
76                      resizeConfigSegment.getEndWidth(),
77                      currentTimePct);
78  
79              height = InterpolateUtil.interpolate(0,
80                      1,
81                      resizeConfigSegment.getStartHeight(),
82                      resizeConfigSegment.getEndHeight(),
83                      currentTimePct);
84          }
85  
86          //set translate so that we're centered after the scaling
87          graphics2D.translate(
88                  context.getWidth() * ((1.0 - width) * .5),
89                  context.getHeight() * ((1.0 - height) * .5)
90          );
91  
92          //set scale
93          graphics2D.scale(width, height);
94          return graphics2D;
95      }
96  
97      /**
98       * After the call to {@link IWidget#paint} is called
99       * the trait can restore some state
100      * @param widget the widget that is being painted
101      * @param context the current context
102      * @param graphics2D the current graphics object
103      */
104     public Graphics2D postPaint(IWidget widget, IContext context, Graphics2D graphics2D) {
105         restoreTransform(graphics2D);
106         return graphics2D;
107     }
108 
109     /**
110      * Always remember some attrs might not be there
111      * @param context
112      */
113     protected void initAttributes(IContext context) {
114         super.initAttributes(context);
115 
116         ObjectUtil.initializeField("width", m_configElement, this, ObjectUtil.NORMALIZING_CONVERSION);
117         ObjectUtil.initializeField("height", m_configElement, this, ObjectUtil.NORMALIZING_CONVERSION);
118         ObjectUtil.initializeField("startWidth", m_configElement, this, ObjectUtil.NORMALIZING_CONVERSION);
119         ObjectUtil.initializeField("endWidth", m_configElement, this, ObjectUtil.NORMALIZING_CONVERSION);
120         ObjectUtil.initializeField("startHeight", m_configElement, this, ObjectUtil.NORMALIZING_CONVERSION);
121         ObjectUtil.initializeField("endHeight", m_configElement, this, ObjectUtil.NORMALIZING_CONVERSION);
122     }
123 
124     protected void addJarlObjectInfo(IJarlObjectInfo jarlObjectInfo) {
125         super.addJarlObjectInfo(jarlObjectInfo);
126         populateInfo(jarlObjectInfo, "width", "Width", JarlInfoUtil.PCT_DISPLAY);
127         populateInfo(jarlObjectInfo, "height", "Height", JarlInfoUtil.PCT_DISPLAY);
128         populateInfo(jarlObjectInfo, "startWidth", "Start Width", JarlInfoUtil.PCT_DISPLAY);
129         populateInfo(jarlObjectInfo, "startHeight", "Start Height", JarlInfoUtil.PCT_DISPLAY);
130         populateInfo(jarlObjectInfo, "endWidth", "End Width", JarlInfoUtil.PCT_DISPLAY);
131         populateInfo(jarlObjectInfo, "endHeight", "End Height", JarlInfoUtil.PCT_DISPLAY);
132     }
133 
134     /**
135      * Create a concrete {@link WidgetConfigSegment} based on this element
136      * @param element
137      * @return a specific {@link WidgetConfigSegment}
138      */
139     public WidgetConfigSegment createSegment(Element element) {
140         return new ResizeConfigSegment(element);
141     }
142 
143     protected static class ResizeConfigSegment extends WidgetConfigSegment {
144 
145         protected double m_startWidth;
146         protected double m_startHeight;
147         protected double m_endWidth;
148         protected double m_endHeight;
149 
150         public ResizeConfigSegment(Element element) {
151             super(element);
152             ObjectUtil.initializeFieldStrict("startWidth", element, this, ObjectUtil.NORMALIZING_CONVERSION);
153             ObjectUtil.initializeFieldStrict("endWidth", element, this, ObjectUtil.NORMALIZING_CONVERSION);
154             ObjectUtil.initializeFieldStrict("startHeight", element, this, ObjectUtil.NORMALIZING_CONVERSION);
155             ObjectUtil.initializeFieldStrict("endHeight", element, this, ObjectUtil.NORMALIZING_CONVERSION);
156         }
157 
158         protected void addJarlObjectInfo(IJarlObjectInfo jarlObjectInfo) {
159             super.addJarlObjectInfo(jarlObjectInfo);
160             populateInfo(jarlObjectInfo, "startWidth", "Start Width", JarlInfoUtil.PCT_DISPLAY);
161             populateInfo(jarlObjectInfo, "startHeight", "Start Height", JarlInfoUtil.PCT_DISPLAY);
162             populateInfo(jarlObjectInfo, "endWidth", "End Width", JarlInfoUtil.PCT_DISPLAY);
163             populateInfo(jarlObjectInfo, "endHeight", "End Height", JarlInfoUtil.PCT_DISPLAY);
164         }
165 
166         public double getStartWidth() {
167             return m_startWidth;
168         }
169 
170         public double getStartHeight() {
171             return m_startHeight;
172         }
173 
174         public double getEndWidth() {
175             return m_endWidth;
176         }
177 
178         public double getEndHeight() {
179             return m_endHeight;
180         }
181     }
182 }