1 /*
  2  * Copyright (c) 2006, 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 /* @test
 25    @bug 5049549 7132413
 26    @summary Tests that the proper icon is used for different states.
 27    @library ../../regtesthelpers
 28    @build Blocker
 29    @run main/manual bug5049549
 30 */
 31 
 32 import javax.swing.BoxLayout;
 33 import javax.swing.Icon;
 34 import javax.swing.ImageIcon;
 35 import javax.swing.JButton;
 36 import javax.swing.JFrame;
 37 import javax.swing.JLabel;
 38 import javax.swing.JPanel;
 39 import javax.swing.SwingUtilities;
 40 import javax.swing.UIManager;
 41 
 42 public class bug5049549 {
 43 
 44     private static ImageIcon DE = new ImageIcon(bug5049549.class.getResource("DE1.gif"));
 45     private static ImageIcon DI = new ImageIcon(bug5049549.class.getResource("DI1.gif"));
 46     private static ImageIcon DS = new ImageIcon(bug5049549.class.getResource("DS1.gif"));
 47     private static ImageIcon RO = new ImageIcon(bug5049549.class.getResource("RO1.gif"));
 48     private static ImageIcon RS = new ImageIcon(bug5049549.class.getResource("RS1.gif"));
 49     private static ImageIcon SE = new ImageIcon(bug5049549.class.getResource("SE1.gif"));
 50     private static ImageIcon PR = new ImageIcon(bug5049549.class.getResource("PR1.gif"));
 51 
 52     private static Blocker blocker = new Blocker();
 53 
 54     private static class KButton extends JButton {
 55 
 56             KButton(String ex) {
 57                 super(ex);
 58             }
 59 
 60             private Icon disabledIcon;
 61             private Icon disabledSelectedIcon;
 62 
 63             public Icon getDisabledIcon() {
 64                 return disabledIcon;
 65             }
 66 
 67             public Icon getDisabledSelectedIcon() {
 68                 return disabledSelectedIcon;
 69             }
 70 
 71             public void setDisabledIcon(Icon icon) {
 72                 disabledIcon = icon;
 73             }
 74 
 75             public void setDisabledSelectedIcon(Icon icon) {
 76                 disabledSelectedIcon = icon;
 77             }
 78     }
 79 
 80     public static void main(String[] args) throws Throwable {
 81         SwingUtilities.invokeLater(new Runnable() {
 82             public void run() {
 83                 UIManager.put("swing.boldMetal", Boolean.FALSE);
 84                 runTest();
 85             }
 86         });
 87 
 88         blocker.blockTillDone();
 89     }
 90 
 91     private static void runTest() {
 92         JFrame frame = blocker.createFrameWithPassFailButtons("Wrong icon is used.");
 93 
 94         JPanel panel = new JPanel();
 95         panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
 96 
 97         JLabel label = new JLabel("The following disabled buttons should have white icons");
 98         panel.add(label);
 99         label = new JLabel("with red text that matches the button text.");
100         panel.add(label);
101 
102         KButton button;
103 
104         /* disabled: default icon */
105         button = new KButton("DE");
106         button.setEnabled(false);
107         button.setIcon(DE);
108         panel.add(button);
109 
110         /* disabled-selected: default icon */
111         button = new KButton("DE");
112         button.setEnabled(false);
113         button.setSelected(true);
114         button.setIcon(DE);
115         panel.add(button);
116 
117         /* disabled: default and disabled icons */
118         button = new KButton("DI");
119         button.setEnabled(false);
120         button.setIcon(DE);
121         button.setDisabledIcon(DI);
122         panel.add(button);
123 
124         /* disabled-selected: default and disabled icons */
125         button = new KButton("DI");
126         button.setEnabled(false);
127         button.setSelected(true);
128         button.setIcon(DE);
129         button.setDisabledIcon(DI);
130         panel.add(button);
131 
132         /* disabled-selected: default, selected and disabled icons */
133         button = new KButton("SE");
134         button.setEnabled(false);
135         button.setSelected(true);
136         button.setIcon(DE);
137         button.setSelectedIcon(SE);
138         button.setDisabledIcon(DI);
139         panel.add(button);
140 
141         /* disabled-selected: default, disabled-selected, selected and disabled icons */
142         button = new KButton("DS");
143         button.setEnabled(false);
144         button.setSelected(true);
145         button.setIcon(DE);
146         button.setSelectedIcon(SE);
147         button.setDisabledIcon(DI);
148         button.setDisabledSelectedIcon(DS);
149         panel.add(button);
150 
151         label = new JLabel("The following buttons have a white icon with red text,");
152         panel.add(label);
153         label = new JLabel("and a triplet of strings separated by commas. The first");
154         panel.add(label);
155         label = new JLabel("string says what the icon text should show in the normal");
156         panel.add(label);
157         label = new JLabel("state. The second the rollover state. And the third shows");
158         panel.add(label);
159         label = new JLabel("what it should show when pressed. Verify each of these");
160         panel.add(label);
161         label = new JLabel("states for each button.");
162         panel.add(label);
163 
164         /* normal: default, rollover icons */
165         button = new KButton("DE, RO, DE");
166         button.setIcon(DE);
167         button.setRolloverIcon(RO);
168         panel.add(button);
169 
170         /* normal: default, rollover, pressed icons */
171         button = new KButton("DE, RO, PR");
172         button.setIcon(DE);
173         button.setRolloverIcon(RO);
174         button.setPressedIcon(PR);
175         panel.add(button);
176 
177         /* selected: default, rollover, pressed icons */
178         button = new KButton("DE, RO, PR");
179         button.setSelected(true);
180         button.setIcon(DE);
181         button.setRolloverIcon(RO);
182         button.setPressedIcon(PR);
183         panel.add(button);
184 
185         /* selected: default, rollover, pressed icons */
186         button = new KButton("DE, DE, PR");
187         button.setSelected(true);
188         button.setIcon(DE);
189         button.setPressedIcon(PR);
190         panel.add(button);
191 
192         /* selected: default, selected, rollover, pressed icons */
193         button = new KButton("SE, SE, PR");
194         button.setSelected(true);
195         button.setIcon(DE);
196         button.setSelectedIcon(SE);
197         button.setRolloverIcon(RO);
198         button.setPressedIcon(PR);
199         panel.add(button);
200 
201         /* selected: default, selected, rollover, rollover-selected, pressed icons */
202         button = new KButton("SE, RS, PR");
203         button.setSelected(true);
204         button.setIcon(DE);
205         button.setRolloverSelectedIcon(RS);
206         button.setSelectedIcon(SE);
207         button.setRolloverIcon(RO);
208         button.setPressedIcon(PR);
209         panel.add(button);
210 
211         /* selected: default, selected, rollover, rollover-selected icons */
212         button = new KButton("SE, RS, SE");
213         button.setSelected(true);
214         button.setIcon(DE);
215         button.setRolloverSelectedIcon(RS);
216         button.setSelectedIcon(SE);
217         button.setRolloverIcon(RO);
218         panel.add(button);
219 
220         /* selected: default, selected icons */
221         button = new KButton("SE, SE, SE");
222         button.setSelected(true);
223         button.setIcon(DE);
224         button.setSelectedIcon(SE);
225         button.setRolloverIcon(RO);
226         panel.add(button);
227 
228         frame.add(panel);
229 
230         frame.pack();
231         frame.setLocationRelativeTo(null);
232         frame.setVisible(true);
233     }
234 }