KickJava   Java API By Example, From Geeks To Geeks.

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


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  * FormatCommandShrink.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.IReportFont;
36 import it.businesslogic.ireport.OperationType;
37 import it.businesslogic.ireport.TextReportElement;
38 import it.businesslogic.ireport.TransformationType;
39 import it.businesslogic.ireport.util.PageSize;
40
41 import java.awt.Point JavaDoc;
42
43
44 public class FormatCommandShrink
45         extends FormatCommand
46 {
47     
48     double reduction;
49     String JavaDoc formatOneDown;
50     
51     FormatCommandShrink()
52     {
53         operationType = OperationType.SHRINK;
54     }
55     
56     FormatCommandShrink( int operationType)
57     {
58         this.operationType = operationType;
59         setTreatAllElements(true);
60     }
61     
62     void preparation()
63     {
64         
65         double marginSpace = (double) (jrf.getReport().getLeftMargin()) ;
66         marginSpace += (double) (jrf.getReport().getRightMargin());
67         
68         int pageWidth = jrf.getReport().getWidth();
69         int pageHeight = jrf.getReport().getHeight();
70         formatOneDown = PageSize.findOneDown( pageWidth, pageHeight);
71         
72         // getMainFrame().logOnConsole("Format one Down " + formatOneDown + "\n");
73

74         int newWidth = PageSize.getFormatSize(formatOneDown).x;
75         
76         pageWidth = jrf.getReport().getWidth() < jrf.getReport().getHeight() ?
77             jrf.getReport().getWidth() : jrf.getReport().getHeight();
78         reduction = ((double) newWidth - marginSpace) / ((double) pageWidth - marginSpace );
79         // getMainFrame().logOnConsole("Reduction: " + reduction + "\n");
80

81     }
82     
83     /**
84      * Reduce position by calculated reduction
85      * Do the same for height and width.
86      * Let's try whether this work.
87      */

88     void modify()
89     {
90         // getMainFrame().logOnConsole("Modifying element " + re.toString() + "\n");
91
int newWidth;
92         
93         double innerX = re.getPosition().x - jrf.getReport().getLeftMargin() -10 ;
94         double innerY = re.getPosition().y - jrf.getReport().getBandYLocation( re.band) -10 ;
95         
96         innerX = innerX * reduction + jrf.getReport().getLeftMargin() + 10;
97         innerY = innerY * reduction + jrf.getReport().getBandYLocation( re.band) + 10 ;
98         
99         // TODO: If right edge of element is equal to position of right margin, then
100
// calculate the exact width based on the position of the right margin in the new format
101
// In such case do not use the reduction.
102
// This way small deviations due to typecasting from double to int are corrected
103
// in a fashionable way.
104

105         if (re.getPosition().x + re.getWidth() -10 == jrf.getReport().getWidth() - jrf.getReport().getRightMargin() )
106         {
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().getHeight() ? oneDownWidth : oneDownHeight );
112             
113             // getMainFrame().logOnConsole(" innerX " + innerX +"\n");
114
// getMainFrame().logOnConsole("(int) innerX " + (int) innerX +"\n");
115

116             newWidth = width - jrf.getReport().getRightMargin() - (int) innerX + 10;
117             
118         }
119         else
120         {
121             newWidth = (int) ((double) re.getWidth() * reduction);
122         }
123         
124         int newHeight = (int) ((double) re.getHeight() * reduction );
125         
126         re.setPosition(new Point JavaDoc( (int) innerX, (int) innerY) );
127         re.trasform(new Point JavaDoc( newWidth - re.getWidth(), newHeight - re.getHeight()), TransformationType.TRANSFORMATION_RESIZE_SE);
128         
129         if (re instanceof TextReportElement )
130         {
131             IReportFont iReportFont = ((TextReportElement)re).getIReportFont();
132             int fontSize = iReportFont.getFontSize();
133             // fontsize never less than 1
134
fontSize = Math.max( 1, (int) (((double) fontSize ) * reduction ) );
135             iReportFont.setFontSize(fontSize);
136             ((TextReportElement)re).setIReportFont(iReportFont );
137         }
138         
139     }
140     
141     void executeDeeper()
142     {
143         
144         resetEnumeration();
145         preparation();
146
147         processElements();
148         
149         // change pageformat when all elements are selected
150
// TODO: set name of format too:
151
if ( jrf.getReport().getElements().size() == this.getSelectedElements().size() )
152         {
153             if ( !formatOneDown.equals( "Custom" ))
154                 
155             {
156                 undoOp.addElement( jrf);
157                 
158                 jrf.getReport().setReportFormat( formatOneDown ) ;
159                 int oneDownWidth = PageSize.getFormatSize(formatOneDown).x;
160                 int oneDownHeight = PageSize.getFormatSize(formatOneDown).y;
161                 if (jrf.getReport().getOrientation().equals("Portrait") )
162                 {
163                     jrf.getReport().setWidth(oneDownWidth);
164                     jrf.getReport().setHeight(oneDownHeight);
165                     jrf.getReport().recalcColumnWidth();
166                     //TODO recalc columnwidth
167
}
168                 else
169                 {
170                     jrf.getReport().setWidth(oneDownHeight);
171                     jrf.getReport().setHeight(oneDownWidth);
172                     //TODO recalc columnwidth
173
jrf.getReport().recalcColumnWidth();
174                 }
175                 undoOp.captureUniqueModified(jrf);
176             }
177         }
178         
179     }
180     
181     
182     
183 }
184
Popular Tags