KickJava   Java API By Example, From Geeks To Geeks.

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


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-2002 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 java.util.*;
20 import java.net.*;
21 import java.sql.*;
22
23 import org.apache.log4j.Logger;
24
25 import org.compiere.print.*;
26 import org.compiere.util.DB;
27
28 /**
29  * Image Element
30  *
31  * @author Jorg Janke
32  * @version $Id: ImageElement.java,v 1.6 2003/10/31 05:32:48 jjanke Exp $
33  */

34 public class ImageElement extends PrintElement
35 {
36     /**
37      * Create Image from Image
38      * @param image image
39      */

40     public ImageElement(Image image)
41     {
42         m_image = image;
43         if (m_image != null)
44             log.debug(image);
45         else
46             log.error("Image is NULL");
47     } // ImageElement
48

49     /**
50      * Create Image from URL
51      * @param imageURLstring image url
52      */

53     public ImageElement(String JavaDoc imageURLstring)
54     {
55         URL imageURL = getURL(imageURLstring);
56         if (imageURL != null)
57             m_image = Toolkit.getDefaultToolkit().getImage(imageURL);
58         if (m_image != null)
59             log.debug("URL=" + imageURL);
60         else
61             log.error("ImageElement not loaded - URL=" + imageURL);
62     } // ImageElement
63

64     /**
65      * Create Image from URL
66      * @param imageURL image url
67      */

68     public ImageElement(URL imageURL)
69     {
70         if (imageURL != null)
71             m_image = Toolkit.getDefaultToolkit().getImage(imageURL);
72         if (m_image != null)
73             log.debug("URL=" + imageURL);
74         else
75             log.error("ImageElement not loaded - URL=" + imageURL);
76     } // ImageElement
77

78     /**
79      * Create Image from Attachment
80      * @param AD_PrintFormatItem_ID record id
81      */

82     public ImageElement(int AD_PrintFormatItem_ID)
83     {
84         loadAttachment(AD_PrintFormatItem_ID);
85     } // ImageElement
86

87     /** Logger */
88     private Logger log = Logger.getLogger (getClass());
89     /** The Image */
90     private Image m_image = null;
91
92     /*************************************************************************/
93
94     /**
95      * Get URL from String
96      * @param urlString url or resource
97      * @return URL or null
98      */

99     private URL getURL (String JavaDoc urlString)
100     {
101         URL url = null;
102         // not a URL - may be a resource
103
if (urlString.indexOf("://") == -1)
104         {
105             ClassLoader JavaDoc cl = getClass().getClassLoader();
106             url = cl.getResource(urlString);
107             if (url != null)
108                 return url;
109             log.error("getURL - Not found - " + urlString);
110             return null;
111         }
112         // load URL
113
try
114         {
115             url = new URL (urlString);
116         }
117         catch (MalformedURLException ex)
118         {
119             log.error("getURL", ex);
120         }
121         return url;
122     } // getURL;
123

124     /**
125      * Load Attachment
126      * @param AD_PrintFormatItem_ID record id
127      */

128     private void loadAttachment(int AD_PrintFormatItem_ID)
129     {
130         String JavaDoc sql = "SELECT BinaryData FROM AD_Attachment "
131             + "WHERE AD_Table_ID=489 AND Record_ID=?";
132         byte[] imageData = null;
133         try
134         {
135             PreparedStatement pstmt = DB.prepareStatement(sql);
136             pstmt.setInt(1, AD_PrintFormatItem_ID);
137             ResultSet rs = pstmt.executeQuery();
138             if (rs.next())
139             {
140                 Blob blob = rs.getBlob(1);
141                 imageData = blob.getBytes(0, (int)blob.length()); // limits it to 64k
142
}
143             rs.close();
144             pstmt.close();
145         }
146         catch (SQLException e)
147         {
148             log.error("loadAttachment", e);
149         }
150         if (imageData != null)
151             m_image = Toolkit.getDefaultToolkit().createImage(imageData);
152         if (m_image != null)
153             log.debug("loadAttachment - Attachment Size=" + imageData.length);
154         else
155             log.error("loadAttachment - ImageElement not loaded - AD_PrintFormatItem_ID=" + AD_PrintFormatItem_ID);
156     } // loadAttachment
157

158     /*************************************************************************/
159
160     /**
161      * Calculate Image Size.
162      * Set p_width & p_height
163      * @return true if calculated
164      */

165     protected boolean calculateSize()
166     {
167         p_width = 0;
168         p_height = 0;
169         if (m_image == null)
170             return true;
171         // we have an image
172
waitForLoad(m_image);
173         p_width = m_image.getWidth(this);
174         p_height = m_image.getHeight(this);
175         return true;
176     } // calculateSize
177

178     /**
179      * Paint Image
180      * @param g2D Graphics
181      * @param pageStart top left Location of page
182      * @param pageNo page number for multi page support (0 = header/footer) - ignored
183      * @param ctx print context
184      * @param isView true if online view (IDs are links)
185      */

186     public void paint(Graphics2D g2D, int pageNo, Point2D pageStart, Properties ctx, boolean isView)
187     {
188         if (m_image == null)
189             return;
190
191         // Position
192
Point2D.Double location = getAbsoluteLocation(pageStart);
193         int x = (int)location.x;
194         if (MPrintFormatItem.FIELDALIGNMENTTYPE_TrailingRight.equals(p_FieldAlignmentType))
195             x += p_maxWidth - p_width;
196         else if (MPrintFormatItem.FIELDALIGNMENTTYPE_Center.equals(p_FieldAlignmentType))
197             x += (p_maxWidth - p_width) / 2;
198         int y = (int)location.y;
199         //
200
g2D.drawImage(m_image, x, y, this);
201     } // paint
202

203 } // ImageElement
204
Popular Tags