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