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.NotNull;
018: import org.jetbrains.annotations.Nullable;
019: import org.pentaho.reportdesigner.crm.report.GraphicsContext;
020: import org.pentaho.reportdesigner.crm.report.PropertyKeys;
021: import org.pentaho.reportdesigner.crm.report.model.functions.ExpressionRegistry;
022: import org.pentaho.reportdesigner.crm.report.reportexporter.ReportCreationException;
023: import org.pentaho.reportdesigner.crm.report.reportexporter.ReportVisitor;
024: import org.pentaho.reportdesigner.lib.client.undo.Undo;
025: import org.pentaho.reportdesigner.lib.client.undo.UndoEntry;
026: import org.pentaho.reportdesigner.lib.client.util.DoubleDimension;
027: import org.pentaho.reportdesigner.lib.client.util.MathUtils;
028: import org.pentaho.reportdesigner.lib.common.xml.ObjectConverterFactory;
029: import org.pentaho.reportdesigner.lib.common.xml.XMLConstants;
030: import org.pentaho.reportdesigner.lib.common.xml.XMLContext;
031: import org.pentaho.reportdesigner.lib.common.xml.XMLUtils;
032: import org.pentaho.reportdesigner.lib.common.xml.XMLWriter;
033:
034: import java.awt.*;
035: import java.awt.geom.Line2D;
036: import java.awt.geom.Rectangle2D;
037: import java.io.IOException;
038:
039: /**
040: * User: Martin
041: * Date: 20.10.2005
042: * Time: 15:40:13
043: */
044: public class LineReportElement extends ReportElement {
045: @NotNull
046: private LineDirection direction;
047: @NotNull
048: private LineDefinition lineDefinition;
049:
050: public LineReportElement() {
051: lineDefinition = new LineDefinition();
052: direction = LineDirection.HORIZONTAL;
053:
054: setElementRepaintBorder(0);
055: }
056:
057: @NotNull
058: public LineDefinition getLineDefinition() {
059: return lineDefinition;
060: }
061:
062: public void setLineDefinition(@NotNull
063: final LineDefinition lineDefinition) {
064: //noinspection ConstantConditions
065: if (lineDefinition == null) {
066: throw new IllegalArgumentException(
067: "lineDefinition must not be null");
068: }
069:
070: final LineDefinition oldLineDefinition = this .lineDefinition;
071: this .lineDefinition = lineDefinition;
072: setElementRepaintBorder(lineDefinition.getWidth()
073: * MathUtils.SQRT_OF_2 / 2 + 1);
074:
075: Undo undo = getUndo();
076: if (undo != null && !undo.isInProgress()) {
077: undo.startTransaction(PropertyKeys.LINE_DEFINITION);
078: undo.add(new UndoEntry() {
079: public void undo() {
080: setLineDefinition(oldLineDefinition);
081: }
082:
083: public void redo() {
084: setLineDefinition(lineDefinition);
085: }
086: });
087: undo.endTransaction();
088: }
089: firePropertyChange(PropertyKeys.LINE_DEFINITION,
090: oldLineDefinition, lineDefinition);
091: }
092:
093: @NotNull
094: public LineDirection getDirection() {
095: return direction;
096: }
097:
098: public void setDirection(@NotNull
099: final LineDirection direction) {
100: //noinspection ConstantConditions
101: if (direction == null) {
102: throw new IllegalArgumentException(
103: "direction must not be null");
104: }
105:
106: final LineDirection oldDirection = this .direction;
107: this .direction = direction;
108:
109: setMinimumSize(new DoubleDimension(
110: getMinimumSize().getHeight(), getMinimumSize()
111: .getWidth()));
112:
113: Undo undo = getUndo();
114: if (undo != null && !undo.isInProgress()) {
115: undo.startTransaction(PropertyKeys.DIRECTION);
116: undo.add(new UndoEntry() {
117: public void undo() {
118: setDirection(oldDirection);
119: }
120:
121: public void redo() {
122: setDirection(direction);
123: }
124: });
125: undo.endTransaction();
126: }
127: firePropertyChange(PropertyKeys.DIRECTION, oldDirection,
128: direction);
129: }
130:
131: public void paint(@NotNull
132: GraphicsContext graphicsContext, @NotNull
133: Graphics2D g2d) {
134: Shape origClip = g2d.getClip();
135: Color origColor = g2d.getColor();
136: Composite origComposite = g2d.getComposite();
137: Stroke origStroke = g2d.getStroke();
138:
139: Rectangle2D.Double rect = getRectangle();
140:
141: paintBackroundAndBorder(g2d);
142:
143: g2d.setColor(lineDefinition.getColor());
144: //if (color != null)
145: //{
146: // g2d.setColor(color);
147: //}
148: //else
149: //{
150: // g2d.setColor(Color.BLACK);
151: //}
152:
153: if (direction == LineDirection.HORIZONTAL) {
154: g2d.setStroke(lineDefinition.getBasicStroke());
155: Line2D.Double s = new Line2D.Double(rect.getX(), (rect
156: .getY() + rect.getHeight() / 2), rect.getX()
157: + rect.getWidth(),
158: (rect.getY() + rect.getHeight() / 2));
159: g2d.draw(s);
160: } else {
161: g2d.setStroke(lineDefinition.getBasicStroke());
162: Line2D.Double s = new Line2D.Double((rect.getX() + rect
163: .getWidth() / 2), rect.getY(), (rect.getX() + rect
164: .getWidth() / 2), rect.getY() + rect.getHeight());
165: g2d.draw(s);
166: }
167:
168: g2d.setStroke(origStroke);
169: g2d.setComposite(origComposite);
170: g2d.setColor(origColor);
171: g2d.setClip(origClip);
172: }
173:
174: public void accept(@Nullable
175: Object parent, @NotNull
176: ReportVisitor reportVisitor) throws ReportCreationException {
177: /*Object newParent = */
178: reportVisitor.visit(parent, this );
179: }
180:
181: protected void externalizeElements(@NotNull
182: XMLWriter xmlWriter, @NotNull
183: XMLContext xmlContext) throws IOException {
184: super .externalizeElements(xmlWriter, xmlContext);
185:
186: //xmlWriter.writeProperty(PropertyKeys.COLOR, ObjectConverterFactory.getInstance().getColorConverter().getString(color));
187: //xmlWriter.writeProperty(PropertyKeys.LINE_WIDTH, String.valueOf(lineWidth));
188: xmlWriter.writeProperty(PropertyKeys.DIRECTION, direction
189: .toString());
190:
191: xmlWriter.startElement(PropertyKeys.LINE_DEFINITION);
192: lineDefinition.externalizeObject(xmlWriter, xmlContext);
193: xmlWriter.closeElement(PropertyKeys.LINE_DEFINITION);
194: }
195:
196: protected void readElement(@NotNull
197: ExpressionRegistry expressions, @NotNull
198: XmlPullNode node, @NotNull
199: XMLContext xmlContext) throws Exception {
200: super .readElement(expressions, node, xmlContext);
201:
202: if (XMLConstants.PROPERTY.equals(node.getRawName())
203: && PropertyKeys.COLOR
204: .equals(node
205: .getAttributeValueFromRawName(XMLConstants.NAME))) {
206: //must be old->there will be no separate borderDefinition in the xml
207: Color color = ObjectConverterFactory.getInstance()
208: .getColorConverter().getObject(
209: XMLUtils.readProperty(PropertyKeys.COLOR,
210: node));
211: lineDefinition = new LineDefinition(color, lineDefinition
212: .getWidth(), lineDefinition.getJoin(),
213: lineDefinition.getCap(), lineDefinition
214: .getMiterlimit(), lineDefinition.getDash(),
215: lineDefinition.getDashPhase());
216: } else if (XMLConstants.PROPERTY.equals(node.getRawName())
217: && PropertyKeys.LINE_WIDTH
218: .equals(node
219: .getAttributeValueFromRawName(XMLConstants.NAME))) {
220: //must be old->there will be no separate borderDefinition in the xml
221: double lineWidth = Double.parseDouble(XMLUtils
222: .readProperty(PropertyKeys.LINE_WIDTH, node));
223: lineDefinition = new LineDefinition(lineDefinition
224: .getColor(), lineWidth, lineDefinition.getJoin(),
225: lineDefinition.getCap(), lineDefinition
226: .getMiterlimit(), lineDefinition.getDash(),
227: lineDefinition.getDashPhase());
228: } else if (XMLConstants.PROPERTY.equals(node.getRawName())
229: && PropertyKeys.DIRECTION
230: .equals(node
231: .getAttributeValueFromRawName(XMLConstants.NAME))) {
232: direction = LineDirection.valueOf(XMLUtils.readProperty(
233: PropertyKeys.DIRECTION, node));
234: } else if (PropertyKeys.LINE_DEFINITION.equals(node
235: .getRawName())) {
236: lineDefinition = new LineDefinition();
237: lineDefinition.readObject(node, xmlContext);
238: }
239: }
240:
241: }
|