001: /*
002: * Copyright (c) 2004 JETA Software, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without modification,
005: * are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JETA Software nor the names of its contributors may
015: * be used to endorse or promote products derived from this software without
016: * specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
021: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
022: * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
023: * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
024: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
025: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
026: * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: */
029:
030: package com.jeta.forms.store.properties.effects;
031:
032: import java.awt.Color;
033: import java.io.IOException;
034:
035: import com.jeta.forms.gui.beans.JETABean;
036: import com.jeta.forms.gui.effects.LinearGradientPainter;
037: import com.jeta.forms.gui.effects.Paintable;
038: import com.jeta.forms.gui.effects.Painter;
039: import com.jeta.forms.store.JETAObjectInput;
040: import com.jeta.forms.store.JETAObjectOutput;
041: import com.jeta.forms.store.properties.ColorProperty;
042: import com.jeta.forms.store.properties.JETAProperty;
043: import com.jeta.open.i18n.I18N;
044:
045: /**
046: * A <code>GradientProperty</code> stores the attributes for a
047: * LinearGradientPainter. See:
048: * {@link com.jeta.forms.gui.effects.LinearGradientPainter}
049: *
050: * @author Jeff Tassin
051: */
052: public class GradientProperty extends JETAProperty implements
053: PaintSupport {
054: static final long serialVersionUID = 8426558667506208881L;
055:
056: /**
057: * The version of this class
058: */
059: public static final int VERSION = 3;
060:
061: /**
062: * Gradient direction constants.
063: */
064: public static final int TOP_BOTTOM = 0;
065:
066: public static final int BOTTOM_TOP = 1;
067:
068: public static final int LEFT_RIGHT = 2;
069:
070: public static final int RIGHT_LEFT = 3;
071:
072: public static final int UP_RIGHT = 4;
073:
074: public static final int UP_LEFT = 5;
075:
076: public static final int DOWN_RIGHT = 6;
077:
078: public static final int DOWN_LEFT = 7;
079:
080: /**
081: * Start and end colors
082: */
083: private ColorProperty m_start_color = new ColorProperty(Color.black);
084:
085: private ColorProperty m_end_color = new ColorProperty(Color.white);
086:
087: /**
088: * Magnitude controls the rate of change from start to end color. A value of
089: * 1.0 means equal rates of change for both colors.
090: */
091: private float m_magnitude = 1.0f;
092:
093: private int m_direction = TOP_BOTTOM;
094:
095: /**
096: * A cached painter
097: */
098: private transient LinearGradientPainter m_painter;
099:
100: /**
101: * Creates an unitialized <code>GradientProperty</code> instance
102: */
103: public GradientProperty() {
104:
105: }
106:
107: /**
108: * Creates a <code>GradientProperty</code> instance with the specified
109: * colors and direction
110: *
111: * @param start
112: * the start color
113: * @param end
114: * the end color
115: * @param direction
116: * the direction of the gradient.
117: */
118: public GradientProperty(ColorProperty start, ColorProperty end,
119: int direction) {
120: m_start_color = start;
121: m_end_color = end;
122: m_direction = direction;
123: }
124:
125: /**
126: * PaintSupport implementation. Creates a painter that renders a linear
127: * gradient using the attributes defined by this object.
128: */
129: public Painter createPainter() {
130: if (m_painter == null)
131: m_painter = new LinearGradientPainter(this );
132:
133: return m_painter;
134: }
135:
136: /**
137: * Returns the direction of the gradient: valid values are: TOP_BOTTOM,
138: * BOTTOM_TOP, LEFT_RIGHT, RIGHT_LEFT, UP_RIGHT, UP_LEFT, DOWN_RIGHT,
139: * DOWN_LEFT
140: */
141: public int getDirection() {
142: return m_direction;
143: }
144:
145: /**
146: * Returns the end color for the gradient
147: *
148: * @return the end color
149: */
150: public ColorProperty getEndColor() {
151: return m_end_color;
152: }
153:
154: /**
155: * Returns the value that controls the rate of change from one color to the
156: * next. The default value is 1.0f
157: */
158: public float getMagnitude() {
159: return m_magnitude;
160: }
161:
162: /**
163: * Returns the start color for the gradient
164: *
165: * @return the start color
166: */
167: public ColorProperty getStartColor() {
168: return m_start_color;
169: }
170:
171: /**
172: * Sets the direction of the gradient: valid values are: TOP_BOTTOM,
173: * BOTTOM_TOP, LEFT_RIGHT, RIGHT_LEFT, UP_RIGHT, UP_LEFT, DOWN_RIGHT,
174: * DOWN_LEFT
175: *
176: * @param direction
177: * the direction
178: */
179: public void setDirection(int direction) {
180: m_direction = direction;
181: m_painter = null;
182: }
183:
184: /**
185: * Sets the start color for the gradient
186: *
187: * @param c
188: * the start color
189: */
190: public void setStartColor(ColorProperty c) {
191: m_start_color = c;
192: m_painter = null;
193: }
194:
195: /**
196: * Sets the end color for the gradient
197: *
198: * @param c
199: * the end color
200: */
201: public void setEndColor(ColorProperty c) {
202: m_end_color = c;
203: m_painter = null;
204: }
205:
206: /**
207: * Sets the value that controls the rate of change from the start color to
208: * the end color. The default value is 1.0f
209: *
210: * @param mag
211: * the magnitude.
212: */
213: public void setMagnitude(float mag) {
214: m_magnitude = mag;
215: m_painter = null;
216: }
217:
218: /**
219: * Sets this property to that of another GradientProperty
220: *
221: * @param prop
222: * a GradientProperty instance
223: */
224: public void setValue(Object prop) {
225: if (prop instanceof GradientProperty) {
226: GradientProperty gp = (GradientProperty) prop;
227: if (m_start_color == null)
228: m_start_color = new ColorProperty();
229: if (m_end_color == null)
230: m_end_color = new ColorProperty();
231: m_start_color.setValue(gp.m_start_color);
232: m_end_color.setValue(gp.m_end_color);
233: m_magnitude = gp.m_magnitude;
234: m_direction = gp.m_direction;
235: m_painter = null;
236: } else {
237: assert (false);
238: }
239: }
240:
241: /**
242: * Updates the bean. Gets the underlying Java bean component associated with
243: * this property and if that component implements the Paintable interface,
244: * sets the background painter.
245: */
246: public void updateBean(JETABean jbean) {
247: LinearGradientPainter painter = (LinearGradientPainter) createPainter();
248: if (jbean != null) {
249: java.awt.Component comp = jbean.getDelegate();
250: if (comp instanceof Paintable) {
251: ((Paintable) comp).setBackgroundPainter(painter);
252: } else {
253: if (comp != null) {
254: System.out
255: .println("Fill property set on non paintable. "
256: + comp.getClass());
257: }
258: }
259: }
260:
261: }
262:
263: /**
264: * @return a string representation of this proprety
265: */
266: public String toString() {
267: return I18N.getLocalizedMessage("Linear Gradient");
268: }
269:
270: /**
271: * Externalizable Implementation
272: */
273: public void read(JETAObjectInput in) throws ClassNotFoundException,
274: IOException {
275: super .read(in.getSuperClassInput());
276: int version = in.readVersion();
277: m_start_color = (ColorProperty) in.readObject("startcolor");
278: m_end_color = (ColorProperty) in.readObject("endcolor");
279: m_magnitude = in.readFloat("magnitude");
280: m_direction = in.readInt("direction");
281: if (version == 1) {
282: /** deprecated */
283: boolean radial = in.readBoolean("radial");
284: }
285: }
286:
287: /**
288: * Externalizable Implementation
289: */
290: public void write(JETAObjectOutput out) throws IOException {
291:
292: super .write(out.getSuperClassOutput(JETAProperty.class));
293: out.writeVersion(VERSION);
294: out.writeObject("startcolor", m_start_color);
295: out.writeObject("endcolor", m_end_color);
296: out.writeFloat("magnitude", m_magnitude);
297: out.writeInt("direction", m_direction);
298: }
299:
300: }
|