1 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 (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 UNSET = new Integer (Integer.MIN_VALUE); 53 private Integer value = null; 54 55 private Integer getValue() { 56 Integer value = this.value; 57 if(value == null) { 58 HTMLElement node = (HTMLElement) this.modelNode; 59 String 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 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 RenderState renderState = this.modelNode.getRenderState(); 82 Integer 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 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 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 |