001/* 002 * All rights reserved. This program and the accompanying materials 003 * are made available under the terms of the GNU Lesser General Public License 004 * (LGPL) version 2.1 which accompanies this distribution, and is available at 005 * http://www.gnu.org/licenses/lgpl.html 006 * 007 * This library is distributed in the hope that it will be useful, 008 * but WITHOUT ANY WARRANTY; without even the implied warranty of 009 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 010 * Lesser General Public License for more details. 011 * 012 * Contributors: 013 * Original file from org.jboss.seam.pdf.ui.UIRectangle.java in jboss-seam-pdf 014 * Anahide Tchertchian 015 */ 016package org.nuxeo.ecm.platform.ui.web.component.seam; 017 018import java.awt.image.BufferedImage; 019import java.io.IOException; 020 021import javax.faces.component.UIComponent; 022import javax.faces.context.FacesContext; 023 024import org.apache.commons.logging.Log; 025import org.apache.commons.logging.LogFactory; 026import org.jboss.seam.pdf.ITextUtils; 027import org.jboss.seam.ui.graphicImage.ImageTransform; 028 029import com.lowagie.text.DocumentException; 030import com.lowagie.text.Image; 031 032/** 033 * Overrides default image to avoid crash when image is not found. 034 * 035 * @since 5.4.2 036 */ 037public class UIImage extends org.jboss.seam.pdf.ui.UIRectangle { 038 039 private static final Log log = LogFactory.getLog(UIImage.class); 040 041 public static final String COMPONENT_TYPE = UIImage.class.getName(); 042 043 Image image; 044 045 Object value; 046 047 float rotation; 048 049 float height; 050 051 float width; 052 053 String alignment; 054 055 String alt; 056 057 Float indentationLeft; 058 059 Float indentationRight; 060 061 Float spacingBefore; 062 063 Float spacingAfter; 064 065 Float widthPercentage; 066 067 Float initialRotation; 068 069 String dpi; 070 071 String scalePercent; 072 073 Boolean wrap; 074 075 Boolean underlying; 076 077 java.awt.Image imageData; 078 079 public void setValue(Object value) { 080 this.value = value; 081 } 082 083 public void setRotation(float rotation) { 084 this.rotation = rotation; 085 } 086 087 public void setHeight(float height) { 088 this.height = height; 089 } 090 091 public void setWidth(float width) { 092 this.width = width; 093 } 094 095 public void setAlignment(String alignment) { 096 this.alignment = alignment; 097 } 098 099 public void setAlt(String alt) { 100 this.alt = alt; 101 } 102 103 public void setWrap(Boolean wrap) { 104 this.wrap = wrap; 105 } 106 107 public void setUnderlying(Boolean underlying) { 108 this.underlying = underlying; 109 } 110 111 public void setDpi(String dpi) { 112 this.dpi = dpi; 113 } 114 115 public void setIndentationLeft(Float indentationLeft) { 116 this.indentationLeft = indentationLeft; 117 } 118 119 public void setIndentationRight(Float indentationRight) { 120 this.indentationRight = indentationRight; 121 } 122 123 public void setInitialRotation(Float initialRotation) { 124 this.initialRotation = initialRotation; 125 } 126 127 public void setSpacingAfter(Float spacingAfter) { 128 this.spacingAfter = spacingAfter; 129 } 130 131 public void setSpacingBefore(Float spacingBefore) { 132 this.spacingBefore = spacingBefore; 133 } 134 135 public void setWidthPercentage(Float widthPercentage) { 136 this.widthPercentage = widthPercentage; 137 } 138 139 public void setScalePercent(String scalePercent) { 140 this.scalePercent = scalePercent; 141 } 142 143 @Override 144 public Object getITextObject() { 145 return image; 146 } 147 148 @Override 149 public void removeITextObject() { 150 image = null; 151 } 152 153 @Override 154 public void createITextObject(FacesContext context) throws IOException, DocumentException { 155 value = valueBinding(context, "value", value); 156 157 // instance() doesn't work here - we need a new instance 158 org.jboss.seam.ui.graphicImage.Image seamImage = new org.jboss.seam.ui.graphicImage.Image(); 159 try { 160 if (value instanceof BufferedImage) { 161 seamImage.setBufferedImage((BufferedImage) value); 162 } else { 163 seamImage.setInput(value); 164 } 165 } catch (IOException e) { 166 log.error("Cannot resolve image for value " + value, e); 167 return; 168 } 169 170 for (UIComponent cmp : getChildren()) { 171 if (cmp instanceof ImageTransform) { 172 ImageTransform imageTransform = (ImageTransform) cmp; 173 imageTransform.applyTransform(seamImage); 174 } 175 } 176 177 byte[] data = seamImage.getImage(); 178 if (data == null) { 179 log.error("Cannot resolve image for value " + value); 180 return; 181 } 182 image = Image.getInstance(data); 183 184 rotation = (Float) valueBinding(context, "rotation", rotation); 185 if (rotation != 0) { 186 image.setRotationDegrees(rotation); 187 } 188 189 height = (Float) valueBinding(context, "height", height); 190 width = (Float) valueBinding(context, "width", width); 191 if (height > 0 || width > 0) { 192 image.scaleAbsolute(width, height); 193 } 194 195 int alignmentValue = 0; 196 197 alignment = (String) valueBinding(context, "alignment", alignment); 198 if (alignment != null) { 199 alignmentValue = (ITextUtils.alignmentValue(alignment)); 200 } 201 202 wrap = (Boolean) valueBinding(context, "wrap", wrap); 203 if (wrap != null && wrap.booleanValue()) { 204 alignmentValue |= Image.TEXTWRAP; 205 } 206 207 underlying = (Boolean) valueBinding(context, "underlying", underlying); 208 if (underlying != null && underlying.booleanValue()) { 209 alignmentValue |= Image.UNDERLYING; 210 } 211 212 image.setAlignment(alignmentValue); 213 214 alt = (String) valueBinding(context, "alt", alt); 215 if (alt != null) { 216 image.setAlt(alt); 217 } 218 219 indentationLeft = (Float) valueBinding(context, "indentationLeft", indentationLeft); 220 if (indentationLeft != null) { 221 image.setIndentationLeft(indentationLeft); 222 } 223 224 indentationRight = (Float) valueBinding(context, "indentationRight", indentationRight); 225 if (indentationRight != null) { 226 image.setIndentationRight(indentationRight); 227 } 228 229 spacingBefore = (Float) valueBinding(context, "spacingBefore", spacingBefore); 230 if (spacingBefore != null) { 231 image.setSpacingBefore(spacingBefore); 232 } 233 234 spacingAfter = (Float) valueBinding(context, "spacingAfter", spacingAfter); 235 if (spacingAfter != null) { 236 image.setSpacingAfter(spacingAfter); 237 } 238 widthPercentage = (Float) valueBinding(context, "widthPercentage", widthPercentage); 239 if (widthPercentage != null) { 240 image.setWidthPercentage(widthPercentage); 241 } 242 243 initialRotation = (Float) valueBinding(context, "initialRotation", initialRotation); 244 if (initialRotation != null) { 245 image.setInitialRotation(initialRotation); 246 } 247 248 dpi = (String) valueBinding(context, "dpi", dpi); 249 if (dpi != null) { 250 int[] dpiValues = ITextUtils.stringToIntArray(dpi); 251 image.setDpi(dpiValues[0], dpiValues[1]); 252 } 253 254 applyRectangleProperties(context, image); 255 256 scalePercent = (String) valueBinding(context, "scalePercent", scalePercent); 257 if (scalePercent != null) { 258 float[] scale = ITextUtils.stringToFloatArray(scalePercent); 259 if (scale.length == 1) { 260 image.scalePercent(scale[0]); 261 } else if (scale.length == 2) { 262 image.scalePercent(scale[0], scale[1]); 263 } else { 264 throw new RuntimeException("scalePercent must contain one or two scale percentages"); 265 } 266 } 267 } 268 269 @Override 270 public void handleAdd(Object o) { 271 throw new RuntimeException("can't add " + o.getClass().getName() + " to image"); 272 } 273 274}