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.PropertyKeys;
020: import org.pentaho.reportdesigner.crm.report.model.functions.ExpressionRegistry;
021: import org.pentaho.reportdesigner.crm.report.reportexporter.ReportCreationException;
022: import org.pentaho.reportdesigner.crm.report.reportexporter.ReportVisitor;
023: import org.pentaho.reportdesigner.lib.client.undo.Undo;
024: import org.pentaho.reportdesigner.lib.client.undo.UndoEntry;
025: import org.pentaho.reportdesigner.lib.common.xml.XMLConstants;
026: import org.pentaho.reportdesigner.lib.common.xml.XMLContext;
027: import org.pentaho.reportdesigner.lib.common.xml.XMLUtils;
028: import org.pentaho.reportdesigner.lib.common.xml.XMLWriter;
029:
030: import java.io.IOException;
031:
032: /**
033: * User: Martin
034: * Date: 27.10.2005
035: * Time: 08:50:18
036: */
037: public class ImageFieldReportElement extends TextReportElement {
038: @NotNull
039: private String fieldName;
040: private boolean keepAspect;
041: @NotNull
042: private Formula formula;
043:
044: public ImageFieldReportElement() {
045: keepAspect = true;
046: fieldName = "field"
047: + FieldNameFactory.getInstance().getNextFreeNumber(
048: "field");//NON-NLS
049: formula = new Formula("");
050: }
051:
052: @NotNull
053: public String getFieldName() {
054: return fieldName;
055: }
056:
057: public void setFieldName(@NotNull
058: final String fieldName) {
059: //noinspection ConstantConditions
060: if (fieldName == null) {
061: throw new IllegalArgumentException(
062: "fieldName must not be null");
063: }
064:
065: final String oldFieldName = this .fieldName;
066: this .fieldName = fieldName;
067:
068: Undo undo = getUndo();
069: if (undo != null && !undo.isInProgress()) {
070: undo.startTransaction(PropertyKeys.FIELD_NAME);
071: undo.add(new UndoEntry() {
072: public void undo() {
073: setFieldName(oldFieldName);
074: }
075:
076: public void redo() {
077: setFieldName(fieldName);
078: }
079: });
080: undo.endTransaction();
081: }
082:
083: firePropertyChange(PropertyKeys.FIELD_NAME, oldFieldName,
084: fieldName);
085: }
086:
087: public boolean isKeepAspect() {
088: return keepAspect;
089: }
090:
091: public void setKeepAspect(final boolean keepAspect) {
092: final boolean oldKeepAspect = this .keepAspect;
093: this .keepAspect = keepAspect;
094:
095: Undo undo = getUndo();
096: if (undo != null && !undo.isInProgress()) {
097: undo.startTransaction(PropertyKeys.KEEP_ASPECT);
098: undo.add(new UndoEntry() {
099: public void undo() {
100: setKeepAspect(oldKeepAspect);
101: }
102:
103: public void redo() {
104: setKeepAspect(keepAspect);
105: }
106: });
107: undo.endTransaction();
108: }
109:
110: firePropertyChange(PropertyKeys.KEEP_ASPECT, Boolean
111: .valueOf(oldKeepAspect), Boolean.valueOf(keepAspect));
112: }
113:
114: @NotNull
115: public Formula getFormula() {
116: return formula;
117: }
118:
119: public void setFormula(@NotNull
120: final Formula formula) {
121: //noinspection ConstantConditions
122: if (formula == null) {
123: throw new IllegalArgumentException(
124: "formula must not be null");
125: }
126:
127: final Formula oldFormula = this .formula;
128: this .formula = formula;
129:
130: Undo undo = getUndo();
131: if (undo != null && !undo.isInProgress()) {
132: undo.startTransaction(PropertyKeys.FORMULA);
133: undo.add(new UndoEntry() {
134: public void undo() {
135: setFormula(oldFormula);
136: }
137:
138: public void redo() {
139: setFormula(formula);
140: }
141: });
142: undo.endTransaction();
143: }
144:
145: firePropertyChange(PropertyKeys.FORMULA, oldFormula, formula);
146: }
147:
148: @NotNull
149: public String getPaintText() {
150: return fieldName;
151: }
152:
153: public void accept(@Nullable
154: Object parent, @NotNull
155: ReportVisitor reportVisitor) throws ReportCreationException {
156: /*Object newParent = */
157: reportVisitor.visit(parent, this );
158: }
159:
160: protected void externalizeElements(@NotNull
161: XMLWriter xmlWriter, @NotNull
162: XMLContext xmlContext) throws IOException {
163: super .externalizeElements(xmlWriter, xmlContext);
164:
165: xmlWriter.writeProperty(PropertyKeys.FIELD_NAME, fieldName);
166: xmlWriter.writeProperty(PropertyKeys.KEEP_ASPECT, String
167: .valueOf(keepAspect));
168:
169: xmlWriter.startElement(PropertyKeys.FORMULA);
170: formula.externalizeObject(xmlWriter, xmlContext);
171: xmlWriter.closeElement(PropertyKeys.FORMULA);
172: }
173:
174: protected void readElement(@NotNull
175: ExpressionRegistry expressions, @NotNull
176: XmlPullNode node, @NotNull
177: XMLContext xmlContext) throws Exception {
178: super .readElement(expressions, node, xmlContext);
179:
180: if (XMLConstants.PROPERTY.equals(node.getRawName())
181: && PropertyKeys.FIELD_NAME
182: .equals(node
183: .getAttributeValueFromRawName(XMLConstants.NAME))) {
184: fieldName = XMLUtils.readProperty(PropertyKeys.FIELD_NAME,
185: node);
186: } else if (XMLConstants.PROPERTY.equals(node.getRawName())
187: && PropertyKeys.KEEP_ASPECT
188: .equals(node
189: .getAttributeValueFromRawName(XMLConstants.NAME))) {
190: keepAspect = Boolean.parseBoolean(XMLUtils.readProperty(
191: PropertyKeys.KEEP_ASPECT, node));
192: } else if (PropertyKeys.FORMULA.equals(node.getRawName())) {
193: formula = new Formula("");
194: formula.readObject(node, xmlContext);
195: }
196: }
197:
198: }
|