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;
031:
032: import java.awt.BasicStroke;
033: import java.awt.Color;
034: import java.awt.Stroke;
035: import java.io.IOException;
036:
037: import com.jeta.forms.gui.beans.JETABean;
038: import com.jeta.forms.store.JETAObjectInput;
039: import com.jeta.forms.store.JETAObjectOutput;
040:
041: /**
042: * A <code>LineProperty</code> is used to define the attributes for a single
043: * line in a compound line component. See:
044: * {@link com.jeta.forms.store.CompoundLineProperty}
045: *
046: * @author Jeff Tassin
047: */
048: public class LineProperty extends JETAProperty {
049: static final long serialVersionUID = -6081805367391562785L;
050:
051: /**
052: * The version for this class.
053: */
054: public static final int VERSION = 2;
055:
056: /**
057: * The line color
058: */
059: private ColorProperty m_color_prop = new ColorProperty();
060:
061: /**
062: * The thickness of the line
063: */
064: private int m_thickness = 1;
065:
066: /**
067: * The stroke definition. Currently not used.
068: */
069: private float[] m_dash;
070:
071: /**
072: * For rendering.
073: */
074: private Stroke m_stroke;
075:
076: public static final String PROPERTY_ID = "lineStyle";
077:
078: /**
079: * Creates a <code>LineProperty</code> object with a thickness of 1 pixel
080: * and a black color.
081: */
082: public LineProperty() {
083: super (PROPERTY_ID);
084: }
085:
086: /**
087: * Returns the line color.
088: *
089: * @return the line color
090: */
091: public Color getColor() {
092: return m_color_prop.getColor();
093: }
094:
095: /**
096: * Returns the color property that defines the line color.
097: *
098: * @return the color property
099: */
100: public ColorProperty getColorProperty() {
101: return m_color_prop;
102: }
103:
104: /**
105: * Returns a Stroke object that is used to render the line in a graphics
106: * context.
107: *
108: * @param maxThickness
109: * defines the maximum thickness for the stroke. If the line
110: * thickness for this property is greater than the maxThickness,
111: * then the maxThickness is used.
112: * @return the stroke object.
113: */
114: public Stroke getStroke(int maxThickness) {
115: int thickness = Math.min(maxThickness, getThickness());
116: if (m_dash != null && m_dash.length > 1) {
117: return new BasicStroke((float) thickness,
118: BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 1.0f,
119: m_dash, 0.0f);
120: } else {
121: return new BasicStroke((float) thickness,
122: BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL);
123: }
124: }
125:
126: /**
127: * Returns the Stroke object used to render the line.
128: *
129: * @return a stroke defined by this property
130: */
131: public Stroke getStroke() {
132: if (m_stroke == null)
133: m_stroke = getStroke(getThickness());
134: return m_stroke;
135: }
136:
137: /**
138: * Returns the line thickness in pixels.
139: *
140: * @return the line thickness
141: */
142: public int getThickness() {
143: return m_thickness;
144: }
145:
146: /**
147: * Returns the stroke style. Currently not used.
148: *
149: * @return the stroke style
150: */
151: public float[] getDash() {
152: return m_dash;
153: }
154:
155: /**
156: * Sets the color property to constant using the specified color
157: *
158: * @param c
159: * the color to apply to this line.
160: */
161: public void setConstantColor(Color c) {
162: m_color_prop.setConstantColor(c);
163: }
164:
165: /**
166: * Sets the line color.
167: *
168: * @param cprop
169: * a color property.
170: */
171: public void setColorProperty(ColorProperty cprop) {
172: m_color_prop.setValue(cprop);
173: }
174:
175: /**
176: * Sets the line thickness
177: *
178: * @param t
179: * the line thickness
180: */
181: public void setThickness(int t) {
182: m_thickness = t;
183: m_stroke = null;
184: }
185:
186: /**
187: * Sets the stroke style. Currently not used.
188: */
189: public void setDash(float[] dash) {
190: m_dash = dash;
191: m_stroke = null;
192: }
193:
194: /**
195: * Sets this property to that of another LineProperty
196: *
197: * @param prop
198: * a LineProperty instance.
199: */
200: public void setValue(Object prop) {
201: if (prop instanceof LineProperty) {
202: LineProperty lp = (LineProperty) prop;
203: m_color_prop.setValue(lp.m_color_prop);
204: m_thickness = lp.m_thickness;
205: m_dash = lp.m_dash;
206: }
207: }
208:
209: /**
210: * Prints this component state to the console
211: */
212: public void print() {
213: System.out.println(" line component: thickness: "
214: + getThickness() + " color: " + getColor());
215: }
216:
217: /**
218: * JETAProperty implementation. No op for this property.
219: */
220: public void updateBean(JETABean jbean) {
221:
222: }
223:
224: /**
225: * JETAPersistable Implementation
226: */
227: public void read(JETAObjectInput in) throws ClassNotFoundException,
228: IOException {
229: super .read(in.getSuperClassInput());
230:
231: int version = in.readVersion();
232:
233: if (version == 1) {
234: Color color = (Color) in.readObject("color");
235: m_color_prop = new ColorProperty(color);
236: } else {
237: m_color_prop = (ColorProperty) in.readObject("color");
238: }
239: m_thickness = in.readInt("thickness");
240: m_dash = (float[]) in.readObject("dash");
241:
242: }
243:
244: /**
245: * JETAPersistable Implementation
246: */
247: public void write(JETAObjectOutput out) throws IOException {
248: super .write(out.getSuperClassOutput(JETAProperty.class));
249: out.writeVersion(VERSION);
250: out.writeObject("color", m_color_prop);
251: out.writeInt("thickness", m_thickness);
252: out.writeObject("dash", m_dash);
253: }
254:
255: }
|