KickJava   Java API By Example, From Geeks To Geeks.

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


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  * DeleteElementsOperation.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 DeleteElementsOperation implements it.businesslogic.ireport.UndoOperation {
49     
50     /*
51      * The report elements that was inserted.
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 DeleteElementsOperation(JReportFrame jrf) {
60         this.elements = new Vector();
61         this.jrf = jrf;
62     }
63     
64     public DeleteElementsOperation(JReportFrame jrf, CrosstabReportElement crosstabReportElement) {
65         this.crosstabReportElement = crosstabReportElement;
66         this.elements = new Vector();
67         this.jrf = jrf;
68     }
69     
70     public void redo()
71     {
72         // We must remove our element...
73
if (jrf == null && crosstabReportElement == null) return;
74         Enumeration e = this.getElements().elements();
75         Vector changed_elements = new Vector();
76         while ( e.hasMoreElements() )
77         {
78             PositionedElement pe = (PositionedElement)e.nextElement();
79             ReportElement element = pe.getElement();
80             
81             if (crosstabReportElement != null)
82             {
83                 jrf.getCrosstabEditor(crosstabReportElement).getPanelEditor().getSelectedElements().remove( element );
84                 crosstabReportElement.getElements().remove( element );
85                 jrf.getCrosstabEditor(crosstabReportElement).getPanelEditor().repaint();
86             }
87             else
88             {
89                 jrf.getSelectedElements().remove( element );
90                 jrf.getReport().getElements().remove( element );
91                 if (element instanceof CrosstabReportElement)
92                 {
93                     jrf.removeCrosstabEditor((CrosstabReportElement)element);
94                 }
95                 jrf.getReportPanel().repaint();
96             }
97             changed_elements.add(element);
98         }
99         jrf.fireReportListenerReportElementsChanged(new ReportElementChangedEvent(jrf, crosstabReportElement, changed_elements , ReportElementChangedEvent.REMOVED));
100         
101     }
102     
103     public void undo()
104     {
105         // We must add our element...
106
if (jrf == null && crosstabReportElement == null) return;
107         Vector changed_elements = new Vector();
108         for (int i= this.getElements().size()-1; i>=0; --i)
109         {
110             PositionedElement pe = (PositionedElement)getElements().get(i);
111             ReportElement element = pe.getElement();
112             // Add element....
113

114             if (crosstabReportElement != null)
115             {
116                 
117                 crosstabReportElement.getElements().insertElementAt(element, pe.getOldPosition());
118             }
119             else
120             {
121                 jrf.getReport().getElements().insertElementAt(element, pe.getOldPosition());
122                 //jrf.addSelectedElement( element, false );
123
if (element instanceof CrosstabReportElement)
124                 {
125                     jrf.addCrosstabEditor((CrosstabReportElement)element);
126                 }
127             }
128             changed_elements.add( element );
129             //jrf.getSelectedElements().remove( element );
130
//jrf.fireReportListenerReportElementsChanged(new ReportElementChangedEvent(jrf, element , ReportElementChangedEvent.REMOVED));
131
}
132         jrf.fireReportListenerReportElementsChanged(new ReportElementChangedEvent(jrf, crosstabReportElement, changed_elements , ReportElementChangedEvent.ADDED));
133         
134         if (crosstabReportElement != null)
135         {
136             jrf.getCrosstabEditor(crosstabReportElement).getPanelEditor().setSelectedElements( changed_elements );
137             jrf.getCrosstabEditor(crosstabReportElement).getPanelEditor().repaint();
138         }
139         else
140         {
141             jrf.setSelectedElements( changed_elements );
142             jrf.getReportPanel().repaint();
143         }
144     }
145     
146     public String JavaDoc toString()
147     {
148         return "delete element(s)";
149     }
150     
151     /** Getter for property elements.
152      * @return Value of property elements.
153      *
154      * To add an element, use the addElement method. Don't access directly
155      * addElement from the vector elements
156      *
157      */

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

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

179      public void addElement(ReportElement element, int position) {
180          
181         PositionedElement pe = new PositionedElement(element, position, position);
182         getElements().add(pe);
183     }
184     
185 }
186
187
188
189
190
Popular Tags