001: package com.xoetrope.swing;
002:
003: import java.awt.Graphics2D;
004: import com.xoetrope.swing.animation.FadeInStep;
005: import javax.swing.SwingConstants;
006:
007: import net.xoetrope.xui.XAttributedComponent;
008: import net.xoetrope.xui.helper.XuiUtilities;
009: import org.jdesktop.animation.timing.Animator;
010:
011: /**
012: * A fade in animation of text. The text is faded in from the background color
013: * to the foreground color.
014: *
015: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
016: * the GNU Public License (GPL), please see license.txt for more details. If
017: * you make commercial use of this software you must purchase a commercial
018: * license from Xoetrope.</p>
019: * <p> $Revision: 1.17 $</p>
020: */
021: public class XAnimatedText extends XFlowedTextComponent implements
022: /*XAnimationContext,*/XAttributedComponent, SwingConstants {
023: private FadeInStep fadeIn = new FadeInStep();
024: private int alignment;
025:
026: /**
027: * Create a new XAnimatedText instance
028: */
029: public XAnimatedText() {
030: setLoopTime(32000);
031: setSleepTime(32);
032: }
033:
034: /**
035: * Initialize the animation surface and prepare the starting point/setup
036: */
037: public void init() {
038: super .init();
039: fadeIn.init();
040: fadeIn.setStartColor(XuiUtilities.unsaturateColor(
041: getForeground(), 15));
042: fadeIn.setEndColor(getForeground());
043: }
044:
045: /**
046: * Specifies a new style for the control.
047: * @param newStyle the new style name
048: */
049: public void useStyle(int newStyle) {
050: repaint();
051: }
052:
053: /**
054: * This method will receive all of the timing events from an Animator
055: * during an animation. The fraction is the percent elapsed (0 to 1)
056: * of the current animation cycle.
057: * @param fraction the fraction of completion between the start and
058: * end of the current cycle. Note that on reversing cycles
059: * ({@link Animator.Direction#BACKWARD}) the fraction decreases
060: * from 1.0 to 0 on backwards-running cycles. Note also that animations
061: * with a duration of {@link Animator#INFINITE INFINITE} will call
062: * timingEvent with an undefined value for fraction, since there is
063: * no fraction that makes sense if the animation has no defined length.
064: * @see Animator.Direction
065: */
066: public void timingEvent(float fraction) {
067: timingFraction = fraction;
068: step(0, 0);
069: repaint();
070: }
071:
072: //=========================================================================
073: // Rendering
074: //=========================================================================
075: /**
076: * Apply the current state to the graphics rendering context
077: * @param g2 the graphics context
078: */
079: public void applyState(Graphics2D g2) {
080: fadeIn.apply(animator, g2);
081: }
082:
083: /**
084: * Adjust the settings for the next step.
085: *
086: * @param w the width
087: * @param h the height
088: */
089: public void reset(int w, int h) {
090: fadeIn.reset();
091: }
092:
093: /**
094: * Adjust the settings for the next step.
095: *
096: *
097: * @param w the width
098: * @param h the height
099: */
100: public void step(int w, int h) {
101: fadeIn.step(animator);
102: }
103:
104: /**
105: * Are there more steps to come?
106: * @param at the animation thread
107: * @return true if the animation has completed, false otherwise
108: */
109: public boolean isFinished(Animator at) {
110: return fadeIn.isFinished(at);
111: }
112:
113: /**
114: * Should the object be animated in this step?
115: * @param at the animation thread
116: * @return true is the animation is running
117: */
118: public boolean isAnimated(Animator at) {
119: return fadeIn.isAnimated(at);
120: }
121:
122: //=========================================================================
123: // Access Functions
124: //=========================================================================
125: /**
126: * Sets the update increment.
127: * @param incr the time in milliseconds between animation steps
128: */
129: public void setIncrement(int incr) {
130: increment = (int) ((float) incr / 100.0f);
131: }
132:
133: /**
134: * Get the update increment
135: * @return the time between animation steps in milliseconds
136: */
137: public int getIncrement() {
138: return (int) (increment * 100.0);
139: }
140:
141: /**
142: * Sets the text alignment.
143: * @param align the alignment
144: */
145: public void setAlignment(int align) {
146: alignment = align;
147: }
148:
149: /**
150: * Get the text alignment
151: * @return the alignment value
152: */
153: public int getAlignment() {
154: return alignment;
155: }
156:
157: /**
158: * Set one or more attributes of the component.
159: * @param attribName the name of the attribute
160: * <ul>
161: * <li>text - set the text</li>
162: * <li>content - set the text</li>
163: * <li>step - set the step size</li>
164: * <li>align - the text alignment, left, right, center</li>
165: * </ul>
166: * @param attribValue the value of the attribute
167: * @return 0 for success, non zero otherwise
168: */
169: public int setAttribute(String attribName, Object attribValue) {
170: String attribNameLwr = attribName.toLowerCase();
171: String attribValueStr = (String) attribValue;
172: String attribValueLwr = attribValueStr.toLowerCase();
173: if (attribNameLwr.equals("step"))
174: setIncrement(Integer.parseInt(attribValueStr));
175: else if (attribNameLwr.equals("align")
176: || attribNameLwr.equals("alignment"))
177: alignment = attribValueLwr.equals("right") ? SwingConstants.RIGHT
178: : attribValueLwr.equals("center") ? SwingConstants.CENTER
179: : SwingConstants.LEFT;
180: else
181: super .setAttribute(attribName, attribValue);
182: return 0;
183: }
184:
185: /**
186: * Set the text content for the component.
187: * @param set the text value
188: */
189: public void setContent(String content) {
190: super .setAttribute("text", content);
191: }
192:
193: /**
194: * Get the text content
195: * @return the text value
196: */
197: public String getContent() {
198: return super.getText();
199: }
200: }
|