KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > html > renderer > BaseBoundableRenderable


1 /*
2     GNU LESSER GENERAL PUBLIC LICENSE
3     Copyright (C) 2006 The Lobo Project
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19     Contact info: xamjadmin@users.sourceforge.net
20 */

21 /*
22  * Created on Apr 17, 2005
23  */

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 /**
33  * @author J. H. S.
34  */

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 Rectangle bounds = new Rectangle();
41
protected final RenderableContainer container;
42     protected final ModelNode modelNode;
43
44     public int x, y, width, height;
45     
46     /**
47      * Starts as true because ancestors could be invalidated.
48      */

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 JavaDoc 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 JavaDoc("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 JavaDoc("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 // /* (non-Javadoc)
122
// * @see net.sourceforge.xamj.domimpl.markup.Renderable#getBounds()
123
// */
124
// public Rectangle getBounds() {
125
// return this.bounds;
126
// }
127
//
128
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     /**
155      * Invalidates this Renderable and its parent (i.e. all
156      * ancestors).
157      */

158     public final void invalidateLayoutUpTree() {
159         if(this.layoutUpTreeCanBeInvalidated) {
160             this.layoutUpTreeCanBeInvalidated = false;
161             this.invalidateLayoutLocal();
162             // Try original parent first.
163
Renderable parent = this.originalParent;
164             if(parent == null) {
165                 parent = this.parent;
166                 if(parent == null) {
167                     // Has to be top block
168
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             // Has to be top RBlock.
200
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     /**
210      * Invalidates the current Renderable (which invalidates its ancestors)
211      * and then requests the top level GUI container to do the layout and repaint.
212      * It's safe to call this method outside the GUI thread.
213      */

214     public void relayout() {
215         if(EventQueue.isDispatchThread()) {
216             this.relayoutImpl(true);
217         }
218         else {
219             EventQueue.invokeLater(new Runnable JavaDoc() {
220                 public void run() {
221                     relayout();
222                 }
223             });
224         }
225     }
226     
227     /**
228      * Parent for graphics coordinates.
229      */

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     /**
241      * Parent for invalidation.
242      */

243     protected RCollection originalParent;
244     
245     public void setOriginalParent(RCollection origParent) {
246         this.originalParent = origParent;
247     }
248     
249     /**
250      * This is the parent based on the original element hierarchy.
251      */

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             // Has to be top RBlock.
271
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 JavaDoc translateDescendentPoint(BoundableRenderable descendent, int x, int y) {
300         while(descendent != this) {
301             if(descendent == null) {
302                 throw new IllegalStateException JavaDoc("Not descendent");
303             }
304             x += descendent.getX();
305             y += descendent.getY();
306             // Coordinates are always relative to actual parent?
307
descendent = descendent.getParent();
308         }
309         return new Point(x, y);
310     }
311 }
312
Popular Tags