KickJava   Java API By Example, From Geeks To Geeks.

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


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  * GroupEmentsOperation.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 import java.awt.*;
39
40 import java.util.*;
41
42 /**
43  * This class handle the Insert operation.
44  * As all operations, the costructor take the JReportFrame (owner of the element)
45  * The ReportElement is not cloned, this can be a problem if not all undo operations
46  * are correctly logged and handled.
47  * @author Giulio Toffoli
48  */

49 public class GroupEmentsOperation implements it.businesslogic.ireport.UndoOperation {
50     
51     /*
52      * The report elements that was inserted.
53      */

54     private Vector elements = null;
55     
56     private JReportFrame jrf = null;
57     
58     private String JavaDoc addedGroup = "";
59
60         
61     public GroupEmentsOperation(JReportFrame jrf) {
62          this.elements = new Vector();
63         this.jrf = jrf;
64     }
65     
66     public void redo()
67     {
68         // We must remove our element...
69
if (jrf == null) return;
70         Enumeration e = this.getElements().elements();
71         while ( e.hasMoreElements() )
72         {
73             PositionedElement pe = (PositionedElement)e.nextElement();
74             ReportElement element = pe.getElement();
75             
76             element.setElementGroup( getAddedGroup() + ((element.getElementGroup().equals("")) ? "" : ".") + element.getElementGroup() );
77             
78             // Add element....
79
jrf.getReport().getElements().remove( pe.getOldPosition() );
80             
81             jrf.getReport().getElements().insertElementAt(element, pe.getNewPosition());
82             jrf.addSelectedElement( element , false);
83         }
84         jrf.fireSelectionChangedEvent();
85         jrf.getReportPanel().repaint();
86     }
87     
88     public void undo()
89     {
90         // We must add our element...
91
if (jrf == null) return;
92         
93         jrf.setSelectedElement(null);
94         for (int i= this.getElements().size()-1; i>=0; --i)
95         {
96             PositionedElement pe = (PositionedElement)getElements().get(i);
97             ReportElement element = pe.getElement();
98             
99             if (element.getElementGroup().startsWith(getAddedGroup()))
100             {
101                 element.setElementGroup( element.getElementGroup().substring(getAddedGroup().length()));
102                 if (element.getElementGroup().startsWith("."))
103                 {
104                     element.setElementGroup( element.getElementGroup().substring(1));
105                 }
106             }
107             
108             // Add element....
109
jrf.getReport().getElements().remove( pe.getNewPosition() );
110             
111             jrf.getReport().getElements().insertElementAt(element, pe.getOldPosition());
112             jrf.addSelectedElement( element , false);
113             
114             //jrf.getSelectedElements().remove( element );
115
//jrf.fireReportListenerReportElementsChanged(new ReportElementChangedEvent(jrf, element , ReportElementChangedEvent.REMOVED));
116
}
117         jrf.fireSelectionChangedEvent();
118         jrf.getReportPanel().repaint();
119         
120     }
121     
122     public String JavaDoc toString()
123     {
124         return "group element(s)";
125     }
126     
127     /** Getter for property elements.
128      * @return Value of property elements.
129      *
130      * To add an element, use the addElement method. Don't access directly
131      * addElement from the vector elements
132      *
133      */

134     public java.util.Vector JavaDoc getElements() {
135         return elements;
136     }
137     
138     /** Setter for property elements.
139      * @param elements New value of property elements.
140      *
141      */

142     public void setElements(java.util.Vector JavaDoc elements) {
143         this.elements = elements;
144     }
145     
146     /*
147      * Add an element to the list of elements handled by this
148      * undo operation. The position is relative to the z position
149      * in the elements vector.
150      * If the undo mechanism works fine, the requested position is
151      * ever available.
152      * The position value should be retrived when the element
153      * is added.
154      */

155      public void addElement(ReportElement element, int oldPosition, int newPostion) {
156          
157         PositionedElement pe = new PositionedElement(element, oldPosition, newPostion);
158         getElements().add(pe);
159     }
160
161     public String JavaDoc getAddedGroup() {
162         return addedGroup;
163     }
164
165     public void setAddedGroup(String JavaDoc addedGroup) {
166         this.addedGroup = addedGroup;
167     }
168
169 }
170
171
Popular Tags