KickJava   Java API By Example, From Geeks To Geeks.

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


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  * TransformElementsOperation.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
38 import java.util.*;
39
40 /**
41  * This class handle the Insert operation.
42  * As all operations, the costructor take the JReportFrame (owner of the element)
43  * The ReportElement is not cloned, this can be a problem if not all undo operations
44  * are correctly logged and handled.
45  * @author Giulio Toffoli
46  * Modified by Robert Lamping, June 3, 2005
47  * // Also other Transformation can now be included
48  * // Once this works it should be redesigned again.
49  * // E.g. make a factory to retrieve the required transformation class.
50  * //
51  */

52 public class TransformElementsOperation implements it.businesslogic.ireport.UndoOperation
53 {
54     
55     /*
56      * The report elements that was inserted.
57      */

58     private Vector transformations = null;
59     
60     private JReportFrame jrf = null;
61     private CrosstabReportElement crosstabReportElement = null;
62     
63     /** Creates a new instance of InsertElementOperation */
64     public TransformElementsOperation(JReportFrame jrf)
65     {
66         this.transformations = new Vector();
67         this.jrf = jrf;
68     }
69     
70     /** Creates a new instance of InsertElementOperation */
71     public TransformElementsOperation(JReportFrame jrf, CrosstabReportElement crosstabReportElement)
72     {
73         this.transformations = new Vector();
74         this.crosstabReportElement = crosstabReportElement;
75         this.jrf = jrf;
76     }
77     
78     public void redo()
79     {
80         if (jrf == null) return;
81         
82         if (crosstabReportElement != null)
83         {
84             jrf.getCrosstabEditor(crosstabReportElement).getPanelEditor().setSelectedElement(null);
85         }
86         else
87         {
88             jrf.setSelectedElement(null);
89         }
90         
91         Vector changed_elements = new Vector();
92         
93         for (int i= 0; i < this.getTransformations().size(); ++i)
94         {
95             ( (UndoOperation) (getTransformations().get(i))).redo();
96             
97             if ( getTransformations().get(i) instanceof ElementTransformation )
98             {
99                 ElementTransformation pe = (ElementTransformation) getTransformations().get(i);
100                 ReportElement element = pe.element;
101                 // Add element....]
102
if (crosstabReportElement != null)
103                 {
104                     jrf.getCrosstabEditor(crosstabReportElement).getPanelEditor().addSelectedElement( element, false );
105                 }
106                 else
107                 {
108                     jrf.addSelectedElement( element, false );
109                 }
110                 
111                 changed_elements.add( element ) ;
112             }
113             
114         }
115         
116         
117         jrf.fireReportListenerReportElementsChanged(new ReportElementChangedEvent(jrf, crosstabReportElement, changed_elements , ReportElementChangedEvent.CHANGED));
118         jrf.getMainFrame().getElementPropertiesDialog().updateSelection();
119         
120         if (crosstabReportElement != null)
121         {
122             jrf.getReportPanel().repaint();
123         }
124     }
125     
126     public void undo()
127     {
128         
129         if (jrf == null) return;
130         
131         if (crosstabReportElement != null)
132         {
133             jrf.getCrosstabEditor(crosstabReportElement).getPanelEditor().setSelectedElement(null);
134         }
135         else
136         {
137             jrf.setSelectedElement(null);
138         }
139         Vector changed_elements = new Vector();
140         
141         for (int i= 0; i < this.getTransformations().size(); ++i)
142         {
143             // no matter what kind of transformation is used: element, page whatever
144
// the undo operation should simply do its work.
145
// By typecasting to UndoOperation this is achieved.
146
// do not typecast to ElementTransformation
147
// Reason: also the Page resize transformation should be able to be called here
148
// Shrink command.
149

150             ( (UndoOperation) (getTransformations().get(i))).undo();
151             
152             if ( getTransformations().get(i) instanceof ElementTransformation )
153             {
154                 ElementTransformation pe = (ElementTransformation)getTransformations().get(i);
155                 ReportElement element = pe.element;
156                 // Add element....
157

158                 if (crosstabReportElement != null)
159                 {
160                     jrf.getCrosstabEditor(crosstabReportElement).getPanelEditor().addSelectedElement( element, false );
161                 }
162                 else
163                 {
164                     jrf.addSelectedElement( element, false );
165                 }
166                 
167                 changed_elements.add( element ) ;
168             }
169             
170         }
171         
172         jrf.fireReportListenerReportElementsChanged(new ReportElementChangedEvent(jrf, crosstabReportElement, changed_elements , ReportElementChangedEvent.CHANGED));
173         jrf.getMainFrame().getElementPropertiesDialog().updateSelection();
174         
175         if (crosstabReportElement != null)
176         {
177             jrf.getReportPanel().repaint();
178         }
179     }
180     
181     public String JavaDoc toString()
182     {
183         return "transformation";
184     }
185     
186     /** Getter for property elements.
187      * @return Value of property elements.
188      *
189      * To add an element, use the addElement method. Don't access directly
190      * addElement from the vector elements
191      *
192      */

193     public java.util.Vector JavaDoc getTransformations()
194     {
195         return transformations;
196     }
197     
198     /** Setter for property elements.
199      * @param elements New value of property elements.
200      *
201      */

202     public void setTransformations(java.util.Vector JavaDoc transformations)
203     {
204         this.transformations = transformations;
205     }
206     
207     /*
208      * Add an element to the list of elements handled by this
209      * undo operation.
210      * You must supply the old and the new bound of you report.
211      * This make more simple the undo/redo operation...
212      *
213      */

214     
215     public void addElement(Object JavaDoc object)
216     {
217         ITransformation et = null;
218         
219         // Kind of factorylike structure.
220
// you might wish to pass this to a superclass of ElementTransformation and PageTransformation
221
// e.g. Transformation.java and use something like: getFactoryTransformation( object );
222
// for now (10 june 2005) this will suffice.
223

224         if (object instanceof ReportElement)
225         {
226             et = new ElementTransformation();
227         }
228         else if (object instanceof JReportFrame)
229         {
230             et = new PageTransformation();
231         }
232         
233         // ITransformation et = Transformation.getTransformation( object );
234
try
235         {
236             this.getTransformations().add(et);
237             et.captureCurrent( object );
238         }
239         catch (Exception JavaDoc ex)
240         {
241             // if not registered, then the object
242
// can't be undone.
243
// TODO: see whether you wish to raise a special exception
244
}
245     }
246     
247     public void captureUniqueModified(Object JavaDoc obj)
248     {
249         int pos = findTransformationElement( obj );
250         try
251         {
252             ((ITransformation) getTransformations().elementAt( pos ) ).captureModified( obj);
253         }
254         catch ( Exception JavaDoc e )
255         {
256             
257         }
258     }
259     
260     private int findTransformationElement( Object JavaDoc object)
261     {
262         int pos = -1; // force exception when used in array
263

264         for (int i= 0; i < this.getTransformations().size(); ++i)
265         {
266             if ( ((ITransformation) getTransformations().get(i)).equals(object) )
267             {
268                 pos = i;
269                 break;
270             }
271         }
272         return pos;
273     }
274     
275 }
276
277
Popular Tags