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/Rotate.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.trait.BaseSegmentTrait;
6   import com.arranger.jarl.util.InterpolateUtil;
7   import com.arranger.jarl.util.ObjectUtil;
8   import com.arranger.jarl.util.WidgetConfigSegment;
9   import com.arranger.jarl.util.JarlInfoUtil;
10  import com.arranger.jarl.widget.IWidget;
11  import org.w3c.dom.Element;
12  
13  import java.awt.*;
14  
15  /**
16   * Rotate will rotate a widget
17   *  Required params:
18   *      startAngle
19   *      endAngle
20   *
21   * For example:
22   * <traitDef name="oneCircle" class="Rotate" startAngle="0" endAngle="360" />
23   *
24   * @traitAttribute angle ## xs:integer ## the angle to rotate the width in degrees.  Eg (1080 would be rotated thee times clockwise)
25   * @traitAttribute startAngle ## xs:integer ## the starting angle
26   * @traitAttribute endAngle ## xs:integer ## the ending angle
27   */
28  public class Rotate extends BaseSegmentTrait {
29  
30      protected double m_angle = Double.NEGATIVE_INFINITY;
31      protected double m_startAngle = Double.NEGATIVE_INFINITY;
32      protected double m_endAngle = Double.NEGATIVE_INFINITY;
33  
34      /**
35       * Prior to the {@link IWidget#paint} the trait can perform some work
36       * @param widget the widget that is being painted
37       * @param context the current context
38       * @param graphics2D the current graphics object
39       * @return the new graphics, or the same
40       */
41      public Graphics2D prePaint(IWidget widget, IContext context, Graphics2D graphics2D) {
42          //get and store transform
43          graphics2D = initGraphicsTransform(graphics2D);
44  
45          double angle = 0.0;
46          if (m_angle != Double.NEGATIVE_INFINITY) {
47              angle = m_angle;
48          } else if (m_startAngle != Double.NEGATIVE_INFINITY && m_endAngle != Double.NEGATIVE_INFINITY) {
49              angle = InterpolateUtil.interpolate(widget.getStartTime(),
50                      widget.getEndTime(),
51                      m_startAngle,
52                      m_endAngle,
53                      context.getTime());
54          } else {
55              //get the current position segment and the pct relative to that segment
56              RotateConfigSegment rotateConfigSegment = (RotateConfigSegment) getCurrentSegment(widget, context);
57              double currentTimePct = getCurrentSegmentTimePct(widget, context, rotateConfigSegment);
58  
59              angle = InterpolateUtil.interpolate(0,
60                      1,
61                      rotateConfigSegment.getStartAngle(),
62                      rotateConfigSegment.getEndAngle(),
63                      currentTimePct);
64          }
65  
66          graphics2D.rotate(Math.toRadians(angle), context.getWidth() / 2, context.getHeight() / 2);
67          return graphics2D;
68      }
69  
70      /**
71       * After the call to {@link IWidget#paint} is called
72       * the trait can restore some state
73       * @param widget the widget that is being painted
74       * @param context the current context
75       * @param graphics2D the current graphics object
76       * @return the new graphics, or the same
77       */
78      public Graphics2D postPaint(IWidget widget, IContext context, Graphics2D graphics2D) {
79          restoreTransform(graphics2D);
80          return graphics2D;
81      }
82  
83      /**
84       * Always remember some attrs might not be there
85       * @param context
86       */
87      protected void initAttributes(IContext context) {
88          super.initAttributes(context);
89  
90          ObjectUtil.initializeField("angle", m_configElement, this, ObjectUtil.PRIMITIVE_CONVERSION);
91          ObjectUtil.initializeField("startAngle", m_configElement, this, ObjectUtil.PRIMITIVE_CONVERSION);
92          ObjectUtil.initializeField("endAngle", m_configElement, this, ObjectUtil.PRIMITIVE_CONVERSION);
93      }
94  
95      protected void addJarlObjectInfo(IJarlObjectInfo jarlObjectInfo) {
96          super.addJarlObjectInfo(jarlObjectInfo);
97          populateInfo(jarlObjectInfo, "angle", "Angle (degrees)", JarlInfoUtil.PRIMITIVE_DISPLAY);
98          populateInfo(jarlObjectInfo, "startAngle", "Start Angle (degrees)", JarlInfoUtil.PRIMITIVE_DISPLAY);
99          populateInfo(jarlObjectInfo, "endAngle", "End Angle (degrees)", JarlInfoUtil.PRIMITIVE_DISPLAY);
100     }
101 
102     /**
103      * Create a concrete {@link WidgetConfigSegment} based on this element
104      * @param element
105      * @return a specific {@link WidgetConfigSegment}
106      */
107     public WidgetConfigSegment createSegment(Element element) {
108         return new RotateConfigSegment(element);
109     }
110 
111     protected static class RotateConfigSegment extends WidgetConfigSegment {
112 
113         protected double m_startAngle = Double.NEGATIVE_INFINITY;
114         protected double m_endAngle = Double.NEGATIVE_INFINITY;
115 
116         public RotateConfigSegment(Element element) {
117             super(element);
118 
119             ObjectUtil.initializeFieldStrict("startAngle", element, this, ObjectUtil.PRIMITIVE_CONVERSION);
120             ObjectUtil.initializeFieldStrict("endAngle", element, this, ObjectUtil.PRIMITIVE_CONVERSION);
121         }
122 
123         protected void addJarlObjectInfo(IJarlObjectInfo jarlObjectInfo) {
124             super.addJarlObjectInfo(jarlObjectInfo);
125             populateInfo(jarlObjectInfo, "startAngle", "Start Angle (degrees)", JarlInfoUtil.PRIMITIVE_DISPLAY);
126             populateInfo(jarlObjectInfo, "endAngle", "End Angle (degrees)", JarlInfoUtil.PRIMITIVE_DISPLAY);
127         }
128 
129         public double getStartAngle() {
130             return m_startAngle;
131         }
132 
133         public double getEndAngle() {
134             return m_endAngle;
135         }
136     }
137 }