KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

55     private Vector elements = null;
56     
57     private JReportFrame jrf = null;
58     private CrosstabReportElement crosstabReportElement = null;
59         
60     public ChangeEmentsOrderOperation(JReportFrame jrf, CrosstabReportElement crosstabReportElement) {
61         this.crosstabReportElement = crosstabReportElement;
62          this.elements = new Vector();
63         this.jrf = jrf;
64     }
65     
66     public ChangeEmentsOrderOperation(JReportFrame jrf) {
67         this(jrf,null);
68     }
69     
70     public void redo()
71     {
72         // We must remove our element...
73
if (jrf == null) return;
74         Enumeration e = this.getElements().elements();
75
76         Vector changed_elements = new Vector();
77         while ( e.hasMoreElements() )
78         {
79             PositionedElement pe = (PositionedElement)e.nextElement();
80             ReportElement element = pe.getElement();
81             // Add element....
82
changed_elements.add(element);
83             if (getCrosstabReportElement() != null)
84             {
85                 getCrosstabReportElement().getElements().remove( pe.getOldPosition() );
86                 getCrosstabReportElement().getElements().insertElementAt(element, pe.getNewPosition());
87                 jrf.getCrosstabEditor(getCrosstabReportElement()).getPanelEditor().addSelectedElement( element , false);
88                 jrf.getCrosstabEditor(getCrosstabReportElement()).getPanelEditor().repaint();
89             }
90             else
91             {
92                 jrf.getReport().getElements().remove( pe.getOldPosition() );
93                 jrf.getReport().getElements().insertElementAt(element, pe.getNewPosition());
94                 jrf.addSelectedElement( element , false);
95                 jrf.getReportPanel().repaint();
96             }
97         }
98         jrf.fireReportListenerReportElementsChanged(new ReportElementChangedEvent(jrf, getCrosstabReportElement(), changed_elements , ReportElementChangedEvent.CHANGED));
99
100     }
101     
102     public void undo()
103     {
104         // We must add our element...
105
if (jrf == null) return;
106         
107         jrf.setSelectedElement(null);
108         Vector changed_elements = new Vector();
109         
110         for (int i= this.getElements().size()-1; i>=0; --i)
111         {
112             PositionedElement pe = (PositionedElement)getElements().get(i);
113             ReportElement element = pe.getElement();
114             changed_elements.add(element);
115             // Add element....
116
if (getCrosstabReportElement() != null)
117             {
118                 getCrosstabReportElement().getElements().remove( pe.getNewPosition() );
119                 getCrosstabReportElement().getElements().insertElementAt(element, pe.getOldPosition());
120                 jrf.getCrosstabEditor(getCrosstabReportElement()).getPanelEditor().addSelectedElement( element , false);
121                 jrf.getCrosstabEditor(getCrosstabReportElement()).getPanelEditor().repaint();
122             }
123             else
124             {
125                 jrf.getReport().getElements().remove( pe.getNewPosition() );
126                 jrf.getReport().getElements().insertElementAt(element, pe.getOldPosition());
127                 jrf.addSelectedElement( element , false);
128                 jrf.getReportPanel().repaint();
129             }
130         }
131         jrf.fireReportListenerReportElementsChanged(new ReportElementChangedEvent(jrf, getCrosstabReportElement(), changed_elements , ReportElementChangedEvent.CHANGED));
132         
133     }
134     
135     public String JavaDoc toString()
136     {
137         return "change element(s) order";
138     }
139     
140     /** Getter for property elements.
141      * @return Value of property elements.
142      *
143      * To add an element, use the addElement method. Don't access directly
144      * addElement from the vector elements
145      *
146      */

147     public java.util.Vector JavaDoc getElements() {
148         return elements;
149     }
150     
151     /** Setter for property elements.
152      * @param elements New value of property elements.
153      *
154      */

155     public void setElements(java.util.Vector JavaDoc elements) {
156         this.elements = elements;
157     }
158     
159     /*
160      * Add an element to the list of elements handled by this
161      * undo operation. The position is relative to the z position
162      * in the elements vector.
163      * If the undo mechanism works fine, the requested position is
164      * ever available.
165      * The position value should be retrived when the element
166      * is added.
167      */

168      public void addElement(ReportElement element, int oldPosition, int newPostion) {
169          
170         PositionedElement pe = new PositionedElement(element, oldPosition, newPostion);
171         getElements().add(pe);
172     }
173
174     public CrosstabReportElement getCrosstabReportElement() {
175         return crosstabReportElement;
176     }
177
178     public void setCrosstabReportElement(CrosstabReportElement crosstabReportElement) {
179         this.crosstabReportElement = crosstabReportElement;
180     }
181     
182 }
183
184
Popular Tags