001: /*
002: * Copyright (c) 2001-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.looks.plastic;
032:
033: import java.awt.EventQueue;
034: import java.awt.event.FocusEvent;
035: import java.awt.event.MouseEvent;
036:
037: import javax.swing.JFormattedTextField;
038: import javax.swing.SwingUtilities;
039: import javax.swing.plaf.UIResource;
040: import javax.swing.text.DefaultCaret;
041: import javax.swing.text.JTextComponent;
042:
043: /**
044: * PlasticFieldCaret is visible in non-editable fields,
045: * and the text is selected after a keyboard focus gained event.
046: * For the latter see also issue #4337647 in Sun's bug database.
047: *
048: * @author Karsten Lentzsch
049: * @version $Revision: 1.4 $
050: */
051: final class PlasticFieldCaret extends DefaultCaret implements
052: UIResource {
053:
054: PlasticFieldCaret() {
055: super ();
056: }
057:
058: private boolean isKeyboardFocusEvent = true;
059:
060: public void focusGained(FocusEvent e) {
061: if (getComponent().isEnabled()) {
062: setVisible(true);
063: setSelectionVisible(true);
064: }
065:
066: final JTextComponent c = getComponent();
067: if (c.isEnabled() && isKeyboardFocusEvent) {
068: if (c instanceof JFormattedTextField) {
069: EventQueue.invokeLater(new Runnable() {
070: public void run() {
071: PlasticFieldCaret.super .setDot(0);
072: PlasticFieldCaret.super .moveDot(c.getDocument()
073: .getLength());
074: }
075: });
076: } else {
077: super .setDot(0);
078: super .moveDot(c.getDocument().getLength());
079: }
080: }
081: }
082:
083: public void focusLost(FocusEvent e) {
084: super .focusLost(e);
085: if (!e.isTemporary()) {
086: isKeyboardFocusEvent = true;
087: }
088: }
089:
090: public void mousePressed(MouseEvent e) {
091: if (SwingUtilities.isLeftMouseButton(e) || e.isPopupTrigger()) {
092: isKeyboardFocusEvent = false;
093: }
094: super .mousePressed(e);
095:
096: }
097:
098: public void mouseReleased(MouseEvent e) {
099: super .mouseReleased(e);
100: if (e.isPopupTrigger()) {
101: isKeyboardFocusEvent = false;
102: if ((getComponent() != null) && getComponent().isEnabled()
103: && getComponent().isRequestFocusEnabled()) {
104: getComponent().requestFocus();
105: }
106: }
107: }
108:
109: }
|