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