KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > java > swing > plaf > motif > MotifTextUI


1 /*
2  * @(#)MotifTextUI.java 1.20 03/12/19
3  *
4  * Copyright 2004 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
9 import java.awt.*;
10 import java.awt.event.*;
11
12 import javax.swing.*;
13 import javax.swing.text.*;
14 import javax.swing.plaf.*;
15
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.20 12/19/03
29  */

30 public class MotifTextUI {
31
32     /**
33      * Creates the object to use for a caret for all of the Motif
34      * text components. The caret is rendered as an I-beam on Motif.
35      *
36      * @return the caret object
37      */

38     public static Caret createCaret() {
39     return new MotifCaret();
40     }
41
42     /**
43      * The motif caret is rendered as an I beam.
44      * <p>
45      * <strong>Warning:</strong>
46      * Serialized objects of this class will not be compatible with
47      * future Swing releases. The current serialization support is appropriate
48      * for short term storage or RMI between applications running the same
49      * version of Swing. A future release of Swing will provide support for
50      * long term persistence.
51      */

52     public static class MotifCaret extends DefaultCaret implements UIResource {
53
54     /**
55      * Called when the component containing the caret gains
56      * focus. This is implemented to repaint the component
57      * so the focus rectangle will be re-rendered, as well
58      * as providing the superclass behavior.
59      *
60      * @param e the focus event
61      * @see FocusListener#focusGained
62      */

63         public void focusGained(FocusEvent e) {
64         super.focusGained(e);
65         getComponent().repaint();
66     }
67
68     /**
69      * Called when the component containing the caret loses
70      * focus. This is implemented to set the caret to visibility
71      * to false.
72      *
73      * @param e the focus event
74      * @see FocusListener#focusLost
75      */

76         public void focusLost(FocusEvent e) {
77         super.focusLost(e);
78         getComponent().repaint();
79     }
80
81     /**
82      * Damages the area surrounding the caret to cause
83      * it to be repainted. If paint() is reimplemented,
84      * this method should also be reimplemented.
85      *
86      * @param r the current location of the caret, does nothing if null
87      * @see #paint
88      */

89         protected void damage(Rectangle r) {
90         if (r != null) {
91         x = r.x - IBeamOverhang - 1;
92         y = r.y;
93         width = r.width + (2 * IBeamOverhang) + 3;
94         height = r.height;
95         repaint();
96         }
97     }
98
99     /**
100      * Renders the caret as a vertical line. If this is reimplemented
101      * the damage method should also be reimplemented as it assumes the
102      * shape of the caret is a vertical line. Does nothing if isVisible()
103          * is false. The caret color is derived from getCaretColor() if
104          * the component has focus, else from getDisabledTextColor().
105      *
106      * @param g the graphics context
107      * @see #damage
108      */

109         public void paint(Graphics g) {
110         if(isVisible()) {
111         try {
112             JTextComponent c = getComponent();
113             Color fg = c.hasFocus() ? c.getCaretColor() :
114             c.getDisabledTextColor();
115             TextUI mapper = c.getUI();
116             int dot = getDot();
117             Rectangle r = mapper.modelToView(c, dot);
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     
133     static final int IBeamOverhang = 2;
134     }
135
136     /**
137      * Default bindings all keymaps implementing the Motif feel.
138      */

139     static final JTextComponent.KeyBinding[] defaultBindings = {
140     new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_INSERT,
141                                     InputEvent.CTRL_MASK),
142                          DefaultEditorKit.copyAction),
143     new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_INSERT,
144                                     InputEvent.SHIFT_MASK),
145                          DefaultEditorKit.pasteAction),
146     new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,
147                                     InputEvent.SHIFT_MASK),
148                          DefaultEditorKit.cutAction),
149     new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT,
150                                     InputEvent.SHIFT_MASK),
151                          DefaultEditorKit.selectionBackwardAction),
152     new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT,
153                                     InputEvent.SHIFT_MASK),
154                          DefaultEditorKit.selectionForwardAction),
155     };
156
157
158 }
159
Popular Tags