1 7 package javax.swing.text.html; 8 9 import java.awt.*; 10 import javax.swing.event.DocumentEvent ; 11 import javax.swing.text.*; 12 import java.util.Enumeration ; 13 import java.lang.Integer ; 14 15 23 class HRuleView extends View { 24 25 30 public HRuleView(Element elem) { 31 super(elem); 32 setPropertiesFromAttributes(); 33 } 34 35 38 protected void setPropertiesFromAttributes() { 39 StyleSheet sheet = ((HTMLDocument )getDocument()).getStyleSheet(); 40 AttributeSet eAttr = getElement().getAttributes(); 41 attr = sheet.getViewAttributes(this); 42 43 alignment = StyleConstants.ALIGN_CENTER; 44 size = 0; 45 noshade = null; 46 widthValue = null; 47 48 if (attr != null) { 49 if (attr.getAttribute(StyleConstants.Alignment) != null) { 53 alignment = StyleConstants.getAlignment(attr); 54 } 55 56 noshade = (String )eAttr.getAttribute(HTML.Attribute.NOSHADE); 57 Object value = eAttr.getAttribute(HTML.Attribute.SIZE); 58 if (value != null && (value instanceof String )) 59 size = Integer.parseInt((String )value); 60 value = attr.getAttribute(CSS.Attribute.WIDTH); 61 if (value != null && (value instanceof CSS.LengthValue )) { 62 widthValue = (CSS.LengthValue )value; 63 } 64 topMargin = getLength(CSS.Attribute.MARGIN_TOP, attr); 65 bottomMargin = getLength(CSS.Attribute.MARGIN_BOTTOM, attr); 66 leftMargin = getLength(CSS.Attribute.MARGIN_LEFT, attr); 67 rightMargin = getLength(CSS.Attribute.MARGIN_RIGHT, attr); 68 } 69 else { 70 topMargin = bottomMargin = leftMargin = rightMargin = 0; 71 } 72 size = Math.max(2, size); 73 } 74 75 private float getLength(CSS.Attribute key, AttributeSet a) { 78 CSS.LengthValue lv = (CSS.LengthValue ) a.getAttribute(key); 79 float len = (lv != null) ? lv.getValue() : 0; 80 return len; 81 } 82 83 85 92 public void paint(Graphics g, Shape a) { 93 Rectangle alloc = (a instanceof Rectangle) ? (Rectangle)a : 94 a.getBounds(); 95 int x = 0; 96 int y = alloc.y + SPACE_ABOVE + (int)topMargin; 97 int width = alloc.width - (int)(leftMargin + rightMargin); 98 if (widthValue != null) { 99 width = (int)widthValue.getValue((float)width); 100 } 101 int height = alloc.height - (SPACE_ABOVE + SPACE_BELOW + 102 (int)topMargin + (int)bottomMargin); 103 if (size > 0) 104 height = size; 105 106 switch (alignment) { 108 case StyleConstants.ALIGN_CENTER: 109 x = alloc.x + (alloc.width / 2) - (width / 2); 110 break; 111 case StyleConstants.ALIGN_RIGHT: 112 x = alloc.x + alloc.width - width - (int)rightMargin; 113 break; 114 case StyleConstants.ALIGN_LEFT: 115 default: 116 x = alloc.x + (int)leftMargin; 117 break; 118 } 119 120 if (noshade != null) { 122 g.setColor(Color.black); 123 g.fillRect(x, y, width, height); 124 } 125 else { 126 Color bg = getContainer().getBackground(); 127 Color bottom, top; 128 if (bg == null || bg.equals(Color.white)) { 129 top = Color.darkGray; 130 bottom = Color.lightGray; 131 } 132 else { 133 top = Color.darkGray; 134 bottom = Color.white; 135 } 136 g.setColor(bottom); 137 g.drawLine(x + width - 1, y, x + width - 1, y + height - 1); 138 g.drawLine(x, y + height - 1, x + width - 1, y + height - 1); 139 g.setColor(top); 140 g.drawLine(x, y, x + width - 1, y); 141 g.drawLine(x, y, x, y + height - 1); 142 } 143 144 } 145 146 147 155 public float getPreferredSpan(int axis) { 156 switch (axis) { 157 case View.X_AXIS: 158 return 1; 159 case View.Y_AXIS: 160 if (size > 0) { 161 return size + SPACE_ABOVE + SPACE_BELOW + topMargin + 162 bottomMargin; 163 } else { 164 if (noshade != null) { 165 return 2 + SPACE_ABOVE + SPACE_BELOW + topMargin + 166 bottomMargin; 167 } else { 168 return SPACE_ABOVE + SPACE_BELOW + topMargin +bottomMargin; 169 } 170 } 171 default: 172 throw new IllegalArgumentException ("Invalid axis: " + axis); 173 } 174 } 175 176 183 public int getResizeWeight(int axis) { 184 if (axis == View.X_AXIS) { 185 return 1; 186 } else if (axis == View.Y_AXIS) { 187 return 0; 188 } else { 189 return 0; 190 } 191 } 192 193 208 public int getBreakWeight(int axis, float pos, float len) { 209 if (axis == X_AXIS) { 210 return ForcedBreakWeight; 211 } 212 return BadBreakWeight; 213 } 214 215 public View breakView(int axis, int offset, float pos, float len) { 216 return null; 217 } 218 219 230 public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException { 231 int p0 = getStartOffset(); 232 int p1 = getEndOffset(); 233 if ((pos >= p0) && (pos <= p1)) { 234 Rectangle r = a.getBounds(); 235 if (pos == p1) { 236 r.x += r.width; 237 } 238 r.width = 0; 239 return r; 240 } 241 return null; 242 } 243 244 255 public int viewToModel(float x, float y, Shape a, Position.Bias[] bias) { 256 Rectangle alloc = (Rectangle) a; 257 if (x < alloc.x + (alloc.width / 2)) { 258 bias[0] = Position.Bias.Forward; 259 return getStartOffset(); 260 } 261 bias[0] = Position.Bias.Backward; 262 return getEndOffset(); 263 } 264 265 270 public AttributeSet getAttributes() { 271 return attr; 272 } 273 274 public void changedUpdate(DocumentEvent changes, Shape a, ViewFactory f) { 275 super.changedUpdate(changes, a, f); 276 int pos = changes.getOffset(); 277 if (pos <= getStartOffset() && (pos + changes.getLength()) >= 278 getEndOffset()) { 279 setPropertiesFromAttributes(); 280 } 281 } 282 283 285 private float topMargin; 286 private float bottomMargin; 287 private float leftMargin; 288 private float rightMargin; 289 private int alignment = StyleConstants.ALIGN_CENTER; 290 private String noshade = null; 291 private int size = 0; 292 private CSS.LengthValue widthValue; 293 294 private static final int SPACE_ABOVE = 3; 295 private static final int SPACE_BELOW = 3; 296 297 298 private AttributeSet attr; 299 } 300 301 | Popular Tags |