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 1027 public ViewFactory getViewFactory() { 1028 View v = getParent(); 1029 return (v != null) ? v.getViewFactory() : null; 1030 } 1031 1032 1040 public String getToolTipText(float x, float y, Shape allocation) { 1041 int viewIndex = getViewIndex(x, y, allocation); 1042 if (viewIndex >= 0) { 1043 allocation = getChildAllocation(viewIndex, allocation); 1044 Rectangle rect = (allocation instanceof Rectangle) ? 1045 (Rectangle)allocation : allocation.getBounds(); 1046 if (rect.contains(x, y)) { 1047 return getView(viewIndex).getToolTipText(x, y, allocation); 1048 } 1049 } 1050 return null; 1051 } 1052 1053 1065 public int getViewIndex(float x, float y, Shape allocation) { 1066 for (int counter = getViewCount() - 1; counter >= 0; counter--) { 1067 Shape childAllocation = getChildAllocation(counter, allocation); 1068 1069 if (childAllocation != null) { 1070 Rectangle rect = (childAllocation instanceof Rectangle) ? 1071 (Rectangle)childAllocation : allocation.getBounds(); 1072 1073 if (rect.contains(x, y)) { 1074 return counter; 1075 } 1076 } 1077 } 1078 return -1; 1079 } 1080 1081 1110 protected boolean updateChildren(DocumentEvent.ElementChange ec, 1111 DocumentEvent e, ViewFactory f) { 1112 Element [] removedElems = ec.getChildrenRemoved(); 1113 Element [] addedElems = ec.getChildrenAdded(); 1114 View [] added = null; 1115 if (addedElems != null) { 1116 added = new View [addedElems.length]; 1117 for (int i = 0; i < addedElems.length; i++) { 1118 added[i] = f.create(addedElems[i]); 1119 } 1120 } 1121 int nremoved = 0; 1122 int index = ec.getIndex(); 1123 if (removedElems != null) { 1124 nremoved = removedElems.length; 1125 } 1126 replace(index, nremoved, added); 1127 return true; 1128 } 1129 1130 1148 protected void forwardUpdate(DocumentEvent.ElementChange ec, 1149 DocumentEvent e, Shape a, ViewFactory f) { 1150 Element elem = getElement(); 1151 int pos = e.getOffset(); 1152 int index0 = getViewIndex(pos, Position.Bias.Forward); 1153 if (index0 == -1 && e.getType() == DocumentEvent.EventType.REMOVE && 1154 pos >= getEndOffset()) { 1155 index0 = getViewCount() - 1; 1159 } 1160 int index1 = index0; 1161 View v = (index0 >= 0) ? getView(index0) : null; 1162 if (v != null) { 1163 if ((v.getStartOffset() == pos) && (pos > 0)) { 1164 index0 = Math.max(index0 - 1, 0); 1167 } 1168 } 1169 if (e.getType() != DocumentEvent.EventType.REMOVE) { 1170 index1 = getViewIndex(pos + e.getLength(), Position.Bias.Forward); 1171 if (index1 < 0) { 1172 index1 = getViewCount() - 1; 1173 } 1174 } 1175 int hole0 = index1 + 1; 1176 int hole1 = hole0; 1177 Element [] addedElems = (ec != null) ? ec.getChildrenAdded() : null; 1178 if ((addedElems != null) && (addedElems.length > 0)) { 1179 hole0 = ec.getIndex(); 1180 hole1 = hole0 + addedElems.length - 1; 1181 } 1182 1183 index0 = Math.max(index0, 0); 1187 for (int i = index0; i <= index1; i++) { 1188 if (! ((i >= hole0) && (i <= hole1))) { 1189 v = getView(i); 1190 if (v != null) { 1191 Shape childAlloc = getChildAllocation(i, a); 1192 forwardUpdateToView(v, e, childAlloc, f); 1193 } 1194 } 1195 } 1196 } 1197 1198 1213 protected void forwardUpdateToView(View v, DocumentEvent e, 1214 Shape a, ViewFactory f) { 1215 DocumentEvent.EventType type = e.getType(); 1216 if (type == DocumentEvent.EventType.INSERT) { 1217 v.insertUpdate(e, a, f); 1218 } else if (type == DocumentEvent.EventType.REMOVE) { 1219 v.removeUpdate(e, a, f); 1220 } else { 1221 v.changedUpdate(e, a, f); 1222 } 1223 } 1224 1225 1240 protected void updateLayout(DocumentEvent.ElementChange ec, 1241 DocumentEvent e, Shape a) { 1242 if ((ec != null) && (a != null)) { 1243 preferenceChanged(null, true, true); 1245 Container host = getContainer(); 1246 if (host != null) { 1247 host.repaint(); 1248 } 1249 } 1250 } 1251 1252 1264 public static final int BadBreakWeight = 0; 1265 1266 1275 public static final int GoodBreakWeight = 1000; 1276 1277 1287 public static final int ExcellentBreakWeight = 2000; 1288 1289 1300 public static final int ForcedBreakWeight = 3000; 1301 1302 1305 public static final int X_AXIS = HORIZONTAL; 1306 1307 1310 public static final int Y_AXIS = VERTICAL; 1311 1312 1326 @Deprecated 1327 public Shape modelToView(int pos, Shape a) throws BadLocationException { 1328 return modelToView(pos, a, Position.Bias.Forward); 1329 } 1330 1331 1332 1344 @Deprecated 1345 public int viewToModel(float x, float y, Shape a) { 1346 sharedBiasReturn[0] = Position.Bias.Forward; 1347 return viewToModel(x, y, a, sharedBiasReturn); 1348 } 1349 1350 static final Position.Bias [] sharedBiasReturn = new Position.Bias [1]; 1353 1354 private View parent; 1355 private Element elem; 1356 1357}; 1358 1359 | Popular Tags |