File: LightweightFocusLostTest.java

package info (click to toggle)
openjdk-21 21.0.8%2B9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 823,976 kB
  • sloc: java: 5,613,338; xml: 1,643,607; cpp: 1,296,296; ansic: 420,291; asm: 404,850; objc: 20,994; sh: 15,271; javascript: 11,245; python: 6,895; makefile: 2,362; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (261 lines) | stat: -rw-r--r-- 8,624 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
 * Copyright (c) 1999, 2024, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
/*
 * @test
 * @bug 4124119
 * @summary Checks that lightweight components do not lose focus after
        dragging the frame by using the mouse.
 * @library /java/awt/regtesthelpers
 * @build PassFailJFrame
 * @run main/manual LightweightFocusLostTest
*/

import java.awt.AWTEvent;
import java.awt.AWTEventMulticaster;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Label;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


public class LightweightFocusLostTest {

     private static final String INSTRUCTIONS = """
        Steps to try to reproduce this problem:
        When this test is run a window will display (Test Focus).
        Click in the text field to give it the focus (a blinking cursor
        will appear) and then move the frame with the mouse. The text field
        (component which uses a lightweight component) should still have focus.
        You should still see the blinking cursor in the text field after the
        frame has been moved. If this is the behavior that you observe, the
        test has passed, Press the Pass button. Otherwise the test has failed,
        Press the Fail button.""";


    public static void main(String[] args) throws Exception {
        PassFailJFrame.builder()
                .title("LightweightFocusLostTest Instructions")
                .instructions(INSTRUCTIONS)
                .rows((int) INSTRUCTIONS.lines().count() + 5)
                .columns(35)
                .testUI(LightweightFocusLostTest::createTestUI)
                .logArea()
                .build()
                .awaitAndCheck();
    }

    private static Frame createTestUI() {
        Frame f = new Frame("LightweightFocusLostTest");
        f.setLayout(new BorderLayout());
        String sLabel = "Lightweight component below (text field)";
        Label TFL = new Label(sLabel, Label.LEFT);
        f.add(TFL, BorderLayout.NORTH);
        SimpleTextField canvas = new SimpleTextField(30, 5);
        f.add(canvas, BorderLayout.CENTER);
        f.setSize(new Dimension(300,125));
        f.requestFocus();
        return f;

    }

}

class SimpleTextField extends Component implements Runnable {
    int border;
    int length;
    Font font;
    FontMetrics fontM;
    char[] buffer;
    int bufferIx;

    boolean hasFocus;
    boolean cursorOn;

    SimpleTextField(int len, int bor) {
        super();
        border = bor;
        length = len;
        buffer = new char[len];
        font = getFont();
        if (font == null) {
            font = new Font("Dialog", Font.PLAIN, 20);
        }
        fontM = getFontMetrics(font);

        // Listen for key and mouse events.
        this.addMouseListener(new MouseEventHandler());
        this.addFocusListener(new FocusEventHandler());
        this.addKeyListener(new KeyEventHandler());

        // Set text cursor.
        setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));

        // Start the thread that blinks the cursor.
        (new Thread(this)).start();
    }

    public Dimension getMinimumSize() {
        // The minimum height depends on the point size.
        int w = fontM.charWidth('m') * length;
        return new Dimension(w + 2 * border, fontM.getHeight() + 2 * border);
    }
    public Dimension getPreferredSize() {
        return getMinimumSize();
    }
    public Dimension getMaximumSize() {
        return new Dimension(Short.MAX_VALUE, getPreferredSize().height);
    }

    public boolean isFocusTraversable() {
        return true;
    }

    public void paint(Graphics g) {
        int y = (getSize().height-fontM.getHeight())/2;

        // Clear the background using the text background color.
        g.setColor(SystemColor.text);
        g.fillRect(0, 0, getSize().width, getSize().height);

        g.setFont(font);
        g.setColor(SystemColor.textText);
        g.drawChars(buffer, 0, bufferIx, border, y + fontM.getAscent());

        // Draw blinking cursor.
        int x = fontM.charsWidth(buffer, 0, bufferIx) + border;
        int w = fontM.charWidth('c');
        if (hasFocus) {
            g.setColor(getForeground());
            g.fillRect(x, y, w, fontM.getHeight());
            if (cursorOn) {
                if (bufferIx < buffer.length) {
                    g.setColor(SystemColor.text);
                    g.fillRect(x + 2, y + 2, w - 4, fontM.getHeight() - 4);
                }
            }
        }
    }

    // Event handlers
    class MouseEventHandler extends MouseAdapter {
        public void mousePressed(MouseEvent evt) {
            requestFocus();
        }
    }
    class FocusEventHandler extends FocusAdapter {
        public void focusGained(FocusEvent evt) {
            PassFailJFrame.log("Focus gained: " + evt);

            hasFocus = true;
            repaint();
        }
        public void focusLost(FocusEvent evt) {
            PassFailJFrame.log("Focus lost: " + evt);
            hasFocus = false;
            repaint();
        }
    }
    class KeyEventHandler extends KeyAdapter {
        public void keyPressed(KeyEvent evt) {
            switch (evt.getKeyCode()) {
              case KeyEvent.VK_DELETE:
              case KeyEvent.VK_BACK_SPACE:
                if (bufferIx > 0) {
                    bufferIx--;
                    repaint();
                }
                break;
              case KeyEvent.VK_ENTER:
                ActionEvent action =
                    new ActionEvent(SimpleTextField.this,
                                    ActionEvent.ACTION_PERFORMED,
                                    String.valueOf(buffer, 0, bufferIx));
                // Send contents of buffer to listeners
                processEvent(action);
                break;
              default:
                repaint();
            }
        }
        public void keyTyped(KeyEvent evt) {
            if (bufferIx < buffer.length
                    && !evt.isActionKey()
                    && !Character.isISOControl(evt.getKeyChar())) {
                buffer[bufferIx++] = evt.getKeyChar();
            }
        }
    }

    // Support for Action Listener.
    ActionListener actionListener;

    public void addActionListener(ActionListener l) {
        actionListener = AWTEventMulticaster.add(actionListener, l);
    }

    // Override processEvent() to deal with ActionEvent.
    protected void processEvent(AWTEvent evt) {
        if (evt instanceof ActionEvent) {
            processActionEvent((ActionEvent)evt);
        } else {
            super.processEvent(evt);
        }
    }

    // Supply method to process Action event.
    protected void processActionEvent(ActionEvent evt) {
        if (actionListener != null) {
            actionListener.actionPerformed(evt);
        }
    }

    public void run() {
        while (true) {
            try {
                // If component has focus, blink the cursor every 1/2 second.
                Thread.sleep(500);
                cursorOn = !cursorOn;
                if (hasFocus) {
                    repaint();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}