| 1 7 package javax.swing.text; 8 9 import java.awt.*; 10 import javax.swing.SwingConstants ; 11 import javax.swing.event.*; 12 13 205 public abstract class View implements SwingConstants { 206 207 212 public View(Element elem) { 213 this.elem = elem; 214 } 215 216 221 public View getParent() { 222 return parent; 223 } 224 225 232 public boolean isVisible() { 233 return true; 234 } 235 236 237 249 public abstract float getPreferredSpan(int axis); 250 251 260 public float getMinimumSpan(int axis) { 261 int w = getResizeWeight(axis); 262 if (w == 0) { 263 return getPreferredSpan(axis); 265 } 266 return 0; 267 } 268 269 278 public float getMaximumSpan(int axis) { 279 int w = getResizeWeight(axis); 280 if (w == 0) { 281 return getPreferredSpan(axis); 283 } 284 return Integer.MAX_VALUE; 285 } 286 287 299 public void preferenceChanged(View child, boolean width, boolean height) { 300 View parent = getParent(); 301 if (parent != null) { 302 parent.preferenceChanged(this, width, height); 303 } 304 } 305 306 318 public float getAlignment(int axis) { 319 return 0.5f; 320 } 321 322 331 public abstract void paint(Graphics g, Shape allocation); 332 333 349 public void setParent(View parent) { 350 if (parent == null) { 352 for (int i = 0; i < getViewCount(); i++) { 353 if (getView(i).getParent() == this) { 354 getView(i).setParent(null); 357 } 358 } 359 } 360 this.parent = parent; 361 } 362 363 371 public int getViewCount() { 372 return 0; 373 } 374 375 382 public View getView(int n) { 383 return null; 384 } 385 386 387 393 public void removeAll() { 394 replace(0, getViewCount(), null); 395 } 396 397 402 public void remove(int i) { 403 replace(i, 1, null); 404 } 405 406 415 public void insert(int offs, View v) { 416 View [] one = new View [1]; 417 one[0] = v; 418 replace(offs, 0, one); 419 } 420 421 429 public void append(View v) { 430 View [] one = new View [1]; 431 one[0] = v; 432 replace(getViewCount(), 0, one); 433 } 434 435 453 public void replace(int offset, int length, View [] views) { 454 } 455 456 467 public int getViewIndex(int pos, Position.Bias b) { 468 return -1; 469 } 470 471 483 public Shape getChildAllocation(int index, Shape a) { 484 return null; 485 } 486 487 511 public int getNextVisualPositionFrom(int pos, Position.Bias b, Shape a, 512 int direction, Position.Bias [] biasRet) 513 throws BadLocationException { 514 515 biasRet[0] = Position.Bias.Forward; 516 switch (direction) { 517 case NORTH: 518 case SOUTH: 519 { 520 if (pos == -1) { 521 pos = (direction == NORTH) ? Math.max(0, getEndOffset() - 1) : 522 getStartOffset(); 523 break; 524 } 525 JTextComponent target = (JTextComponent ) getContainer(); 526 Caret c = (target != null) ? target.getCaret() : null; 527 Point mcp; 530 if (c != null) { 531 mcp = c.getMagicCaretPosition(); 532 } 533 else { 534 mcp = null; 535 } 536 int x; 537 if (mcp == null) { 538 Rectangle loc = target.modelToView(pos); 539 x = (loc == null) ? 0 : loc.x; 540 } 541 else { 542 x = mcp.x; 543 } 544 if (direction == NORTH) { 545 pos = Utilities.getPositionAbove(target, pos, x); 546 } 547 else { 548 pos = Utilities.getPositionBelow(target, pos, x); 549 } 550 } 551 break; 552 case WEST: 553 if(pos == -1) { 554 pos = Math.max(0, getEndOffset() - 1); 555 } 556 else { 557 pos = Math.max(0, pos - 1); 558 } 559 break; 560 case EAST: 561 if(pos == -1) { 562 pos = getStartOffset(); 563 } 564 else { 565 pos = Math.min(pos + 1, getDocument().getLength()); 566 } 567 break; 568 default: 569 throw new IllegalArgumentException ("Bad direction: " + direction); 570 } 571 return pos; 572 } 573 574 597 public abstract Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException ; 598 599 628 public Shape modelToView(int p0, Position.Bias b0, int p1, Position.Bias b1, Shape a) throws BadLocationException { 629 Shape s0 = modelToView(p0, a, b0); 630 Shape s1; 631 if (p1 == getEndOffset()) { 632 try { 633 s1 = modelToView(p1, a, b1); 634 } catch (BadLocationException ble) { 635 s1 = null; 636 } 637 if (s1 == null) { 638 Rectangle alloc = (a instanceof Rectangle) ? (Rectangle)a : 640 a.getBounds(); 641 s1 = new Rectangle(alloc.x + alloc.width - 1, alloc.y, 642 1, alloc.height); 643 } 644 } 645 else { 646 s1 = modelToView(p1, a, b1); 647 } 648 Rectangle r0 = s0.getBounds(); 649 Rectangle r1 = (s1 instanceof Rectangle) ? (Rectangle) s1 : 650 s1.getBounds(); 651 if (r0.y != r1.y) { 652 Rectangle alloc = (a instanceof Rectangle) ? (Rectangle)a : 654 a.getBounds(); 655 r0.x = alloc.x; 656 r0.width = alloc.width; 657 } 658 r0.add(r1); 659 return r0; 660 } 661 662 678 public abstract int viewToModel(float x, float y, Shape a, Position.Bias [] biasReturn); 679 680 705 public void insertUpdate(DocumentEvent e, Shape a, ViewFactory f) { 706 if (getViewCount() > 0) { 707 Element elem = getElement(); 708 DocumentEvent.ElementChange ec = e.getChange(elem); 709 if (ec != null) { 710 if (! updateChildren(ec, e, f)) { 711 ec = null; 714 } 715 } 716 forwardUpdate(ec, e, a, f); 717 updateLayout(ec, e, a); 718 } 719 } 720 721 746 public void removeUpdate(DocumentEvent e, Shape a, ViewFactory f) { 747 if (getViewCount() > 0) { 748 Element elem = getElement(); 749 DocumentEvent.ElementChange ec = e.getChange(elem); 750 if (ec != null) { 751 if (! updateChildren(ec, e, f)) { 752 ec = null; 755 } 756 } 757 forwardUpdate(ec, e, a, f); 758 updateLayout(ec, e, a); 759 } 760 } 761 762 787 public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) { 788 if (getViewCount() > 0) { 789 Element elem = getElement(); 790 DocumentEvent.ElementChange ec = e.getChange(elem); 791 if (ec != null) { 792 if (! updateChildren(ec, e, f)) { 793 ec = null; 796 } 797 } 798 forwardUpdate(ec, e, a, f); 799 updateLayout(ec, e, a); 800 } 801 } 802 803 809 public Document getDocument() { 810 return elem.getDocument(); 811 } 812 813 820 public int getStartOffset() { 821 return elem.getStartOffset(); 822 } 823 824 831 public int getEndOffset() { 832 return elem.getEndOffset(); 833 } 834 835 843 public Element getElement() { 844 return elem; 845 } 846 847 856 public Graphics getGraphics() { 857 Component c = getContainer(); 859 return c.getGraphics(); 860 } 861 862 875 public AttributeSet getAttributes() { 876 return elem.getAttributes(); 877 } 878 879 911 public View breakView(int axis, int offset, float pos, float len) { 912 return this; 913 } 914 915 932 public View createFragment(int p0, int p1) { 933 return this; 934 } 935 936 975 public int getBreakWeight(int axis, float pos, float len) { 976 if (len > getPreferredSpan(axis)) { 977 return GoodBreakWeight; 978 } 979 return BadBreakWeight; 980 } 981 982 990 public int getResizeWeight(int axis) { 991 return 0; 992 } 993 994 1002 public void setSize(float width, float height) { 1003 } 1004 1005 1013 public Container getContainer() { 1014 View v = getParent(); 1015 return (v != null) ? v.getContainer() : null; 1016 } 1017 1018 |