KickJava   Java API By Example, From Geeks To Geeks.

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


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 Nov 19, 2005
23  */

24 package org.lobobrowser.html.renderer;
25
26 import java.awt.*;
27 import java.awt.event.MouseEvent JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.SortedSet JavaDoc;
31 import java.util.TreeSet JavaDoc;
32
33 import org.lobobrowser.html.*;
34 import org.lobobrowser.html.domimpl.*;
35
36 class RTable extends BaseElementRenderable {
37     private final TableMatrix tableMatrix;
38     private SortedSet JavaDoc positionedRenderables;
39     private int otherOrdinal;
40     
41     public RTable(HTMLElementImpl modelNode, UserAgentContext pcontext, HtmlRendererContext rcontext, FrameContext frameContext, RenderableContainer container) {
42         super(container, modelNode, pcontext);
43         this.tableMatrix = new TableMatrix(modelNode, pcontext, rcontext, frameContext, this, this);
44     }
45
46     public int getVAlign() {
47         // Not used
48
return VALIGN_BASELINE;
49     }
50
51     protected void applyStyle() {
52         super.applyStyle();
53         Insets bi = this.borderInsets;
54         if(bi == null) {
55             HTMLElementImpl element = (HTMLElementImpl) this.modelNode;
56             String JavaDoc borderText = element.getAttribute("border");
57             int border = 0;
58             if(borderText != null) {
59                 try {
60                     border = Integer.parseInt(borderText);
61                     if(border < 0) {
62                         border = 0;
63                     }
64                 } catch(NumberFormatException JavaDoc nfe) {
65                     // ignore
66
}
67             }
68             if(border > 0) {
69                 this.borderInsets = new Insets(border, border, border, border);
70             }
71         }
72         if(this.borderTopColor == null && this.borderLeftColor == null) {
73             this.borderTopColor = this.borderLeftColor = Color.LIGHT_GRAY;
74         }
75         if(this.borderBottomColor == null && this.borderRightColor == null) {
76             this.borderBottomColor = this.borderRightColor = Color.DARK_GRAY;
77         }
78     }
79
80     public void paint(Graphics g) {
81         try {
82             this.prePaint(g);
83             Dimension size = this.getSize();
84             //TODO: No scrollbars
85
TableMatrix tm = this.tableMatrix;
86             tm.paint(g, size);
87             Collection JavaDoc prs = this.positionedRenderables;
88             if(prs != null) {
89                 Iterator JavaDoc i = prs.iterator();
90                 while(i.hasNext()) {
91                     PositionedRenderable pr = (PositionedRenderable) i.next();
92                     BoundableRenderable r = pr.renderable;
93                     r.paintTranslated(g);
94                 }
95             }
96         } finally {
97             // Must always call super implementation
98
super.paint(g);
99         }
100     }
101     
102     private volatile int lastAvailWidth = -1;
103     private volatile int lastAvailHeight = -1;
104 // private volatile Dimension layoutSize = null;
105

106     public void doLayout(int availWidth, int availHeight, boolean expandWidth, boolean expandHeight) {
107         if (availWidth != this.lastAvailWidth || availHeight != this.lastAvailHeight) {
108             Collection JavaDoc prs = this.positionedRenderables;
109             if(prs != null) {
110                 prs.clear();
111             }
112             this.otherOrdinal = 0;
113             this.clearGUIComponents();
114             this.clearDelayedPairs();
115             this.applyStyle();
116             this.lastAvailHeight = availHeight;
117             this.lastAvailWidth = availWidth;
118             TableMatrix tm = this.tableMatrix;
119             Insets insets = this.getInsets(false, false);
120             tm.reset(insets, availWidth, availHeight);
121             //TODO: No scrollbars
122
tm.build(availWidth, availHeight);
123             tm.doLayout(insets);
124             this.width = tm.getTableWidth();
125             this.height = tm.getTableHeight();
126             
127             // Import applicable delayed pairs.
128
// Only needs to be done if layout was
129
// forced. Otherwise, they should've
130
// been imported already.
131
Collection JavaDoc pairs = this.delayedPairs;
132             if(pairs != null) {
133                 Iterator JavaDoc i = pairs.iterator();
134                 while(i.hasNext()) {
135                     DelayedPair pair = (DelayedPair) i.next();
136                     if(pair.targetParent == this) {
137                         this.importDelayedPair(pair);
138                     }
139                 }
140             }
141             
142         } else {
143             // Nothing to do - dimensions already set.
144
}
145         this.sendGUIComponentsToParent();
146         this.sendDelayedPairsToParent();
147     }
148     
149 // /* (non-Javadoc)
150
// * @see org.xamjwg.html.renderer.UIControl#paintSelection(java.awt.Graphics, boolean, org.xamjwg.html.renderer.RenderablePoint, org.xamjwg.html.renderer.RenderablePoint)
151
// */
152
// public boolean paintSelection(Graphics g, boolean inSelection, RenderableSpot startPoint, RenderableSpot endPoint) {
153
// return this.tableMatrix.paintSelection(g, inSelection, startPoint, endPoint);
154
// }
155
//
156
// public boolean extractSelectionText(StringBuffer buffer, boolean inSelection, RenderableSpot startPoint, RenderableSpot endPoint) {
157
// return this.tableMatrix.extractSelectionText(buffer, inSelection, startPoint, endPoint);
158
// }
159

160     public void invalidateLayoutLocal() {
161         super.invalidateLayoutLocal();
162         this.lastAvailHeight = -1;
163         this.lastAvailWidth = -1;
164     }
165     
166     /* (non-Javadoc)
167      * @see org.xamjwg.html.renderer.BoundableRenderable#getRenderablePoint(int, int)
168      */

169     public RenderableSpot getLowestRenderableSpot(int x, int y) {
170         Collection JavaDoc prs = this.positionedRenderables;
171         if(prs != null) {
172             Iterator JavaDoc i = prs.iterator();
173             while(i.hasNext()) {
174                 PositionedRenderable pr = (PositionedRenderable) i.next();
175                 BoundableRenderable r = pr.renderable;
176                 int childX = x - r.getX();
177                 int childY = y - r.getY();
178                 RenderableSpot rs = r.getLowestRenderableSpot(childX, childY);
179                 if(rs != null) {
180                     return rs;
181                 }
182             }
183         }
184         RenderableSpot rs = this.tableMatrix.getLowestRenderableSpot(x, y);
185         if(rs != null) {
186             return rs;
187         }
188         return new RenderableSpot(this, x, y);
189     }
190     
191     /* (non-Javadoc)
192      * @see org.xamjwg.html.renderer.BoundableRenderable#onMouseClick(java.awt.event.MouseEvent, int, int)
193      */

194     public boolean onMouseClick(MouseEvent JavaDoc event, int x, int y) {
195         Collection JavaDoc prs = this.positionedRenderables;
196         if(prs != null) {
197             Iterator JavaDoc i = prs.iterator();
198             while(i.hasNext()) {
199                 PositionedRenderable pr = (PositionedRenderable) i.next();
200                 BoundableRenderable r = pr.renderable;
201                 Rectangle bounds = r.getBounds();
202                 if(bounds.contains(x, y)) {
203                     int childX = x - r.getX();
204                     int childY = y - r.getY();
205                     if(!r.onMouseClick(event, childX, childY)) {
206                         return false;
207                     }
208                 }
209             }
210         }
211         return this.tableMatrix.onMouseClick(event, x, y);
212     }
213
214     public boolean onDoubleClick(MouseEvent JavaDoc event, int x, int y) {
215         Collection JavaDoc prs = this.positionedRenderables;
216         if(prs != null) {
217             Iterator JavaDoc i = prs.iterator();
218             while(i.hasNext()) {
219                 PositionedRenderable pr = (PositionedRenderable) i.next();
220                 BoundableRenderable r = pr.renderable;
221                 Rectangle bounds = r.getBounds();
222                 if(bounds.contains(x, y)) {
223                     int childX = x - r.getX();
224                     int childY = y - r.getY();
225                     if(!r.onDoubleClick(event, childX, childY)) {
226                         return false;
227                     }
228                 }
229             }
230         }
231         return this.tableMatrix.onDoubleClick(event, x, y);
232     }
233
234     /* (non-Javadoc)
235      * @see org.xamjwg.html.renderer.BoundableRenderable#onMouseDisarmed(java.awt.event.MouseEvent)
236      */

237     public boolean onMouseDisarmed(MouseEvent JavaDoc event) {
238         return this.tableMatrix.onMouseDisarmed(event);
239     }
240
241     /* (non-Javadoc)
242      * @see org.xamjwg.html.renderer.BoundableRenderable#onMousePressed(java.awt.event.MouseEvent, int, int)
243      */

244     public boolean onMousePressed(MouseEvent JavaDoc event, int x, int y) {
245         Collection JavaDoc prs = this.positionedRenderables;
246         if(prs != null) {
247             Iterator JavaDoc i = prs.iterator();
248             while(i.hasNext()) {
249                 PositionedRenderable pr = (PositionedRenderable) i.next();
250                 BoundableRenderable r = pr.renderable;
251                 Rectangle bounds = r.getBounds();
252                 if(bounds.contains(x, y)) {
253                     int childX = x - r.getX();
254                     int childY = y - r.getY();
255                     if(!r.onMousePressed(event, childX, childY)) {
256                         return false;
257                     }
258                 }
259             }
260         }
261         return this.tableMatrix.onMousePressed(event, x, y);
262     }
263
264     /* (non-Javadoc)
265      * @see org.xamjwg.html.renderer.BoundableRenderable#onMouseReleased(java.awt.event.MouseEvent, int, int)
266      */

267     public boolean onMouseReleased(MouseEvent JavaDoc event, int x, int y) {
268         Collection JavaDoc prs = this.positionedRenderables;
269         if(prs != null) {
270             Iterator JavaDoc i = prs.iterator();
271             while(i.hasNext()) {
272                 PositionedRenderable pr = (PositionedRenderable) i.next();
273                 BoundableRenderable r = pr.renderable;
274                 Rectangle bounds = r.getBounds();
275                 if(bounds.contains(x, y)) {
276                     int childX = x - r.getX();
277                     int childY = y - r.getY();
278                     if(!r.onMouseReleased(event, childX, childY)) {
279                         return false;
280                     }
281                 }
282             }
283         }
284         return this.tableMatrix.onMouseReleased(event, x, y);
285     }
286
287     /* (non-Javadoc)
288      * @see org.xamjwg.html.renderer.RCollection#getRenderables()
289      */

290     public Iterator JavaDoc getRenderables() {
291         Collection JavaDoc prs = this.positionedRenderables;
292         if(prs != null) {
293             Collection JavaDoc c = new java.util.LinkedList JavaDoc();
294             Iterator JavaDoc i = prs.iterator();
295             while(i.hasNext()) {
296                 PositionedRenderable pr = (PositionedRenderable) i.next();
297                 BoundableRenderable r = pr.renderable;
298                 c.add(r);
299             }
300             Iterator JavaDoc i2 = this.tableMatrix.getRenderables();
301             while(i2.hasNext()) {
302                 c.add(i2.next());
303             }
304             return c.iterator();
305         }
306         else {
307             return this.tableMatrix.getRenderables();
308         }
309     }
310
311     public void repaint(ModelNode modelNode) {
312         //NOP
313
}
314     
315     /* (non-Javadoc)
316      * @see org.xamjwg.html.renderer.RenderableContainer#getBackground()
317      */

318     public Color getPaintedBackgroundColor() {
319         return this.container.getPaintedBackgroundColor();
320     }
321     
322     private final void addPositionedRenderable(BoundableRenderable renderable, boolean verticalAlignable) {
323         // Expected to be called only in GUI thread.
324
SortedSet JavaDoc others = this.positionedRenderables;
325         if(others == null) {
326             others = new TreeSet JavaDoc(new ZIndexComparator());
327             this.positionedRenderables = others;
328         }
329         others.add(new PositionedRenderable(renderable, verticalAlignable, this.otherOrdinal++));
330         renderable.setParent(this);
331         if(renderable instanceof RUIControl) {
332             this.container.add(((RUIControl) renderable).widget.getComponent());
333         }
334     }
335
336     private void importDelayedPair(DelayedPair pair) {
337         BoundableRenderable r = pair.child;
338         r.setOrigin(pair.x, pair.y);
339         this.addPositionedRenderable(r, false);
340     }
341     
342     public String JavaDoc toString() {
343         return "RTable[this=" + System.identityHashCode(this) + ",node=" + this.modelNode + "]";
344     }
345 }
346
Popular Tags