/* (@)JCustomCaretTest.java */
/* Copyright 2011 Sebastian Haufe
* Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
[url]http://www.apache.org/licenses/LICENSE-2.0[/url]
* Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
package com.ebenius;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.plaf.TextUI;
import javax.swing.text.*;
import javax.swing.text.Position.Bias;
/**
* TODO: Javadoc me!
*
* @version $Revision$ as of $Date$
* @author Sebastian Haufe
* @since Playground-3.8
*/
public class JCustomCaretTest {
/** Creates the GUI. Call on EDT, only! */
static void createAndShowGui() {
final JPanel contentPane = new JPanel(new BorderLayout(6, 6));
final JTextField tf = new JTextField();
final DefaultCaret caret = new DefaultCaret() {
private static final long serialVersionUID = 1L;
private Timer flasher;
private FlashHandler flashHandler;
private boolean hide;
private volatile int caretHeight = 3; // adjust this value
private volatile int caretWidth = 0;
class FlashHandler implements ActionListener, java.io.Serializable {
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent e) {
hide = !hide;
repaint();
}
}
@Override
public void install(JTextComponent c) {
super.install(c);
flashHandler = new FlashHandler();
if (flasher != null) {
flasher.addActionListener(flashHandler);
}
}
@Override
public void deinstall(JTextComponent c) {
super.deinstall(c);
if (flasher != null) {
flasher.stop();
flasher.removeActionListener(flashHandler);
}
flashHandler = null;
}
@Override
public int getBlinkRate() {
return (flasher == null) ? 0 : flasher.getDelay();
}
@Override
public void setBlinkRate(int rate) {
if (rate != 0) {
if (flasher == null) {
flasher = new Timer(rate, flashHandler);
}
flasher.setDelay(rate);
} else {
if (flasher != null) {
flasher.stop();
flasher.removeActionListener(flashHandler);
flasher = null;
}
}
}
@Override
public void paint(Graphics g) {
if (isVisible()) {
try {
final JTextComponent component = getComponent();
TextUI mapper = component.getUI();
final int dot = getDot();
final Bias dotBias = getDotBias();
Rectangle r = mapper.modelToView(component, dot, dotBias);
if ((r == null) || ((r.width == 0) && (r.height == 0))) {
return;
}
g.setFont(getComponent().getFont());
caretWidth = g.getFontMetrics().charWidth('M');
if (width > 0
&& height > 0
&& !this.contains(r.x, r.y, r.width, r.height)) {
// We seem to have gotten out of sync and no longer
// contain the right location, adjust accordingly.
Rectangle clip = g.getClipBounds();
if (clip != null && !clip.contains(this)) {
// Clip doesn't contain the old location, force it
// to be repainted lest we leave a caret around.
repaint();
}
// This will potentially cause a repaint of something
// we're already repainting, but without changing the
// semantics of damage we can't really get around this.
damage(r);
}
if (!hide) {
g.setColor(component.getCaretColor());
final int h = Math.min(caretHeight, r.height);
g.fillRect(r.x, r.y + height - h, caretWidth, h);
}
} catch (BadLocationException e) {
// can't render I guess
// System.err.println("Can't render cursor");
}
}
}
@Override
protected synchronized void damage(Rectangle r) {
if (r != null) {
int damageWidth = caretWidth;
x = r.x;
y = r.y;
width = damageWidth;
height = r.height + caretHeight;
repaint();
}
}
@Override
public void setVisible(boolean visible) {
if (visible) {
final JTextComponent component = getComponent();
final Font font = component.getFont();
caretWidth = component.getFontMetrics(font).charWidth('M');
}
super.setVisible(visible);
if (flasher != null) {
if (visible) {
flasher.start();
} else {
flasher.stop();
}
}
}
};
caret.setBlinkRate(300);
tf.setCaret(caret);
contentPane.add(tf);
final JFrame f = new JFrame("Test Frame: JCustomCaretTest"); //$NON-NLS-1$
f.setContentPane(contentPane);
f.setSize(400, 400);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setVisible(true);
}
/** @param args ignored */
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}