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.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.common.xml.XMLConstants;
027: import org.pentaho.reportdesigner.lib.common.xml.XMLContext;
028: import org.pentaho.reportdesigner.lib.common.xml.XMLUtils;
029: import org.pentaho.reportdesigner.lib.common.xml.XMLWriter;
030:
031: import java.io.IOException;
032: import java.util.ArrayList;
033:
034: /**
035: * User: Martin
036: * Date: 25.10.2005
037: * Time: 10:09:46
038: */
039: public class ReportGroup extends ReportElement {
040: @NotNull
041: private static final String[] EMPTY_STRING_ARRAY = new String[0];
042:
043: @NotNull
044: private BandToplevelGroupReportElement groupHeader;
045:
046: @NotNull
047: private BandToplevelGroupReportElement groupFooter;
048: @NotNull
049: private String[] groupFields;
050:
051: public ReportGroup() {
052: groupHeader = new BandToplevelGroupReportElement();
053: groupHeader.setBandToplevelType(BandToplevelType.GROUP_HEADER);
054: groupHeader.setShowInLayoutGUI(false);
055:
056: groupFooter = new BandToplevelGroupReportElement();
057: groupFooter.setBandToplevelType(BandToplevelType.GROUP_FOOTER);
058: groupFooter.setShowInLayoutGUI(false);
059:
060: super .addChild(groupHeader);
061: super .addChild(groupFooter);
062:
063: groupFields = EMPTY_STRING_ARRAY;
064: }
065:
066: public void addChild(@NotNull
067: ReportElement child) {
068: if (child instanceof BandToplevelGroupReportElement) {
069: BandToplevelGroupReportElement bandToplevelGroupReportElement = (BandToplevelGroupReportElement) child;
070: if (bandToplevelGroupReportElement.getBandToplevelType() == BandToplevelType.GROUP_HEADER) {
071: ReportElement childToRemove = getChildren().get(0);
072: super .removeChild(childToRemove);
073: super .insertChild(bandToplevelGroupReportElement, 0);
074: groupHeader = bandToplevelGroupReportElement;
075: } else if (bandToplevelGroupReportElement
076: .getBandToplevelType() == BandToplevelType.GROUP_FOOTER) {
077: ReportElement childToRemove = getChildren().get(
078: getChildren().size() - 1);
079: super .removeChild(childToRemove);
080: super .addChild(bandToplevelGroupReportElement);
081: groupFooter = bandToplevelGroupReportElement;
082: } else {
083: throw new UnsupportedOperationException(
084: "can not add additional elements to group");
085: }
086: } else if (child instanceof ReportGroup
087: && getChildren().size() == 2) {
088: super .insertChild(child, 1);
089: } else {
090: throw new UnsupportedOperationException(
091: "can not add additional elements to group");
092: }
093: }
094:
095: public void removeChild(@NotNull
096: ReportElement reportElement) {
097: if (reportElement instanceof ReportGroup
098: && getChildren().size() == 3) {
099: super .removeChild(reportElement);
100: } else {
101: throw new UnsupportedOperationException(
102: "can not remove elements from group");
103: }
104: }
105:
106: @NotNull
107: public BandToplevelGroupReportElement getGroupHeader() {
108: return groupHeader;
109: }
110:
111: @NotNull
112: public BandToplevelGroupReportElement getGroupFooter() {
113: return groupFooter;
114: }
115:
116: @NotNull
117: public String[] getGroupFields() {
118: return groupFields;
119: }
120:
121: public void setGroupFields(@NotNull
122: @NonNls
123: final String[] groupFields) {
124: //noinspection ConstantConditions
125: if (groupFields == null) {
126: throw new IllegalArgumentException(
127: "groupFields must not be null");
128: }
129:
130: final String[] oldGroupFields = this .groupFields;
131: this .groupFields = groupFields;
132:
133: Undo undo = getUndo();
134: if (undo != null && !undo.isInProgress()) {
135: undo.startTransaction(PropertyKeys.GROUP_FIELDS);
136: undo.add(new UndoEntry() {
137: public void undo() {
138: setGroupFields(oldGroupFields);
139: }
140:
141: public void redo() {
142: setGroupFields(groupFields);
143: }
144: });
145: undo.endTransaction();
146: }
147:
148: firePropertyChange(PropertyKeys.GROUP_FIELDS, oldGroupFields,
149: groupFields);
150: }
151:
152: public void accept(@Nullable
153: Object parent, @NotNull
154: ReportVisitor reportVisitor) throws ReportCreationException {
155: Object newParent = reportVisitor.visit(parent, this );
156:
157: ArrayList<ReportElement> children = new ArrayList<ReportElement>(
158: getChildren());
159: for (ReportElement reportElement : children) {
160: reportElement.accept(newParent, reportVisitor);
161: }
162: }
163:
164: protected void externalizeElements(@NotNull
165: XMLWriter xmlWriter, @NotNull
166: XMLContext xmlContext) throws IOException {
167: super .externalizeElements(xmlWriter, xmlContext);
168:
169: if (groupFields.length > 0) {
170: xmlWriter.startElement(XMLConstants.PROPERTY);
171: xmlWriter.writeAttribute(XMLConstants.NAME,
172: PropertyKeys.GROUP_FIELDS);
173: xmlWriter.writeAttribute(XMLConstants.ARRAY, "true");
174: for (int i = 0; i < groupFields.length; i++) {
175: String groupField = groupFields[i];
176: if (groupField != null) {
177: xmlWriter.writeProperty(String.valueOf(i),
178: groupField);
179: }
180: }
181: xmlWriter.closeElement(XMLConstants.PROPERTY);
182: }
183: }
184:
185: protected void readElement(@NotNull
186: ExpressionRegistry expressions, @NotNull
187: XmlPullNode node, @NotNull
188: XMLContext xmlContext) throws Exception {
189: super .readElement(expressions, node, xmlContext);
190:
191: if (XMLConstants.PROPERTY.equals(node.getRawName())
192: && PropertyKeys.GROUP_FIELDS
193: .equals(node
194: .getAttributeValueFromRawName(XMLConstants.NAME))) {
195: if (node.getAttributeValueFromRawName(XMLConstants.ARRAY) != null) {
196: ArrayList<String> al = new ArrayList<String>();
197: int n = 0;
198: while (!node.isFinished()) {
199: Object childNodeList = node.readNextChild();
200: if (childNodeList instanceof XmlPullNode) {
201: XmlPullNode child = (XmlPullNode) childNodeList;
202: if (XMLConstants.PROPERTY.equals(child
203: .getRawName())) {
204: al.add(XMLUtils.readProperty(String
205: .valueOf(n), child));
206: n++;
207: }
208: }
209: }
210: groupFields = al.toArray(new String[al.size()]);
211: }
212: }
213: }
214:
215: }
|