|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * This code is free software; you can redistribute it and/or modify it
|
|
25 | 25 | * @bug 4449413
|
26 | 26 | * @summary Tests that checkbox and radiobuttons' check marks are visible when background is black
|
27 | 27 | * @author Ilya Boyandin
|
28 |
| - * @library /test/lib |
29 |
| - * @modules java.desktop/sun.awt |
30 |
| - * @build jdk.test.lib.Platform |
31 |
| - * @run applet/manual=yesno bug4449413.html |
| 28 | + * @run main/manual bug4449413 |
32 | 29 | */
|
33 | 30 |
|
34 |
| -import javax.swing.*; |
35 |
| -import javax.swing.plaf.metal.*; |
36 |
| -import java.awt.event.*; |
37 |
| -import java.awt.*; |
| 31 | +import javax.swing.AbstractButton; |
| 32 | +import javax.swing.BoxLayout; |
| 33 | +import javax.swing.JButton; |
| 34 | +import javax.swing.JCheckBox; |
| 35 | +import javax.swing.JCheckBoxMenuItem; |
| 36 | +import javax.swing.JFrame; |
| 37 | +import javax.swing.JPanel; |
| 38 | +import javax.swing.JRadioButton; |
| 39 | +import javax.swing.JRadioButtonMenuItem; |
| 40 | +import javax.swing.JTextArea; |
| 41 | +import javax.swing.SwingUtilities; |
| 42 | +import javax.swing.UIManager; |
| 43 | +import javax.swing.plaf.metal.DefaultMetalTheme; |
| 44 | +import javax.swing.plaf.metal.MetalLookAndFeel; |
| 45 | +import javax.swing.plaf.metal.MetalTheme; |
| 46 | +import javax.swing.plaf.metal.OceanTheme; |
| 47 | +import java.awt.Color; |
| 48 | +import java.awt.GridLayout; |
| 49 | +import java.awt.Insets; |
| 50 | +import java.awt.event.ActionListener; |
| 51 | +import java.awt.event.ItemEvent; |
| 52 | +import java.awt.event.WindowAdapter; |
| 53 | +import java.awt.event.WindowEvent; |
| 54 | +import java.util.concurrent.CountDownLatch; |
| 55 | +import java.util.concurrent.TimeUnit; |
| 56 | + |
| 57 | +public class bug4449413 extends JFrame { |
| 58 | + |
| 59 | + private static final String INSTRUCTIONS = |
| 60 | + "There are eight controls with black backgrounds.\n" + |
| 61 | + "Four enabled (on the left side) and four disabled (on the right side)\n" + |
| 62 | + "checkboxes and radiobuttons.\n\n" + |
| 63 | + "1. If at least one of the controls' check marks is not visible:\n" + |
| 64 | + " the test fails.\n"; |
| 65 | + |
| 66 | + private static final String INSTRUCTIONS_ADDITIONS_METAL = |
| 67 | + "\n" + |
| 68 | + "2. Uncheck the \"Use Ocean Theme\" check box.\n" + |
| 69 | + " If now at least one of the controls' check marks is not visible:\n" + |
| 70 | + " the test fails.\n"; |
| 71 | + |
| 72 | + private static final CountDownLatch latch = new CountDownLatch(1); |
| 73 | + private static volatile boolean failed = true; |
| 74 | + |
| 75 | + private final MetalTheme defaultMetalTheme = new DefaultMetalTheme(); |
| 76 | + private final MetalTheme oceanTheme = new OceanTheme(); |
| 77 | + |
| 78 | + private static bug4449413 instance; |
| 79 | + |
| 80 | + boolean isMetalLookAndFeel() { |
| 81 | + return UIManager.getLookAndFeel() instanceof MetalLookAndFeel; |
| 82 | + } |
| 83 | + |
| 84 | + public static void main(String[] args) throws Exception { |
| 85 | + SwingUtilities.invokeLater(() -> { |
| 86 | + instance = new bug4449413(); |
| 87 | + instance.createAndShowGUI(); |
| 88 | + }); |
38 | 89 |
|
39 |
| -import jdk.test.lib.Platform; |
| 90 | + boolean timeoutHappened = !latch.await(2, TimeUnit.MINUTES); |
| 91 | + |
| 92 | + SwingUtilities.invokeAndWait(() -> { |
| 93 | + if (instance != null) { |
| 94 | + instance.dispose(); |
| 95 | + } |
| 96 | + }); |
40 | 97 |
|
41 |
| -public class bug4449413 extends JApplet { |
| 98 | + System.out.println("Passed: " + !failed); |
| 99 | + |
| 100 | + if (timeoutHappened || failed) { |
| 101 | + throw new RuntimeException("Test failed!"); |
| 102 | + } |
| 103 | + } |
42 | 104 |
|
43 |
| - @Override |
44 |
| - public void init() { |
| 105 | + private void createAndShowGUI() { |
| 106 | + setTitle(UIManager.getLookAndFeel().getClass().getName()); |
45 | 107 |
|
46 |
| - try { |
| 108 | + addComponentsToPane(); |
47 | 109 |
|
48 |
| - if (Platform.isOSX()) { |
49 |
| - UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); |
| 110 | + addWindowListener(new WindowAdapter() { |
| 111 | + @Override |
| 112 | + public void windowClosing(WindowEvent e) { |
| 113 | + latch.countDown(); |
50 | 114 | }
|
| 115 | + }); |
| 116 | + |
| 117 | + setLocationRelativeTo(null); |
| 118 | + pack(); |
| 119 | + setVisible(true); |
| 120 | + } |
| 121 | + |
| 122 | + public void addComponentsToPane() { |
| 123 | + setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS)); |
| 124 | + |
| 125 | + JPanel testedPanel = new JPanel(); |
| 126 | + testedPanel.setLayout(new GridLayout(4, 6, 10, 15)); |
| 127 | + for (int k = 0; k <= 3; k++) { |
| 128 | + for (int j = 1; j >= 0; j--) { |
| 129 | + AbstractButton b = createButton(j, k); |
| 130 | + testedPanel.add(b); |
| 131 | + } |
| 132 | + } |
51 | 133 |
|
52 |
| - final MetalTheme oceanTheme = (MetalTheme) sun.awt.AppContext.getAppContext().get("currentMetalTheme"); |
53 |
| - |
54 |
| - |
55 |
| - SwingUtilities.invokeAndWait(new Runnable() { |
56 |
| - |
57 |
| - @Override |
58 |
| - public void run() { |
59 |
| - getContentPane().setLayout(new FlowLayout()); |
60 |
| - final JPanel panel = new JPanel(); |
61 |
| - |
62 |
| - JCheckBox box = new JCheckBox("Use Ocean theme", true); |
63 |
| - getContentPane().add(box); |
64 |
| - box.addItemListener(new ItemListener() { |
65 |
| - |
66 |
| - @Override |
67 |
| - public void itemStateChanged(ItemEvent e) { |
68 |
| - if (e.getStateChange() == ItemEvent.SELECTED) { |
69 |
| - MetalLookAndFeel.setCurrentTheme(oceanTheme); |
70 |
| - } else { |
71 |
| - MetalLookAndFeel.setCurrentTheme(new DefaultMetalTheme()); |
72 |
| - } |
73 |
| - SwingUtilities.updateComponentTreeUI(panel); |
74 |
| - } |
75 |
| - }); |
76 |
| - |
77 |
| - getContentPane().add(panel); |
78 |
| - panel.setLayout(new GridLayout(4, 6, 10, 15)); |
79 |
| - for (int k = 0; k <= 3; k++) { |
80 |
| - for (int j = 1; j >= 0; j--) { |
81 |
| - AbstractButton b = createButton(j, k); |
82 |
| - panel.add(b); |
83 |
| - } |
84 |
| - } |
| 134 | + add(testedPanel); |
| 135 | + |
| 136 | + |
| 137 | + if (isMetalLookAndFeel()) { |
| 138 | + JCheckBox oceanThemeSwitch = new JCheckBox("Use Ocean theme", true); |
| 139 | + oceanThemeSwitch.addItemListener(e -> { |
| 140 | + if (e.getStateChange() == ItemEvent.SELECTED) { |
| 141 | + MetalLookAndFeel.setCurrentTheme(oceanTheme); |
| 142 | + } else { |
| 143 | + MetalLookAndFeel.setCurrentTheme(defaultMetalTheme); |
85 | 144 | }
|
| 145 | + SwingUtilities.updateComponentTreeUI(testedPanel); |
86 | 146 | });
|
87 | 147 |
|
88 |
| - } catch (Exception e) { |
89 |
| - throw new RuntimeException(e); |
| 148 | + add(oceanThemeSwitch); |
90 | 149 | }
|
| 150 | + |
| 151 | + JTextArea instructionArea = new JTextArea( |
| 152 | + isMetalLookAndFeel() |
| 153 | + ? INSTRUCTIONS + INSTRUCTIONS_ADDITIONS_METAL |
| 154 | + : INSTRUCTIONS |
| 155 | + ); |
| 156 | + |
| 157 | + instructionArea.setEditable(false); |
| 158 | + instructionArea.setFocusable(false); |
| 159 | + instructionArea.setMargin(new Insets(10,10,10,10)); |
| 160 | + |
| 161 | + add(instructionArea); |
| 162 | + |
| 163 | + |
| 164 | + JButton passButton = new JButton("Pass"); |
| 165 | + JButton failButton = new JButton("Fail"); |
| 166 | + |
| 167 | + ActionListener actionListener = e -> { |
| 168 | + failed = e.getSource() == failButton; |
| 169 | + latch.countDown(); |
| 170 | + }; |
| 171 | + |
| 172 | + passButton.addActionListener(actionListener); |
| 173 | + failButton.addActionListener(actionListener); |
| 174 | + |
| 175 | + JPanel passFailPanel = new JPanel(); |
| 176 | + passFailPanel.add(passButton); |
| 177 | + passFailPanel.add(failButton); |
| 178 | + |
| 179 | + add(passFailPanel); |
91 | 180 | }
|
92 | 181 |
|
93 | 182 | static AbstractButton createButton(int enabled, int type) {
|
94 |
| - AbstractButton b = null; |
95 |
| - switch (type) { |
96 |
| - case 0: |
97 |
| - b = new JRadioButton("RadioButton"); |
98 |
| - break; |
99 |
| - case 1: |
100 |
| - b = new JCheckBox("CheckBox"); |
101 |
| - break; |
102 |
| - case 2: |
103 |
| - b = new JRadioButtonMenuItem("RBMenuItem"); |
104 |
| - break; |
105 |
| - case 3: |
106 |
| - b = new JCheckBoxMenuItem("CBMenuItem"); |
107 |
| - break; |
108 |
| - } |
| 183 | + AbstractButton b = switch (type) { |
| 184 | + case 0 -> new JRadioButton("RadioButton"); |
| 185 | + case 1 -> new JCheckBox("CheckBox"); |
| 186 | + case 2 -> new JRadioButtonMenuItem("RBMenuItem"); |
| 187 | + case 3 -> new JCheckBoxMenuItem("CBMenuItem"); |
| 188 | + default -> throw new IllegalArgumentException("type should be in range of 0..3"); |
| 189 | + }; |
| 190 | + |
| 191 | + b.setOpaque(true); |
109 | 192 | b.setBackground(Color.black);
|
110 | 193 | b.setForeground(Color.white);
|
111 | 194 | b.setEnabled(enabled == 1);
|
|
0 commit comments