KickJava   Java API By Example, From Geeks To Geeks.

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


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.Color JavaDoc;
27 import java.awt.Dimension JavaDoc;
28 import java.awt.Graphics JavaDoc;
29 import java.awt.Insets JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import javax.swing.*;
32
33 import org.lobobrowser.html.domimpl.*;
34 import org.lobobrowser.html.*;
35 import org.lobobrowser.html.style.RenderState;
36
37 /**
38  * @author J. H. S.
39  */

40 class RUIControl extends BaseElementRenderable implements RElement {
41     public final UIControl widget;
42     private final ModelNode modelNode;
43     private final FrameContext frameContext;
44     
45     public RUIControl(ModelNode me, UIControl widget, RenderableContainer container, FrameContext frameContext, UserAgentContext ucontext) {
46         super(container, me, ucontext);
47         this.modelNode = me;
48         this.widget = widget;
49         this.frameContext = frameContext;
50         widget.setRUIControl(this);
51     }
52     
53     public void focus() {
54         super.focus();
55         java.awt.Component JavaDoc c = this.widget.getComponent();
56         c.requestFocus();
57     }
58     
59     public final void invalidateLayoutLocal() {
60         // Invalidate widget (some redundancy)
61
super.invalidateLayoutLocal();
62         this.widget.invalidate();
63         // Invalidate cached values
64
this.lastAvailHeight = -1;
65         this.lastAvailWidth = -1;
66     }
67     
68     public int getVAlign() {
69         return this.widget.getVAlign();
70     }
71
72     public final void paint(Graphics JavaDoc g) {
73         // We need to paint the GUI component.
74
// For various reasons, we need to do that
75
// instead of letting AWT do it.
76
this.widget.paint(g);
77         // Super implementation marks style validated,
78
// and must always be called.
79
super.paint(g);
80     }
81     
82     public boolean onMouseClick(java.awt.event.MouseEvent JavaDoc event, int x, int y) {
83         ModelNode me = this.modelNode;
84         if(me != null) {
85             return HtmlController.getInstance().onMouseClick(me, event, x, y);
86         }
87         else {
88             return true;
89         }
90     }
91
92     public boolean onDoubleClick(java.awt.event.MouseEvent JavaDoc event, int x, int y) {
93         ModelNode me = this.modelNode;
94         if(me != null) {
95             return HtmlController.getInstance().onDoubleClick(me, event, x, y);
96         }
97         else {
98             return true;
99         }
100     }
101
102     public boolean onMousePressed(java.awt.event.MouseEvent JavaDoc event, int x, int y) {
103         ModelNode me = this.modelNode;
104         if(me != null) {
105             return HtmlController.getInstance().onMouseDown(me, event, x, y);
106         }
107         else {
108             return true;
109         }
110     }
111
112     public boolean onMouseReleased(java.awt.event.MouseEvent JavaDoc event, int x, int y) {
113         ModelNode me = this.modelNode;
114         if(me != null) {
115             return HtmlController.getInstance().onMouseUp(me, event, x, y);
116         }
117         else {
118             return true;
119         }
120     }
121     
122     public boolean onMouseDisarmed(java.awt.event.MouseEvent JavaDoc event) {
123         ModelNode me = this.modelNode;
124         if(me != null) {
125             return HtmlController.getInstance().onMouseDisarmed(me, event);
126         }
127         else {
128             return true;
129         }
130     }
131
132     /* (non-Javadoc)
133      * @see org.xamjwg.html.renderer.BoundableRenderable#invalidateState(org.xamjwg.html.renderer.RenderableContext)
134      */

135     public void invalidateRenderStyle() {
136         //NOP - No RenderStyle below this node.
137
}
138
139     /* (non-Javadoc)
140      * @see org.xamjwg.html.domimpl.ContainingBlockContext#repaint(org.xamjwg.html.renderer.RenderableContext)
141      */

142     public void repaint(ModelNode modelNode) {
143         Object JavaDoc widget = this.widget;
144         if(widget instanceof UINode) {
145             ((UINode) widget).repaint(modelNode);
146         }
147         else {
148             this.repaint();
149         }
150     }
151     
152     public void updateWidgetBounds(int guiX, int guiY) {
153         // Overrides
154
super.updateWidgetBounds(guiX, guiY);
155         this.widget.setBounds(guiX, guiY, this.width, this.height);
156     }
157     
158     public Color JavaDoc getBlockBackgroundColor() {
159         return this.widget.getBackgroundColor();
160     }
161
162     /* (non-Javadoc)
163      * @see org.xamjwg.html.renderer.BoundableRenderable#paintSelection(java.awt.Graphics, boolean, org.xamjwg.html.renderer.RenderablePoint, org.xamjwg.html.renderer.RenderablePoint)
164      */

165     public boolean paintSelection(Graphics JavaDoc g, boolean inSelection, RenderableSpot startPoint, RenderableSpot endPoint) {
166         inSelection = super.paintSelection(g, inSelection, startPoint, endPoint);
167         if(inSelection) {
168             Color JavaDoc over = new Color JavaDoc(0, 0, 255, 50);
169             if(over != null) {
170                 Color JavaDoc oldColor = g.getColor();
171                 try {
172                     g.setColor(over);
173                     g.fillRect(0, 0, this.width, this.height);
174                 } finally {
175                     g.setColor(oldColor);
176                 }
177             }
178         }
179         return inSelection;
180     }
181
182     public boolean extractSelectionText(StringBuffer JavaDoc buffer, boolean inSelection, RenderableSpot startPoint, RenderableSpot endPoint) {
183         // No text here
184
return inSelection;
185     }
186     
187     public RenderableSpot getLowestRenderableSpot(int x, int y) {
188         // Nothing draggable - return self
189
return new RenderableSpot(this, x, y);
190     }
191
192     private int lastAvailWidth = -1;
193     private int lastAvailHeight = -1;
194     private int declaredWidth = -1;
195     private int declaredHeight = -1;
196     
197     public void doLayout(int availWidth, int availHeight, boolean expandWidth, boolean expandHeight) {
198         if(availWidth != this.lastAvailWidth || availHeight != this.lastAvailHeight) {
199             this.applyStyle();
200             this.lastAvailWidth = availWidth;
201             this.lastAvailHeight = availHeight;
202             UIControl widget = this.widget;
203             widget.reset(availWidth, availHeight);
204             RenderState renderState = this.modelNode.getRenderState();
205             int dw = this.getDeclaredWidth(renderState, availWidth);
206             int dh = this.getDeclaredHeight(renderState, availHeight);
207             this.declaredWidth = dw;
208             this.declaredHeight = dh;
209             if(dw == -1 || dh == -1) {
210                 Dimension JavaDoc size = widget.getPreferredSize();
211                 if(dw == -1) {
212                     dw = size.width;
213                 }
214                 if(dh == -1) {
215                     dh = size.height;
216                 }
217             }
218             this.width = dw;
219             this.height = dh;
220         }
221     }
222
223     /**
224      * May be called by controls when
225      * they wish to modifiy their preferred
226      * size (e.g. an image after it's loaded).
227      * This method must be called
228      * in the GUI thread.
229      */

230     public final void preferredSizeInvalidated() {
231         int dw = RUIControl.this.declaredWidth;
232         int dh = RUIControl.this.declaredHeight;
233         if(dw == -1 || dh == -1) {
234             this.frameContext.delayedRelayout((NodeImpl) this.modelNode);
235         }
236         else {
237             RUIControl.this.repaint();
238         }
239     }
240     
241     public Iterator JavaDoc getRenderables() {
242         // No children for GUI controls
243
return null;
244     }
245
246     public Color JavaDoc getPaintedBackgroundColor() {
247         return this.container.getPaintedBackgroundColor();
248     }
249
250     public Insets JavaDoc getInsets() {
251         return RBlockViewport.ZERO_INSETS;
252     }
253 }
254
Popular Tags