File: ListScrollbarTest.java

package info (click to toggle)
openjdk-25 25~32ea-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 825,280 kB
  • sloc: java: 5,584,902; cpp: 1,333,941; xml: 1,321,242; ansic: 487,993; asm: 404,003; objc: 21,088; sh: 15,102; javascript: 13,265; python: 8,319; makefile: 2,515; perl: 357; awk: 351; pascal: 103; exp: 83; sed: 72; jsp: 24
file content (197 lines) | stat: -rw-r--r-- 6,921 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (c) 2001, 2025, 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 4096445
 * @summary Test to verify List Scollbar appears/disappears automatically
 * @library /java/awt/regtesthelpers
 * @build PassFailJFrame
 * @run main/manual ListScrollbarTest
 */

import java.awt.Button;
import java.awt.Component;
import java.awt.Event;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.List;

public class ListScrollbarTest extends Frame {
    static final int ITEMS = 10;
    List ltList;
    List rtList;

    public static void main(String[] args) throws Exception {
        String INSTRUCTIONS = """
                1. There are two lists added to the Frame separated by
                   a column of buttons
                2. Double click on any item(s) on the left list, you would see
                   a '*' added at the end of the item
                3. Keep double clicking on the same item till the length of the
                   item exceeds the width of the list
                4. Now, if you don't get the horizontal scrollbar on
                   the left list click FAIL.
                5. If you get horizontal scrollbar, select the item
                   (that you double clicked) and press the '>' button
                   to move the item to the right list.
                6. If horizontal scroll bar appears on the right list
                   as well as disappears from the left list [only if both
                   happen] proceed with step 8 else click FAIL
                7. Now move the same item to the left list, by pressing
                     '<' button
                8. If the horizontal scrollbar appears on the left list
                   and disappears from the right list[only if both happen]
                   click PASS else click FAIL.
                """;
        PassFailJFrame.builder()
                .instructions(INSTRUCTIONS)
                .columns(35)
                .testUI(ListScrollbarTest::new)
                .build()
                .awaitAndCheck();
    }

    public ListScrollbarTest() {
        super("List scroll bar test");
        GridBagLayout gbl = new GridBagLayout();
        ltList = new List(ITEMS, true);
        rtList = new List(0, true);
        setLayout(gbl);
        add(ltList, 0, 0, 1, 5, 1.0, 1.0);
        add(rtList, 2, 0, 1, 5, 1.0, 1.0);
        add(new Button(">"), 1, 0, 1, 1, 0, 1.0);
        add(new Button(">>"), 1, 1, 1, 1, 0, 1.0);
        add(new Button("<"), 1, 2, 1, 1, 0, 1.0);
        add(new Button("<<"), 1, 3, 1, 1, 0, 1.0);
        add(new Button("!"), 1, 4, 1, 1, 0, 1.0);

        for (int i = 0; i < ITEMS; i++) {
            ltList.addItem("item " + i);
        }
        setSize(220, 250);
    }

    void add(Component comp, int x, int y, int w, int h, double weightx, double weighty) {
        GridBagLayout gbl = (GridBagLayout) getLayout();
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.BOTH;
        c.gridx = x;
        c.gridy = y;
        c.gridwidth = w;
        c.gridheight = h;
        c.weightx = weightx;
        c.weighty = weighty;
        add(comp);
        gbl.setConstraints(comp, c);
    }

    void reverseSelections(List l) {
        for (int i = 0; i < l.countItems(); i++) {
            if (l.isSelected(i)) {
                l.deselect(i);
            } else {
                l.select(i);
            }
        }
    }

    void deselectAll(List l) {
        for (int i = 0; i < l.countItems(); i++) {
            l.deselect(i);
        }
    }

    void replaceItem(List l, String item) {
        for (int i = 0; i < l.countItems(); i++) {
            if (l.getItem(i).equals(item)) {
                l.replaceItem(item + "*", i);
            }
        }
    }

    void move(List l1, List l2, boolean all) {

        // if all the items are to be moved
        if (all) {
            for (int i = 0; i < l1.countItems(); i++) {
                l2.addItem(l1.getItem(i));
            }
            l1.delItems(0, l1.countItems() - 1);
        } else { // else move the selected items
            String[] items = l1.getSelectedItems();
            int[] itemIndexes = l1.getSelectedIndexes();

            deselectAll(l2);
            for (int i = 0; i < items.length; i++) {
                l2.addItem(items[i]);
                l2.select(l2.countItems() - 1);
                if (i == 0) {
                    l2.makeVisible(l2.countItems() - 1);
                }
            }
            for (int i = itemIndexes.length - 1; i >= 0; i--) {
                l1.delItem(itemIndexes[i]);
            }
        }
    }

    @Override
    public boolean action(Event evt, Object arg) {
        if (">".equals(arg)) {
            move(ltList, rtList, false);
        } else if (">>".equals(arg)) {
            move(ltList, rtList, true);
        } else if ("<".equals(arg)) {
            move(rtList, ltList, false);
        } else if ("<<".equals(arg)) {
            move(rtList, ltList, true);
        } else if ("!".equals(arg)) {
            if (ltList.getSelectedItems().length > 0) {
                reverseSelections(ltList);
            } else if (rtList.getSelectedItems().length > 0) {
                reverseSelections(rtList);
            }
        } else if (evt.target == rtList || evt.target == ltList) {
            replaceItem((List) evt.target, (String) arg);
        } else {
            return false;
        }
        return true;
    }

    @Override
    public boolean handleEvent(Event evt) {
        if (evt.id == Event.LIST_SELECT
                || evt.id == Event.LIST_DESELECT) {
            if (evt.target == ltList) {
                deselectAll(rtList);
            } else if (evt.target == rtList) {
                deselectAll(ltList);
            }
            return true;
        }
        return super.handleEvent(evt);
    }
}