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: * ChangeEmentsOrderOperation.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.CrosstabReportElement;
036: import it.businesslogic.ireport.gui.event.*;
037: import it.businesslogic.ireport.*;
038: import it.businesslogic.ireport.gui.*;
039: import it.businesslogic.ireport.util.*;
040: import java.awt.*;
041:
042: import java.util.*;
043:
044: /**
045: * This class handle the Insert operation.
046: * As all operations, the costructor take the JReportFrame (owner of the element)
047: * The ReportElement is not cloned, this can be a problem if not all undo operations
048: * are correctly logged and handled.
049: * @author Giulio Toffoli
050: */
051: public class ChangeEmentsOrderOperation implements
052: it.businesslogic.ireport.UndoOperation {
053:
054: /*
055: * The report elements that was inserted.
056: */
057: private Vector elements = null;
058:
059: private JReportFrame jrf = null;
060: private CrosstabReportElement crosstabReportElement = null;
061:
062: public ChangeEmentsOrderOperation(JReportFrame jrf,
063: CrosstabReportElement crosstabReportElement) {
064: this .crosstabReportElement = crosstabReportElement;
065: this .elements = new Vector();
066: this .jrf = jrf;
067: }
068:
069: public ChangeEmentsOrderOperation(JReportFrame jrf) {
070: this (jrf, null);
071: }
072:
073: public void redo() {
074: // We must remove our element...
075: if (jrf == null)
076: return;
077: Enumeration e = this .getElements().elements();
078:
079: Vector changed_elements = new Vector();
080: while (e.hasMoreElements()) {
081: PositionedElement pe = (PositionedElement) e.nextElement();
082: ReportElement element = pe.getElement();
083: // Add element....
084: changed_elements.add(element);
085: if (getCrosstabReportElement() != null) {
086: getCrosstabReportElement().getElements().remove(
087: pe.getOldPosition());
088: getCrosstabReportElement().getElements()
089: .insertElementAt(element, pe.getNewPosition());
090: jrf.getCrosstabEditor(getCrosstabReportElement())
091: .getPanelEditor().addSelectedElement(element,
092: false);
093: jrf.getCrosstabEditor(getCrosstabReportElement())
094: .getPanelEditor().repaint();
095: } else {
096: jrf.getReport().getElements().remove(
097: pe.getOldPosition());
098: jrf.getReport().getElements().insertElementAt(element,
099: pe.getNewPosition());
100: jrf.addSelectedElement(element, false);
101: jrf.getReportPanel().repaint();
102: }
103: }
104: jrf
105: .fireReportListenerReportElementsChanged(new ReportElementChangedEvent(
106: jrf, getCrosstabReportElement(),
107: changed_elements,
108: ReportElementChangedEvent.CHANGED));
109:
110: }
111:
112: public void undo() {
113: // We must add our element...
114: if (jrf == null)
115: return;
116:
117: jrf.setSelectedElement(null);
118: Vector changed_elements = new Vector();
119:
120: for (int i = this .getElements().size() - 1; i >= 0; --i) {
121: PositionedElement pe = (PositionedElement) getElements()
122: .get(i);
123: ReportElement element = pe.getElement();
124: changed_elements.add(element);
125: // Add element....
126: if (getCrosstabReportElement() != null) {
127: getCrosstabReportElement().getElements().remove(
128: pe.getNewPosition());
129: getCrosstabReportElement().getElements()
130: .insertElementAt(element, pe.getOldPosition());
131: jrf.getCrosstabEditor(getCrosstabReportElement())
132: .getPanelEditor().addSelectedElement(element,
133: false);
134: jrf.getCrosstabEditor(getCrosstabReportElement())
135: .getPanelEditor().repaint();
136: } else {
137: jrf.getReport().getElements().remove(
138: pe.getNewPosition());
139: jrf.getReport().getElements().insertElementAt(element,
140: pe.getOldPosition());
141: jrf.addSelectedElement(element, false);
142: jrf.getReportPanel().repaint();
143: }
144: }
145: jrf
146: .fireReportListenerReportElementsChanged(new ReportElementChangedEvent(
147: jrf, getCrosstabReportElement(),
148: changed_elements,
149: ReportElementChangedEvent.CHANGED));
150:
151: }
152:
153: public String toString() {
154: return "change element(s) order";
155: }
156:
157: /** Getter for property elements.
158: * @return Value of property elements.
159: *
160: * To add an element, use the addElement method. Don't access directly
161: * addElement from the vector elements
162: *
163: */
164: public java.util.Vector getElements() {
165: return elements;
166: }
167:
168: /** Setter for property elements.
169: * @param elements New value of property elements.
170: *
171: */
172: public void setElements(java.util.Vector elements) {
173: this .elements = elements;
174: }
175:
176: /*
177: * Add an element to the list of elements handled by this
178: * undo operation. The position is relative to the z position
179: * in the elements vector.
180: * If the undo mechanism works fine, the requested position is
181: * ever available.
182: * The position value should be retrived when the element
183: * is added.
184: */
185: public void addElement(ReportElement element, int oldPosition,
186: int newPostion) {
187:
188: PositionedElement pe = new PositionedElement(element,
189: oldPosition, newPostion);
190: getElements().add(pe);
191: }
192:
193: public CrosstabReportElement getCrosstabReportElement() {
194: return crosstabReportElement;
195: }
196:
197: public void setCrosstabReportElement(
198: CrosstabReportElement crosstabReportElement) {
199: this.crosstabReportElement = crosstabReportElement;
200: }
201:
202: }
|