1. /*
  2. * @(#)MotifTextUI.java 1.16 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.java.swing.plaf.motif;
  8. import java.awt.*;
  9. import java.awt.event.*;
  10. import javax.swing.*;
  11. import javax.swing.text.*;
  12. import javax.swing.plaf.*;
  13. /**
  14. * Provides the look and feel features that are common across
  15. * the Motif/CDE text LAF implementations.
  16. * <p>
  17. * <strong>Warning:</strong>
  18. * Serialized objects of this class will not be compatible with
  19. * future Swing releases. The current serialization support is appropriate
  20. * for short term storage or RMI between applications running the same
  21. * version of Swing. A future release of Swing will provide support for
  22. * long term persistence.
  23. *
  24. * @author Timothy Prinzing
  25. * @version 1.16 11/29/01
  26. */
  27. public class MotifTextUI {
  28. /**
  29. * Creates the object to use for a caret for all of the Motif
  30. * text components. The caret is rendered as an I-beam on Motif.
  31. *
  32. * @return the caret object
  33. */
  34. public static Caret createCaret() {
  35. return new MotifCaret();
  36. }
  37. /**
  38. * The motif caret is rendered as an I beam.
  39. * <p>
  40. * <strong>Warning:</strong>
  41. * Serialized objects of this class will not be compatible with
  42. * future Swing releases. The current serialization support is appropriate
  43. * for short term storage or RMI between applications running the same
  44. * version of Swing. A future release of Swing will provide support for
  45. * long term persistence.
  46. */
  47. public static class MotifCaret extends DefaultCaret implements UIResource {
  48. /**
  49. * Called when the component containing the caret gains
  50. * focus. This is implemented to repaint the component
  51. * so the focus rectangle will be re-rendered, as well
  52. * as providing the superclass behavior.
  53. *
  54. * @param e the focus event
  55. * @see FocusListener#focusGained
  56. */
  57. public void focusGained(FocusEvent e) {
  58. super.focusGained(e);
  59. getComponent().repaint();
  60. }
  61. /**
  62. * Called when the component containing the caret loses
  63. * focus. This is implemented to set the caret to visibility
  64. * to false.
  65. *
  66. * @param e the focus event
  67. * @see FocusListener#focusLost
  68. */
  69. public void focusLost(FocusEvent e) {
  70. super.focusLost(e);
  71. getComponent().repaint();
  72. }
  73. /**
  74. * Damages the area surrounding the caret to cause
  75. * it to be repainted. If paint() is reimplemented,
  76. * this method should also be reimplemented.
  77. *
  78. * @param r the current location of the caret, does nothing if null
  79. * @see #paint
  80. */
  81. protected void damage(Rectangle r) {
  82. if (r != null) {
  83. x = r.x - IBeamOverhang - 1;
  84. y = r.y;
  85. width = r.width + (2 * IBeamOverhang) + 3;
  86. height = r.height;
  87. repaint();
  88. }
  89. }
  90. /**
  91. * Renders the caret as a vertical line. If this is reimplemented
  92. * the damage method should also be reimplemented as it assumes the
  93. * shape of the caret is a vertical line. Does nothing if isVisible()
  94. * is false. The caret color is derived from getCaretColor() if
  95. * the component has focus, else from getDisabledTextColor().
  96. *
  97. * @param g the graphics context
  98. * @see #damage
  99. */
  100. public void paint(Graphics g) {
  101. if(isVisible()) {
  102. try {
  103. JTextComponent c = getComponent();
  104. Color fg = c.hasFocus() ? c.getCaretColor() :
  105. c.getDisabledTextColor();
  106. TextUI mapper = c.getUI();
  107. int dot = getDot();
  108. Rectangle r = mapper.modelToView(c, dot);
  109. int x0 = r.x - IBeamOverhang;
  110. int x1 = r.x + IBeamOverhang;
  111. int y0 = r.y + 1;
  112. int y1 = r.y + r.height - 2;
  113. g.setColor(fg);
  114. g.drawLine(r.x, y0, r.x, y1);
  115. g.drawLine(x0, y0, x1, y0);
  116. g.drawLine(x0, y1, x1, y1);
  117. } catch (BadLocationException e) {
  118. // can't render I guess
  119. //System.err.println("Can't render caret");
  120. }
  121. }
  122. }
  123. static final int IBeamOverhang = 2;
  124. }
  125. /**
  126. * Default bindings all keymaps implementing the Motif feel.
  127. */
  128. static final JTextComponent.KeyBinding[] defaultBindings = {
  129. new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_INSERT,
  130. InputEvent.CTRL_MASK),
  131. DefaultEditorKit.copyAction),
  132. new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_INSERT,
  133. InputEvent.SHIFT_MASK),
  134. DefaultEditorKit.pasteAction),
  135. new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,
  136. InputEvent.SHIFT_MASK),
  137. DefaultEditorKit.cutAction),
  138. new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT,
  139. InputEvent.SHIFT_MASK),
  140. DefaultEditorKit.selectionBackwardAction),
  141. new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT,
  142. InputEvent.SHIFT_MASK),
  143. DefaultEditorKit.selectionForwardAction),
  144. };
  145. }