1 /* 2 * Copyright (c) 1997, 1998, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package com.sun.java.swing.plaf.motif; 26 27 import java.awt.*; 28 import java.awt.event.*; 29 30 import javax.swing.*; 31 import javax.swing.text.*; 32 import javax.swing.plaf.*; 33 34 /** 35 * Provides the look and feel features that are common across 36 * the Motif/CDE text LAF implementations. 37 * <p> 38 * <strong>Warning:</strong> 39 * Serialized objects of this class will not be compatible with 40 * future Swing releases. The current serialization support is appropriate 41 * for short term storage or RMI between applications running the same 42 * version of Swing. A future release of Swing will provide support for 43 * long term persistence. 44 * 45 * @author Timothy Prinzing 46 */ 47 public class MotifTextUI { 48 49 /** 50 * Creates the object to use for a caret for all of the Motif 51 * text components. The caret is rendered as an I-beam on Motif. 52 * 53 * @return the caret object 54 */ 55 public static Caret createCaret() { 56 return new MotifCaret(); 57 } 58 59 /** 60 * The motif caret is rendered as an I beam. 61 * <p> 62 * <strong>Warning:</strong> 63 * Serialized objects of this class will not be compatible with 64 * future Swing releases. The current serialization support is appropriate 65 * for short term storage or RMI between applications running the same 66 * version of Swing. A future release of Swing will provide support for 67 * long term persistence. 68 */ 69 public static class MotifCaret extends DefaultCaret implements UIResource { 70 71 /** 72 * Called when the component containing the caret gains 73 * focus. This is implemented to repaint the component 74 * so the focus rectangle will be re-rendered, as well 75 * as providing the superclass behavior. 76 * 77 * @param e the focus event 78 * @see FocusListener#focusGained 79 */ 80 public void focusGained(FocusEvent e) { 81 super.focusGained(e); 82 getComponent().repaint(); 83 } 84 85 /** 86 * Called when the component containing the caret loses 87 * focus. This is implemented to set the caret to visibility 88 * to false. 89 * 90 * @param e the focus event 91 * @see FocusListener#focusLost 92 */ 93 public void focusLost(FocusEvent e) { 94 super.focusLost(e); 95 getComponent().repaint(); 96 } 97 98 /** 99 * Damages the area surrounding the caret to cause 100 * it to be repainted. If paint() is reimplemented, 101 * this method should also be reimplemented. 102 * 103 * @param r the current location of the caret, does nothing if null 104 * @see #paint 105 */ 106 protected void damage(Rectangle r) { 107 if (r != null) { 108 x = r.x - IBeamOverhang - 1; 109 y = r.y; 110 width = r.width + (2 * IBeamOverhang) + 3; 111 height = r.height; 112 repaint(); 113 } 114 } 115 116 /** 117 * Renders the caret as a vertical line. If this is reimplemented 118 * the damage method should also be reimplemented as it assumes the 119 * shape of the caret is a vertical line. Does nothing if isVisible() 120 * is false. The caret color is derived from getCaretColor() if 121 * the component has focus, else from getDisabledTextColor(). 122 * 123 * @param g the graphics context 124 * @see #damage 125 */ 126 public void paint(Graphics g) { 127 if(isVisible()) { 128 try { 129 JTextComponent c = getComponent(); 130 Color fg = c.hasFocus() ? c.getCaretColor() : 131 c.getDisabledTextColor(); 132 TextUI mapper = c.getUI(); 133 int dot = getDot(); 134 Rectangle r = mapper.modelToView(c, dot); 135 int x0 = r.x - IBeamOverhang; 136 int x1 = r.x + IBeamOverhang; 137 int y0 = r.y + 1; 138 int y1 = r.y + r.height - 2; 139 g.setColor(fg); 140 g.drawLine(r.x, y0, r.x, y1); 141 g.drawLine(x0, y0, x1, y0); 142 g.drawLine(x0, y1, x1, y1); 143 } catch (BadLocationException e) { 144 // can't render I guess 145 //System.err.println("Can't render caret"); 146 } 147 } 148 } 149 150 static final int IBeamOverhang = 2; 151 } 152 153 /** 154 * Default bindings all keymaps implementing the Motif feel. 155 */ 156 static final JTextComponent.KeyBinding[] defaultBindings = { 157 new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_INSERT, 158 InputEvent.CTRL_MASK), 159 DefaultEditorKit.copyAction), 160 new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_INSERT, 161 InputEvent.SHIFT_MASK), 162 DefaultEditorKit.pasteAction), 163 new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 164 InputEvent.SHIFT_MASK), 165 DefaultEditorKit.cutAction), 166 new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 167 InputEvent.SHIFT_MASK), 168 DefaultEditorKit.selectionBackwardAction), 169 new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 170 InputEvent.SHIFT_MASK), 171 DefaultEditorKit.selectionForwardAction), 172 }; 173 174 175 }