jdk/src/solaris/classes/sun/awt/X11/XTextFieldPeer.java
author mchung
Tue, 29 Sep 2009 16:03:03 -0700
changeset 3938 ef327bd847c0
parent 2 90ce3da70b43
child 4371 dc9dcb8b0ae7
permissions -rw-r--r--
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes Summary: Replace calls to Logger with sun.util.logging.PlatformLogger Reviewed-by: prr, art, alexp, dcherepanov, igor, dav, anthony
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.awt.X11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.peer.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.event.ActionEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.event.ActionListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.event.TextEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.swing.event.DocumentListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.swing.event.DocumentEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.swing.plaf.ComponentUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.swing.InputMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.swing.JPasswordField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.swing.SwingUtilities;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.swing.TransferHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.awt.event.MouseEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.awt.event.FocusEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.awt.event.KeyEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.swing.plaf.UIResource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.swing.UIDefaults;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.swing.JTextField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import javax.swing.JComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import javax.swing.border.Border;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import com.sun.java.swing.plaf.motif.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.awt.im.InputMethodRequests;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    55
import sun.util.logging.PlatformLogger;
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    56
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import sun.awt.CausedFocusEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import sun.awt.ComponentAccessor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
public class XTextFieldPeer extends XComponentPeer implements TextFieldPeer {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    61
    private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.X11.XTextField");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    String text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    XAWTTextField xtext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    boolean firstChangeSkipped;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public XTextFieldPeer(TextField target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        super(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        int start, end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        firstChangeSkipped = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        text = target.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        xtext = new XAWTTextField(text,this, target.getParent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        xtext.getDocument().addDocumentListener(xtext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        xtext.setCursor(target.getCursor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        target.enableInputMethods(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        xtext.enableInputMethods(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        XToolkit.specialPeerMap.put(xtext,this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        TextField txt = (TextField) target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        initTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        setText(txt.getText());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (txt.echoCharIsSet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            setEchoChar(txt.getEchoChar());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        else setEchoChar((char)0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        start = txt.getSelectionStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        end = txt.getSelectionEnd();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if (end > start) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            select(start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        // Fix for 5100200
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        // Restoring Motif behaviour
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        // Since the end position of the selected text can be greater then the length of the text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        // so we should set caret to max position of the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        int caretPosition = Math.min(end, text.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        setCaretPosition(caretPosition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        setEditable(txt.isEditable());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        // After this line we should not change the component's text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        firstChangeSkipped = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public void dispose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        XToolkit.specialPeerMap.remove(xtext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        xtext.removeNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        super.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    void initTextField() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        setVisible(target.isVisible());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        setBounds(x, y, width, height, SET_BOUNDS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        foreground = ComponentAccessor.getForeground(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (foreground == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            foreground = SystemColor.textText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        setForeground(foreground);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        background = ComponentAccessor.getBackground(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (background == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (((TextField)target).isEditable()) background = SystemColor.text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            else background = SystemColor.control;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        setBackground(background);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (!target.isBackgroundSet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            // This is a way to set the background color of the TextArea
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            // without calling setBackground - go through reflection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            ComponentAccessor.setBackground(target, background);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (!target.isForegroundSet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            target.setForeground(SystemColor.textText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        setFont(font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @see java.awt.peer.TextComponentPeer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public void setEditable(boolean editable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (xtext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            xtext.setEditable(editable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            xtext.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @see java.awt.peer.ComponentPeer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public void setEnabled(boolean enabled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        super.setEnabled(enabled);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (xtext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            xtext.setEnabled(enabled);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            xtext.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @see java.awt.peer.TextComponentPeer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public InputMethodRequests getInputMethodRequests() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (xtext != null) return xtext.getInputMethodRequests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        else  return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    void handleJavaInputMethodEvent(InputMethodEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        if (xtext != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            xtext.processInputMethodEventImpl(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @see java.awt.peer.TextFieldPeer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public void setEchoChar(char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (xtext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            xtext.setEchoChar(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            xtext.putClientProperty("JPasswordField.cutCopyAllowed",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    xtext.echoCharIsSet() ? Boolean.FALSE : Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @see java.awt.peer.TextComponentPeer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    public int getSelectionStart() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        return xtext.getSelectionStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @see java.awt.peer.TextComponentPeer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public int getSelectionEnd() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return xtext.getSelectionEnd();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @see java.awt.peer.TextComponentPeer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public String getText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        return xtext.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @see java.awt.peer.TextComponentPeer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    public void setText(String txt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        setXAWTTextField(txt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    protected boolean setXAWTTextField(String txt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        text = txt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if (xtext != null)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            // JTextField.setText() posts two different events (remove & insert).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            // Since we make no differences between text events,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            // the document listener has to be disabled while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            // JTextField.setText() is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            xtext.getDocument().removeDocumentListener(xtext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            xtext.setText(txt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            if (firstChangeSkipped) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                postEvent(new TextEvent(target, TextEvent.TEXT_VALUE_CHANGED));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            xtext.getDocument().addDocumentListener(xtext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            xtext.setCaretPosition(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * to be implemented.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @see java.awt.peer.TextComponentPeer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    public void setCaretPosition(int position) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        if (xtext != null) xtext.setCaretPosition(position);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * DEPRECATED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @see java.awt.peer.TextFieldPeer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    public void setEchoCharacter(char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        setEchoChar(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    void repaintText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        xtext.repaintNow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    public void setBackground(Color c) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
   260
        if (log.isLoggable(PlatformLogger.FINE)) log.fine("target="+ target + ", old=" + background + ", new=" + c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        background = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        if (xtext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            xtext.setBackground(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            xtext.setSelectedTextColor(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        repaintText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    public void setForeground(Color c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        foreground = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        if (xtext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            xtext.setForeground(foreground);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            xtext.setSelectionColor(foreground);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            xtext.setCaretColor(foreground);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        repaintText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    public void setFont(Font f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        synchronized (getStateLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            font = f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            if (xtext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                xtext.setFont(font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        xtext.validate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * DEPRECATED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @see java.awt.peer.TextFieldPeer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    public Dimension preferredSize(int cols) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        return getPreferredSize(cols);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * Deselects the the highlighted text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    public void deselect() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        int selStart=xtext.getSelectionStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        int selEnd=xtext.getSelectionEnd();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        if (selStart != selEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            xtext.select(selStart,selStart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * to be implemented.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @see java.awt.peer.TextComponentPeer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    public int getCaretPosition() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        return xtext.getCaretPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @see java.awt.peer.TextComponentPeer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    public void select(int s, int e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        xtext.select(s,e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        // Fixed 5100806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        // We must take care that Swing components repainted correctly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        xtext.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    public Dimension getMinimumSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        return xtext.getMinimumSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    public Dimension getPreferredSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        return xtext.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    public Dimension getPreferredSize(int cols) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        return getMinimumSize(cols);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    private static final int PADDING = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    public Dimension getMinimumSize(int cols) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        Font f = xtext.getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        FontMetrics fm = xtext.getFontMetrics(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        return new Dimension(fm.charWidth('0') * cols + 10,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                             fm.getMaxDescent() + fm.getMaxAscent() + PADDING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    public boolean isFocusable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    // NOTE: This method is called by privileged threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    //       DO NOT INVOKE CLIENT CODE ON THIS THREAD!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    public void action(final long when, final int modifiers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        postEvent(new ActionEvent(target, ActionEvent.ACTION_PERFORMED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                                  text, when,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                                  modifiers));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    protected void disposeImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    public void repaint() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        if (xtext  != null) xtext.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    public void paint(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        if (xtext  != null) xtext.paint(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    public void print(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        if (xtext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            xtext.print(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    public void focusLost(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        super.focusLost(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        xtext.forwardFocusLost(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    public void focusGained(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        super.focusGained(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        xtext.forwardFocusGained(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    void handleJavaKeyEvent(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        ComponentAccessor.processEvent(xtext,e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    public void handleJavaMouseEvent( MouseEvent mouseEvent ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        super.handleJavaMouseEvent(mouseEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        if (xtext != null)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            mouseEvent.setSource(xtext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            int id = mouseEvent.getID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            if (id == MouseEvent.MOUSE_DRAGGED || id == MouseEvent.MOUSE_MOVED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                xtext.processMouseMotionEventImpl(mouseEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                xtext.processMouseEventImpl(mouseEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * DEPRECATED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    public Dimension minimumSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        return getMinimumSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * DEPRECATED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    public Dimension minimumSize(int cols) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        return getMinimumSize(cols);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    public void setVisible(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        super.setVisible(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        if (xtext != null) xtext.setVisible(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    public void setBounds(int x, int y, int width, int height, int op) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        super.setBounds(x, y, width, height, op);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        if (xtext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
             * Fixed 6277332, 6198290:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
             * the coordinates is coming (to peer): relatively to closest HW parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
             * the coordinates is setting (to textField): relatively to closest ANY parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
             * the parent of peer is target.getParent()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
             * the parent of textField is the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
             * see 6277332, 6198290 for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            int childX = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            int childY = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            Component parent = target.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            // we up to heavyweight parent in order to be sure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            // that the coordinates of the text pane is relatively to closest parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            while (parent.isLightweight()){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                childX -= parent.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                childY -= parent.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                parent = parent.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            xtext.setBounds(childX,childY,width,height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            xtext.validate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    // Accessibility support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    // stub functions: to be fully implemented in a future release
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    public int getIndexAtPoint(int x, int y) { return -1; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    public Rectangle getCharacterBounds(int i) { return null; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    public long filterEvents(long mask) { return 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    /*  To be fully implemented in a future release
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        int oldSelectionStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        int oldSelectionEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        public native int getIndexAtPoint(int x, int y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        public native Rectangle getCharacterBounds(int i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        public native long filterEvents(long mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
         * Handle a change in the text selection endpoints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
         * (Note: could be simply a change in the caret location)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
         public void selectionValuesChanged(int start, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
         return;  // Need to write implemetation of this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    class  AWTTextFieldUI extends MotifPasswordFieldUI {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
         * Creates a UI for a JTextField.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
         * @param c the text field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
         * @return the UI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        JTextField jtf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        protected String getPropertyPrefix() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            JTextComponent comp = getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            if (comp instanceof JPasswordField && ((JPasswordField)comp).echoCharIsSet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                return "PasswordField";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                return "TextField";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        public void installUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            super.installUI(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            jtf = (JTextField) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            JTextField editor = jtf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            UIDefaults uidefaults = XToolkit.getUIDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            String prefix = getPropertyPrefix();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            Font f = editor.getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            if ((f == null) || (f instanceof UIResource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                editor.setFont(uidefaults.getFont(prefix + ".font"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            Color bg = editor.getBackground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            if ((bg == null) || (bg instanceof UIResource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                editor.setBackground(uidefaults.getColor(prefix + ".background"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            Color fg = editor.getForeground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            if ((fg == null) || (fg instanceof UIResource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                editor.setForeground(uidefaults.getColor(prefix + ".foreground"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            Color color = editor.getCaretColor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            if ((color == null) || (color instanceof UIResource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                editor.setCaretColor(uidefaults.getColor(prefix + ".caretForeground"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            Color s = editor.getSelectionColor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            if ((s == null) || (s instanceof UIResource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                editor.setSelectionColor(uidefaults.getColor(prefix + ".selectionBackground"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            Color sfg = editor.getSelectedTextColor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            if ((sfg == null) || (sfg instanceof UIResource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                editor.setSelectedTextColor(uidefaults.getColor(prefix + ".selectionForeground"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            Color dfg = editor.getDisabledTextColor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            if ((dfg == null) || (dfg instanceof UIResource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                editor.setDisabledTextColor(uidefaults.getColor(prefix + ".inactiveForeground"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            Border b = editor.getBorder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            if ((b == null) || (b instanceof UIResource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                editor.setBorder(uidefaults.getBorder(prefix + ".border"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            Insets margin = editor.getMargin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            if (margin == null || margin instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                editor.setMargin(uidefaults.getInsets(prefix + ".margin"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        protected void installKeyboardActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            super.installKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            JTextComponent comp = getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            UIDefaults uidefaults = XToolkit.getUIDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            String prefix = getPropertyPrefix();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            InputMap map = (InputMap)uidefaults.get(prefix + ".focusInputMap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            if (map != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                SwingUtilities.replaceUIInputMap(comp, JComponent.WHEN_FOCUSED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                                                 map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        protected Caret createCaret() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            return new XAWTCaret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    class XAWTCaret extends DefaultCaret {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        public void focusGained(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            super.focusGained(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            getComponent().repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        public void focusLost(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            super.focusLost(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            getComponent().repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        // Fix for 5100950: textarea.getSelectedText() returns the de-selected text, on XToolkit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        // Restoring Motif behaviour
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        // If the text is unhighlighted then we should sets the selection range to zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        public void setSelectionVisible(boolean vis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            if (vis){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                super.setSelectionVisible(vis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            }else{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                // In order to de-select the selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                setDot(getDot());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    class XAWTTextField extends JPasswordField
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        implements ActionListener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                   DocumentListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        boolean isFocused = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        XComponentPeer peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        public XAWTTextField(String text, XComponentPeer peer, Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            super(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            this.peer = peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            setDoubleBuffered(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            setFocusable(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            ComponentAccessor.setParent(this,parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            setBackground(peer.getPeerBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            setForeground(peer.getPeerForeground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            setFont(peer.getPeerFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            setCaretPosition(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            addNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        public void actionPerformed( ActionEvent actionEvent ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            peer.postEvent(new ActionEvent(peer.target,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                                           ActionEvent.ACTION_PERFORMED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                                           getText(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                                           actionEvent.getWhen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                                           actionEvent.getModifiers()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        public void insertUpdate(DocumentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                peer.postEvent(new TextEvent(peer.target,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                                             TextEvent.TEXT_VALUE_CHANGED));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        public void removeUpdate(DocumentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                peer.postEvent(new TextEvent(peer.target,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                                             TextEvent.TEXT_VALUE_CHANGED));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        public void changedUpdate(DocumentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                peer.postEvent(new TextEvent(peer.target,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                                             TextEvent.TEXT_VALUE_CHANGED));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        public ComponentPeer getPeer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            return (ComponentPeer) peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        public void repaintNow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            paintImmediately(getBounds());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        public Graphics getGraphics() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            return peer.getGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        public void updateUI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            ComponentUI ui = new AWTTextFieldUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            setUI(ui);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        void forwardFocusGained( FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            isFocused = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            FocusEvent fe = CausedFocusEvent.retarget(e, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            super.processFocusEvent(fe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        void forwardFocusLost( FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            isFocused = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            FocusEvent fe = CausedFocusEvent.retarget(e, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            super.processFocusEvent(fe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        public boolean hasFocus() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            return isFocused;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        public void processInputMethodEventImpl(InputMethodEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            processInputMethodEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        public void processMouseEventImpl(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            processMouseEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        public void processMouseMotionEventImpl(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            processMouseMotionEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        // Fix for 4915454 - override the default implementation to avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        // loading SystemFlavorMap and associated classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        public void setTransferHandler(TransferHandler newHandler) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            TransferHandler oldHandler = (TransferHandler)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                getClientProperty(XTextTransferHelper.getTransferHandlerKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            putClientProperty(XTextTransferHelper.getTransferHandlerKey(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                              newHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            firePropertyChange("transferHandler", oldHandler, newHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        public void setEchoChar(char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            super.setEchoChar(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            ((AWTTextFieldUI)ui).installKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
}