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.lineal;
015:
016: import org.gjt.xpp.XmlPullNode;
017: import org.gjt.xpp.XmlPullParserException;
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.UndoConstants;
022: import org.pentaho.reportdesigner.crm.report.model.Report;
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.XMLContext;
026: import org.pentaho.reportdesigner.lib.common.xml.XMLExternalizable;
027: import org.pentaho.reportdesigner.lib.common.xml.XMLWriter;
028:
029: import java.io.IOException;
030: import java.util.LinkedHashSet;
031:
032: /**
033: * User: Martin
034: * Date: 26.01.2006
035: * Time: 10:44:19
036: */
037: public class LinealModel implements XMLExternalizable {
038: @Nullable
039: private Report report;
040: @NotNull
041: private LinkedHashSet<GuideLine> guideLines;
042:
043: @NotNull
044: private LinkedHashSet<LinealModelListener> linealModelListeners;
045:
046: public LinealModel(@Nullable
047: Report report) {
048: this .report = report;
049: guideLines = new LinkedHashSet<GuideLine>();
050: linealModelListeners = new LinkedHashSet<LinealModelListener>();
051: }
052:
053: public void setReport(@Nullable
054: Report report) {
055: this .report = report;
056: }
057:
058: @Nullable
059: public Report getReport() {
060: return report;
061: }
062:
063: @NotNull
064: public LinkedHashSet<GuideLine> getGuideLines() {
065: return guideLines;
066: }
067:
068: public void setActive(@NotNull
069: final GuideLine guideLine, final boolean active) {
070: if (guideLine.isActive() != active) {
071: final boolean oldActive = guideLine.isActive();
072:
073: Report report = this .report;
074:
075: if (report != null) {
076: Undo undo = report.getUndo();
077: if (undo != null && !undo.isInProgress()) {
078: undo.startTransaction(UndoConstants.SET_ACTIVE);
079: undo.add(new UndoEntry() {
080: public void undo() {
081: setActive(guideLine, oldActive);
082: }
083:
084: public void redo() {
085: setActive(guideLine, active);
086: }
087: });
088: undo.endTransaction();
089: }
090: }
091:
092: guideLine.setActive(active);
093: LinkedHashSet<LinealModelListener> lml = new LinkedHashSet<LinealModelListener>(
094: linealModelListeners);
095: for (LinealModelListener linealModelListener : lml) {
096: linealModelListener.activationChanged(guideLine);
097: }
098: }
099: }
100:
101: public void setPosition(@NotNull
102: final GuideLine guideLine, final double position) {
103: final double oldPosition = guideLine.getPosition();
104: guideLine.setPosition(position);
105:
106: Report report = this .report;
107:
108: if (report != null) {
109: Undo undo = report.getUndo();
110: if (undo != null && !undo.isInProgress()) {
111: undo.startTransaction(UndoConstants.SET_POSITION);
112: undo.add(new UndoEntry() {
113: public void undo() {
114: setPosition(guideLine, oldPosition);
115: }
116:
117: public void redo() {
118: setPosition(guideLine, position);
119: }
120: });
121: undo.endTransaction();
122: }
123: }
124:
125: LinkedHashSet<LinealModelListener> lml = new LinkedHashSet<LinealModelListener>(
126: linealModelListeners);
127: for (LinealModelListener linealModelListener : lml) {
128: linealModelListener.positionChanged(guideLine, oldPosition);
129: }
130: }
131:
132: public void removeGuideLine(@NotNull
133: final GuideLine guideLine) {
134: boolean removed = guideLines.remove(guideLine);
135: if (removed) {
136: Report report = this .report;
137: if (report != null) {
138: Undo undo = report.getUndo();
139: if (undo != null && !undo.isInProgress()) {
140: undo
141: .startTransaction(UndoConstants.REMOVE_GUIDE_LINE);
142: undo.add(new UndoEntry() {
143: public void undo() {
144: addGuidLine(guideLine);
145: }
146:
147: public void redo() {
148: removeGuideLine(guideLine);
149: }
150: });
151: undo.endTransaction();
152: }
153: }
154:
155: LinkedHashSet<LinealModelListener> lml = new LinkedHashSet<LinealModelListener>(
156: linealModelListeners);
157: for (LinealModelListener linealModelListener : lml) {
158: linealModelListener.guidLineRemoved(guideLine);
159: }
160: }
161: }
162:
163: public void addGuidLine(@NotNull
164: final GuideLine guideLine) {
165: boolean added = guideLines.add(guideLine);
166:
167: if (added) {
168: Report report = this .report;
169: if (report != null) {
170: Undo undo = report.getUndo();
171: if (undo != null && !undo.isInProgress()) {
172: undo.startTransaction(UndoConstants.ADD_GUIDE_LINE);
173: undo.add(new UndoEntry() {
174: public void undo() {
175: removeGuideLine(guideLine);
176: }
177:
178: public void redo() {
179: addGuidLine(guideLine);
180: }
181: });
182: undo.endTransaction();
183: }
184: }
185:
186: LinkedHashSet<LinealModelListener> lml = new LinkedHashSet<LinealModelListener>(
187: linealModelListeners);
188: for (LinealModelListener linealModelListener : lml) {
189: linealModelListener.guidLineAdded(guideLine);
190: }
191: }
192: }
193:
194: public void addLinealModelListener(@NotNull
195: LinealModelListener linealModelListener) {
196: linealModelListeners.add(linealModelListener);
197: }
198:
199: public void removeLinealModelListener(@NotNull
200: LinealModelListener linealModelListener) {
201: linealModelListeners.remove(linealModelListener);
202: }
203:
204: public void externalizeObject(@NotNull
205: XMLWriter xmlWriter, @NotNull
206: XMLContext xmlContext) throws IOException {
207: for (GuideLine guideLine : guideLines) {
208: xmlWriter.startElement(PropertyKeys.GUIDE_LINE);
209: guideLine.externalizeObject(xmlWriter, xmlContext);
210: xmlWriter.closeElement(PropertyKeys.GUIDE_LINE);
211: }
212: }
213:
214: public void readObject(@NotNull
215: XmlPullNode node, @NotNull
216: XMLContext xmlContext) throws IOException, XmlPullParserException {
217: while (!node.isFinished()) {
218: Object childNodeList = node.readNextChild();
219: if (childNodeList instanceof XmlPullNode) {
220: XmlPullNode child = (XmlPullNode) childNodeList;
221: if (PropertyKeys.GUIDE_LINE.equals(child.getRawName())) {
222: GuideLine guideLine = new GuideLine(0, false);
223: guideLine.readObject(child, xmlContext);
224: guideLines.add(guideLine);
225: }
226: }
227: }
228: }
229: }
|