1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * %W% %E%
 *
 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.sun.java.swing.plaf.motif;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.text.*;
import javax.swing.plaf.*;

/**
 * Provides the look and feel features that are common across
 * the Motif/CDE text LAF implementations.  
 * <p>
 * <strong>Warning:</strong>
 * Serialized objects of this class will not be compatible with
 * future Swing releases.  The current serialization support is appropriate
 * for short term storage or RMI between applications running the same
 * version of Swing.  A future release of Swing will provide support for
 * long term persistence.
 *
 * @author  Timothy Prinzing
 * @version %I% %G%
 */
public class MotifTextUI {

    /**
     * Creates the object to use for a caret for all of the Motif
     * text components.  The caret is rendered as an I-beam on Motif.
     *
     * @return the caret object
     */
    public static Caret createCaret() {
    return new MotifCaret();
    }

    /**
     * The motif caret is rendered as an I beam.
     * <p>
     * <strong>Warning:</strong>
     * Serialized objects of this class will not be compatible with
     * future Swing releases.  The current serialization support is appropriate
     * for short term storage or RMI between applications running the same
     * version of Swing.  A future release of Swing will provide support for
     * long term persistence.
     */
    public static class MotifCaret extends DefaultCaret implements UIResource {

    /**
     * Called when the component containing the caret gains
     * focus.  This is implemented to repaint the component 
     * so the focus rectangle will be re-rendered, as well
     * as providing the superclass behavior.
     *
     * @param e the focus event
     * @see FocusListener#focusGained
     */
        public void focusGained(FocusEvent e) {
        super.focusGained(e);
        getComponent().repaint();
    }

    /**
     * Called when the component containing the caret loses
     * focus.  This is implemented to set the caret to visibility
     * to false.
     *
     * @param e the focus event
     * @see FocusListener#focusLost
     */
        public void focusLost(FocusEvent e) {
        super.focusLost(e);
        getComponent().repaint();
    }

    /**
     * Damages the area surrounding the caret to cause
     * it to be repainted.  If paint() is reimplemented,
     * this method should also be reimplemented.
     *
     * @param r  the current location of the caret, does nothing if null
     * @see #paint
     */
        protected void damage(Rectangle r) {
        if (r != null) {
        x = r.x - IBeamOverhang - 1;
        y = r.y;
        width = r.width + (2 * IBeamOverhang) + 3;
        height = r.height;
        repaint();
        }
    }

    /**
     * Renders the caret as a vertical line.  If this is reimplemented
     * the damage method should also be reimplemented as it assumes the
     * shape of the caret is a vertical line.  Does nothing if isVisible()
         * is false.  The caret color is derived from getCaretColor() if
         * the component has focus, else from getDisabledTextColor().
     *
     * @param g the graphics context
     * @see #damage
     */
        public void paint(Graphics g) {
        if(isVisible()) {
        try {
            JTextComponent c = getComponent();
            Color fg = c.hasFocus() ? c.getCaretColor() : 
            c.getDisabledTextColor();
            TextUI mapper = c.getUI();
            int dot = getDot();
            Rectangle r = mapper.modelToView(c, dot);
            int x0 = r.x - IBeamOverhang;
            int x1 = r.x + IBeamOverhang;
            int y0 = r.y + 1;
            int y1 = r.y + r.height - 2;
            g.setColor(fg);
            g.drawLine(r.x, y0, r.x, y1);
            g.drawLine(x0, y0, x1, y0);
            g.drawLine(x0, y1, x1, y1);
        } catch (BadLocationException e) {
            // can't render I guess
            //System.err.println("Can't render caret");
        }
        }
    }
    
    static final int IBeamOverhang = 2;
    }

    /**
     * Default bindings all keymaps implementing the Motif feel.
     */
    static final JTextComponent.KeyBinding[] defaultBindings = {
    new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_INSERT, 
                                    InputEvent.CTRL_MASK),
                         DefaultEditorKit.copyAction),
    new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_INSERT, 
                                    InputEvent.SHIFT_MASK),
                         DefaultEditorKit.pasteAction),
    new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 
                                    InputEvent.SHIFT_MASK),
                         DefaultEditorKit.cutAction),
    new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 
                                    InputEvent.SHIFT_MASK),
                         DefaultEditorKit.selectionBackwardAction),
    new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 
                                    InputEvent.SHIFT_MASK),
                         DefaultEditorKit.selectionForwardAction),
    };


}
			
			

Browsed Source: [clear]