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