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: * TransformElementsOperation.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:
039: import java.util.*;
040:
041: /**
042: * This class handle the Insert operation.
043: * As all operations, the costructor take the JReportFrame (owner of the element)
044: * The ReportElement is not cloned, this can be a problem if not all undo operations
045: * are correctly logged and handled.
046: * @author Giulio Toffoli
047: * Modified by Robert Lamping, June 3, 2005
048: * // Also other Transformation can now be included
049: * // Once this works it should be redesigned again.
050: * // E.g. make a factory to retrieve the required transformation class.
051: * //
052: */
053: public class TransformElementsOperation implements
054: it.businesslogic.ireport.UndoOperation {
055:
056: /*
057: * The report elements that was inserted.
058: */
059: private Vector transformations = null;
060:
061: private JReportFrame jrf = null;
062: private CrosstabReportElement crosstabReportElement = null;
063:
064: /** Creates a new instance of InsertElementOperation */
065: public TransformElementsOperation(JReportFrame jrf) {
066: this .transformations = new Vector();
067: this .jrf = jrf;
068: }
069:
070: /** Creates a new instance of InsertElementOperation */
071: public TransformElementsOperation(JReportFrame jrf,
072: CrosstabReportElement crosstabReportElement) {
073: this .transformations = new Vector();
074: this .crosstabReportElement = crosstabReportElement;
075: this .jrf = jrf;
076: }
077:
078: public void redo() {
079: if (jrf == null)
080: return;
081:
082: if (crosstabReportElement != null) {
083: jrf.getCrosstabEditor(crosstabReportElement)
084: .getPanelEditor().setSelectedElement(null);
085: } else {
086: jrf.setSelectedElement(null);
087: }
088:
089: Vector changed_elements = new Vector();
090:
091: for (int i = 0; i < this .getTransformations().size(); ++i) {
092: ((UndoOperation) (getTransformations().get(i))).redo();
093:
094: if (getTransformations().get(i) instanceof ElementTransformation) {
095: ElementTransformation pe = (ElementTransformation) getTransformations()
096: .get(i);
097: ReportElement element = pe.element;
098: // Add element....]
099: if (crosstabReportElement != null) {
100: jrf.getCrosstabEditor(crosstabReportElement)
101: .getPanelEditor().addSelectedElement(
102: element, false);
103: } else {
104: jrf.addSelectedElement(element, false);
105: }
106:
107: changed_elements.add(element);
108: }
109:
110: }
111:
112: jrf
113: .fireReportListenerReportElementsChanged(new ReportElementChangedEvent(
114: jrf, crosstabReportElement, changed_elements,
115: ReportElementChangedEvent.CHANGED));
116: jrf.getMainFrame().getElementPropertiesDialog()
117: .updateSelection();
118:
119: if (crosstabReportElement != null) {
120: jrf.getReportPanel().repaint();
121: }
122: }
123:
124: public void undo() {
125:
126: if (jrf == null)
127: return;
128:
129: if (crosstabReportElement != null) {
130: jrf.getCrosstabEditor(crosstabReportElement)
131: .getPanelEditor().setSelectedElement(null);
132: } else {
133: jrf.setSelectedElement(null);
134: }
135: Vector changed_elements = new Vector();
136:
137: for (int i = 0; i < this .getTransformations().size(); ++i) {
138: // no matter what kind of transformation is used: element, page whatever
139: // the undo operation should simply do its work.
140: // By typecasting to UndoOperation this is achieved.
141: // do not typecast to ElementTransformation
142: // Reason: also the Page resize transformation should be able to be called here
143: // Shrink command.
144:
145: ((UndoOperation) (getTransformations().get(i))).undo();
146:
147: if (getTransformations().get(i) instanceof ElementTransformation) {
148: ElementTransformation pe = (ElementTransformation) getTransformations()
149: .get(i);
150: ReportElement element = pe.element;
151: // Add element....
152:
153: if (crosstabReportElement != null) {
154: jrf.getCrosstabEditor(crosstabReportElement)
155: .getPanelEditor().addSelectedElement(
156: element, false);
157: } else {
158: jrf.addSelectedElement(element, false);
159: }
160:
161: changed_elements.add(element);
162: }
163:
164: }
165:
166: jrf
167: .fireReportListenerReportElementsChanged(new ReportElementChangedEvent(
168: jrf, crosstabReportElement, changed_elements,
169: ReportElementChangedEvent.CHANGED));
170: jrf.getMainFrame().getElementPropertiesDialog()
171: .updateSelection();
172:
173: if (crosstabReportElement != null) {
174: jrf.getReportPanel().repaint();
175: }
176: }
177:
178: public String toString() {
179: return "transformation";
180: }
181:
182: /** Getter for property elements.
183: * @return Value of property elements.
184: *
185: * To add an element, use the addElement method. Don't access directly
186: * addElement from the vector elements
187: *
188: */
189: public java.util.Vector getTransformations() {
190: return transformations;
191: }
192:
193: /** Setter for property elements.
194: * @param elements New value of property elements.
195: *
196: */
197: public void setTransformations(java.util.Vector transformations) {
198: this .transformations = transformations;
199: }
200:
201: /*
202: * Add an element to the list of elements handled by this
203: * undo operation.
204: * You must supply the old and the new bound of you report.
205: * This make more simple the undo/redo operation...
206: *
207: */
208:
209: public void addElement(Object object) {
210: ITransformation et = null;
211:
212: // Kind of factorylike structure.
213: // you might wish to pass this to a superclass of ElementTransformation and PageTransformation
214: // e.g. Transformation.java and use something like: getFactoryTransformation( object );
215: // for now (10 june 2005) this will suffice.
216:
217: if (object instanceof ReportElement) {
218: et = new ElementTransformation();
219: } else if (object instanceof JReportFrame) {
220: et = new PageTransformation();
221: }
222:
223: // ITransformation et = Transformation.getTransformation( object );
224: try {
225: this .getTransformations().add(et);
226: et.captureCurrent(object);
227: } catch (Exception ex) {
228: // if not registered, then the object
229: // can't be undone.
230: // TODO: see whether you wish to raise a special exception
231: }
232: }
233:
234: public void captureUniqueModified(Object obj) {
235: int pos = findTransformationElement(obj);
236: try {
237: ((ITransformation) getTransformations().elementAt(pos))
238: .captureModified(obj);
239: } catch (Exception e) {
240:
241: }
242: }
243:
244: private int findTransformationElement(Object object) {
245: int pos = -1; // force exception when used in array
246:
247: for (int i = 0; i < this .getTransformations().size(); ++i) {
248: if (((ITransformation) getTransformations().get(i))
249: .equals(object)) {
250: pos = i;
251: break;
252: }
253: }
254: return pos;
255: }
256:
257: }
|