Test6524757.java revision 14851:980da45565c8
1/* 2 * Copyright (c) 2007, 2016, 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/* 25 * @test 26 * @key headful 27 * @bug 6524757 28 * @summary Tests different locales 29 * @author Sergey Malenkov 30 * @modules java.desktop/sun.swing 31 */ 32 33import java.awt.Component; 34import java.awt.Container; 35import java.awt.Dimension; 36import java.util.ArrayList; 37import java.util.List; 38import java.util.Locale; 39import javax.swing.AbstractButton; 40import javax.swing.JColorChooser; 41import javax.swing.JComponent; 42import javax.swing.JDialog; 43import javax.swing.JFrame; 44import javax.swing.JLabel; 45import javax.swing.JPanel; 46import javax.swing.UIManager; 47import javax.swing.WindowConstants; 48import javax.swing.colorchooser.AbstractColorChooserPanel; 49 50import sun.swing.SwingUtilities2; 51 52public class Test6524757 { 53 private static final String[] KEYS = { 54 "ColorChooser.okText", // NON-NLS: string key from JColorChooser 55 "ColorChooser.cancelText", // NON-NLS: string key from JColorChooser 56 "ColorChooser.resetText", // NON-NLS: string key from JColorChooser 57 "ColorChooser.resetMnemonic", // NON-NLS: int key from JColorChooser 58 59//NotAvail: "ColorChooser.sampleText", // NON-NLS: string key from DefaultPreviewPanel 60 61 "ColorChooser.swatchesNameText", // NON-NLS: string key from DefaultSwatchChooserPanel 62 "ColorChooser.swatchesMnemonic", // NON-NLS: string key from DefaultSwatchChooserPanel:int 63 "ColorChooser.swatchesSwatchSize", // NON-NLS: dimension key from DefaultSwatchChooserPanel 64 "ColorChooser.swatchesRecentText", // NON-NLS: string key from DefaultSwatchChooserPanel 65 "ColorChooser.swatchesRecentSwatchSize", // NON-NLS: dimension key from DefaultSwatchChooserPanel 66//NotAvail: "ColorChooser.swatchesDefaultRecentColor", // NON-NLS: color key from DefaultSwatchChooserPanel 67 68 "ColorChooser.hsvNameText", // NON-NLS: string key from HSV ColorChooserPanel 69 "ColorChooser.hsvMnemonic", // NON-NLS: int key from HSV ColorChooserPanel 70 "ColorChooser.hsvHueText", // NON-NLS: string key from HSV ColorChooserPanel 71 "ColorChooser.hsvSaturationText", // NON-NLS: string key from HSV ColorChooserPanel 72 "ColorChooser.hsvValueText", // NON-NLS: string key from HSV ColorChooserPanel 73 "ColorChooser.hsvTransparencyText", // NON-NLS: string key from HSV ColorChooserPanel 74 75 "ColorChooser.hslNameText", // NON-NLS: string key from HSV ColorChooserPanel 76 "ColorChooser.hslMnemonic", // NON-NLS: int key from HSV ColorChooserPanel 77 "ColorChooser.hslHueText", // NON-NLS: string key from HSV ColorChooserPanel 78 "ColorChooser.hslSaturationText", // NON-NLS: string key from HSV ColorChooserPanel 79 "ColorChooser.hslLightnessText", // NON-NLS: string key from HSV ColorChooserPanel 80 "ColorChooser.hslTransparencyText", // NON-NLS: string key from HSV ColorChooserPanel 81 82 "ColorChooser.rgbNameText", // NON-NLS: string key from HSV ColorChooserPanel 83 "ColorChooser.rgbMnemonic", // NON-NLS: int key from HSV ColorChooserPanel 84 "ColorChooser.rgbRedText", // NON-NLS: string key from HSV ColorChooserPanel 85 "ColorChooser.rgbGreenText", // NON-NLS: string key from HSV ColorChooserPanel 86 "ColorChooser.rgbBlueText", // NON-NLS: string key from HSV ColorChooserPanel 87 "ColorChooser.rgbAlphaText", // NON-NLS: string key from HSV ColorChooserPanel 88 "ColorChooser.rgbHexCodeText", // NON-NLS: string key from HSV ColorChooserPanel 89 "ColorChooser.rgbHexCodeMnemonic", // NON-NLS: int key from HSV ColorChooserPanel 90 91 "ColorChooser.cmykNameText", // NON-NLS: string key from HSV ColorChooserPanel 92 "ColorChooser.cmykMnemonic", // NON-NLS: int key from HSV ColorChooserPanel 93 "ColorChooser.cmykCyanText", // NON-NLS: string key from HSV ColorChooserPanel 94 "ColorChooser.cmykMagentaText", // NON-NLS: string key from HSV ColorChooserPanel 95 "ColorChooser.cmykYellowText", // NON-NLS: string key from HSV ColorChooserPanel 96 "ColorChooser.cmykBlackText", // NON-NLS: string key from HSV ColorChooserPanel 97 "ColorChooser.cmykAlphaText", // NON-NLS: string key from HSV ColorChooserPanel 98 }; 99 private static final Object[] KOREAN = convert(Locale.KOREAN, KEYS); 100 private static final Object[] FRENCH = convert(Locale.FRENCH, KEYS); 101 102 public static void main(String[] args) { 103 Locale reservedLocale = Locale.getDefault(); 104 try { 105 // it affects Swing because it is not initialized 106 Locale.setDefault(Locale.KOREAN); 107 validate(KOREAN, create()); 108 109 // it does not affect Swing because it is initialized 110 Locale.setDefault(Locale.CANADA); 111 validate(KOREAN, create()); 112 113 // it definitely should affect Swing 114 JComponent.setDefaultLocale(Locale.FRENCH); 115 validate(FRENCH, create()); 116 } finally { 117 // restore the reserved locale 118 Locale.setDefault(reservedLocale); 119 } 120 } 121 122 private static void validate(Object[] expected, Object[] actual) { 123 int count = expected.length; 124 if (count != actual.length) { 125 throw new Error("different size: " + count + " <> " + actual.length); 126 } 127 for (int i = 0; i < count; i++) { 128 if (!expected[i].equals(actual[i])) { 129 throw new Error("unexpected value for key: " + KEYS[i]); 130 } 131 } 132 } 133 134 private static Object[] convert(Locale locale, String[] keys) { 135 int count = keys.length; 136 Object[] array = new Object[count]; 137 for (int i = 0; i < count; i++) { 138 array[i] = convert(locale, keys[i]); 139 } 140 return array; 141 } 142 143 private static Object convert(Locale locale, String key) { 144 if (key.endsWith("Text")) { // NON-NLS: suffix for text message 145 return UIManager.getString(key, locale); 146 } 147 if (key.endsWith("Size")) { // NON-NLS: suffix for dimension 148 return UIManager.getDimension(key, locale); 149 } 150 if (key.endsWith("Color")) { // NON-NLS: suffix for color 151 return UIManager.getColor(key, locale); 152 } 153 int value = SwingUtilities2.getUIDefaultsInt(key, locale, -1); 154 return Integer.valueOf(value); 155 } 156 157 private static Object[] create() { 158 JFrame frame = new JFrame(); 159 frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 160 frame.setVisible(true); 161 162 // show color chooser 163 JColorChooser chooser = new JColorChooser(); 164 JDialog dialog = JColorChooser.createDialog(frame, null, false, chooser, null, null); 165 dialog.setVisible(true); 166 167 // process all values 168 List<Object> list = new ArrayList<Object>(KEYS.length); 169 170 Component component = getC(getC(dialog.getLayeredPane(), 0), 1); 171 AbstractButton ok = (AbstractButton) getC(component, 0); 172 AbstractButton cancel = (AbstractButton) getC(component, 1); 173 AbstractButton reset = (AbstractButton) getC(component, 2); 174 list.add(ok.getText()); 175 list.add(cancel.getText()); 176 list.add(reset.getText()); 177 list.add(Integer.valueOf(reset.getMnemonic())); 178 179 for (int i = 0; i < 5; i++) { 180 AbstractColorChooserPanel panel = (AbstractColorChooserPanel) getC(getC(getC(chooser, 0), i), 0); 181 list.add(panel.getDisplayName()); 182 list.add(Integer.valueOf(panel.getMnemonic())); 183 if (i == 0) { 184 JLabel label = (JLabel) getC(getC(panel, 0), 1); 185 JPanel upper = (JPanel) getC(getC(getC(panel, 0), 0), 0); 186 JPanel lower = (JPanel) getC(getC(getC(panel, 0), 2), 0); 187 addSize(list, upper, 1, 1, 31, 9); 188 list.add(label.getText()); 189 addSize(list, lower, 1, 1, 5, 7); 190 } 191 else { 192 Component container = getC(panel, 0); 193 for (int j = 0; j < 3; j++) { 194 AbstractButton button = (AbstractButton) getC(container, j); 195 list.add(button.getText()); 196 } 197 JLabel label = (JLabel) getC(container, 3); 198 list.add(label.getText()); 199 if (i == 4) { 200 label = (JLabel) getC(container, 4); 201 list.add(label.getText()); 202 } 203 if (i == 3) { 204 label = (JLabel) getC(panel, 1); 205 list.add(label.getText()); 206 list.add(Integer.valueOf(label.getDisplayedMnemonic())); 207 } 208 } 209 } 210 211 // close dialog 212 dialog.setVisible(false); 213 dialog.dispose(); 214 215 // close frame 216 frame.setVisible(false); 217 frame.dispose(); 218 219 return list.toArray(); 220 } 221 222 private static void addSize(List<Object> list, Component component, int x, int y, int w, int h) { 223 Dimension size = component.getPreferredSize(); 224 int width = (size.width + 1) / w - x; 225 int height = (size.height + 1) / h - y; 226 list.add(new Dimension(width, height)); 227 } 228 229 private static Component getC(Component component, int index) { 230 Container container = (Container) component; 231 return container.getComponent(index); 232 } 233} 234