|
| 1 | +/* |
| 2 | + * Copyright (c) 2001, 2025, 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. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +/* |
| 25 | + * @test |
| 26 | + * @bug 4096445 |
| 27 | + * @summary Test to verify List Scollbar appears/disappears automatically |
| 28 | + * @library /java/awt/regtesthelpers |
| 29 | + * @build PassFailJFrame |
| 30 | + * @run main/manual ListScrollbarTest |
| 31 | + */ |
| 32 | + |
| 33 | +import java.awt.Button; |
| 34 | +import java.awt.Component; |
| 35 | +import java.awt.Event; |
| 36 | +import java.awt.Frame; |
| 37 | +import java.awt.GridBagConstraints; |
| 38 | +import java.awt.GridBagLayout; |
| 39 | +import java.awt.List; |
| 40 | + |
| 41 | +public class ListScrollbarTest extends Frame { |
| 42 | + static final int ITEMS = 10; |
| 43 | + List ltList; |
| 44 | + List rtList; |
| 45 | + |
| 46 | + public static void main(String[] args) throws Exception { |
| 47 | + String INSTRUCTIONS = """ |
| 48 | + 1. There are two lists added to the Frame separated by |
| 49 | + a column of buttons |
| 50 | + 2. Double click on any item(s) on the left list, you would see |
| 51 | + a '*' added at the end of the item |
| 52 | + 3. Keep double clicking on the same item till the length of the |
| 53 | + item exceeds the width of the list |
| 54 | + 4. Now, if you don't get the horizontal scrollbar on |
| 55 | + the left list click FAIL. |
| 56 | + 5. If you get horizontal scrollbar, select the item |
| 57 | + (that you double clicked) and press the '>' button |
| 58 | + to move the item to the right list. |
| 59 | + 6. If horizontal scroll bar appears on the right list |
| 60 | + as well as disappears from the left list [only if both |
| 61 | + happen] proceed with step 8 else click FAIL |
| 62 | + 7. Now move the same item to the left list, by pressing |
| 63 | + '<' button |
| 64 | + 8. If the horizontal scrollbar appears on the left list |
| 65 | + and disappears from the right list[only if both happen] |
| 66 | + click PASS else click FAIL. |
| 67 | + """; |
| 68 | + PassFailJFrame.builder() |
| 69 | + .instructions(INSTRUCTIONS) |
| 70 | + .columns(35) |
| 71 | + .testUI(ListScrollbarTest::new) |
| 72 | + .build() |
| 73 | + .awaitAndCheck(); |
| 74 | + } |
| 75 | + |
| 76 | + public ListScrollbarTest() { |
| 77 | + super("List scroll bar test"); |
| 78 | + GridBagLayout gbl = new GridBagLayout(); |
| 79 | + ltList = new List(ITEMS, true); |
| 80 | + rtList = new List(0, true); |
| 81 | + setLayout(gbl); |
| 82 | + add(ltList, 0, 0, 1, 5, 1.0, 1.0); |
| 83 | + add(rtList, 2, 0, 1, 5, 1.0, 1.0); |
| 84 | + add(new Button(">"), 1, 0, 1, 1, 0, 1.0); |
| 85 | + add(new Button(">>"), 1, 1, 1, 1, 0, 1.0); |
| 86 | + add(new Button("<"), 1, 2, 1, 1, 0, 1.0); |
| 87 | + add(new Button("<<"), 1, 3, 1, 1, 0, 1.0); |
| 88 | + add(new Button("!"), 1, 4, 1, 1, 0, 1.0); |
| 89 | + |
| 90 | + for (int i = 0; i < ITEMS; i++) { |
| 91 | + ltList.addItem("item " + i); |
| 92 | + } |
| 93 | + setSize(220, 250); |
| 94 | + } |
| 95 | + |
| 96 | + void add(Component comp, int x, int y, int w, int h, double weightx, double weighty) { |
| 97 | + GridBagLayout gbl = (GridBagLayout) getLayout(); |
| 98 | + GridBagConstraints c = new GridBagConstraints(); |
| 99 | + c.fill = GridBagConstraints.BOTH; |
| 100 | + c.gridx = x; |
| 101 | + c.gridy = y; |
| 102 | + c.gridwidth = w; |
| 103 | + c.gridheight = h; |
| 104 | + c.weightx = weightx; |
| 105 | + c.weighty = weighty; |
| 106 | + add(comp); |
| 107 | + gbl.setConstraints(comp, c); |
| 108 | + } |
| 109 | + |
| 110 | + void reverseSelections(List l) { |
| 111 | + for (int i = 0; i < l.countItems(); i++) { |
| 112 | + if (l.isSelected(i)) { |
| 113 | + l.deselect(i); |
| 114 | + } else { |
| 115 | + l.select(i); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + void deselectAll(List l) { |
| 121 | + for (int i = 0; i < l.countItems(); i++) { |
| 122 | + l.deselect(i); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + void replaceItem(List l, String item) { |
| 127 | + for (int i = 0; i < l.countItems(); i++) { |
| 128 | + if (l.getItem(i).equals(item)) { |
| 129 | + l.replaceItem(item + "*", i); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + void move(List l1, List l2, boolean all) { |
| 135 | + |
| 136 | + // if all the items are to be moved |
| 137 | + if (all) { |
| 138 | + for (int i = 0; i < l1.countItems(); i++) { |
| 139 | + l2.addItem(l1.getItem(i)); |
| 140 | + } |
| 141 | + l1.delItems(0, l1.countItems() - 1); |
| 142 | + } else { // else move the selected items |
| 143 | + String[] items = l1.getSelectedItems(); |
| 144 | + int[] itemIndexes = l1.getSelectedIndexes(); |
| 145 | + |
| 146 | + deselectAll(l2); |
| 147 | + for (int i = 0; i < items.length; i++) { |
| 148 | + l2.addItem(items[i]); |
| 149 | + l2.select(l2.countItems() - 1); |
| 150 | + if (i == 0) { |
| 151 | + l2.makeVisible(l2.countItems() - 1); |
| 152 | + } |
| 153 | + } |
| 154 | + for (int i = itemIndexes.length - 1; i >= 0; i--) { |
| 155 | + l1.delItem(itemIndexes[i]); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public boolean action(Event evt, Object arg) { |
| 162 | + if (">".equals(arg)) { |
| 163 | + move(ltList, rtList, false); |
| 164 | + } else if (">>".equals(arg)) { |
| 165 | + move(ltList, rtList, true); |
| 166 | + } else if ("<".equals(arg)) { |
| 167 | + move(rtList, ltList, false); |
| 168 | + } else if ("<<".equals(arg)) { |
| 169 | + move(rtList, ltList, true); |
| 170 | + } else if ("!".equals(arg)) { |
| 171 | + if (ltList.getSelectedItems().length > 0) { |
| 172 | + reverseSelections(ltList); |
| 173 | + } else if (rtList.getSelectedItems().length > 0) { |
| 174 | + reverseSelections(rtList); |
| 175 | + } |
| 176 | + } else if (evt.target == rtList || evt.target == ltList) { |
| 177 | + replaceItem((List) evt.target, (String) arg); |
| 178 | + } else { |
| 179 | + return false; |
| 180 | + } |
| 181 | + return true; |
| 182 | + } |
| 183 | + |
| 184 | + @Override |
| 185 | + public boolean handleEvent(Event evt) { |
| 186 | + if (evt.id == Event.LIST_SELECT |
| 187 | + || evt.id == Event.LIST_DESELECT) { |
| 188 | + if (evt.target == ltList) { |
| 189 | + deselectAll(rtList); |
| 190 | + } else if (evt.target == rtList) { |
| 191 | + deselectAll(ltList); |
| 192 | + } |
| 193 | + return true; |
| 194 | + } |
| 195 | + return super.handleEvent(evt); |
| 196 | + } |
| 197 | +} |
0 commit comments