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