1 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 34 public class ImageElement extends PrintElement 35 { 36 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 } 49 53 public ImageElement(String 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 } 64 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 } 78 82 public ImageElement(int AD_PrintFormatItem_ID) 83 { 84 loadAttachment(AD_PrintFormatItem_ID); 85 } 87 88 private Logger log = Logger.getLogger (getClass()); 89 90 private Image m_image = null; 91 92 93 94 99 private URL getURL (String urlString) 100 { 101 URL url = null; 102 if (urlString.indexOf("://") == -1) 104 { 105 ClassLoader 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 try 114 { 115 url = new URL (urlString); 116 } 117 catch (MalformedURLException ex) 118 { 119 log.error("getURL", ex); 120 } 121 return url; 122 } 124 128 private void loadAttachment(int AD_PrintFormatItem_ID) 129 { 130 String 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()); } 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 } 158 159 160 165 protected boolean calculateSize() 166 { 167 p_width = 0; 168 p_height = 0; 169 if (m_image == null) 170 return true; 171 waitForLoad(m_image); 173 p_width = m_image.getWidth(this); 174 p_height = m_image.getHeight(this); 175 return true; 176 } 178 186 public void paint(Graphics2D g2D, int pageNo, Point2D pageStart, Properties ctx, boolean isView) 187 { 188 if (m_image == null) 189 return; 190 191 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 g2D.drawImage(m_image, x, y, this); 201 } 203 } | Popular Tags |