|
| 1 | +/* |
| 2 | + * Copyright (c) 2003, 2022, 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 | +import java.awt.Color; |
| 25 | +import java.awt.Component; |
| 26 | +import java.awt.Robot; |
| 27 | +import java.util.Arrays; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Vector; |
| 30 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 31 | +import java.util.stream.Collectors; |
| 32 | +import java.util.stream.IntStream; |
| 33 | +import javax.swing.JButton; |
| 34 | +import javax.swing.JComboBox; |
| 35 | +import javax.swing.JFrame; |
| 36 | +import javax.swing.JList; |
| 37 | +import javax.swing.JPanel; |
| 38 | +import javax.swing.ListCellRenderer; |
| 39 | +import javax.swing.SwingUtilities; |
| 40 | +import javax.swing.UIManager; |
| 41 | +import javax.swing.UIManager.LookAndFeelInfo; |
| 42 | +import javax.swing.UnsupportedLookAndFeelException; |
| 43 | + |
| 44 | +import static javax.swing.UIManager.getInstalledLookAndFeels; |
| 45 | + |
| 46 | +/* |
| 47 | + * @test |
| 48 | + * @key headful |
| 49 | + * @bug 4231298 |
| 50 | + * @summary This testcase tests the RFE 4231298 request, JComboBox Custom |
| 51 | + * Renderer should not be called for non displaying elements if |
| 52 | + * setPrototypeDisplayValue() has been invoked. |
| 53 | + * @run main JComboBoxPrototypeDisplayValueTest |
| 54 | + */ |
| 55 | +public class JComboBoxPrototypeDisplayValueTest { |
| 56 | + |
| 57 | + private static Robot robot; |
| 58 | + private static JFrame frame; |
| 59 | + private static JComboBox buttonComboBox; |
| 60 | + private static volatile int count; |
| 61 | + |
| 62 | + public static void main(String[] args) throws Exception { |
| 63 | + robot = new Robot(); |
| 64 | + robot.setAutoWaitForIdle(true); |
| 65 | + robot.setAutoDelay(200); |
| 66 | + List<String> lafs = Arrays.stream(getInstalledLookAndFeels()) |
| 67 | + .map(LookAndFeelInfo::getClassName) |
| 68 | + .collect(Collectors.toList()); |
| 69 | + for (final String laf : lafs) { |
| 70 | + try { |
| 71 | + count = 0; |
| 72 | + AtomicBoolean lafSetSuccess = new AtomicBoolean(false); |
| 73 | + SwingUtilities.invokeAndWait(() -> { |
| 74 | + lafSetSuccess.set(setLookAndFeel(laf)); |
| 75 | + if (lafSetSuccess.get()) { |
| 76 | + createUI(); |
| 77 | + } |
| 78 | + }); |
| 79 | + if (!lafSetSuccess.get()) { |
| 80 | + continue; |
| 81 | + } |
| 82 | + robot.waitForIdle(); |
| 83 | + |
| 84 | + SwingUtilities |
| 85 | + .invokeAndWait(() -> buttonComboBox.getPreferredSize()); |
| 86 | + |
| 87 | + robot.waitForIdle(); |
| 88 | + if (count > 6) { |
| 89 | + System.out.println("Test Failed"); |
| 90 | + throw new RuntimeException( |
| 91 | + "Custom Renderer got called " + count + " times, " + |
| 92 | + "even after calling setPrototypeDisplayValue(), " + |
| 93 | + "but the expected maximum is 6 times for " + laf); |
| 94 | + } else { |
| 95 | + System.out.println("Test Passed for " + laf); |
| 96 | + } |
| 97 | + } finally { |
| 98 | + SwingUtilities.invokeAndWait( |
| 99 | + JComboBoxPrototypeDisplayValueTest::disposeFrame); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + public static void createUI() { |
| 105 | + Vector data = new Vector(IntStream.rangeClosed(1, 100).boxed() |
| 106 | + .map(i -> new JButton("" + i)) |
| 107 | + .collect(Collectors.toList())); |
| 108 | + buttonComboBox = new JComboBox(data); |
| 109 | + ButtonRenderer renderer = new ButtonRenderer(); |
| 110 | + buttonComboBox.setRenderer(renderer); |
| 111 | + buttonComboBox.setMaximumRowCount(25); |
| 112 | + |
| 113 | + // New method introduced in Java 1.4 |
| 114 | + buttonComboBox.setPrototypeDisplayValue(new JButton("111111111")); |
| 115 | + |
| 116 | + frame = new JFrame(); |
| 117 | + JPanel panel = new JPanel(); |
| 118 | + panel.add(buttonComboBox); |
| 119 | + frame.getContentPane().add(panel); |
| 120 | + frame.setSize(200, 100); |
| 121 | + frame.setLocationRelativeTo(null); |
| 122 | + frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); |
| 123 | + frame.setVisible(true); |
| 124 | + } |
| 125 | + |
| 126 | + private static boolean setLookAndFeel(String lafName) { |
| 127 | + try { |
| 128 | + UIManager.setLookAndFeel(lafName); |
| 129 | + } catch (UnsupportedLookAndFeelException ignored) { |
| 130 | + System.out.println("Ignoring Unsupported L&F: " + lafName); |
| 131 | + return false; |
| 132 | + } catch (ClassNotFoundException | InstantiationException |
| 133 | + | IllegalAccessException e) { |
| 134 | + throw new RuntimeException(e); |
| 135 | + } |
| 136 | + return true; |
| 137 | + } |
| 138 | + |
| 139 | + private static void disposeFrame() { |
| 140 | + if (frame != null) { |
| 141 | + frame.dispose(); |
| 142 | + frame = null; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Custom ListCellRenderer used for the drop down portion and the text |
| 148 | + * portion of the ComboBox. |
| 149 | + */ |
| 150 | + private static class ButtonRenderer implements ListCellRenderer { |
| 151 | + private final Color selectedBackground; |
| 152 | + private final Color selectedForeground; |
| 153 | + private final Color background; |
| 154 | + private final Color foreground; |
| 155 | + |
| 156 | + public ButtonRenderer() { |
| 157 | + selectedBackground = Color.BLUE; |
| 158 | + selectedForeground = Color.YELLOW; |
| 159 | + background = Color.GRAY; |
| 160 | + foreground = Color.RED; |
| 161 | + } |
| 162 | + |
| 163 | + public Component getListCellRendererComponent(JList list, Object value, |
| 164 | + int index, |
| 165 | + boolean isSelected, |
| 166 | + boolean cellHasFocus) { |
| 167 | + JButton button = (JButton) value; |
| 168 | + System.out.println( |
| 169 | + "getListCellRendererComponent index = " + index + ", " + |
| 170 | + "isSelected = " + isSelected + ", cellHasFocus = " + |
| 171 | + cellHasFocus); |
| 172 | + |
| 173 | + button.setBackground(isSelected ? selectedBackground : background); |
| 174 | + button.setForeground(isSelected ? selectedForeground : foreground); |
| 175 | + |
| 176 | + count++; |
| 177 | + System.out.println("Value of the Counter is " + count); |
| 178 | + |
| 179 | + return button; |
| 180 | + } |
| 181 | + |
| 182 | + } |
| 183 | + |
| 184 | +} |
0 commit comments