KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > print > layout > PrintElement


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.print.layout;
15
16 import java.awt.*;
17 import java.awt.geom.*;
18 import java.awt.image.*;
19 import javax.swing.*;
20 import java.util.*;
21
22 import org.compiere.print.*;
23 import org.compiere.model.*;
24 import org.compiere.util.*;
25
26 /**
27  * Print Element
28  *
29  * @author Jorg Janke
30  * @version $Id: PrintElement.java,v 1.11 2003/10/31 05:32:48 jjanke Exp $
31  */

32 public abstract class PrintElement implements ImageObserver
33 {
34     /**
35      * Constructor
36      */

37     protected PrintElement ()
38     {
39     } // PrintElement
40

41     /** Link Color */
42     public static final Color LINK_COLOR = Color.blue;
43
44     /** Calculated Size of Element */
45     protected float p_width = 0f;
46     protected float p_height = 0f;
47     protected boolean p_sizeCalculated = false;
48     /** Max Size of Element */
49     protected float p_maxWidth = 0f;
50     protected float p_maxHeight = 0f;
51     /** Field Align Type */
52     protected String JavaDoc p_FieldAlignmentType;
53     /** Location on Page */
54     protected Point2D.Double p_pageLocation = null;
55
56     /*************************************************************************/
57
58     /**
59      * Get Calculated Width
60      * @return Width
61      */

62     public float getWidth()
63     {
64         if (!p_sizeCalculated)
65             p_sizeCalculated = calculateSize();
66         return p_width;
67     } // getWidth
68

69     /**
70      * Get Calculated Height
71      * @return Height
72      */

73     public float getHeight()
74     {
75         if (!p_sizeCalculated)
76             p_sizeCalculated = calculateSize();
77         return p_height;
78     } // getHeight
79

80     /**
81      * Get Calculated Height on page
82      * @param pageNo page number
83      * @return Height
84      */

85     public float getHeight (int pageNo)
86     {
87         return getHeight();
88     } // getHeight
89

90     /**
91      * Get number of pages
92      * @return page count (1)
93      */

94     public int getPageCount()
95     {
96         return 1;
97     } // getPageCount
98

99     /**
100      * Layout and Calculate Size
101      * Set p_width & p_height
102      * @return true if calculated
103      */

104     protected abstract boolean calculateSize();
105
106     /**
107      * Layout Element
108      * @param maxWidth max width
109      * @param maxHeight max height
110      * @param isHeightOneLine just one line
111      * @param FieldAlignmentType alignment type (MPrintFormatItem.FIELD_ALIGN_*)
112      */

113     public void layout (float maxWidth, float maxHeight, boolean isHeightOneLine,
114         String JavaDoc FieldAlignmentType)
115     {
116         if (isHeightOneLine)
117             p_maxHeight = -1f;
118         else if (maxHeight > 0f)
119             p_maxHeight = maxHeight;
120         p_maxWidth = maxWidth;
121         //
122
p_FieldAlignmentType = FieldAlignmentType;
123         if (p_FieldAlignmentType == null || p_FieldAlignmentType == MPrintFormatItem.FIELDALIGNMENTTYPE_Default)
124             p_FieldAlignmentType = MPrintFormatItem.FIELDALIGNMENTTYPE_LeadingLeft;
125         //
126
p_sizeCalculated = calculateSize();
127     } // layout
128

129     /**
130      * Set Maximum Height
131      * @param maxHeight maximum height (0) is no limit
132      */

133     public void setMaxHeight (float maxHeight)
134     {
135         p_maxHeight = maxHeight;
136     } // setMaxHeight
137

138     /**
139      * Set Maximum Width
140      * @param maxWidth maximum width (0) is no limit
141      */

142     public void setMaxWidth (float maxWidth)
143     {
144         p_maxWidth = maxWidth;
145     } // setMaxWidth
146

147     /**
148      * Set Location within page
149      * @param pageLocation location within page
150      */

151     public void setLocation (Point2D pageLocation)
152     {
153         p_pageLocation = new Point2D.Double(pageLocation.getX(), pageLocation.getY());
154     } // setLocation
155

156     /**
157      * Get Location within page
158      * @return location within page
159      */

160     public Point2D getLocation()
161     {
162         return p_pageLocation;
163     } // getLocation
164

165     /**
166      * Return Absolute Position
167      * @param pageStart start of page
168      * @return absolite position
169      */

170     protected Point2D.Double getAbsoluteLocation(Point2D pageStart)
171     {
172         Point2D.Double retValue = new Point2D.Double(
173             p_pageLocation.x + pageStart.getX(), p_pageLocation.y + pageStart.getY());
174     // Log.trace(9, "PrintElement.getAbsoluteLocation", "PageStart=" + pageStart.getX() + "/" + pageStart.getY()
175
// + ",PageLocaton=" + p_pageLocation.x + "/" + p_pageLocation.y + " => " + retValue.x + "/" + retValue.y);
176
return retValue;
177     } // getAbsoluteLocation
178

179     /**
180      * Get relative Bounds of Element
181      * @return bounds relative position on page
182      */

183     public Rectangle getBounds()
184     {
185         if (p_pageLocation == null)
186             return new Rectangle (0,0, (int)p_width, (int)p_height);
187         return new Rectangle ((int)p_pageLocation.x, (int)p_pageLocation.y, (int)p_width, (int)p_height);
188     } // getBounds
189

190     /**
191      * Get Drill Down value
192      * @param relativePoint relative Point
193      * @param pageNo page number
194      * @return null (subclasses overwrite)
195      */

196     public MQuery getDrillDown (Point relativePoint, int pageNo)
197     {
198         return null;
199     } // getDrillDown
200

201     /**
202      * Get Drill Across value
203      * @param relativePoint relative Point
204      * @param pageNo page number
205      * @return null (subclasses overwrite)
206      */

207     public MQuery getDrillAcross (Point relativePoint, int pageNo)
208     {
209         return null;
210     } // getDrillAcross
211

212     /*************************************************************************/
213
214     /**
215      * Translate Context if required.
216      * If content is translated, the element needs to stay in the bounds
217      * of the originally calculated size and need to align the field.
218      * @param ctx context
219      */

220     public void translate (Properties ctx)
221     {
222     // noop
223
} // translate
224

225     /**
226      * Content is translated
227      * @return false
228      */

229     public boolean isTranslated()
230     {
231         return false;
232     } // translate
233

234     /*************************************************************************/
235
236     /**
237      * Paint/Print.
238      * @param g2D Graphics
239      * @param pageNo page number for multi page support (0 = header/footer)
240      * @param pageStart top left Location of page
241      * @param ctx context
242      * @param isView true if online view (IDs are links)
243      */

244     public abstract void paint (Graphics2D g2D, int pageNo, Point2D pageStart, Properties ctx, boolean isView);
245
246     /*************************************************************************/
247
248     /**
249      * Image Observer
250      * @param img image
251      * @param infoflags Observer flags
252      * @param x x coordinate
253      * @param y y coordinate
254      * @param width image width
255      * @param height image height
256      * @return false if the infoflags indicate that the image is completely loaded; true otherwise
257      */

258     public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height)
259     {
260         // copied from java.awt.component
261
boolean imageNotLoaded = (infoflags & (ALLBITS|ABORT)) == 0;
262     // Log.trace(Log.l6_Database, "PrintElement.imageUpdate - Flags=" + infoflags
263
// + ", x=" + x + ", y=" + y + ", width=" + width + ", height=" + height + " => " + m_imageNotLoaded);
264
return imageNotLoaded;
265     } // imageUpdate
266

267     /**
268      * Wait until Image is loaded.
269      * @param image image
270      */

271     public void waitForLoad (Image image)
272     {
273         long start = System.currentTimeMillis();
274         int count = 0;
275         while (!Toolkit.getDefaultToolkit().prepareImage(image, -1, -1, this))
276         {
277             try
278             {
279                 Thread.sleep(1);
280             }
281             catch (InterruptedException JavaDoc ex)
282             {
283                 Log.error("PrintElement.waitForLoad", ex);
284                 break;
285             }
286             count++;
287         }
288         if (count > 0)
289             Log.trace(Log.l6_Database, "PrintElement.waitForLoad - " + (System.currentTimeMillis()-start) + "ms - #" + count);
290     } // waitForLoad
291

292     /**
293      * String Representation
294      * @return info
295      */

296     public String JavaDoc toString()
297     {
298         String JavaDoc cn = getClass().getName();
299         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
300         sb.append(cn.substring(cn.lastIndexOf('.')+1)).append("[");
301         sb.append("Bounds=").append(getBounds())
302             .append(",Height=").append(p_height).append("(").append(p_maxHeight)
303             .append("),Width=").append(p_width).append("(").append(p_maxHeight)
304             .append("),PageLocation=").append(p_pageLocation);
305         sb.append("]");
306         return sb.toString();
307     } // toString
308

309 } // PrintElement
310
Popular Tags