blockcaret.java
来自「具有不同语法高亮的编辑器实例」· Java 代码 · 共 139 行
JAVA
139 行
/*
* 11/14/2003
*
* BlockCaret.java - A block caret; useful for "overwrite" modes in
* JTextComponents.
* Copyright (C) 2003 Robert Futrell
* email@address.com
* www.website.com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package org.fife.ui;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.io.Serializable;
import javax.swing.plaf.TextUI;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultCaret;
import javax.swing.text.JTextComponent;
import javax.swing.text.Segment;
/**
* A caret that draws a rectangular box around the current dot location in
* the document. The color used is the caret color specified by the
* <code>JTextComponent</code>. This caret is useful to visually signify
* being in "overwrite mode."
*
* @author Robert Futrell
* @version 1.0
*/
public class BlockCaret extends DefaultCaret implements Serializable {
/**
*
*/
private static final long serialVersionUID = 8765113138327312426L;
/**
* Used for fastest-possible retrieval of the character at the
* caret's position in the document.
*/
private Segment segment;
/*****************************************************************************/
/**
* Constructs a new <code>BlockCaret</code>.
*/
public BlockCaret() {
super();
segment = new Segment();
}
/*****************************************************************************/
/**
* Damages the area surrounding the caret to cause it to be repainted in a
* new location. If paint() is reimplemented, this method should also be
* reimplemented. This method should update the caret bounds
* (x, y, width, and height).
*
* @param r The current location of the caret.
*/
protected synchronized void damage(Rectangle r) {
if (r != null) {
JTextComponent component = getComponent();
x = r.x;
y = r.y;
width = component.getFontMetrics(component.getFont()).
charWidth( 'w' ); // Widest char.
height = r.height;
repaint();
}
}
/*****************************************************************************/
/**
* Paints the block cursor.
*
* @param g The graphics context in which to paint.
*/
public void paint(Graphics g) {
// If the cursor is currently visible...
if (isVisible()) {
// Draw a big rectangle, and xor the text (foreground) color.
try {
JTextComponent component = getComponent();
TextUI mapper = component.getUI();
int dot = getDot();
Rectangle rect = mapper.modelToView(component, dot);
try {
component.getDocument().getText(dot,1, segment);
width = g.getFontMetrics().charWidth(segment.array[segment.offset]);
// For when we're at the end of a line, as the '\n'
// char has width 0 (like all unprintable chars?).
if (width==0)
width = g.getFontMetrics().charWidth(' ');
} catch (BadLocationException ble) {
// Shouldn't ever happen.
width = g.getFontMetrics().charWidth(' ');
}
g.setXORMode(Color.WHITE);
g.setColor(component.getCaretColor());
g.fillRect(rect.x,rect.y, width,rect.height-2);
}
catch (BadLocationException ble) { }
}
}
/*****************************************************************************/
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?