Source code: com/arranger/jarl/trait/base/Transparent.java
1 package com.arranger.jarl.trait.base;
2
3 import com.arranger.jarl.trait.BaseSegmentTrait;
4 import com.arranger.jarl.widget.IWidget;
5 import com.arranger.jarl.base.IContext;
6 import com.arranger.jarl.base.IJarlObjectInfo;
7 import com.arranger.jarl.util.*;
8
9 import java.awt.*;
10
11 import org.w3c.dom.Element;
12
13 /**
14 * Transparent created on Feb 25, 2003
15 *
16 * @traitAttribute alpha ## xs:string ## the level of transparency from 0 (transparent) to 1 (opaque)
17 * @traitAttribute startAlpha ## xs:string ## the starting alpha level
18 * @traitAttribute endAlpha ## xs:string ## the ending alpha level
19 */
20 public class Transparent extends BaseSegmentTrait {
21
22 protected float m_alpha = Float.NEGATIVE_INFINITY;
23 protected float m_startAlpha = Float.NEGATIVE_INFINITY;
24 protected float m_endAlpha = Float.NEGATIVE_INFINITY;
25
26
27 /**
28 * Prior to the {@link IWidget#paint} the trait can perform some work
29 * @param widget the widget that is being painted
30 * @param context the current context
31 * @param graphics2D the current graphics object
32 * @return the new graphics, or the same
33 */
34 public Graphics2D prePaint(IWidget widget, IContext context, Graphics2D graphics2D) {
35 store(graphics2D.getComposite());
36
37 float alpha;
38 if (m_alpha != Float.NEGATIVE_INFINITY) {
39 alpha = m_alpha;
40 } else if (m_startAlpha != Float.NEGATIVE_INFINITY && m_endAlpha != Float.NEGATIVE_INFINITY) {
41 alpha = (float) InterpolateUtil.interpolate(widget.getStartTime(),
42 widget.getEndTime(),
43 m_startAlpha,
44 m_endAlpha,
45 context.getTime());
46 } else {
47 TransparentConfigSegment transparentConfigSegment = (TransparentConfigSegment) getCurrentSegment(widget, context);
48 double currentTimePct = getCurrentSegmentTimePct(widget, context, transparentConfigSegment);
49
50 alpha = (float) InterpolateUtil.interpolate(0,
51 1,
52 transparentConfigSegment.getStartAlpha(),
53 transparentConfigSegment.getEndAlpha(),
54 currentTimePct);
55 }
56 graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
57
58 return graphics2D;
59 }
60
61 /**
62 * After the call to {@link IWidget#paint} is called
63 * the trait can restore some state
64 * @param widget the widget that is being painted
65 * @param context the current context
66 * @param graphics2D the current graphics object
67 * @return the new graphics, or the same
68 */
69 public Graphics2D postPaint(IWidget widget, IContext context, Graphics2D graphics2D) {
70 restoreComposite(graphics2D);
71 return graphics2D;
72 }
73
74 /**
75 * Always remember some attrs might not be there
76 * @param context
77 */
78 protected void initAttributes(IContext context) {
79 super.initAttributes(context);
80
81 ObjectUtil.initializeField("alpha", m_configElement, this, ObjectUtil.NORMALIZING_CONVERSION);
82 ObjectUtil.initializeField("startAlpha", m_configElement, this, ObjectUtil.NORMALIZING_CONVERSION);
83 ObjectUtil.initializeField("endAlpha", m_configElement, this, ObjectUtil.NORMALIZING_CONVERSION);
84 }
85
86 /**
87 * Override this, and for every field that you're using, call {@link #populateInfo}
88 * for example:
89 * <code>
90 * populateInfo(jarlObjectInfo, "zOrder", "Z-Order", JarlInfoUtil.PRIMITIVE_DISPLAY);
91 * </code>
92 *
93 * @param jarlObjectInfo
94 *
95 * @see JarlInfoUtil#PRIMITIVE_DISPLAY
96 * @see #populateInfo
97 * @see ObjectUtil#initializeField
98 */
99 protected void addJarlObjectInfo(IJarlObjectInfo jarlObjectInfo) {
100 super.addJarlObjectInfo(jarlObjectInfo);
101 populateInfo(jarlObjectInfo, "alpha", "Alpha", JarlInfoUtil.PCT_DISPLAY);
102 populateInfo(jarlObjectInfo, "startAlpha", "Start Alpha", JarlInfoUtil.PCT_DISPLAY);
103 populateInfo(jarlObjectInfo, "endAlpha", "End Alpha", JarlInfoUtil.PCT_DISPLAY);
104 }
105
106 /**
107 * Create a concrete {@link WidgetConfigSegment} based on this element
108 * @param element
109 * @return a specific {@link WidgetConfigSegment}
110 */
111 public WidgetConfigSegment createSegment(Element element) {
112 return new TransparentConfigSegment(element);
113 }
114
115 protected static class TransparentConfigSegment extends WidgetConfigSegment {
116
117 protected float m_startAlpha = Float.NEGATIVE_INFINITY;
118 protected float m_endAlpha = Float.NEGATIVE_INFINITY;
119
120 public TransparentConfigSegment(Element element) {
121 super(element);
122
123 ObjectUtil.initializeFieldStrict("startAlpha", element, this, ObjectUtil.NORMALIZING_CONVERSION);
124 ObjectUtil.initializeFieldStrict("endAlpha", element, this, ObjectUtil.NORMALIZING_CONVERSION);
125 }
126
127 protected void addJarlObjectInfo(IJarlObjectInfo jarlObjectInfo) {
128 super.addJarlObjectInfo(jarlObjectInfo);
129 populateInfo(jarlObjectInfo, "startAlpha", "Start Alpha", JarlInfoUtil.PCT_DISPLAY);
130 populateInfo(jarlObjectInfo, "endAlpha", "End Alpha", JarlInfoUtil.PCT_DISPLAY);
131 }
132
133 public float getStartAlpha() {
134 return m_startAlpha;
135 }
136
137 public float getEndAlpha() {
138 return m_endAlpha;
139 }
140 }
141 }