KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.lobobrowser.html.renderer;
22
23 import java.awt.*;
24
25 import org.lobobrowser.html.HtmlRendererContext;
26 import org.lobobrowser.html.UserAgentContext;
27 import org.lobobrowser.html.domimpl.NodeImpl;
28 import org.lobobrowser.html.style.ListStyle;
29 import org.lobobrowser.html.style.RenderState;
30 import org.w3c.dom.html2.*;
31
32 class RListItem extends BaseRListElement {
33     private static final int BULLET_WIDTH = 5;
34     private static final int BULLET_HEIGHT = 5;
35     private static final int BULLET_RMARGIN = 5;
36     private static final int BULLET_SPACE_WIDTH = 36;
37
38     public RListItem(NodeImpl modelNode, int listNesting, UserAgentContext pcontext, HtmlRendererContext rcontext, FrameContext frameContext, RenderableContainer parentContainer, RCollection parent) {
39         super(modelNode, listNesting, pcontext, rcontext, frameContext, parentContainer);
40         this.defaultMarginInsets = new java.awt.Insets JavaDoc(0, BULLET_SPACE_WIDTH, 0, 0);
41     }
42     
43     public int getViewportListNesting(int blockNesting) {
44         return blockNesting + 1;
45     }
46
47     public void invalidateLayoutLocal() {
48         super.invalidateLayoutLocal();
49         this.value = null;
50     }
51
52     private static final Integer JavaDoc UNSET = new Integer JavaDoc(Integer.MIN_VALUE);
53     private Integer JavaDoc value = null;
54     
55     private Integer JavaDoc getValue() {
56         Integer JavaDoc value = this.value;
57         if(value == null) {
58             HTMLElement node = (HTMLElement) this.modelNode;
59             String JavaDoc valueText = node == null ? null : node.getAttribute("value");
60             if(valueText == null) {
61                 value = UNSET;
62             }
63             else {
64                 try {
65                     value = Integer.valueOf(valueText);
66                 } catch(NumberFormatException JavaDoc nfe) {
67                     value = UNSET;
68                 }
69             }
70             this.value = value;
71         }
72         return value;
73     }
74
75     private int count;
76     
77     public void doLayout(int availWidth, int availHeight, boolean expandWidth, boolean expandHeight, FloatingBounds floatBounds, int tentativeY, int defaultOverflow) {
78         super.doLayout(availWidth, availHeight, expandWidth, expandHeight,
79                 floatBounds, tentativeY, defaultOverflow);
80         //Note: Count must be calculated even if layout is valid.
81
RenderState renderState = this.modelNode.getRenderState();
82         Integer JavaDoc value = this.getValue();
83         if(value == UNSET) {
84             this.count = renderState.incrementCount(DEFAULT_COUNTER_NAME, this.listNesting);
85         }
86         else {
87             int newCount = value.intValue();
88             this.count = newCount;
89             renderState.resetCount(DEFAULT_COUNTER_NAME, this.listNesting, newCount + 1);
90         }
91     }
92
93     public void paint(Graphics g) {
94         super.paint(g);
95         RenderState rs = this.modelNode.getRenderState();
96         Insets marginInsets = this.getMarginInsets(rs);
97         RBlockViewport layout = this.bodyLayout;
98         if(layout != null) {
99             ListStyle listStyle = this.listStyle;
100             int bulletType = listStyle == null ? ListStyle.TYPE_UNSET : listStyle.type;
101             if(bulletType == ListStyle.TYPE_UNSET) {
102                 RCollection parent = this.getOriginalOrCurrentParent();
103                 if(!(parent instanceof RList)) {
104                     parent = parent.getOriginalOrCurrentParent();
105                 }
106                 if(parent instanceof RList) {
107                     ListStyle parentListStyle = ((RList) parent).listStyle;
108                     bulletType = parentListStyle == null ? ListStyle.TYPE_DISC : parentListStyle.type;
109                 }
110                 else {
111                     bulletType = ListStyle.TYPE_DISC;
112                 }
113             }
114             // Paint bullets
115
Color prevColor = g.getColor();
116             g.setColor(Color.BLACK);
117             try {
118                 Insets insets = this.getInsets(this.hasHScrollBar, this.hasVScrollBar);
119                 Insets paddingInsets = this.getPaddingInsets(rs);
120                 int baselineOffset = layout.getFirstBaselineOffset();
121                 int bulletRight = marginInsets.left - BULLET_RMARGIN;
122                 int bulletBottom = insets.top + baselineOffset + (paddingInsets == null ? 0 : paddingInsets.top);
123                 int bulletTop = bulletBottom - BULLET_HEIGHT;
124                 int bulletLeft = bulletRight - BULLET_WIDTH;
125                 int bulletNumber = this.count;
126                 String JavaDoc numberText = null;
127                 switch(bulletType) {
128                 case ListStyle.TYPE_DECIMAL:
129                     numberText = bulletNumber + ".";
130                     break;
131                 case ListStyle.TYPE_LOWER_ALPHA:
132                     numberText = ((char) ('a' + bulletNumber)) + ".";
133                     break;
134                 case ListStyle.TYPE_UPPER_ALPHA:
135                     numberText = ((char) ('A' + bulletNumber)) + ".";
136                     break;
137                 case ListStyle.TYPE_DISC:
138                     g.fillOval(bulletLeft, bulletTop, BULLET_WIDTH, BULLET_HEIGHT);
139                     break;
140                 case ListStyle.TYPE_CIRCLE:
141                     g.drawOval(bulletLeft, bulletTop, BULLET_WIDTH, BULLET_HEIGHT);
142                     break;
143                 case ListStyle.TYPE_SQUARE:
144                     g.fillRect(bulletLeft, bulletTop, BULLET_WIDTH, BULLET_HEIGHT);
145                     break;
146                 }
147                 if(numberText != null) {
148                     FontMetrics fm = g.getFontMetrics();
149                     int numberLeft = bulletRight - fm.stringWidth(numberText);
150                     int numberY = bulletBottom;
151                     g.drawString(numberText, numberLeft, numberY);
152                 }
153             } finally {
154                 g.setColor(prevColor);
155             }
156         }
157     }
158 }
159
Popular Tags