001: /*
002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
003: * http://www.jaspersoft.com.
004: *
005: * Unless you have purchased a commercial license agreement from JasperSoft,
006: * the following license terms apply:
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License version 2 as published by
010: * the Free Software Foundation.
011: *
012: * This program is distributed WITHOUT ANY WARRANTY; and without the
013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
014: * See the GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
018: * or write to:
019: *
020: * Free Software Foundation, Inc.,
021: * 59 Temple Place - Suite 330,
022: * Boston, MA USA 02111-1307
023: *
024: *
025: *
026: *
027: * ReplacedElementsOperation.java
028: *
029: * Created on 19 giugno 2003, 23.23
030: *
031: */
032:
033: package it.businesslogic.ireport.undo;
034:
035: import it.businesslogic.ireport.gui.event.*;
036: import it.businesslogic.ireport.*;
037: import it.businesslogic.ireport.gui.*;
038: import it.businesslogic.ireport.util.*;
039:
040: import java.util.*;
041:
042: /**
043: * This class handle the Insert operation.
044: * As all operations, the costructor take the JReportFrame (owner of the element)
045: * The ReportElement is not cloned, this can be a problem if not all undo operations
046: * are correctly logged and handled.
047: * @author Giulio Toffoli
048: */
049: public class ReplacedElementsOperation implements
050: it.businesslogic.ireport.UndoOperation {
051:
052: /*
053: * The report elements that was replaced.
054: */
055: private Vector elements = null;
056:
057: private JReportFrame jrf = null;
058: private CrosstabReportElement crosstabReportElement = null;
059:
060: /** Creates a new instance of InsertElementOperation */
061: public ReplacedElementsOperation(JReportFrame jrf,
062: CrosstabReportElement crosstabReportElement) {
063: this .setCrosstabReportElement(crosstabReportElement);
064: this .elements = new Vector();
065: this .jrf = jrf;
066: }
067:
068: public ReplacedElementsOperation(JReportFrame jrf) {
069: this (jrf, null);
070: }
071:
072: public void redo() {
073: // We must remove our element...
074: if (jrf == null)
075: return;
076: Enumeration e = this .getElements().elements();
077: Vector added_elements = new Vector();
078: Vector removed_elements = new Vector();
079:
080: while (e.hasMoreElements()) {
081: ReplacedElementItem ei = (ReplacedElementItem) e
082: .nextElement();
083: ReportElement newElement = ei.getNewElement();
084: ReportElement oldElement = ei.getOldElement();
085:
086: if (getCrosstabReportElement() != null) {
087: int index = getCrosstabReportElement().getElements()
088: .indexOf(oldElement);
089: getCrosstabReportElement().getElements().remove(
090: oldElement);
091: getCrosstabReportElement().getElements().add(index,
092: newElement);
093: added_elements.add(newElement);
094: removed_elements.add(oldElement);
095: } else {
096: int index = jrf.getReport().getElements().indexOf(
097: oldElement);
098: jrf.getReport().getElements().remove(oldElement);
099: jrf.getReport().getElements().add(index, newElement);
100: added_elements.add(newElement);
101: removed_elements.add(oldElement);
102: }
103: }
104: jrf
105: .fireReportListenerReportElementsChanged(new ReportElementChangedEvent(
106: jrf, getCrosstabReportElement(),
107: removed_elements,
108: ReportElementChangedEvent.REMOVED));
109: jrf
110: .fireReportListenerReportElementsChanged(new ReportElementChangedEvent(
111: jrf, getCrosstabReportElement(),
112: added_elements, ReportElementChangedEvent.ADDED));
113: if (getCrosstabReportElement() != null)
114: jrf.getCrosstabEditor(getCrosstabReportElement())
115: .getPanelEditor().repaint();
116: else
117: jrf.getReportPanel().repaint();
118: }
119:
120: public void undo() {
121: // We must add our element...
122: if (jrf == null)
123: return;
124:
125: Vector added_elements = new Vector();
126: Vector removed_elements = new Vector();
127:
128: for (int i = this .getElements().size() - 1; i >= 0; --i) {
129: ReplacedElementItem ei = (ReplacedElementItem) getElements()
130: .get(i);
131: ReportElement newElement = ei.getNewElement();
132: ReportElement oldElement = ei.getOldElement();
133:
134: if (getCrosstabReportElement() != null) {
135: int index = getCrosstabReportElement().getElements()
136: .indexOf(newElement);
137: getCrosstabReportElement().getElements().remove(
138: newElement);
139: getCrosstabReportElement().getElements().add(index,
140: oldElement);
141: added_elements.add(oldElement);
142: removed_elements.add(newElement);
143: } else {
144: int index = jrf.getReport().getElements().indexOf(
145: newElement);
146: jrf.getReport().getElements().remove(newElement);
147: jrf.getReport().getElements().add(index, oldElement);
148: added_elements.add(oldElement);
149: removed_elements.add(newElement);
150: }
151:
152: }
153: jrf
154: .fireReportListenerReportElementsChanged(new ReportElementChangedEvent(
155: jrf, getCrosstabReportElement(),
156: removed_elements,
157: ReportElementChangedEvent.REMOVED));
158: jrf
159: .fireReportListenerReportElementsChanged(new ReportElementChangedEvent(
160: jrf, getCrosstabReportElement(),
161: added_elements, ReportElementChangedEvent.ADDED));
162:
163: if (getCrosstabReportElement() != null)
164: jrf.getCrosstabEditor(getCrosstabReportElement())
165: .getPanelEditor().repaint();
166: else
167: jrf.getReportPanel().repaint();
168: }
169:
170: public String toString() {
171: return "Transformed element(s)";
172: }
173:
174: /** Getter for property elements.
175: * @return Value of property elements.
176: *
177: * To add an element, use the addElement method. Don't access directly
178: * addElement from the vector elements
179: *
180: */
181: public java.util.Vector getElements() {
182: return elements;
183: }
184:
185: /** Setter for property elements.
186: * @param elements New value of property elements.
187: *
188: */
189: public void setElements(java.util.Vector elements) {
190: this .elements = elements;
191: }
192:
193: /*
194: * Add an element to the list of elements handled by this
195: * undo operation. The position is relative to the z position
196: * in the elements vector.
197: * If the undo mechanism works fine, the requested position is
198: * ever available.
199: * The position value should be retrived when the element
200: * is added.
201: */
202: public void addElement(ReportElement oldEelement,
203: ReportElement newElement) {
204: ReplacedElementItem ei = new ReplacedElementItem(oldEelement,
205: newElement);
206: getElements().add(ei);
207: }
208:
209: public CrosstabReportElement getCrosstabReportElement() {
210: return crosstabReportElement;
211: }
212:
213: public void setCrosstabReportElement(
214: CrosstabReportElement crosstabReportElement) {
215: this.crosstabReportElement = crosstabReportElement;
216: }
217:
218: }
|