| 1 7 package javax.swing.plaf.basic; 8 9 import java.util.*; 10 import java.awt.*; 11 import java.awt.event.*; 12 import java.awt.font.*; 13 import java.awt.datatransfer.*; 14 import java.awt.dnd.*; 15 import java.awt.im.InputContext ; 16 import java.beans.*; 17 import java.io.*; 18 import java.net.*; 19 import javax.swing.*; 20 import javax.swing.plaf.*; 21 import javax.swing.text.*; 22 import javax.swing.event.*; 23 import javax.swing.border.Border ; 24 import javax.swing.plaf.UIResource ; 25 import sun.swing.DefaultLookup; 26 import sun.awt.AppContext; 27 import javax.swing.plaf.basic.DragRecognitionSupport.BeforeDrag ; 28 29 94 public abstract class BasicTextUI extends TextUI implements ViewFactory { 95 96 99 public BasicTextUI() { 100 painted = false; 101 } 102 103 111 protected Caret createCaret() { 112 return new BasicCaret(); 113 } 114 115 123 protected Highlighter createHighlighter() { 124 return new BasicHighlighter(); 125 } 126 127 135 protected String getKeymapName() { 136 String nm = getClass().getName(); 137 int index = nm.lastIndexOf('.'); 138 if (index >= 0) { 139 nm = nm.substring(index+1, nm.length()); 140 } 141 return nm; 142 } 143 144 161 protected Keymap createKeymap() { 162 String nm = getKeymapName(); 163 Keymap map = JTextComponent.getKeymap(nm); 164 if (map == null) { 165 Keymap parent = JTextComponent.getKeymap(JTextComponent.DEFAULT_KEYMAP); 166 map = JTextComponent.addKeymap(nm, parent); 167 String prefix = getPropertyPrefix(); 168 Object o = DefaultLookup.get(editor, this, 169 prefix + ".keyBindings"); 170 if ((o != null) && (o instanceof JTextComponent.KeyBinding[])) { 171 JTextComponent.KeyBinding[] bindings = (JTextComponent.KeyBinding[]) o; 172 JTextComponent.loadKeymap(map, bindings, getComponent().getActions()); 173 } 174 } 175 return map; 176 } 177 178 189 protected void propertyChange(PropertyChangeEvent evt) { 190 } 191 192 199 protected abstract String getPropertyPrefix(); 200 201 212 protected void installDefaults() 213 { 214 String prefix = getPropertyPrefix(); 215 Font f = editor.getFont(); 216 if ((f == null) || (f instanceof UIResource )) { 217 editor.setFont(UIManager.getFont(prefix + ".font")); 218 } 219 220 Color bg = editor.getBackground(); 221 if ((bg == null) || (bg instanceof UIResource )) { 222 editor.setBackground(UIManager.getColor(prefix + ".background")); 223 } 224 225 Color fg = editor.getForeground(); 226 if ((fg == null) || (fg instanceof UIResource )) { 227 editor.setForeground(UIManager.getColor(prefix + ".foreground")); 228 } 229 230 Color color = editor.getCaretColor(); 231 if ((color == null) || (color instanceof UIResource )) { 232 editor.setCaretColor(UIManager.getColor(prefix + ".caretForeground")); 233 } 234 235 Color s = editor.getSelectionColor(); 236 if ((s == null) || (s instanceof UIResource )) { 237 editor.setSelectionColor(UIManager.getColor(prefix + ".selectionBackground")); 238 } 239 240 Color sfg = editor.getSelectedTextColor(); 241 if ((sfg == null) || (sfg instanceof UIResource )) { 242 editor.setSelectedTextColor(UIManager.getColor(prefix + ".selectionForeground")); 243 } 244 245 Color dfg = editor.getDisabledTextColor(); 246 if ((dfg == null) || (dfg instanceof UIResource )) { 247 editor.setDisabledTextColor(UIManager.getColor(prefix + ".inactiveForeground")); 248 } 249 250 Border b = editor.getBorder(); 251 if ((b == null) || (b instanceof UIResource )) { 252 editor.setBorder(UIManager.getBorder(prefix + ".border")); 253 } 254 255 Insets margin = editor.getMargin(); 256 if (margin == null || margin instanceof UIResource ) { 257 editor.setMargin(UIManager.getInsets(prefix + ".margin")); 258 } 259 } 260 261 private void installDefaults2() { 262 editor.addMouseListener(dragListener); 263 editor.addMouseMotionListener(dragListener); 264 265 String prefix = getPropertyPrefix(); 266 267 Caret caret = editor.getCaret(); 268 if (caret == null || caret instanceof UIResource ) { 269 caret = createCaret(); 270 editor.setCaret(caret); 271 272 int rate = DefaultLookup.getInt(getComponent(), this, prefix + ".caretBlinkRate", 500); 273 caret.setBlinkRate(rate); 274 } 275 276 Highlighter highlighter = editor.getHighlighter(); 277 if (highlighter == null || highlighter instanceof UIResource ) { 278 editor.setHighlighter(createHighlighter()); 279 } 280 281 TransferHandler th = editor.getTransferHandler(); 282 if (th == null || th instanceof UIResource ) { 283 editor.setTransferHandler(getTransferHandler()); 284 } 285 DropTarget dropTarget = editor.getDropTarget(); 286 if (dropTarget instanceof UIResource ) { 287 if (defaultDropTargetListener == null) { 288 defaultDropTargetListener = new TextDropTargetListener(); 289 } 290 try { 291 dropTarget.addDropTargetListener(defaultDropTargetListener); 292 } catch (TooManyListenersException tmle) { 293 } 295 } 296 } 297 298 306 protected void uninstallDefaults() 307 { 308 editor.removeMouseListener(dragListener); 309 editor.removeMouseMotionListener(dragListener); 310 311 if (editor.getCaretColor() instanceof UIResource ) { 312 editor.setCaretColor(null); 313 } 314 315 if (editor.getSelectionColor() instanceof UIResource ) { 316 editor.setSelectionColor(null); 317 } 318 319 if (editor.getDisabledTextColor() instanceof UIResource ) { 320 editor.setDisabledTextColor(null); 321 } 322 323 if (editor.getSelectedTextColor() instanceof UIResource ) { 324 editor.setSelectedTextColor(null); 325 } 326 327 if (editor.getBorder() instanceof UIResource ) { 328 editor.setBorder(null); 329 } 330 331 if (editor.getMargin() instanceof UIResource ) { 332 editor.setMargin(null); 333 } 334 335 if (editor.getCaret() instanceof UIResource ) { 336 editor.setCaret(null); 337 } 338 339 if (editor.getHighlighter() instanceof UIResource ) { 340 editor.setHighlighter(null); 341 } 342 343 if (editor.getTransferHandler() instanceof UIResource ) { 344 editor.setTransferHandler(null); 345 } 346 } 347 348 351 protected void installListeners() { 352 } 353 354 357 protected void uninstallListeners() { 358 } 359 360 protected void installKeyboardActions() { 361 editor.setKeymap(createKeymap()); 364 365 InputMap km = getInputMap(); 366 if (km != null) { 367 SwingUtilities.replaceUIInputMap(editor, JComponent.WHEN_FOCUSED, 368 km); 369 } 370 371 ActionMap map = getActionMap(); 372 if (map != null) { 373 SwingUtilities.replaceUIActionMap(editor, map); 374 } 375 376 updateFocusAcceleratorBinding(false); 377 } 378 379 382 InputMap getInputMap() { 383 InputMap map = new InputMapUIResource(); 384 385 InputMap shared = 386 (InputMap)DefaultLookup.get(editor, this, 387 getPropertyPrefix() + ".focusInputMap"); 388 if (shared != null) { 389 map.setParent(shared); 390 } 391 return map; 392 } 393 394 398 void updateFocusAcceleratorBinding(boolean changed) { 399 char accelerator = editor.getFocusAccelerator(); 400 401 if (changed || accelerator != '\0') { 402 InputMap km = SwingUtilities.getUIInputMap 403 (editor, JComponent.WHEN_IN_FOCUSED_WINDOW); 404 405 if (km == null && accelerator != '\0') { 406 km = new ComponentInputMapUIResource(editor); 407 SwingUtilities.replaceUIInputMap(editor, JComponent. 408 WHEN_IN_FOCUSED_WINDOW, km); 409 ActionMap am = getActionMap(); 410 SwingUtilities.replaceUIActionMap(editor, am); 411 } 412 if (km != null) { 413 km.clear(); 414 if (accelerator != '\0') { 415 km.put(KeyStroke.getKeyStroke(accelerator, 416 ActionEvent.ALT_MASK), 417 "requestFocus"); 418 } 419 } 420 } 421 } 422 423 424 432 433 void updateFocusTraversalKeys() { 434 438 EditorKit editorKit = getEditorKit(editor); 439 if ( editorKit != null 440 && editorKit instanceof DefaultEditorKit) { 441 Set storedForwardTraversalKeys = editor. 442 getFocusTraversalKeys(KeyboardFocusManager. 443 FORWARD_TRAVERSAL_KEYS); 444 Set storedBackwardTraversalKeys = editor. 445 getFocusTraversalKeys(KeyboardFocusManager. 446 BACKWARD_TRAVERSAL_KEYS); 447 Set forwardTraversalKeys = 448 new HashSet(storedForwardTraversalKeys); 449 Set backwardTraversalKeys = 450 new HashSet(storedBackwardTraversalKeys); 451 if (editor.isEditable()) { 452 forwardTraversalKeys. 453 remove(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0)); 454 backwardTraversalKeys. 455 remove(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 456 InputEvent.SHIFT_MASK)); 457 } else { 458 forwardTraversalKeys.add(KeyStroke. 459 getKeyStroke(KeyEvent.VK_TAB, 0)); 460 backwardTraversalKeys. 461 add(KeyStroke. 462 getKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_MASK)); 463 } 464 LookAndFeel.installProperty(editor, 465 "focusTraversalKeysForward", 466 forwardTraversalKeys); 467 LookAndFeel.installProperty(editor, 468 "focusTraversalKeysBackward", 469 backwardTraversalKeys); 470 } 471 472 } 473 474 478 TransferHandler getTransferHandler() { 479 return defaultTransferHandler; 480 } 481 482 485 ActionMap getActionMap() { 486 String mapName = getPropertyPrefix() + ".actionMap"; 487 ActionMap map = (ActionMap)UIManager.get(mapName); 488 489 if (map == null) { 490 map = createActionMap(); 491 if (map != null) { 492 UIManager.getLookAndFeelDefaults().put(mapName, map); 493 } 494 } 495 ActionMap componentMap = new ActionMapUIResource(); 496 componentMap.put("requestFocus", new FocusAction()); 497 506 if (getEditorKit(editor) instanceof DefaultEditorKit) { 507 if (map != null) { 508 Object obj = map.get(DefaultEditorKit.insertBreakAction); 509 if (obj != null 510 && obj instanceof DefaultEditorKit.InsertBreakAction) { 511 Action action = new TextActionWrapper((TextAction)obj); 512 componentMap.put(action.getValue(Action.NAME),action); 513 } 514 } 515 } 516 if (map != null) { 517 componentMap.setParent(map); 518 } 519 return componentMap; 520 } 521 522 526 ActionMap createActionMap() { 527 ActionMap map = new ActionMapUIResource(); 528 Action[] actions = editor.getActions(); 529 int n = actions.length; 531 for (int i = 0; i < n; i++) { 532 Action a = actions[i]; 533 map.put(a.getValue(Action.NAME), a); 534 } 536 map.put(TransferHandler.getCutAction().getValue(Action.NAME), 537 TransferHandler.getCutAction()); 538 map.put(TransferHandler.getCopyAction().getValue(Action.NAME), 539 TransferHandler.getCopyAction()); 540 map.put(TransferHandler.getPasteAction().getValue(Action.NAME), 541 TransferHandler.getPasteAction()); 542 return map; 543 } 544 545 protected void uninstallKeyboardActions() { 546 editor.setKeymap(null); 547 SwingUtilities.replaceUIInputMap(editor, JComponent. 548 WHEN_IN_FOCUSED_WINDOW, null); 549 SwingUtilities.replaceUIActionMap(editor, null); 550 } 551 552 560 protected void paintBackground(Graphics g) { 561 g.setColor(editor.getBackground()); 562 g.fillRect(0, 0, editor.getWidth(), editor.getHeight()); 563 } 564 565 572 protected final JTextComponent getComponent() { 573 return editor; 574 } 575 576 583 protected void modelChanged() { 584 ViewFactory f = rootView.getViewFactory(); 586 Document doc = editor.getDocument(); 587 Element elem = doc.getDefaultRootElement(); 588 setView(f.create(elem)); 589 } 590 591 598 protected final void setView(View v) { 599 rootView.setView(v); 600 painted = false; 601 editor.revalidate(); 602 editor.repaint(); 603 } 604 605 625 protected void paintSafely(Graphics g) { 626 painted = true; 627 Highlighter highlighter = editor.getHighlighter(); 628 Caret caret = editor.getCaret(); 629 630 if (editor.isOpaque()) { 632 paintBackground(g); 633 } 634 635 if (highlighter != null) { 637 highlighter.paint(g); 638 } 639 640 Rectangle alloc = getVisibleEditorRect(); 642 if (alloc != null) { 643 rootView.paint(g, alloc); 644 } 645 646 if (caret != null) { 648 caret.paint(g); 649 } 650 } 651 652 654 677 public void installUI(JComponent c) { 678 if (c instanceof JTextComponent) { 679 editor = (JTextComponent) c; 680 681 installDefaults(); 683 installDefaults2(); 684 685 LookAndFeel.installProperty(editor, "opaque", Boolean.TRUE); 689 LookAndFeel.installProperty(editor, "autoscrolls", Boolean.TRUE); 690 691 editor.addPropertyChangeListener(updateHandler); 693 Document doc = editor.getDocument(); 694 if (doc == null) { 695 editor.setDocument(getEditorKit(editor).createDefaultDocument()); 699 } else { 700 doc.addDocumentListener(updateHandler); 701 modelChanged(); 702 } 703 704 installListeners(); 706 installKeyboardActions(); 707 708 LayoutManager oldLayout = editor.getLayout(); 709 if ((oldLayout == null) || (oldLayout instanceof UIResource )) { 710 editor.setLayout(updateHandler); 713 } 714 715 } else { 716 throw new Error ("TextUI needs JTextComponent"); 717 } 718 } 719 720 727 public void uninstallUI(JComponent c) { 728 editor.removePropertyChangeListener(updateHandler); 730 editor.getDocument().removeDocumentListener(updateHandler); 731 732 painted = false; 734 uninstallDefaults(); 735 rootView.setView(null); 736 c.removeAll(); 737 LayoutManager lm = c.getLayout(); 738 if (lm instanceof UIResource ) { 739 c.setLayout(null); 740 } 741 742 uninstallKeyboardActions(); 744 uninstallListeners(); 745 746 editor = null; 747 } 748 749 759 public void update(Graphics g, JComponent c) { 760 paint(g, c); 761 } 762 763 774 public final void paint(Graphics g, JComponent c) { 775 if ((rootView.getViewCount() > 0) && (rootView.getView(0) != null)) { 776 Document doc = editor.getDocument(); 777 if (doc instanceof AbstractDocument) { 778 ((AbstractDocument)doc).readLock(); 779 } 780 try { 781 paintSafely(g); 782 } finally { 783 if (doc instanceof AbstractDocument) { 784 ((AbstractDocument)doc).readUnlock(); 785 } 786 } 787 } 788 } 789 790 802 public Dimension getPreferredSize(JComponent c) { 803 Document doc = editor.getDocument(); 804 Insets i = c.getInsets(); 805 Dimension d = c.getSize(); 806 807 if (doc instanceof AbstractDocument) { 808 ((AbstractDocument)doc).readLock(); 809 } 810 try { 811 if ((d.width > (i.left + i.right)) && (d.height > (i.top + i.bottom))) { 812 rootView.setSize(d.width - i.left - i.right, d.height - i.top - i.bottom); 813 } 814 else if (d.width == 0 && d.height == 0) { 815 rootView.setSize(Integer.MAX_VALUE, Integer.MAX_VALUE); 818 } 819 d.width = (int) Math.min((long) rootView.getPreferredSpan(View.X_AXIS) + 820 (long) i.left + (long) i.right, Integer.MAX_VALUE); 821 d.height = (int) Math.min((long) rootView.getPreferredSpan(View.Y_AXIS) + 822 (long) i.top + (long) i.bottom, Integer.MAX_VALUE); 823 } finally { 824 if (doc instanceof AbstractDocument) { 825 ((AbstractDocument)doc).readUnlock(); 826 } 827 } 828 return d; 829 } 830 831 837 public Dimension getMinimumSize(JComponent c) { 838 Document doc = editor.getDocument(); 839 Insets i = c.getInsets(); 840 Dimension d = new Dimension(); 841 if (doc instanceof AbstractDocument) { 842 ((AbstractDocument)doc).readLock(); 843 } 844 try { 845 d.width = (int) rootView.getMinimumSpan(View.X_AXIS) + i.left + i.right; 846 d.height = (int) rootView.getMinimumSpan(View.Y_AXIS) + i.top + i.bottom; 847 } finally { 848 if (doc instanceof AbstractDocument) { 849 ((AbstractDocument)doc).readUnlock(); 850 } 851 } 852 return d; 853 } 854 855 861 public Dimension getMaximumSize(JComponent c) { 862 Document doc = editor.getDocument(); 863 Insets i = c.getInsets(); 864 Dimension d = new Dimension(); 865 if (doc instanceof AbstractDocument) { 866 ((AbstractDocument)doc).readLock(); 867 } 868 try { 869 d.width = (int) Math.min((long) rootView.getMaximumSpan(View.X_AXIS) + 870 (long) i.left + (long) i.right, Integer.MAX_VALUE); 871 d.height = (int) Math.min((long) rootView.getMaximumSpan(View.Y_AXIS) + 872 (long) i.top + (long) i.bottom, Integer.MAX_VALUE); 873 } finally { 874 if (doc instanceof AbstractDocument) { 875 ((AbstractDocument)doc).readUnlock(); 876 } 877 } 878 return d; 879 } 880 881 883 884 894 protected Rectangle getVisibleEditorRect() { 895 Rectangle alloc = editor.getBounds(); 896 if ((alloc.width > 0) && (alloc.height > 0)) { 897 alloc.x = alloc.y = 0; 898 Insets insets = editor.getInsets(); 899 alloc.x += insets.left; 900 alloc.y += insets.top; 901 alloc.width -= insets.left + insets.right; 902 alloc.height -= insets.top + insets.bottom; 903 return alloc; 904 } 905 return null; 906 } 907 908 921 public Rectangle modelToView(JTextComponent tc, int pos) throws BadLocationException { 922 return modelToView(tc, pos, Position.Bias.Forward); 923 } 924 925 938 public Rectangle modelToView(JTextComponent tc, int pos, Position.Bias bias) throws BadLocationException { 939 Document doc = editor.getDocument(); 940 if (doc instanceof AbstractDocument) { 941 ((AbstractDocument)doc).readLock(); 942 } 943 try { 944 Rectangle alloc = getVisibleEditorRect(); 945 if (alloc != null) { 946 rootView.setSize(alloc.width, alloc.height); 947 Shape s = rootView.modelToView(pos, alloc, bias); 948 if (s != null) { 949 return s.getBounds(); 950 } 951 } 952 } finally { 953 if (doc instanceof AbstractDocument) { 954 ((AbstractDocument)doc).readUnlock(); 955 } 956 } 957 return null; 958 } 959 960 973 public int viewToModel(JTextComponent tc, Point pt) { 974 return viewToModel(tc, pt, discardBias); 975 } 976 977 990 public int viewToModel(JTextComponent tc, Point pt, 991 Position.Bias[] biasReturn) { 992 int offs = -1; 993 Document doc = editor.getDocument(); 994 if (doc instanceof AbstractDocument) { 995 ((AbstractDocument)doc).readLock(); 996 } 997 try { 998 Rectangle alloc = getVisibleEditorRect(); 999 if (alloc != null) { 1000 rootView.setSize(alloc.width, alloc.height); 1001 offs = rootView.viewToModel(pt.x, pt.y, alloc, biasReturn); 1002 } 1003 } finally { 1004 if (doc instanceof AbstractDocument) { 1005 ((AbstractDocument)doc).readUnlock(); 1006<
|