1 21 24 package org.lobobrowser.html.renderer; 25 26 import java.awt.*; 27 import java.awt.image.ImageObserver ; 28 29 import org.lobobrowser.html.domimpl.*; 30 import org.lobobrowser.html.style.HtmlValues; 31 32 class ImgControl extends BaseControl implements ImageListener { 33 private volatile Image image; 34 private String lastSrc; 36 37 public ImgControl(HTMLImageElementImpl modelNode) { 38 super(modelNode); 39 modelNode.addImageListener(this); 41 } 42 43 public void paintComponent(Graphics g) { 44 super.paintComponent(g); 45 Dimension size = this.getSize(); 46 Insets insets = this.getInsets(); 47 synchronized(this) {} 48 Image image = this.image; 49 if(image != null) { 50 g.drawImage(image, 51 insets.left, insets.top, 52 size.width - insets.left - insets.right, 53 size.height - insets.top - insets.bottom, this); 54 } 55 else { 56 } 58 } 59 60 private int valign = RElement.VALIGN_BASELINE; 61 private Dimension preferredSize; 62 private int declaredWidth; 63 private int declaredHeight; 64 65 public void reset(int availWidth, int availHeight) { 66 HTMLElementImpl element = this.controlElement; 68 int dw = HtmlValues.getOldSyntaxPixelSize(element.getAttribute("width"), availWidth, -1); 69 int dh = HtmlValues.getOldSyntaxPixelSize(element.getAttribute("height"), availHeight, -1); 70 this.declaredWidth = dw; 71 this.declaredHeight = dh; 72 this.preferredSize = this.createPreferredSize(dw, dh); 73 int valign; 74 String alignText = element.getAttribute("align"); 75 if(alignText == null) { 76 valign = RElement.VALIGN_BASELINE; 77 } 78 else { 79 alignText = alignText.toLowerCase().trim(); 80 if("middle".equals(alignText)) { 81 valign = RElement.VALIGN_MIDDLE; 82 } 83 else if("absmiddle".equals(alignText)) { 84 valign = RElement.VALIGN_ABSMIDDLE; 85 } 86 else if("top".equals(alignText)) { 87 valign = RElement.VALIGN_TOP; 88 } 89 else if("bottom".equals(alignText)) { 90 valign = RElement.VALIGN_BOTTOM; 91 } 92 else if("baseline".equals(alignText)) { 93 valign = RElement.VALIGN_BASELINE; 94 } 95 else if("absbottom".equals(alignText)) { 96 valign = RElement.VALIGN_ABSBOTTOM; 97 } 98 else { 99 valign = RElement.VALIGN_BASELINE; 100 } 101 } 102 this.valign = valign; 103 } 104 105 public int getVAlign() { 106 return this.valign; 107 } 108 109 public Dimension getPreferredSize() { 110 Dimension ps = this.preferredSize; 111 return ps == null ? new Dimension(0, 0) : ps; 112 } 113 114 public Dimension createPreferredSize(int dw, int dh) { 115 Image img = this.image; 116 if(dw == -1) { 117 dw = img == null ? -1 : img.getWidth(this); 118 if(dw == -1) { 119 dw = 0; 120 } 121 } 122 if(dh == -1) { 123 dh = img == null ? -1 : img.getHeight(this); 124 if(dh == -1) { 125 dh = 0; 126 } 127 } 128 return new Dimension(dw, dh); 129 } 130 131 private final boolean checkPreferredSizeChange() { 132 Dimension newPs = this.createPreferredSize(this.declaredWidth, this.declaredHeight); 133 Dimension ps = this.preferredSize; 134 if(ps == null) { 135 return true; 136 } 137 if(ps.width != newPs.width || ps.height != newPs.height) { 138 this.preferredSize = newPs; 139 return true; 140 } 141 else { 142 return false; 143 } 144 } 145 146 168 171 public boolean imageUpdate(Image img, int infoflags, int x, int y, final int w, final int h) { 172 if((infoflags & ImageObserver.ALLBITS) != 0 || (infoflags & ImageObserver.FRAMEBITS) != 0) { 173 EventQueue.invokeLater(new Runnable () { 174 public void run() { 175 if(!checkPreferredSizeChange()) { 176 repaint(); 177 } 178 else { 179 ruicontrol.preferredSizeInvalidated(); 180 } 181 } 182 }); 183 } 184 return true; 185 } 186 187 190 public void imageUpdate(Image img, final int w, final int h) { 191 EventQueue.invokeLater(new Runnable () { 192 public void run() { 193 if(!checkPreferredSizeChange()) { 194 repaint(); 195 } 196 else { 197 ruicontrol.preferredSizeInvalidated(); 198 } 199 } 200 }); 201 } 202 203 public boolean paintSelection(Graphics g, boolean inSelection, RenderableSpot startPoint, RenderableSpot endPoint) { 204 return inSelection; 205 } 206 207 public void imageLoaded(ImageEvent event) { 208 Image image = event.image; 210 this.image = image; 211 int width = image.getWidth(this); 212 int height = image.getHeight(this); 213 if(width != -1 && height != -1) { 214 this.imageUpdate(image, width, height); 215 } 216 } 217 218 public String toString() { 219 return "ImgControl[src=" + this.lastSrc + "]"; 220 } 221 } 222 | Popular Tags |