KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > undo > ReplacedElementsOperation


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * ReplacedElementsOperation.java
28  *
29  * Created on 19 giugno 2003, 23.23
30  *
31  */

32
33 package it.businesslogic.ireport.undo;
34 import it.businesslogic.ireport.gui.event.*;
35 import it.businesslogic.ireport.*;
36 import it.businesslogic.ireport.gui.*;
37 import it.businesslogic.ireport.util.*;
38
39 import java.util.*;
40
41 /**
42  * This class handle the Insert operation.
43  * As all operations, the costructor take the JReportFrame (owner of the element)
44  * The ReportElement is not cloned, this can be a problem if not all undo operations
45  * are correctly logged and handled.
46  * @author Giulio Toffoli
47  */

48 public class ReplacedElementsOperation implements it.businesslogic.ireport.UndoOperation {
49     
50     /*
51      * The report elements that was replaced.
52      */

53     private Vector elements = null;
54     
55     private JReportFrame jrf = null;
56     private CrosstabReportElement crosstabReportElement = null;
57     
58     /** Creates a new instance of InsertElementOperation */
59     public ReplacedElementsOperation(JReportFrame jrf, CrosstabReportElement crosstabReportElement) {
60         this.setCrosstabReportElement(crosstabReportElement);
61         this.elements = new Vector();
62         this.jrf = jrf;
63     }
64     
65     public ReplacedElementsOperation(JReportFrame jrf) {
66         this(jrf,null);
67     }
68     
69     public void redo()
70     {
71         // We must remove our element...
72
if (jrf == null) return;
73         Enumeration e = this.getElements().elements();
74         Vector added_elements = new Vector();
75         Vector removed_elements = new Vector();
76         
77         while ( e.hasMoreElements() )
78         {
79             ReplacedElementItem ei = (ReplacedElementItem)e.nextElement();
80             ReportElement newElement = ei.getNewElement();
81             ReportElement oldElement = ei.getOldElement();
82               
83             if (getCrosstabReportElement() != null)
84             {
85                 int index = getCrosstabReportElement().getElements().indexOf( oldElement );
86                 getCrosstabReportElement().getElements().remove( oldElement );
87                 getCrosstabReportElement().getElements().add(index, newElement);
88                 added_elements.add(newElement);
89                 removed_elements.add(oldElement);
90             }
91             else
92             {
93                 int index = jrf.getReport().getElements().indexOf( oldElement );
94                 jrf.getReport().getElements().remove( oldElement );
95                 jrf.getReport().getElements().add(index, newElement);
96                 added_elements.add(newElement);
97                 removed_elements.add(oldElement);
98             }
99         }
100         jrf.fireReportListenerReportElementsChanged(new ReportElementChangedEvent(jrf, getCrosstabReportElement(),removed_elements , ReportElementChangedEvent.REMOVED));
101         jrf.fireReportListenerReportElementsChanged(new ReportElementChangedEvent(jrf, getCrosstabReportElement(), added_elements , ReportElementChangedEvent.ADDED));
102         if (getCrosstabReportElement() != null) jrf.getCrosstabEditor(getCrosstabReportElement()).getPanelEditor().repaint();
103         else jrf.getReportPanel().repaint();
104     }
105     
106     public void undo()
107     {
108         // We must add our element...
109
if (jrf == null) return;
110
111         Vector added_elements = new Vector();
112         Vector removed_elements = new Vector();
113         
114         for (int i= this.getElements().size()-1; i>=0; --i)
115         {
116             ReplacedElementItem ei = (ReplacedElementItem)getElements().get(i);
117             ReportElement newElement = ei.getNewElement();
118             ReportElement oldElement = ei.getOldElement();
119               
120             if (getCrosstabReportElement() != null)
121             {
122                 int index = getCrosstabReportElement().getElements().indexOf( newElement );
123                 getCrosstabReportElement().getElements().remove( newElement );
124                 getCrosstabReportElement().getElements().add(index, oldElement);
125                 added_elements.add( oldElement );
126                 removed_elements.add(newElement);
127             }
128             else
129             {
130                 int index = jrf.getReport().getElements().indexOf( newElement );
131             jrf.getReport().getElements().remove( newElement );
132             jrf.getReport().getElements().add(index, oldElement);
133             added_elements.add( oldElement );
134             removed_elements.add(newElement);
135             }
136             
137         }
138          jrf.fireReportListenerReportElementsChanged(new ReportElementChangedEvent(jrf, getCrosstabReportElement(), removed_elements , ReportElementChangedEvent.REMOVED));
139          jrf.fireReportListenerReportElementsChanged(new ReportElementChangedEvent(jrf, getCrosstabReportElement(), added_elements , ReportElementChangedEvent.ADDED));
140         
141         if (getCrosstabReportElement() != null) jrf.getCrosstabEditor(getCrosstabReportElement()).getPanelEditor().repaint();
142         else jrf.getReportPanel().repaint();
143     }
144     
145     public String JavaDoc toString()
146     {
147         return "Transformed element(s)";
148     }
149     
150     /** Getter for property elements.
151      * @return Value of property elements.
152      *
153      * To add an element, use the addElement method. Don't access directly
154      * addElement from the vector elements
155      *
156      */

157     public java.util.Vector JavaDoc getElements() {
158         return elements;
159     }
160     
161     /** Setter for property elements.
162      * @param elements New value of property elements.
163      *
164      */

165     public void setElements(java.util.Vector JavaDoc elements) {
166         this.elements = elements;
167     }
168     
169     /*
170      * Add an element to the list of elements handled by this
171      * undo operation. The position is relative to the z position
172      * in the elements vector.
173      * If the undo mechanism works fine, the requested position is
174      * ever available.
175      * The position value should be retrived when the element
176      * is added.
177      */

178      public void addElement(ReportElement oldEelement,ReportElement newElement ) {
179          ReplacedElementItem ei = new ReplacedElementItem(oldEelement, newElement );
180          getElements().add(ei);
181     }
182
183     public CrosstabReportElement getCrosstabReportElement() {
184         return crosstabReportElement;
185     }
186
187     public void setCrosstabReportElement(CrosstabReportElement crosstabReportElement) {
188         this.crosstabReportElement = crosstabReportElement;
189     }
190     
191 }
192
193
194
195
196
Popular Tags