KickJava   Java API By Example, From Geeks To Geeks.

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


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  * ElementTransformation.java
28  *
29  * Created on 29 giugno 2003, 0.32
30  *
31  */

32
33 package it.businesslogic.ireport.undo;
34 import it.businesslogic.ireport.*;
35 import it.businesslogic.ireport.IReportFont;
36 import it.businesslogic.ireport.crosstab.CrosstabCell;
37 import java.awt.*;
38
39
40 /**
41  *
42  * @author Giulio
43  * Modified by Robert Lamping, June 3, 2005
44  * Consideration:
45  * An ElementTransformation should be able by itself to undo or redo
46  * its transformation. This is behaviour that should be encapsulated.
47  *
48  * How to inform the environment that the element has changed is another thing to
49  * research.
50  *
51  * What to capture to be able to restore the old situation should be
52  * as much as possible be the responsibility of the ElementTransformation.
53  *
54  */

55 public class ElementTransformation implements it.businesslogic.ireport.UndoOperation, ITransformation
56 {
57     public ReportElement element;
58     
59     public Rectangle oldBounds;
60     public Rectangle newBounds;
61     
62     public Band oldBand;
63     public Band newBand;
64     
65     public CrosstabCell oldCell;
66     public CrosstabCell newCell;
67     
68     private Object JavaDoc oldFontSize;
69     private Object JavaDoc newFontSize;
70     
71     
72     public ElementTransformation()
73     {
74         // oldBounds = newBounds = new Rectangle();
75
}
76     
77     // As the element will not be removed.
78
// the element should be able to undo itself
79

80     
81     
82     public boolean equals(Object JavaDoc obj)
83     {
84         boolean isEqual = false;
85         if (obj instanceof ReportElement)
86         {
87             if (this.element != null )
88             {
89                 isEqual = this.element.equals( (ReportElement) obj );
90             }
91         }
92         
93         return isEqual;
94     }
95     
96     public void undo()
97     {
98         //System.out.println("Old bound: " + this.oldBounds);
99

100         element.setPosition( new Point( this.oldBounds.x, this.oldBounds.y) ) ;
101         
102         element.setWidth( this.oldBounds.width);
103         element.setHeight(this.oldBounds.height);
104         
105         element.updateBounds();
106         
107         // band
108
if ( this.oldBand != null)
109         {
110             element.setBand( this.oldBand );
111         }
112         
113         if ( this.oldCell != null)
114         {
115             element.setCell( this.oldCell );
116         }
117         
118         // Restore relative position...
119
if (element.getCell() != null)
120         {
121             element.setRelativePosition( new Point(element.getPosition().x - element.getCell().getLeft()- 10, element.getPosition().y - element.getCell().getTop()- 10));
122         }
123         
124         // font
125
if ( element instanceof TextReportElement)
126         {
127             try
128             {
129                 ((TextReportElement) element).getIReportFont().setPropertyValue( IReportFont.FONT_SIZE, oldFontSize);
130             }
131             catch (Exception JavaDoc e )
132             {
133                 // fontsize not properly set
134
}
135         }
136         
137     }
138     
139     public void redo()
140     {
141         // location and size
142
if (element == null || this.newBounds == null) return;
143         element.setPosition( new Point( this.newBounds.x, this.newBounds.y) );
144         
145         element.setWidth(this.newBounds.width);
146         element.setHeight(this.newBounds.height);
147         
148         element.updateBounds( );
149         
150         // band
151
if ( this.newBand != null)
152         {
153             element.setBand( this.newBand );
154         }
155         
156         if ( this.newCell != null)
157         {
158             element.setCell( this.newCell );
159         }
160         
161         // Restore relative position...
162
if (element.getCell() != null)
163         {
164             element.setRelativePosition( new Point(element.getPosition().x - element.getCell().getLeft()- 10, element.getPosition().y - element.getCell().getTop()- 10));
165         }
166         
167         // font
168
if ( element instanceof TextReportElement)
169         {
170             try
171             {
172                  ((TextReportElement) element).getIReportFont().setPropertyValue( IReportFont.FONT_SIZE, newFontSize);
173             }
174             catch (Exception JavaDoc e )
175             {
176                 // fontsize not properly set
177
}
178         }
179         
180     }
181     
182     /**
183      * Let this class decide on its own which information is captures
184      * for a later redo.
185      */

186     public void captureCurrent( Object JavaDoc obj)
187     {
188         ReportElement element = (ReportElement) obj;
189         this.element = element;
190         this.oldBounds = new Rectangle( element.getBounds() );
191         this.oldBand = element.getBand();
192         this.oldCell = element.getCell();
193         
194         if ( element instanceof TextReportElement)
195         {
196             this.oldFontSize = ((TextReportElement) element).getIReportFont().getPropertyValue( IReportFont.FONT_SIZE);
197         }
198     }
199     
200     /**
201      * Let this class decide on its own which information is captures
202      * for a later unddo
203      */

204     public void captureModified(Object JavaDoc obj) // implicit typecast from Object to ReportElement?? NO!!
205
{
206         ReportElement element = (ReportElement) obj;
207         this.newBounds = new Rectangle( element.getBounds() );
208         this.newBand = element.getBand();
209         this.newCell = element.getCell();
210         
211         if ( element instanceof TextReportElement)
212         {
213             this.newFontSize = ((TextReportElement) element).getIReportFont().getPropertyValue( IReportFont.FONT_SIZE);
214         }
215         
216     }
217     
218     
219 }
220
Popular Tags