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: * FormatCommandShrink.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.IReportFont;
036: import it.businesslogic.ireport.OperationType;
037: import it.businesslogic.ireport.TextReportElement;
038: import it.businesslogic.ireport.TransformationType;
039: import it.businesslogic.ireport.util.PageSize;
040:
041: import java.awt.Point;
042:
043: public class FormatCommandShrink extends FormatCommand {
044:
045: double reduction;
046: String formatOneDown;
047:
048: FormatCommandShrink() {
049: operationType = OperationType.SHRINK;
050: }
051:
052: FormatCommandShrink(int operationType) {
053: this .operationType = operationType;
054: setTreatAllElements(true);
055: }
056:
057: void preparation() {
058:
059: double marginSpace = (double) (jrf.getReport().getLeftMargin());
060: marginSpace += (double) (jrf.getReport().getRightMargin());
061:
062: int pageWidth = jrf.getReport().getWidth();
063: int pageHeight = jrf.getReport().getHeight();
064: formatOneDown = PageSize.findOneDown(pageWidth, pageHeight);
065:
066: // getMainFrame().logOnConsole("Format one Down " + formatOneDown + "\n");
067:
068: int newWidth = PageSize.getFormatSize(formatOneDown).x;
069:
070: pageWidth = jrf.getReport().getWidth() < jrf.getReport()
071: .getHeight() ? jrf.getReport().getWidth() : jrf
072: .getReport().getHeight();
073: reduction = ((double) newWidth - marginSpace)
074: / ((double) pageWidth - marginSpace);
075: // getMainFrame().logOnConsole("Reduction: " + reduction + "\n");
076:
077: }
078:
079: /**
080: * Reduce position by calculated reduction
081: * Do the same for height and width.
082: * Let's try whether this work.
083: */
084: void modify() {
085: // getMainFrame().logOnConsole("Modifying element " + re.toString() + "\n");
086: int newWidth;
087:
088: double innerX = re.getPosition().x
089: - jrf.getReport().getLeftMargin() - 10;
090: double innerY = re.getPosition().y
091: - jrf.getReport().getBandYLocation(re.band) - 10;
092:
093: innerX = innerX * reduction + jrf.getReport().getLeftMargin()
094: + 10;
095: innerY = innerY * reduction
096: + jrf.getReport().getBandYLocation(re.band) + 10;
097:
098: // TODO: If right edge of element is equal to position of right margin, then
099: // calculate the exact width based on the position of the right margin in the new format
100: // In such case do not use the reduction.
101: // This way small deviations due to typecasting from double to int are corrected
102: // in a fashionable way.
103:
104: if (re.getPosition().x + re.getWidth() - 10 == jrf.getReport()
105: .getWidth()
106: - jrf.getReport().getRightMargin()) {
107:
108: int oneDownWidth = PageSize.getFormatSize(formatOneDown).x;
109: int oneDownHeight = PageSize.getFormatSize(formatOneDown).y;
110: //int length =
111: int width = (jrf.getReport().getWidth() < jrf.getReport()
112: .getHeight() ? oneDownWidth : oneDownHeight);
113:
114: // getMainFrame().logOnConsole(" innerX " + innerX +"\n");
115: // getMainFrame().logOnConsole("(int) innerX " + (int) innerX +"\n");
116:
117: newWidth = width - jrf.getReport().getRightMargin()
118: - (int) innerX + 10;
119:
120: } else {
121: newWidth = (int) ((double) re.getWidth() * reduction);
122: }
123:
124: int newHeight = (int) ((double) re.getHeight() * reduction);
125:
126: re.setPosition(new Point((int) innerX, (int) innerY));
127: re.trasform(new Point(newWidth - re.getWidth(), newHeight
128: - re.getHeight()),
129: TransformationType.TRANSFORMATION_RESIZE_SE);
130:
131: if (re instanceof TextReportElement) {
132: IReportFont iReportFont = ((TextReportElement) re)
133: .getIReportFont();
134: int fontSize = iReportFont.getFontSize();
135: // fontsize never less than 1
136: fontSize = Math.max(1,
137: (int) (((double) fontSize) * reduction));
138: iReportFont.setFontSize(fontSize);
139: ((TextReportElement) re).setIReportFont(iReportFont);
140: }
141:
142: }
143:
144: void executeDeeper() {
145:
146: resetEnumeration();
147: preparation();
148:
149: processElements();
150:
151: // change pageformat when all elements are selected
152: // TODO: set name of format too:
153: if (jrf.getReport().getElements().size() == this
154: .getSelectedElements().size()) {
155: if (!formatOneDown.equals("Custom"))
156:
157: {
158: undoOp.addElement(jrf);
159:
160: jrf.getReport().setReportFormat(formatOneDown);
161: int oneDownWidth = PageSize
162: .getFormatSize(formatOneDown).x;
163: int oneDownHeight = PageSize
164: .getFormatSize(formatOneDown).y;
165: if (jrf.getReport().getOrientation().equals("Portrait")) {
166: jrf.getReport().setWidth(oneDownWidth);
167: jrf.getReport().setHeight(oneDownHeight);
168: jrf.getReport().recalcColumnWidth();
169: //TODO recalc columnwidth
170: } else {
171: jrf.getReport().setWidth(oneDownHeight);
172: jrf.getReport().setHeight(oneDownWidth);
173: //TODO recalc columnwidth
174: jrf.getReport().recalcColumnWidth();
175: }
176: undoOp.captureUniqueModified(jrf);
177: }
178: }
179:
180: }
181:
182: }
|