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.awt.*;
031: import java.io.IOException;
032:
033: /**
034: * User: Martin
035: * Date: 27.10.2005
036: * Time: 09:00:29
037: */
038: public class TextFieldReportElement extends TextReportElement {
039: @NotNull
040: private String fieldName;
041: @NotNull
042: private String nullString;
043: @NotNull
044: private Formula formula;
045:
046: public TextFieldReportElement() {
047: fieldName = "field"
048: + FieldNameFactory.getInstance().getNextFreeNumber(
049: "field");//NON-NLS
050: nullString = "";
051: formula = new Formula("");
052: setName("TextField@" + System.identityHashCode(this ));
053: }
054:
055: public TextFieldReportElement(@NotNull
056: Font font) {
057: super (font);
058:
059: fieldName = "field"
060: + FieldNameFactory.getInstance().getNextFreeNumber(
061: "field");//NON-NLS
062: nullString = "";
063: formula = new Formula("");
064: setName("TextField@" + System.identityHashCode(this ));
065: }
066:
067: @NotNull
068: public String getFieldName() {
069: return fieldName;
070: }
071:
072: public void setFieldName(@NotNull
073: final String fieldName) {
074: //noinspection ConstantConditions
075: if (fieldName == null) {
076: throw new IllegalArgumentException(
077: "fieldName must not be null");
078: }
079:
080: final String oldFieldName = this .fieldName;
081: this .fieldName = fieldName;
082:
083: Undo undo = getUndo();
084: if (undo != null && !undo.isInProgress()) {
085: undo.startTransaction(PropertyKeys.FIELD_NAME);
086: undo.add(new UndoEntry() {
087: public void undo() {
088: setFieldName(oldFieldName);
089: }
090:
091: public void redo() {
092: setFieldName(fieldName);
093: }
094: });
095: undo.endTransaction();
096: }
097:
098: firePropertyChange(PropertyKeys.FIELD_NAME, oldFieldName,
099: fieldName);
100: }
101:
102: @NotNull
103: public String getNullString() {
104: return nullString;
105: }
106:
107: public void setNullString(@NotNull
108: final String nullString) {
109: //noinspection ConstantConditions
110: if (nullString == null) {
111: throw new IllegalArgumentException(
112: "nullString must not be null");
113: }
114:
115: final String oldNullString = this .nullString;
116: this .nullString = nullString;
117:
118: Undo undo = getUndo();
119: if (undo != null && !undo.isInProgress()) {
120: undo.startTransaction(PropertyKeys.PAGE_BREAK_BEFORE);
121: undo.add(new UndoEntry() {
122: public void undo() {
123: setNullString(oldNullString);
124: }
125:
126: public void redo() {
127: setNullString(nullString);
128: }
129: });
130: undo.endTransaction();
131: }
132:
133: firePropertyChange(PropertyKeys.NULL_STRING, oldNullString,
134: nullString);
135: }
136:
137: @NotNull
138: public Formula getFormula() {
139: return formula;
140: }
141:
142: public void setFormula(@NotNull
143: final Formula formula) {
144: //noinspection ConstantConditions
145: if (formula == null) {
146: throw new IllegalArgumentException(
147: "formula must not be null");
148: }
149:
150: final Formula oldFormula = this .formula;
151: this .formula = formula;
152:
153: Undo undo = getUndo();
154: if (undo != null && !undo.isInProgress()) {
155: undo.startTransaction(PropertyKeys.FORMULA);
156: undo.add(new UndoEntry() {
157: public void undo() {
158: setFormula(oldFormula);
159: }
160:
161: public void redo() {
162: setFormula(formula);
163: }
164: });
165: undo.endTransaction();
166: }
167:
168: firePropertyChange(PropertyKeys.FORMULA, oldFormula, formula);
169: }
170:
171: @NotNull
172: public String getPaintText() {
173: String fieldName = getFieldName();
174: if ("".equals(fieldName)) {
175: return getFormula().getText();
176: }
177: return fieldName;
178: }
179:
180: public void accept(@Nullable
181: Object parent, @NotNull
182: ReportVisitor reportVisitor) throws ReportCreationException {
183: /*Object newParent = */
184: reportVisitor.visit(parent, this );
185: }
186:
187: protected void externalizeElements(@NotNull
188: XMLWriter xmlWriter, @NotNull
189: XMLContext xmlContext) throws IOException {
190: super .externalizeElements(xmlWriter, xmlContext);
191:
192: xmlWriter.writeProperty(PropertyKeys.FIELD_NAME, fieldName);
193: xmlWriter.writeProperty(PropertyKeys.NULL_STRING, nullString);
194:
195: xmlWriter.startElement(PropertyKeys.FORMULA);
196: formula.externalizeObject(xmlWriter, xmlContext);
197: xmlWriter.closeElement(PropertyKeys.FORMULA);
198: }
199:
200: protected void readElement(@NotNull
201: ExpressionRegistry expressions, @NotNull
202: XmlPullNode node, @NotNull
203: XMLContext xmlContext) throws Exception {
204: super .readElement(expressions, node, xmlContext);
205:
206: if (XMLConstants.PROPERTY.equals(node.getRawName())
207: && PropertyKeys.FIELD_NAME
208: .equals(node
209: .getAttributeValueFromRawName(XMLConstants.NAME))) {
210: fieldName = XMLUtils.readProperty(PropertyKeys.FIELD_NAME,
211: node);
212: } else if (XMLConstants.PROPERTY.equals(node.getRawName())
213: && PropertyKeys.NULL_STRING
214: .equals(node
215: .getAttributeValueFromRawName(XMLConstants.NAME))) {
216: nullString = XMLUtils.readProperty(
217: PropertyKeys.NULL_STRING, node);
218: } else if (PropertyKeys.FORMULA.equals(node.getRawName())) {
219: formula = new Formula("");
220: formula.readObject(node, xmlContext);
221: }
222: }
223: }
|