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