001: /*
002: * Copyright 2006-2007 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt.
007: *
008: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
009: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
010: * the license for the specific language governing your rights and limitations.
011: *
012: * Additional Contributor(s): Martin Schmid gridvision engineering GmbH
013: */
014: package org.pentaho.reportdesigner.crm.report.model;
015:
016: import org.gjt.xpp.XmlPullNode;
017: import org.jetbrains.annotations.NonNls;
018: import org.jetbrains.annotations.NotNull;
019: import org.jetbrains.annotations.Nullable;
020: import org.pentaho.reportdesigner.crm.report.PropertyKeys;
021: import org.pentaho.reportdesigner.crm.report.util.GraphicUtils;
022: import org.pentaho.reportdesigner.lib.client.util.MathUtils;
023: import org.pentaho.reportdesigner.lib.common.xml.ObjectConverterFactory;
024: import org.pentaho.reportdesigner.lib.common.xml.XMLContext;
025: import org.pentaho.reportdesigner.lib.common.xml.XMLExternalizable;
026: import org.pentaho.reportdesigner.lib.common.xml.XMLWriter;
027:
028: import java.awt.*;
029: import java.io.IOException;
030: import java.util.logging.Level;
031: import java.util.logging.Logger;
032:
033: /**
034: * User: Martin
035: * Date: 25.05.2006
036: * Time: 11:59:08
037: */
038: public class BorderDefinition implements XMLExternalizable {
039: @NonNls
040: @NotNull
041: private static final Logger LOG = Logger
042: .getLogger(BorderDefinition.class.getName());
043:
044: private enum DashType {
045: @NotNull
046: DOTTED, @NotNull
047: DASHED, @NotNull
048: DOT_DASH, @NotNull
049: DOT_DOT_DASH, @NotNull
050: SOLID
051: }
052:
053: @NotNull
054: private Color color;
055:
056: private double width;
057: private int join;
058: private int cap;
059: private double miterlimit;
060: @Nullable
061: private double[] dash;
062: private double dashPhase;
063:
064: @NotNull
065: private BasicStroke basicStroke;
066:
067: public BorderDefinition() {
068: this (1);
069: }
070:
071: public BorderDefinition(double width) {
072: this (Color.BLACK, width, BasicStroke.JOIN_MITER,
073: BasicStroke.CAP_SQUARE, 10, null, 0);
074: }
075:
076: public BorderDefinition(@NotNull
077: BasicStroke basicStroke) {
078: this (Color.BLACK, basicStroke);
079: }
080:
081: public BorderDefinition(@NotNull
082: Color color, @NotNull
083: BasicStroke basicStroke) {
084: //noinspection ConstantConditions
085: if (color == null) {
086: throw new IllegalArgumentException("color must not be null");
087: }
088: //noinspection ConstantConditions
089: if (basicStroke == null) {
090: throw new IllegalArgumentException(
091: "basicStroke must not be null");
092: }
093:
094: this .color = color;
095: this .width = basicStroke.getLineWidth();
096: this .join = basicStroke.getLineJoin();
097: this .cap = basicStroke.getEndCap();
098: this .miterlimit = basicStroke.getMiterLimit();
099:
100: double[] dash = null;
101: if (basicStroke.getDashArray() != null) {
102: dash = new double[basicStroke.getDashArray().length];
103: for (int i = 0; i < basicStroke.getDashArray().length; i++) {
104: dash[i] = basicStroke.getDashArray()[i];
105: }
106: }
107:
108: this .dash = dash;
109: this .dashPhase = basicStroke.getDashPhase();
110:
111: this .basicStroke = new BasicStroke((float) width, cap, join,
112: (float) miterlimit, GraphicUtils.getArrayCopy(dash),
113: (float) dashPhase);
114: }
115:
116: public BorderDefinition(@NotNull
117: Color borderColor, double borderWidth, int join, int cap,
118: double miterlimit, @Nullable
119: double[] dash, double dashPhase) {
120: //noinspection ConstantConditions
121: if (borderColor == null) {
122: throw new IllegalArgumentException(
123: "borderColor must not be null");
124: }
125:
126: this .color = borderColor;
127: this .width = borderWidth;
128: this .join = join;
129: this .cap = cap;
130: this .miterlimit = miterlimit;
131: if (dash != null) {
132: this .dash = dash.clone();
133: }
134: this .dashPhase = dashPhase;
135:
136: basicStroke = new BasicStroke((float) borderWidth, cap, join,
137: (float) miterlimit, GraphicUtils.getArrayCopy(dash),
138: (float) dashPhase);
139: }
140:
141: @NotNull
142: public BasicStroke getBasicStroke() {
143: return basicStroke;
144: }
145:
146: @NotNull
147: public Color getColor() {
148: return color;
149: }
150:
151: public double getWidth() {
152: return width;
153: }
154:
155: public int getJoin() {
156: return join;
157: }
158:
159: public int getCap() {
160: return cap;
161: }
162:
163: public double getMiterlimit() {
164: return miterlimit;
165: }
166:
167: @Nullable
168: public double[] getDash() {
169: if (dash != null) {
170: return dash.clone();
171: }
172: return null;
173: }
174:
175: public double getDashPhase() {
176: return dashPhase;
177: }
178:
179: public boolean isSolid() {
180: return dash == null;
181: }
182:
183: public boolean isDotted() {
184: double[] dash = this .dash;
185: return dash != null
186: && MathUtils.approxEquals(dash, getDotted(width));
187: }
188:
189: public boolean isDashed() {
190: double[] dash = this .dash;
191: return dash != null
192: && MathUtils.approxEquals(dash, getDashed(width));
193: }
194:
195: public boolean isDotDash() {
196: double[] dash = this .dash;
197: return dash != null
198: && MathUtils.approxEquals(dash, getDotDash(width));
199: }
200:
201: public boolean isDotDotDash() {
202: double[] dash = this .dash;
203: return dash != null
204: && MathUtils.approxEquals(dash, getDotDotDash(width));
205: }
206:
207: @NotNull
208: public static double[] getDotted(double lineWidth) {
209: return new double[] { 0, 2 * lineWidth };
210: }
211:
212: @NotNull
213: public static double[] getDashed(double lineWidth) {
214: return new double[] { 6 * lineWidth, 6 * lineWidth };
215: }
216:
217: @NotNull
218: public static double[] getDotDash(double lineWidth) {
219: return new double[] { 0, 2 * lineWidth, 6 * lineWidth,
220: 2 * lineWidth };
221: }
222:
223: @NotNull
224: public static double[] getDotDotDash(double lineWidth) {
225: return new double[] { 0, 2 * lineWidth, 0, 2 * lineWidth,
226: 6 * lineWidth, 2 * lineWidth };
227: }
228:
229: @NotNull
230: public BorderDefinition derive(double width) {
231: return new BorderDefinition(color, width, join, cap,
232: miterlimit, dash, dashPhase);
233: }
234:
235: @NotNull
236: public BorderDefinition derive(@NotNull
237: Color color) {
238: return new BorderDefinition(color, width, join, cap,
239: miterlimit, dash, dashPhase);
240: }
241:
242: public void externalizeObject(@NotNull
243: XMLWriter xmlWriter, @NotNull
244: XMLContext xmlContext) throws IOException {
245: xmlWriter.writeAttribute(PropertyKeys.COLOR,
246: ObjectConverterFactory.getInstance()
247: .getColorConverter().getString(color));
248: xmlWriter.writeAttribute(PropertyKeys.WIDTH, String
249: .valueOf(width));
250: xmlWriter.writeAttribute(PropertyKeys.JOIN, String
251: .valueOf(join));
252: xmlWriter.writeAttribute(PropertyKeys.CAP, String.valueOf(cap));
253: xmlWriter.writeAttribute(PropertyKeys.MITERLIMIT, String
254: .valueOf(miterlimit));
255: if (dash != null) {
256: if (isDotted()) {
257: xmlWriter.writeAttribute(PropertyKeys.DASH,
258: DashType.DOTTED.toString());
259: } else if (isDashed()) {
260: xmlWriter.writeAttribute(PropertyKeys.DASH,
261: DashType.DASHED.toString());
262: } else if (isDotDash()) {
263: xmlWriter.writeAttribute(PropertyKeys.DASH,
264: DashType.DOT_DASH.toString());
265: } else if (isDotDotDash()) {
266: xmlWriter.writeAttribute(PropertyKeys.DASH,
267: DashType.DOT_DOT_DASH.toString());
268: }
269: }
270: //don't write anything for solid->default
271: xmlWriter.writeAttribute(PropertyKeys.DASH_PHASE, String
272: .valueOf(dashPhase));
273: }
274:
275: public void readObject(@NotNull
276: XmlPullNode node, @NotNull
277: XMLContext xmlContext) throws IOException {
278: color = ObjectConverterFactory
279: .getInstance()
280: .getColorConverter()
281: .getObject(
282: node
283: .getAttributeValueFromRawName(PropertyKeys.COLOR));
284: width = Double.parseDouble(node
285: .getAttributeValueFromRawName(PropertyKeys.WIDTH));
286: join = Integer.parseInt(node
287: .getAttributeValueFromRawName(PropertyKeys.JOIN));
288: cap = Integer.parseInt(node
289: .getAttributeValueFromRawName(PropertyKeys.CAP));
290: miterlimit = Double.parseDouble(node
291: .getAttributeValueFromRawName(PropertyKeys.MITERLIMIT));
292:
293: String dash = node
294: .getAttributeValueFromRawName(PropertyKeys.DASH);
295: if (dash != null) {
296: try {
297: DashType dashType = DashType.valueOf(dash);
298: if (dashType == DashType.DOTTED) {
299: this .dash = getDotted(width);
300: } else if (dashType == DashType.DASHED) {
301: this .dash = getDashed(width);
302: } else if (dashType == DashType.DOT_DASH) {
303: this .dash = getDotDash(width);
304: } else if (dashType == DashType.DOT_DOT_DASH) {
305: this .dash = getDotDotDash(width);
306: }
307: } catch (IllegalArgumentException e) {
308: if (LOG.isLoggable(Level.FINE))
309: LOG.log(Level.FINE, "BorderDefinition.readObject ",
310: e);
311: //fall back to default->solid->dashPhase=null
312: }
313: }
314: dashPhase = Double.parseDouble(node
315: .getAttributeValueFromRawName(PropertyKeys.DASH_PHASE));
316:
317: basicStroke = new BasicStroke((float) width, cap, join,
318: (float) miterlimit, GraphicUtils
319: .getArrayCopy(this .dash), (float) dashPhase);
320: }
321: }
|