1 7 package javax.swing.text.html; 8 9 import java.util.Enumeration ; 10 import java.awt.*; 11 import javax.swing.SizeRequirements ; 12 import javax.swing.border.*; 13 import javax.swing.event.DocumentEvent ; 14 import javax.swing.text.*; 15 16 23 public class BlockView extends BoxView { 24 25 33 public BlockView(Element elem, int axis) { 34 super(elem, axis); 35 } 36 37 54 public void setParent(View parent) { 55 super.setParent(parent); 56 if (parent != null) { 57 setPropertiesFromAttributes(); 58 } 59 } 60 61 68 protected SizeRequirements calculateMajorAxisRequirements(int axis, SizeRequirements r) { 69 if (r == null) { 70 r = new SizeRequirements (); 71 } 72 if (! spanSetFromAttributes(axis, r, cssWidth, cssHeight)) { 73 r = super.calculateMajorAxisRequirements(axis, r); 74 } 75 else { 76 SizeRequirements parentR = super.calculateMajorAxisRequirements( 79 axis, null); 80 int margin = (axis == X_AXIS) ? getLeftInset() + getRightInset() : 81 getTopInset() + getBottomInset(); 82 r.minimum -= margin; 83 r.preferred -= margin; 84 r.maximum -= margin; 85 constrainSize(axis, r, parentR); 86 } 87 return r; 88 } 89 90 98 protected SizeRequirements calculateMinorAxisRequirements(int axis, SizeRequirements r) { 99 if (r == null) { 100 r = new SizeRequirements (); 101 } 102 103 if (! spanSetFromAttributes(axis, r, cssWidth, cssHeight)) { 104 105 112 129 r = super.calculateMinorAxisRequirements(axis, r); 130 } 131 else { 132 SizeRequirements parentR = super.calculateMinorAxisRequirements( 135 axis, null); 136 int margin = (axis == X_AXIS) ? getLeftInset() + getRightInset() : 137 getTopInset() + getBottomInset(); 138 r.minimum -= margin; 139 r.preferred -= margin; 140 r.maximum -= margin; 141 constrainSize(axis, r, parentR); 142 } 143 144 149 if (axis == X_AXIS) { 150 Object o = getAttributes().getAttribute(CSS.Attribute.TEXT_ALIGN); 151 if (o != null) { 152 String align = o.toString(); 153 if (align.equals("center")) { 154 r.alignment = 0.5f; 155 } else if (align.equals("right")) { 156 r.alignment = 1.0f; 157 } else { 158 r.alignment = 0.0f; 159 } 160 } 161 } 162 return r; 164 } 165 166 boolean isPercentage(int axis, AttributeSet a) { 167 if (axis == X_AXIS) { 168 if (cssWidth != null) { 169 return cssWidth.isPercentage(); 170 } 171 } else { 172 if (cssHeight != null) { 173 return cssHeight.isPercentage(); 174 } 175 } 176 return false; 177 } 178 179 185 static boolean spanSetFromAttributes(int axis, SizeRequirements r, 186 CSS.LengthValue cssWidth, 187 CSS.LengthValue cssHeight) { 188 if (axis == X_AXIS) { 189 if ((cssWidth != null) && (! cssWidth.isPercentage())) { 190 r.minimum = r.preferred = r.maximum = (int) cssWidth.getValue(); 191 return true; 192 } 193 } else { 194 if ((cssHeight != null) && (! cssHeight.isPercentage())) { 195 r.minimum = r.preferred = r.maximum = (int) cssHeight.getValue(); 196 return true; 197 } 198 } 199 return false; 200 } 201 202 219 protected void layoutMinorAxis(int targetSpan, int axis, int[] offsets, int[] spans) { 220 int n = getViewCount(); 221 Object key = (axis == X_AXIS) ? CSS.Attribute.WIDTH : CSS.Attribute.HEIGHT; 222 for (int i = 0; i < n; i++) { 223 View v = getView(i); 224 int min = (int) v.getMinimumSpan(axis); 225 int max; 226 227 AttributeSet a = v.getAttributes(); 229 CSS.LengthValue lv = (CSS.LengthValue ) a.getAttribute(key); 230 if ((lv != null) && lv.isPercentage()) { 231 min = Math.max((int) lv.getValue(targetSpan), min); 233 max = min; 234 } else { 235 max = (int)v.getMaximumSpan(axis); 236 } 237 238 if (max < targetSpan) { 240 float align = v.getAlignment(axis); 242 offsets[i] = (int) ((targetSpan - max) * align); 243 spans[i] = max; 244 } else { 245 offsets[i] = 0; 247 spans[i] = Math.max(min, targetSpan); 248 } 249 } 250 } 251 252 253 263 public void paint(Graphics g, Shape allocation) { 264 Rectangle a = (Rectangle) allocation; 265 painter.paint(g, a.x, a.y, a.width, a.height, this); 266 super.paint(g, a); 267 } 268 269 274 public AttributeSet getAttributes() { 275 if (attr == null) { 276 StyleSheet sheet = getStyleSheet(); 277 attr = sheet.getViewAttributes(this); 278 } 279 return attr; 280 } 281 282 289 public int getResizeWeight(int axis) { 290 switch (axis) { 291 case View.X_AXIS: 292 return 1; 293 case View.Y_AXIS: 294 return 0; 295 default: 296 throw new IllegalArgumentException ("Invalid axis: " + axis); 297 } 298 } 299 300 306 public float getAlignment(int axis) { 307 switch (axis) { 308 case View.X_AXIS: 309 return 0; 310 case View.Y_AXIS: 311 if (getViewCount() == 0) { 312 return 0; 313 } 314 float span = getPreferredSpan(View.Y_AXIS); 315 View v = getView(0); 316 float above = v.getPreferredSpan(View.Y_AXIS); 317 float a = (((int)span) != 0) ? (above * v.getAlignment(View.Y_AXIS)) / span: 0; 318 return a; 319 default: 320 throw new IllegalArgumentException ("Invalid axis: " + axis); 321 } 322 } 323 324 public void changedUpdate(DocumentEvent changes, Shape a, ViewFactory f) { 325 super.changedUpdate(changes, a, f); 326 int pos = changes.getOffset(); 327 if (pos <= getStartOffset() && (pos + changes.getLength()) >= 328 getEndOffset()) { 329 setPropertiesFromAttributes(); 330 } 331 } 332 333 345 public float getPreferredSpan(int axis) { 346 return super.getPreferredSpan(axis); 347 } 348 349 361 public float getMinimumSpan(int axis) { 362 return super.getMinimumSpan(axis); 363 } 364 365 377 public float getMaximumSpan(int axis) { 378 return super.getMaximumSpan(axis); 379 } 380 381 384 protected void setPropertiesFromAttributes() { 385 386 StyleSheet sheet = getStyleSheet(); 388 attr = sheet.getViewAttributes(this); 389 390 painter = sheet.getBoxPainter(attr); 392 if (attr != null) { 393 setInsets((short) painter.getInset(TOP, this), 394 (short) painter.getInset(LEFT, this), 395 (short) painter.getInset(BOTTOM, this), 396 (short) painter.getInset(RIGHT, this)); 397 } 398 399 cssWidth = (CSS.LengthValue ) attr.getAttribute(CSS.Attribute.WIDTH); 401 cssHeight = (CSS.LengthValue ) attr.getAttribute(CSS.Attribute.HEIGHT); 402 } 403 404 protected StyleSheet getStyleSheet() { 405 HTMLDocument doc = (HTMLDocument ) getDocument(); 406 return doc.getStyleSheet(); 407 } 408 409 413 private void constrainSize(int axis, SizeRequirements want, 414 SizeRequirements min) { 415 if (min.minimum > want.minimum) { 416 want.minimum = want.preferred = min.minimum; 417 want.maximum = Math.max(want.maximum, min.maximum); 418 } 419 } 420 421 private AttributeSet attr; 422 private StyleSheet.BoxPainter painter; 423 424 private CSS.LengthValue cssWidth; 425 private CSS.LengthValue cssHeight; 426 427 } 428 | Popular Tags |