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