KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > gui > command > FormatCommandEqualsSpaceH


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  * FormatCommandEqualsSpaceH.java
28  *
29  * Created on 9 mei 2005, 21:36
30  *
31  */

32
33 package it.businesslogic.ireport.gui.command;
34
35 import it.businesslogic.ireport.Band;
36 import it.businesslogic.ireport.OperationType;
37 import it.businesslogic.ireport.ReportElement;
38
39 import java.awt.Point JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import java.util.Vector JavaDoc;
42
43
44 /**
45  * FormatCommandEqualsSpaceH equilizes the interdistance between elements.
46  * The average interdistance between the elements of the active band (band with first selected element)
47  * is forced upon the selected elements in the other bands.
48  *
49  * In the active band the left and right most elements will keep their position.
50  * In the other bands, the left most element will keep its position and the others will be repositioned.
51  *
52  */

53 public class FormatCommandEqualsSpaceH extends FormatCommand {
54     int actualX;
55     int interDistance;
56     
57     Band activeBand ;
58     Vector JavaDoc bands = null;
59     ReportElement fardestElement;
60     
61     FormatCommandEqualsSpaceH() {
62         int operationType = OperationType.EQUALS_SPACE_H;
63     }
64     
65     boolean preCondition(){
66         activeBand = (Band) ((ReportElement) this.getSelectedElements().firstElement()).getBand();
67         int counter = 0;
68         
69         for (Iterator JavaDoc i = this.getSelectedElements().iterator(); i.hasNext(); ) {
70             ReportElement re = (ReportElement) i.next();
71             if (re.getBand() == activeBand) {
72                 counter++;
73             }
74         }
75         return ( counter > 1 );
76         
77     }
78     
79     void executeDeeper(){
80         resetEnumeration();
81         
82         preparation();
83         
84         bands = getBands();
85         
86         for (Iterator JavaDoc h = bands.iterator(); h.hasNext(); ) {
87             Band b = (Band) h.next();
88             //Vector bandElements = getBandElements(b);
89
Vector JavaDoc bandElements = getBandElements(b);
90             bandElements = sortXY( bandElements.elements());
91             
92             if (bandElements.size()>1) {
93                 ReportElement re = (ReportElement) bandElements.firstElement();
94                 actualX = re.getPosition().x + re.getWidth() + interDistance;
95                 
96                 // the highest element in the band doesn't have to be moved
97
bandElements.removeElement(re);
98                 
99                 // remove bottom element as well if this band is the active band
100
if (b == activeBand) {
101                     bandElements.remove( bandElements.indexOf(fardestElement) );
102                 }
103                 
104                 processElements(bandElements.elements());
105             }
106             
107         }
108         
109     }
110     
111      /*
112       * Calculate available height
113       */

114     void preparation() {
115         // calculate the average interdistance of the selected elements in the active Band
116
// The active Band is the band with the first selected element.
117
// The active band is set in the preCondition() method.
118

119         Band b = activeBand;
120         
121         int counter = 0;
122         int usedSize = 0;
123         int minX = 0;
124         int maxX = 0;
125         
126         ReportElement re = null;
127         
128         for (Iterator JavaDoc i = this.getSelectedElements().iterator(); i.hasNext(); ) {
129             re = (ReportElement) i.next();
130             
131             if( re.getBand() == b){
132                 counter++;
133                 usedSize += re.getWidth();
134                 
135                 if (counter == 1) {
136                     minX = re.getPosition().x;
137                     maxX = re.getPosition().x + re.getWidth();
138                     fardestElement = re;
139                 } else {
140                     if (minX > re.getPosition().x ) {
141                         minX = re.getPosition().x;
142                     }
143                     if (maxX < re.getPosition().x + re.getWidth()) {
144                         maxX = re.getPosition().x + re.getWidth();
145                         fardestElement = re; // bottomElement of active Band
146
}
147                 }
148             }
149         }
150         
151         interDistance = ( maxX - minX - usedSize )/ (counter - 1);
152         
153     }
154     
155     public void modify() {
156         re.setPosition(new Point JavaDoc(actualX, re.getPosition().y) );
157         actualX += re.getWidth() + interDistance;
158     }
159     
160 }
161
Popular Tags