001: /*
002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
003: * http://www.jaspersoft.com.
004: *
005: * Unless you have purchased a commercial license agreement from JasperSoft,
006: * the following license terms apply:
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License version 2 as published by
010: * the Free Software Foundation.
011: *
012: * This program is distributed WITHOUT ANY WARRANTY; and without the
013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
014: * See the GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
018: * or write to:
019: *
020: * Free Software Foundation, Inc.,
021: * 59 Temple Place - Suite 330,
022: * Boston, MA USA 02111-1307
023: *
024: *
025: *
026: *
027: * FormatCommandEqualsSpaceH.java
028: *
029: * Created on 9 mei 2005, 21:36
030: *
031: */
032:
033: package it.businesslogic.ireport.gui.command;
034:
035: import it.businesslogic.ireport.Band;
036: import it.businesslogic.ireport.OperationType;
037: import it.businesslogic.ireport.ReportElement;
038:
039: import java.awt.Point;
040: import java.util.Iterator;
041: import java.util.Vector;
042:
043: /**
044: * FormatCommandEqualsSpaceH equilizes the interdistance between elements.
045: * The average interdistance between the elements of the active band (band with first selected element)
046: * is forced upon the selected elements in the other bands.
047: *
048: * In the active band the left and right most elements will keep their position.
049: * In the other bands, the left most element will keep its position and the others will be repositioned.
050: *
051: */
052: public class FormatCommandEqualsSpaceH extends FormatCommand {
053: int actualX;
054: int interDistance;
055:
056: Band activeBand;
057: Vector bands = null;
058: ReportElement fardestElement;
059:
060: FormatCommandEqualsSpaceH() {
061: int operationType = OperationType.EQUALS_SPACE_H;
062: }
063:
064: boolean preCondition() {
065: activeBand = (Band) ((ReportElement) this .getSelectedElements()
066: .firstElement()).getBand();
067: int counter = 0;
068:
069: for (Iterator i = this .getSelectedElements().iterator(); i
070: .hasNext();) {
071: ReportElement re = (ReportElement) i.next();
072: if (re.getBand() == activeBand) {
073: counter++;
074: }
075: }
076: return (counter > 1);
077:
078: }
079:
080: void executeDeeper() {
081: resetEnumeration();
082:
083: preparation();
084:
085: bands = getBands();
086:
087: for (Iterator h = bands.iterator(); h.hasNext();) {
088: Band b = (Band) h.next();
089: //Vector bandElements = getBandElements(b);
090: Vector bandElements = getBandElements(b);
091: bandElements = sortXY(bandElements.elements());
092:
093: if (bandElements.size() > 1) {
094: ReportElement re = (ReportElement) bandElements
095: .firstElement();
096: actualX = re.getPosition().x + re.getWidth()
097: + interDistance;
098:
099: // the highest element in the band doesn't have to be moved
100: bandElements.removeElement(re);
101:
102: // remove bottom element as well if this band is the active band
103: if (b == activeBand) {
104: bandElements.remove(bandElements
105: .indexOf(fardestElement));
106: }
107:
108: processElements(bandElements.elements());
109: }
110:
111: }
112:
113: }
114:
115: /*
116: * Calculate available height
117: */
118: void preparation() {
119: // calculate the average interdistance of the selected elements in the active Band
120: // The active Band is the band with the first selected element.
121: // The active band is set in the preCondition() method.
122:
123: Band b = activeBand;
124:
125: int counter = 0;
126: int usedSize = 0;
127: int minX = 0;
128: int maxX = 0;
129:
130: ReportElement re = null;
131:
132: for (Iterator i = this .getSelectedElements().iterator(); i
133: .hasNext();) {
134: re = (ReportElement) i.next();
135:
136: if (re.getBand() == b) {
137: counter++;
138: usedSize += re.getWidth();
139:
140: if (counter == 1) {
141: minX = re.getPosition().x;
142: maxX = re.getPosition().x + re.getWidth();
143: fardestElement = re;
144: } else {
145: if (minX > re.getPosition().x) {
146: minX = re.getPosition().x;
147: }
148: if (maxX < re.getPosition().x + re.getWidth()) {
149: maxX = re.getPosition().x + re.getWidth();
150: fardestElement = re; // bottomElement of active Band
151: }
152: }
153: }
154: }
155:
156: interDistance = (maxX - minX - usedSize) / (counter - 1);
157:
158: }
159:
160: public void modify() {
161: re.setPosition(new Point(actualX, re.getPosition().y));
162: actualX += re.getWidth() + interDistance;
163: }
164:
165: }
|