KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.awt.*;
26
27 import org.lobobrowser.html.domimpl.ModelNode;
28 import org.lobobrowser.html.style.RenderState;
29
30 class RWord extends BaseBoundableRenderable {
31 // public final WordInfo wordInfo;
32
public final String JavaDoc word;
33     
34     public final FontMetrics fontMetrics;
35     public final int descent;
36     public final int ascentPlusLeading;
37     
38 // private final int textDecoration;
39
// private final Color backgroundColor;
40

41     //private final RenderState renderState;
42
public RWord(ModelNode me, String JavaDoc word, RenderableContainer container, FontMetrics fontMetrics, int descent, int ascentPlusLeading, int height) {
43         super(container, me);
44         this.word = word;
45         this.fontMetrics = fontMetrics;
46         this.descent = descent;
47         this.ascentPlusLeading = ascentPlusLeading;
48         this.height = height;
49         // TODO: In anti-aliasing, stringWidth is said not to be reliable.
50
// Dimenions set when constructed.
51
this.width = fontMetrics.stringWidth(word);
52     }
53     
54     protected void invalidateLayoutLocal() {
55     }
56     
57     /* (non-Javadoc)
58      * @see net.sourceforge.xamj.domimpl.markup.Renderable#paint(java.awt.Graphics)
59      */

60     public void paint(Graphics g) {
61         RenderState rs = this.modelNode.getRenderState();
62         String JavaDoc word = this.word;
63         int width = this.width;
64         int ascentPlusLeading = this.ascentPlusLeading;
65         int height = this.height;
66         int textDecoration = rs.getTextDecorationMask();
67         Color bkg = rs.getTextBackgroundColor();
68         if(bkg != null) {
69             Color oldColor = g.getColor();
70             try {
71                 g.setColor(bkg);
72                 g.fillRect(0, 0, width, height);
73             } finally {
74                 g.setColor(oldColor);
75             }
76         }
77         g.drawString(word, 0, ascentPlusLeading);
78         int td = textDecoration;
79         if(td != 0) {
80             if((td & RenderState.MASK_TEXTDECORATION_UNDERLINE) != 0) {
81                 int lineOffset = ascentPlusLeading + 2;
82                 g.drawLine(0, lineOffset, width, lineOffset);
83             }
84             if ((td & RenderState.MASK_TEXTDECORATION_LINE_THROUGH) != 0) {
85                 FontMetrics fm = this.fontMetrics;
86                 int lineOffset = fm.getLeading() + (fm.getAscent() + fm.getDescent()) / 2;
87                 g.drawLine(0, lineOffset, width, lineOffset);
88             }
89             if ((td & RenderState.MASK_TEXTDECORATION_OVERLINE) != 0) {
90                 FontMetrics fm = this.fontMetrics;
91                 int lineOffset = fm.getLeading();
92                 g.drawLine(0, lineOffset, width, lineOffset);
93             }
94             if ((td & RenderState.MASK_TEXTDECORATION_BLINK) != 0) {
95                 //TODO
96
}
97         }
98         Color over = rs.getOverlayColor();
99         if(over != null) {
100             Color oldColor = g.getColor();
101             try {
102                 g.setColor(over);
103                 g.fillRect(0, 0, width, height);
104             } finally {
105                 g.setColor(oldColor);
106             }
107         }
108     }
109     
110     public boolean paintSelection(Graphics g, boolean inSelection, RenderableSpot startPoint, RenderableSpot endPoint) {
111         int startX = -1;
112         int endX = -1;
113         if(this == startPoint.renderable) {
114             startX = startPoint.x;
115         }
116         if(this == endPoint.renderable) {
117             endX = endPoint.x;
118         }
119         if(!inSelection && startX == -1 && endX == -1) {
120             return false;
121         }
122         if(startX != -1 && endX != -1) {
123             if(endX < startX) {
124                 int temp = startX;
125                 startX = endX;
126                 endX = temp;
127             }
128         }
129         else if(startX != -1 && endX == -1 && inSelection) {
130             endX = startX;
131             startX = -1;
132         }
133         else if(startX == -1 && endX != -1 && !inSelection) {
134             startX = endX;
135             endX = -1;
136         }
137         int width1 = -1;
138         int width2 = -1;
139         char[] wordChars = this.word.toCharArray();
140         if(startX != -1) {
141             width1 = 0;
142             FontMetrics fm = this.fontMetrics;
143             for(int len = 0; len < wordChars.length; len++) {
144                 int w = fm.charsWidth(wordChars, 0, len);
145                 if(w > startX) {
146                     break;
147                 }
148                 width1 = w;
149             }
150         }
151         if(endX != -1) {
152             width2 = 0;
153             FontMetrics fm = this.fontMetrics;
154             for(int len = 0; len < wordChars.length; len++) {
155                 int w = fm.charsWidth(wordChars, 0, len);
156                 if(w > endX) {
157                     break;
158                 }
159                 width2 = w;
160             }
161         }
162         if(width1 != -1 || width2 != -1) {
163             int startPaint = width1 == -1 ? 0 : width1;
164             int endPaint = width2 == -1 ? this.width : width2;
165             g.setColor(SELECTION_COLOR);
166             g.setXORMode(SELECTION_XOR);
167             g.fillRect(startPaint, 0, endPaint - startPaint, this.height);
168             g.setPaintMode();
169             return (width2 == -1);
170         }
171         else {
172             if(inSelection) {
173                 g.setColor(SELECTION_COLOR);
174                 g.setXORMode(SELECTION_XOR);
175                 g.fillRect(0, 0, this.width, this.height);
176                 g.setPaintMode();
177             }
178             return inSelection;
179         }
180     }
181
182     public boolean extractSelectionText(StringBuffer JavaDoc buffer, boolean inSelection, RenderableSpot startPoint, RenderableSpot endPoint) {
183         int startX = -1;
184         int endX = -1;
185         if(this == startPoint.renderable) {
186             startX = startPoint.x;
187         }
188         if(this == endPoint.renderable) {
189             endX = endPoint.x;
190         }
191         if(!inSelection && startX == -1 && endX == -1) {
192             return false;
193         }
194         if(startX != -1 && endX != -1) {
195             if(endX < startX) {
196                 int temp = startX;
197                 startX = endX;
198                 endX = temp;
199             }
200         }
201         else if(startX != -1 && endX == -1 && inSelection) {
202             endX = startX;
203             startX = -1;
204         }
205         else if(startX == -1 && endX != -1 && !inSelection) {
206             startX = endX;
207             endX = -1;
208         }
209         int index1 = -1;
210         int index2 = -1;
211         char[] wordChars = this.word.toCharArray();
212         if(startX != -1) {
213             index1 = 0;
214             FontMetrics fm = this.fontMetrics;
215             for(int len = 0; len < wordChars.length; len++) {
216                 int w = fm.charsWidth(wordChars, 0, len);
217                 if(w > startX) {
218                     break;
219                 }
220                 index1 = len;
221             }
222         }
223         if(endX != -1) {
224             index2 = 0;
225             FontMetrics fm = this.fontMetrics;
226             for(int len = 0; len < wordChars.length; len++) {
227                 int w = fm.charsWidth(wordChars, 0, len);
228                 if(w > endX) {
229                     break;
230                 }
231                 index2 = len;
232             }
233         }
234         if(index1 != -1 || index2 != -1) {
235             int startIndex = index1 == -1 ? 0 : index1;
236             int endIndex = index2 == -1 ? wordChars.length : index2;
237             buffer.append(wordChars, startIndex, endIndex - startIndex);
238         }
239         else {
240             if(inSelection) {
241                 buffer.append(wordChars);
242                 return true;
243             }
244         }
245         if(index1 != -1 && index2 != -1) {
246             return false;
247         }
248         else {
249             return !inSelection;
250         }
251     }
252
253     public boolean onMouseClick(java.awt.event.MouseEvent JavaDoc event, int x, int y) {
254         ModelNode me = this.modelNode;
255         if(me != null) {
256             return HtmlController.getInstance().onMouseClick(me, event, x, y);
257         }
258         else {
259             return true;
260         }
261     }
262     
263     public boolean onDoubleClick(java.awt.event.MouseEvent JavaDoc event, int x, int y) {
264         ModelNode me = this.modelNode;
265         if(me != null) {
266             return HtmlController.getInstance().onDoubleClick(me, event, x, y);
267         }
268         else {
269             return true;
270         }
271     }
272
273     public boolean onMousePressed(java.awt.event.MouseEvent JavaDoc event, int x, int y) {
274         ModelNode me = this.modelNode;
275         if(me != null) {
276             return HtmlController.getInstance().onMouseDown(me, event, x, y);
277         }
278         else {
279             return true;
280         }
281     }
282     
283     public boolean onMouseReleased(java.awt.event.MouseEvent JavaDoc event, int x, int y) {
284         ModelNode me = this.modelNode;
285         if(me != null) {
286             return HtmlController.getInstance().onMouseUp(me, event, x, y);
287         }
288         else {
289             return true;
290         }
291     }
292     
293     public boolean onMouseDisarmed(java.awt.event.MouseEvent JavaDoc event) {
294         ModelNode me = this.modelNode;
295         if(me != null) {
296             return HtmlController.getInstance().onMouseDisarmed(me, event);
297         }
298         else {
299             return true;
300         }
301     }
302     
303     public RenderableSpot getLowestRenderableSpot(int x, int y) {
304         return new RenderableSpot(this, x, y);
305     }
306     
307     public String JavaDoc toString() {
308         return "RWord[word=" + this.word + "]";
309     }
310 }
311
Popular Tags