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: * DeleteElementsOperation.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 DeleteElementsOperation implements
050: it.businesslogic.ireport.UndoOperation {
051:
052: /*
053: * The report elements that was inserted.
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 DeleteElementsOperation(JReportFrame jrf) {
062: this .elements = new Vector();
063: this .jrf = jrf;
064: }
065:
066: public DeleteElementsOperation(JReportFrame jrf,
067: CrosstabReportElement crosstabReportElement) {
068: this .crosstabReportElement = crosstabReportElement;
069: this .elements = new Vector();
070: this .jrf = jrf;
071: }
072:
073: public void redo() {
074: // We must remove our element...
075: if (jrf == null && crosstabReportElement == null)
076: return;
077: Enumeration e = this .getElements().elements();
078: Vector changed_elements = new Vector();
079: while (e.hasMoreElements()) {
080: PositionedElement pe = (PositionedElement) e.nextElement();
081: ReportElement element = pe.getElement();
082:
083: if (crosstabReportElement != null) {
084: jrf.getCrosstabEditor(crosstabReportElement)
085: .getPanelEditor().getSelectedElements().remove(
086: element);
087: crosstabReportElement.getElements().remove(element);
088: jrf.getCrosstabEditor(crosstabReportElement)
089: .getPanelEditor().repaint();
090: } else {
091: jrf.getSelectedElements().remove(element);
092: jrf.getReport().getElements().remove(element);
093: if (element instanceof CrosstabReportElement) {
094: jrf
095: .removeCrosstabEditor((CrosstabReportElement) element);
096: }
097: jrf.getReportPanel().repaint();
098: }
099: changed_elements.add(element);
100: }
101: jrf
102: .fireReportListenerReportElementsChanged(new ReportElementChangedEvent(
103: jrf, crosstabReportElement, changed_elements,
104: ReportElementChangedEvent.REMOVED));
105:
106: }
107:
108: public void undo() {
109: // We must add our element...
110: if (jrf == null && crosstabReportElement == null)
111: return;
112: Vector changed_elements = new Vector();
113: for (int i = this .getElements().size() - 1; i >= 0; --i) {
114: PositionedElement pe = (PositionedElement) getElements()
115: .get(i);
116: ReportElement element = pe.getElement();
117: // Add element....
118:
119: if (crosstabReportElement != null) {
120:
121: crosstabReportElement.getElements().insertElementAt(
122: element, pe.getOldPosition());
123: } else {
124: jrf.getReport().getElements().insertElementAt(element,
125: pe.getOldPosition());
126: //jrf.addSelectedElement( element, false );
127: if (element instanceof CrosstabReportElement) {
128: jrf
129: .addCrosstabEditor((CrosstabReportElement) element);
130: }
131: }
132: changed_elements.add(element);
133: //jrf.getSelectedElements().remove( element );
134: //jrf.fireReportListenerReportElementsChanged(new ReportElementChangedEvent(jrf, element , ReportElementChangedEvent.REMOVED));
135: }
136: jrf
137: .fireReportListenerReportElementsChanged(new ReportElementChangedEvent(
138: jrf, crosstabReportElement, changed_elements,
139: ReportElementChangedEvent.ADDED));
140:
141: if (crosstabReportElement != null) {
142: jrf.getCrosstabEditor(crosstabReportElement)
143: .getPanelEditor().setSelectedElements(
144: changed_elements);
145: jrf.getCrosstabEditor(crosstabReportElement)
146: .getPanelEditor().repaint();
147: } else {
148: jrf.setSelectedElements(changed_elements);
149: jrf.getReportPanel().repaint();
150: }
151: }
152:
153: public String toString() {
154: return "delete element(s)";
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 position) {
186:
187: PositionedElement pe = new PositionedElement(element, position,
188: position);
189: getElements().add(pe);
190: }
191:
192: }
|