File: ProcessEvent.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 (455 lines) | stat: -rw-r--r-- 12,949 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/*
 * 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 4292099
 * @summary AWT Event delivery to processEvent
 * @library /java/awt/regtesthelpers
 * @build PassFailJFrame
 * @run main/manual ProcessEvent
 */

import java.awt.AWTEvent;
import java.awt.AWTEventMulticaster;
import java.awt.Adjustable;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.ItemSelectable;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
import java.lang.reflect.InvocationTargetException;

public class ProcessEvent extends Frame {

    static final String INSTRUCTIONS = """
                Press each of the four buttons for ActionEvent, AdjustmentEvent,
                ItemEvent and TextEvent. If a message for each corresponding event
                appears in the log area and says the event listener was
                called, then press Pass otherwise press Fail.
            """;
    ActionBtn af;
    AdjustmentBtn adjf;
    ItemBtn itf;
    TextBtn txtf;

    public ProcessEvent() {
        setLayout(new FlowLayout());
        add(af = new ActionBtn());
        af.setBackground(Color.green);

        add(adjf = new AdjustmentBtn());
        adjf.setBackground(Color.green);

        add(itf = new ItemBtn());
        itf.setBackground(Color.green);

        add(txtf = new TextBtn());
        txtf.setBackground(Color.green);

        // These action listeners simply provide feedback of when
        // the event is delivered properly.
        af.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                PassFailJFrame.log(af.getText()
                        + ": action listener called: "
                        + ae.toString());
            }
        });

        adjf.addAdjustmentListener(new AdjustmentListener() {
            public void adjustmentValueChanged(AdjustmentEvent ae) {
                PassFailJFrame.log(adjf.getText()
                        + ": adjustment listener called: "
                        + ae.toString());
            }
        });

        itf.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                PassFailJFrame.log(itf.getText()
                        + ": item listener called: "
                        + e.toString());
            }
        });

        txtf.addTextListener(new TextListener() {
            public void textValueChanged(TextEvent e) {
                PassFailJFrame.log(txtf.getText()
                        + ": text listener called: "
                        + e.toString());
            }
        });

        pack();
    }

    public static void main(String[] args) throws InterruptedException,
            InvocationTargetException {
        PassFailJFrame.builder()
                .title("Process Events Test")
                .testUI(ProcessEvent::new)
                .instructions(INSTRUCTIONS)
                .columns(40)
                .logArea()
                .build()
                .awaitAndCheck();
    }
}

class ButtonComponent extends Component implements ItemSelectable, Adjustable {

    transient protected TextListener textListener;
    transient ActionListener actionListener;
    transient AdjustmentListener adjustmentListener;
    transient ItemListener itemListener;
    String actionCommand = null;

    String text = null;

    public ButtonComponent(String label) {
        super();
        text = label;
    }

    public String getText() {
        return text;
    }

    public Dimension getPreferredSize() {
        return new Dimension(200, 30);
    }

    public Dimension getMinimumSize() {
        return getPreferredSize();
    }

    public String getActionCommand() {
        if (actionCommand == null)
            return getText();
        else
            return actionCommand;
    }

    public void setActionCommand(String ac) {
        actionCommand = ac;
    }

    // ActionEvent listener support

    public synchronized void addActionListener(ActionListener l) {
        if (l == null) {
            return;
        }
        enableEvents(AWTEvent.ACTION_EVENT_MASK);
        actionListener = AWTEventMulticaster.add(actionListener, l);
    }

    public synchronized void removeActionListener(ActionListener l) {
        if (l == null) {
            return;
        }
        actionListener = AWTEventMulticaster.remove(actionListener, l);
    }

    // AdjustmentEvent listener support

    public synchronized void addAdjustmentListener(AdjustmentListener l) {
        if (l == null) {
            return;
        }
        enableEvents(AWTEvent.ADJUSTMENT_EVENT_MASK);
        adjustmentListener = AWTEventMulticaster.add(adjustmentListener, l);
    }

    public synchronized void removeAdjustmentListener(AdjustmentListener l) {
        if (l == null) {
            return;
        }
        adjustmentListener = AWTEventMulticaster.remove(adjustmentListener, l);
    }

    // ItemEvent listener support

    public synchronized void addItemListener(ItemListener l) {
        if (l == null) {
            return;
        }
        enableEvents(AWTEvent.ITEM_EVENT_MASK);
        itemListener = AWTEventMulticaster.add(itemListener, l);
    }

    public synchronized void removeItemListener(ItemListener l) {
        if (l == null) {
            return;
        }
        itemListener = AWTEventMulticaster.remove(itemListener, l);
    }

    // TextEvent listener support

    public synchronized void addTextListener(TextListener l) {
        if (l == null) {
            return;
        }
        enableEvents(AWTEvent.TEXT_EVENT_MASK);
        textListener = AWTEventMulticaster.add(textListener, l);
    }

    public synchronized void removeTextListener(TextListener l) {
        if (l == null) {
            return;
        }
        textListener = AWTEventMulticaster.remove(textListener, l);
    }

    // Implement the processEvent and processXXXEvent methods to
    // handle reception and processing of the event types.

    protected void processEvent(AWTEvent e) {
        if (e instanceof ActionEvent) {
            processActionEvent((ActionEvent) e);
            return;
        }
        if (e instanceof AdjustmentEvent) {
            processAdjustmentEvent((AdjustmentEvent) e);
            return;
        }
        if (e instanceof ItemEvent) {
            processItemEvent((ItemEvent) e);
            return;
        }
        if (e instanceof TextEvent) {
            processTextEvent((TextEvent) e);
            return;
        }
        super.processEvent(e);
    }

    protected void processActionEvent(ActionEvent e) {
        if (actionListener != null) {
            actionListener.actionPerformed(e);
        }
    }

    protected void processAdjustmentEvent(AdjustmentEvent e) {
        if (adjustmentListener != null) {
            adjustmentListener.adjustmentValueChanged(e);
        }
    }

    protected void processItemEvent(ItemEvent e) {
        if (itemListener != null) {
            itemListener.itemStateChanged(e);
        }
    }

    protected void processTextEvent(TextEvent e) {
        if (textListener != null) {
            textListener.textValueChanged(e);
        }
    }

    public void paint(Graphics g) {
        Dimension dim = getSize();
        g.clearRect(0, 0, dim.width, dim.height);
        g.setColor(getForeground());
        g.drawString(text, 2, dim.height - 2);
    }

    /**
     * Returns the selected items or null if no items are selected.
     */
    public Object[] getSelectedObjects() {
        return null;
    }

    /**
     * Gets the orientation of the adjustable object.
     */
    public int getOrientation() {
        return 0;
    }

    /**
     * Gets the minimum value of the adjustable object.
     */
    public int getMinimum() {
        return 0;
    }

    /**
     * Sets the minimum value of the adjustable object.
     *
     * @param min the minimum value
     */
    public void setMinimum(int min) {
    }

    /**
     * Gets the maximum value of the adjustable object.
     */
    public int getMaximum() {
        return 0;
    }

    /**
     * Sets the maximum value of the adjustable object.
     *
     * @param max the maximum value
     */
    public void setMaximum(int max) {
    }

    /**
     * Gets the unit value increment for the adjustable object.
     */
    public int getUnitIncrement() {
        return 0;
    }

    /**
     * Sets the unit value increment for the adjustable object.
     *
     * @param u the unit increment
     */
    public void setUnitIncrement(int u) {
    }

    /**
     * Gets the block value increment for the adjustable object.
     */
    public int getBlockIncrement() {
        return 0;
    }

    /**
     * Sets the block value increment for the adjustable object.
     *
     * @param b the block increment
     */
    public void setBlockIncrement(int b) {
    }

    /**
     * Gets the length of the propertional indicator.
     */
    public int getVisibleAmount() {
        return 0;
    }

    /**
     * Sets the length of the proportionl indicator of the
     * adjustable object.
     *
     * @param v the length of the indicator
     */
    public void setVisibleAmount(int v) {
    }

    /**
     * Gets the current value of the adjustable object.
     */
    public int getValue() {
        return 0;
    }

    /**
     * Sets the current value of the adjustable object. This
     * value must be within the range defined by the minimum and
     * maximum values for this object.
     *
     * @param v the current value
     */
    public void setValue(int v) {
    }

}

class ActionBtn extends ButtonComponent {
    public ActionBtn() {
        super("ActionEvent");
        addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                ActionEvent ae = new ActionEvent(e.getSource(),
                        ActionEvent.ACTION_PERFORMED,
                        getActionCommand());
                Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(ae);
            }
        });
    }
}

class AdjustmentBtn extends ButtonComponent {
    public AdjustmentBtn() {
        super("AdjustmentEvent");
        addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                AdjustmentEvent ae = new AdjustmentEvent((Adjustable) e.getSource(),
                        AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED,
                        1, 1);
                Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(ae);
            }
        });
    }
}

class ItemBtn extends ButtonComponent {
    public ItemBtn() {
        super("ItemEvent");
        addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                ItemEvent ae = new ItemEvent((ItemSelectable) e.getSource(),
                        ItemEvent.ITEM_STATE_CHANGED,
                        e.getSource(), 1);
                Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(ae);
            }
        });
    }
}

class TextBtn extends ButtonComponent {
    public TextBtn() {
        super("TextEvent");
        addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                TextEvent ae = new TextEvent(e.getSource(),
                        TextEvent.TEXT_VALUE_CHANGED);
                Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(ae);
            }
        });
    }
}