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