| 1 21 24 package org.lobobrowser.html.renderer; 25 26 import java.awt.*; 27 28 import org.lobobrowser.html.domimpl.*; 29 30 import java.util.logging.*; 31 32 35 abstract class BaseBoundableRenderable extends BaseRenderable implements BoundableRenderable { 36 protected static final Logger logger = Logger.getLogger(BaseBoundableRenderable.class.getName()); 37 protected static final Color SELECTION_COLOR = Color.BLUE; 38 protected static final Color SELECTION_XOR = Color.LIGHT_GRAY; 39 40 protected final RenderableContainer container; 42 protected final ModelNode modelNode; 43 44 public int x, y, width, height; 45 46 49 protected boolean layoutUpTreeCanBeInvalidated = true; 50 51 public BaseBoundableRenderable(RenderableContainer container, ModelNode modelNode) { 52 this.container = container; 53 this.modelNode = modelNode; 54 } 55 56 public java.awt.Point getGUIPoint(int clientX, int clientY) { 57 Renderable parent = this.getParent(); 58 if(parent instanceof BoundableRenderable) { 59 return ((BoundableRenderable) parent).getGUIPoint(clientX + this.x, clientY + this.y); 60 } 61 else if(parent == null) { 62 return this.container.getGUIPoint(clientX + this.x, clientY + this.y); 63 } 64 else { 65 throw new IllegalStateException ("parent=" + parent); 66 } 67 } 68 69 public Point getRenderablePoint(int guiX, int guiY) { 70 Renderable parent = this.getParent(); 71 if(parent instanceof BoundableRenderable) { 72 return ((BoundableRenderable) parent).getRenderablePoint(guiX - this.x, guiY - this.y); 73 } 74 else if(parent == null) { 75 return new Point(guiX - this.x, guiY - this.y); 76 } 77 else { 78 throw new IllegalStateException ("parent=" + parent); 79 } 80 } 81 82 public int getHeight() { 83 return height; 84 } 85 86 87 public int getWidth() { 88 return width; 89 } 90 91 92 public void setWidth(int width) { 93 this.width = width; 94 } 95 96 public int getX() { 97 return x; 98 } 99 100 101 public int getY() { 102 return y; 103 } 104 105 public boolean contains(int x, int y) { 106 return x >= this.x && y >= this.y && x < this.x + this.width && y < this.y + this.height; 107 } 108 109 public Rectangle getBounds() { 110 return new Rectangle(this.x, this.y, this.width, this.height); 111 } 112 113 public Dimension getSize() { 114 return new Dimension(this.width, this.height); 115 } 116 117 public ModelNode getModelNode() { 118 return this.modelNode; 119 } 120 121 public void setBounds(int x, int y, int width, int height) { 129 this.x = x; 130 this.y = y; 131 this.width = width; 132 this.height = height; 133 } 134 135 public void setX(int x) { 136 this.x = x; 137 } 138 139 public void setY(int y) { 140 this.y = y; 141 } 142 143 public void setHeight(int height) { 144 this.height = height; 145 } 146 147 public void setOrigin(int x, int y) { 148 this.x = x; 149 this.y = y; 150 } 151 152 protected abstract void invalidateLayoutLocal(); 153 154 158 public final void invalidateLayoutUpTree() { 159 if(this.layoutUpTreeCanBeInvalidated) { 160 this.layoutUpTreeCanBeInvalidated = false; 161 this.invalidateLayoutLocal(); 162 Renderable parent = this.originalParent; 164 if(parent == null) { 165 parent = this.parent; 166 if(parent == null) { 167 RenderableContainer rc = this.container; 169 if(rc != null) { 170 rc.invalidateLayoutUpTree(); 171 } 172 } 173 else { 174 parent.invalidateLayoutUpTree(); 175 } 176 } 177 else { 178 parent.invalidateLayoutUpTree(); 179 } 180 } 181 else { 182 } 183 } 184 185 protected boolean isValid() { 186 return this.layoutUpTreeCanBeInvalidated; 187 } 188 189 190 protected final void relayoutImpl(boolean invalidateLocal) { 191 if(invalidateLocal) { 192 this.invalidateLayoutUpTree(); 193 } 194 Renderable parent = this.parent; 195 if(parent instanceof BaseBoundableRenderable) { 196 ((BaseBoundableRenderable) parent).relayoutImpl(false); 197 } 198 else if(parent == null) { 199 this.container.relayout(); 201 } 202 else { 203 if(logger.isLoggable(Level.INFO)) { 204 logger.warning("relayout(): Don't know how to relayout " + this + ", parent being " + parent); 205 } 206 } 207 } 208 209 214 public void relayout() { 215 if(EventQueue.isDispatchThread()) { 216 this.relayoutImpl(true); 217 } 218 else { 219 EventQueue.invokeLater(new Runnable () { 220 public void run() { 221 relayout(); 222 } 223 }); 224 } 225 } 226 227 230 protected RCollection parent; 231 232 public void setParent(RCollection parent) { 233 this.parent = parent; 234 } 235 236 public RCollection getParent() { 237 return this.parent; 238 } 239 240 243 protected RCollection originalParent; 244 245 public void setOriginalParent(RCollection origParent) { 246 this.originalParent = origParent; 247 } 248 249 252 public RCollection getOriginalParent() { 253 return this.originalParent; 254 } 255 256 public RCollection getOriginalOrCurrentParent() { 257 RCollection origParent = this.originalParent; 258 if(origParent == null) { 259 return this.parent; 260 } 261 return origParent; 262 } 263 264 public void repaint(int x, int y, int width, int height) { 265 Renderable parent = this.parent; 266 if(parent instanceof BoundableRenderable) { 267 ((BoundableRenderable) parent).repaint(x + this.x, y + this.y, width, height); 268 } 269 else if(parent == null) { 270 this.container.repaint(x, y, width, height); 272 } 273 else { 274 if(logger.isLoggable(Level.INFO)) { 275 logger.warning("repaint(): Don't know how to repaint " + this + ", parent being " + parent); 276 } 277 } 278 } 279 280 public void repaint() { 281 this.repaint(0, 0, this.width, this.height); 282 } 283 284 public Color getBlockBackgroundColor() { 285 return this.container.getPaintedBackgroundColor(); 286 } 287 288 public final void paintTranslated(Graphics g) { 289 int x = this.x; 290 int y = this.y; 291 g.translate(x, y); 292 try { 293 this.paint(g); 294 } finally { 295 g.translate(-x, -y); 296 } 297 } 298 299 protected final java.awt.Point translateDescendentPoint(BoundableRenderable descendent, int x, int y) { 300 while(descendent != this) { 301 if(descendent == null) { 302 throw new IllegalStateException ("Not descendent"); 303 } 304 x += descendent.getX(); 305 y += descendent.getY(); 306 descendent = descendent.getParent(); 308 } 309 return new Point(x, y); 310 } 311 } 312 | Popular Tags |