File: Test6524757.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (233 lines) | stat: -rw-r--r-- 10,470 bytes parent folder | download | duplicates (16)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
 * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @key headful
 * @bug 6524757
 * @summary Tests different locales
 * @author Sergey Malenkov
 * @modules java.desktop/sun.swing
 */

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import javax.swing.AbstractButton;
import javax.swing.JColorChooser;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.colorchooser.AbstractColorChooserPanel;

import sun.swing.SwingUtilities2;

public class Test6524757 {
    private static final String[] KEYS = {
            "ColorChooser.okText", // NON-NLS: string key from JColorChooser
            "ColorChooser.cancelText", // NON-NLS: string key from JColorChooser
            "ColorChooser.resetText", // NON-NLS: string key from JColorChooser
            "ColorChooser.resetMnemonic", // NON-NLS: int key from JColorChooser

//NotAvail: "ColorChooser.sampleText", // NON-NLS: string key from DefaultPreviewPanel

            "ColorChooser.swatchesNameText", // NON-NLS: string key from DefaultSwatchChooserPanel
            "ColorChooser.swatchesMnemonic", // NON-NLS: string key from DefaultSwatchChooserPanel:int
            "ColorChooser.swatchesSwatchSize", // NON-NLS: dimension key from DefaultSwatchChooserPanel
            "ColorChooser.swatchesRecentText", // NON-NLS: string key from DefaultSwatchChooserPanel
            "ColorChooser.swatchesRecentSwatchSize", // NON-NLS: dimension key from DefaultSwatchChooserPanel
//NotAvail: "ColorChooser.swatchesDefaultRecentColor", // NON-NLS: color key from DefaultSwatchChooserPanel

            "ColorChooser.hsvNameText", // NON-NLS: string key from HSV ColorChooserPanel
            "ColorChooser.hsvMnemonic", // NON-NLS: int key from HSV ColorChooserPanel
            "ColorChooser.hsvHueText", // NON-NLS: string key from HSV ColorChooserPanel
            "ColorChooser.hsvSaturationText", // NON-NLS: string key from HSV ColorChooserPanel
            "ColorChooser.hsvValueText", // NON-NLS: string key from HSV ColorChooserPanel
            "ColorChooser.hsvTransparencyText", // NON-NLS: string key from HSV ColorChooserPanel

            "ColorChooser.hslNameText", // NON-NLS: string key from HSV ColorChooserPanel
            "ColorChooser.hslMnemonic", // NON-NLS: int key from HSV ColorChooserPanel
            "ColorChooser.hslHueText", // NON-NLS: string key from HSV ColorChooserPanel
            "ColorChooser.hslSaturationText", // NON-NLS: string key from HSV ColorChooserPanel
            "ColorChooser.hslLightnessText", // NON-NLS: string key from HSV ColorChooserPanel
            "ColorChooser.hslTransparencyText", // NON-NLS: string key from HSV ColorChooserPanel

            "ColorChooser.rgbNameText", // NON-NLS: string key from HSV ColorChooserPanel
            "ColorChooser.rgbMnemonic", // NON-NLS: int key from HSV ColorChooserPanel
            "ColorChooser.rgbRedText", // NON-NLS: string key from HSV ColorChooserPanel
            "ColorChooser.rgbGreenText", // NON-NLS: string key from HSV ColorChooserPanel
            "ColorChooser.rgbBlueText", // NON-NLS: string key from HSV ColorChooserPanel
            "ColorChooser.rgbAlphaText", // NON-NLS: string key from HSV ColorChooserPanel
            "ColorChooser.rgbHexCodeText", // NON-NLS: string key from HSV ColorChooserPanel
            "ColorChooser.rgbHexCodeMnemonic", // NON-NLS: int key from HSV ColorChooserPanel

            "ColorChooser.cmykNameText", // NON-NLS: string key from HSV ColorChooserPanel
            "ColorChooser.cmykMnemonic", // NON-NLS: int key from HSV ColorChooserPanel
            "ColorChooser.cmykCyanText", // NON-NLS: string key from HSV ColorChooserPanel
            "ColorChooser.cmykMagentaText", // NON-NLS: string key from HSV ColorChooserPanel
            "ColorChooser.cmykYellowText", // NON-NLS: string key from HSV ColorChooserPanel
            "ColorChooser.cmykBlackText", // NON-NLS: string key from HSV ColorChooserPanel
            "ColorChooser.cmykAlphaText", // NON-NLS: string key from HSV ColorChooserPanel
    };
    private static final Object[] KOREAN = convert(Locale.KOREAN, KEYS);
    private static final Object[] FRENCH = convert(Locale.FRENCH, KEYS);

    public static void main(String[] args) {
        Locale reservedLocale = Locale.getDefault();
        try {
            // it affects Swing because it is not initialized
            Locale.setDefault(Locale.KOREAN);
            validate(KOREAN, create());

            // it does not affect Swing because it is initialized
            Locale.setDefault(Locale.CANADA);
            validate(KOREAN, create());

            // it definitely should affect Swing
            JComponent.setDefaultLocale(Locale.FRENCH);
            validate(FRENCH, create());
        } finally {
            // restore the reserved locale
            Locale.setDefault(reservedLocale);
        }
    }

    private static void validate(Object[] expected, Object[] actual) {
        int count = expected.length;
        if (count != actual.length) {
            throw new Error("different size: " + count + " <> " + actual.length);
        }
        for (int i = 0; i < count; i++) {
            if (!expected[i].equals(actual[i])) {
                throw new Error("unexpected value for key: " + KEYS[i]);
            }
        }
    }

    private static Object[] convert(Locale locale, String[] keys) {
        int count = keys.length;
        Object[] array = new Object[count];
        for (int i = 0; i < count; i++) {
            array[i] = convert(locale, keys[i]);
        }
        return array;
    }

    private static Object convert(Locale locale, String key) {
        if (key.endsWith("Text")) { // NON-NLS: suffix for text message
            return UIManager.getString(key, locale);
        }
        if (key.endsWith("Size")) { // NON-NLS: suffix for dimension
            return UIManager.getDimension(key, locale);
        }
        if (key.endsWith("Color")) { // NON-NLS: suffix for color
            return UIManager.getColor(key, locale);
        }
        int value = SwingUtilities2.getUIDefaultsInt(key, locale, -1);
        return Integer.valueOf(value);
    }

    private static Object[] create() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        frame.setVisible(true);

        // show color chooser
        JColorChooser chooser = new JColorChooser();
        JDialog dialog = JColorChooser.createDialog(frame, null, false, chooser, null, null);
        dialog.setVisible(true);

        // process all values
        List<Object> list = new ArrayList<Object>(KEYS.length);

        Component component = getC(getC(dialog.getLayeredPane(), 0), 1);
        AbstractButton ok = (AbstractButton) getC(component, 0);
        AbstractButton cancel = (AbstractButton) getC(component, 1);
        AbstractButton reset = (AbstractButton) getC(component, 2);
        list.add(ok.getText());
        list.add(cancel.getText());
        list.add(reset.getText());
        list.add(Integer.valueOf(reset.getMnemonic()));

        for (int i = 0; i < 5; i++) {
            AbstractColorChooserPanel panel = (AbstractColorChooserPanel) getC(getC(getC(chooser, 0), i), 0);
            list.add(panel.getDisplayName());
            list.add(Integer.valueOf(panel.getMnemonic()));
            if (i == 0) {
                JLabel label = (JLabel) getC(getC(panel, 0), 1);
                JPanel upper = (JPanel) getC(getC(getC(panel, 0), 0), 0);
                JPanel lower = (JPanel) getC(getC(getC(panel, 0), 2), 0);
                addSize(list, upper, 1, 1, 31, 9);
                list.add(label.getText());
                addSize(list, lower, 1, 1, 5, 7);
            }
            else {
                Component container = getC(panel, 0);
                for (int j = 0; j < 3; j++) {
                    AbstractButton button = (AbstractButton) getC(container, j);
                    list.add(button.getText());
                }
                JLabel label = (JLabel) getC(container, 3);
                list.add(label.getText());
                if (i == 4) {
                    label = (JLabel) getC(container, 4);
                    list.add(label.getText());
                }
                if (i == 3) {
                    label = (JLabel) getC(panel, 1);
                    list.add(label.getText());
                    list.add(Integer.valueOf(label.getDisplayedMnemonic()));
                }
            }
        }

        // close dialog
        dialog.setVisible(false);
        dialog.dispose();

        // close frame
        frame.setVisible(false);
        frame.dispose();

        return list.toArray();
    }

    private static void addSize(List<Object> list, Component component, int x, int y, int w, int h) {
        Dimension size = component.getPreferredSize();
        int width = (size.width + 1) / w - x;
        int height = (size.height + 1) / h - y;
        list.add(new Dimension(width, height));
    }

    private static Component getC(Component component, int index) {
        Container container = (Container) component;
        return container.getComponent(index);
    }
}