1/*
2 * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package sun.awt.windows;
27
28import java.awt.*;
29import java.awt.peer.*;
30import java.awt.event.TextEvent;
31
32abstract
33class WTextComponentPeer extends WComponentPeer implements TextComponentPeer {
34
35    static {
36        initIDs();
37    }
38
39    // TextComponentPeer implementation
40
41    @Override
42    public void setEditable(boolean editable) {
43        enableEditing(editable);
44        setBackground(((TextComponent)target).getBackground());
45    }
46    @Override
47    public native String getText();
48    @Override
49    public native void setText(String text);
50    @Override
51    public native int getSelectionStart();
52    @Override
53    public native int getSelectionEnd();
54    @Override
55    public native void select(int selStart, int selEnd);
56
57    // Toolkit & peer internals
58
59    WTextComponentPeer(TextComponent target) {
60        super(target);
61    }
62
63    @Override
64    void initialize() {
65        TextComponent tc = (TextComponent)target;
66        String text = tc.getText();
67
68        if (text != null) {
69            setText(text);
70        }
71        select(tc.getSelectionStart(), tc.getSelectionEnd());
72        setEditable(tc.isEditable());
73
74        super.initialize();
75    }
76
77    native void enableEditing(boolean e);
78
79    @Override
80    public boolean isFocusable() {
81        return true;
82    }
83
84    /*
85     * Set the caret position by doing an empty selection. This
86     * unfortunately resets the selection, but seems to be the
87     * only way to get this to work.
88     */
89    @Override
90    public void setCaretPosition(int pos) {
91        select(pos,pos);
92    }
93
94    /*
95     * Get the caret position by looking up the end of the current
96     * selection.
97     */
98    @Override
99    public int getCaretPosition() {
100        return getSelectionStart();
101    }
102
103    /*
104     * Post a new TextEvent when the value of a text component changes.
105     */
106    public void valueChanged() {
107        postEvent(new TextEvent(target, TextEvent.TEXT_VALUE_CHANGED));
108    }
109
110    /**
111     * Initialize JNI field and method IDs
112     */
113    private static native void initIDs();
114
115    @Override
116    public boolean shouldClearRectBeforePaint() {
117        return false;
118    }
119}
120