Path: blob/master/src/java.desktop/macosx/classes/sun/lwawt/LWTextComponentPeer.java
41153 views
/*1* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/242526package sun.lwawt;2728import java.awt.Dimension;29import java.awt.FontMetrics;30import java.awt.Insets;31import java.awt.SystemColor;32import java.awt.TextComponent;33import java.awt.event.TextEvent;34import java.awt.event.InputMethodListener;35import java.awt.event.InputMethodEvent;36import java.awt.im.InputMethodRequests;37import java.awt.peer.TextComponentPeer;38import sun.awt.AWTAccessor;3940import javax.swing.JComponent;41import javax.swing.event.DocumentEvent;42import javax.swing.event.DocumentListener;43import javax.swing.text.Document;44import javax.swing.text.JTextComponent;4546/**47* Lightweight implementation of {@link TextComponentPeer}. Provides useful48* methods for {@link LWTextAreaPeer} and {@link LWTextFieldPeer}49*/50abstract class LWTextComponentPeer<T extends TextComponent, D extends JComponent>51extends LWComponentPeer<T, D>52implements DocumentListener, TextComponentPeer, InputMethodListener {5354private volatile boolean firstChangeSkipped;5556LWTextComponentPeer(final T target,57final PlatformComponent platformComponent) {58super(target, platformComponent);59if (!getTarget().isBackgroundSet()) {60getTarget().setBackground(SystemColor.text);61}62}6364@Override65void initializeImpl() {66super.initializeImpl();67synchronized (getDelegateLock()) {68// This listener should be added before setText().69getTextComponent().getDocument().addDocumentListener(this);70}71setEditable(getTarget().isEditable());72setText(getTarget().getText());73setCaretPosition(getTarget().getCaretPosition());74getTarget().addInputMethodListener(this);75final int start = getTarget().getSelectionStart();76final int end = getTarget().getSelectionEnd();77if (end > start) {78// Should be called after setText() and setCaretPosition()79select(start, end);80}81firstChangeSkipped = true;82}8384@Override85protected final void disposeImpl() {86synchronized (getDelegateLock()) {87// visible caret has a timer thread which must be stopped88getTextComponent().getCaret().setVisible(false);89}90super.disposeImpl();91}9293/**94* This method should be called under getDelegateLock().95*/96abstract JTextComponent getTextComponent();9798public Dimension getMinimumSize(final int rows, final int columns) {99final Insets insets;100synchronized (getDelegateLock()) {101insets = getTextComponent().getInsets();102}103final int borderHeight = insets.top + insets.bottom;104final int borderWidth = insets.left + insets.right;105final FontMetrics fm = getFontMetrics(getFont());106return new Dimension(fm.charWidth(WIDE_CHAR) * columns + borderWidth,107fm.getHeight() * rows + borderHeight);108}109110@Override111public final void setEditable(final boolean editable) {112synchronized (getDelegateLock()) {113getTextComponent().setEditable(editable);114}115}116117@Override118public final String getText() {119synchronized (getDelegateLock()) {120return getTextComponent().getText();121}122}123124@Override125public final void setText(final String text) {126synchronized (getDelegateLock()) {127// JTextArea.setText() posts two different events (remove & insert).128// Since we make no differences between text events,129// the document listener has to be disabled while130// JTextArea.setText() is called.131final Document document = getTextComponent().getDocument();132document.removeDocumentListener(this);133getTextComponent().setText(text);134revalidate();135if (firstChangeSkipped) {136postEvent(new TextEvent(getTarget(),137TextEvent.TEXT_VALUE_CHANGED));138}139document.addDocumentListener(this);140}141repaintPeer();142}143144@Override145public final int getSelectionStart() {146synchronized (getDelegateLock()) {147return getTextComponent().getSelectionStart();148}149}150151@Override152public final int getSelectionEnd() {153synchronized (getDelegateLock()) {154return getTextComponent().getSelectionEnd();155}156}157158@Override159public final void select(final int selStart, final int selEnd) {160synchronized (getDelegateLock()) {161getTextComponent().select(selStart, selEnd);162}163repaintPeer();164}165166@Override167public final void setCaretPosition(final int pos) {168synchronized (getDelegateLock()) {169getTextComponent().setCaretPosition(pos);170}171repaintPeer();172}173174@Override175public final int getCaretPosition() {176synchronized (getDelegateLock()) {177return getTextComponent().getCaretPosition();178}179}180181@Override182public final InputMethodRequests getInputMethodRequests() {183synchronized (getDelegateLock()) {184return getTextComponent().getInputMethodRequests();185}186}187188//TODO IN XAWT we just return true..189@Override190public final boolean isFocusable() {191return getTarget().isFocusable();192}193194protected final void revalidate() {195synchronized (getDelegateLock()) {196getTextComponent().invalidate();197getDelegate().validate();198}199}200201protected final void postTextEvent() {202postEvent(new TextEvent(getTarget(), TextEvent.TEXT_VALUE_CHANGED));203synchronized (getDelegateLock()) {204revalidate();205}206}207208@Override209public final void changedUpdate(final DocumentEvent e) {210postTextEvent();211}212213@Override214public final void insertUpdate(final DocumentEvent e) {215postTextEvent();216}217218@Override219public final void removeUpdate(final DocumentEvent e) {220postTextEvent();221}222223@Override224public void inputMethodTextChanged(final InputMethodEvent event) {225synchronized (getDelegateLock()) {226AWTAccessor.getComponentAccessor().processEvent(getTextComponent(), event);227}228}229230@Override231public void caretPositionChanged(final InputMethodEvent event) {232synchronized (getDelegateLock()) {233AWTAccessor.getComponentAccessor().processEvent(getTextComponent(), event);234}235}236}237238239