J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any 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.JButton; |
| 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.JRadioButton; |
| 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.swatchesDisplayedMnemonicIndex", // NON-NLS: int key from DefaultSwatchChooserPanel |
| 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.hsbNameText", // NON-NLS: string key from DefaultHSBChooserPanel |
| 69 | "ColorChooser.hsbMnemonic", // NON-NLS: int key from DefaultHSBChooserPanel |
| 70 | "ColorChooser.hsbDisplayedMnemonicIndex", // NON-NLS: int key from DefaultHSBChooserPanel |
| 71 | "ColorChooser.hsbHueText", // NON-NLS: string key from DefaultHSBChooserPanel |
| 72 | "ColorChooser.hsbSaturationText", // NON-NLS: string key from DefaultHSBChooserPanel |
| 73 | "ColorChooser.hsbBrightnessText", // NON-NLS: string key from DefaultHSBChooserPanel |
| 74 | "ColorChooser.hsbRedText", // NON-NLS: string key from DefaultHSBChooserPanel |
| 75 | "ColorChooser.hsbGreenText", // NON-NLS: string key from DefaultHSBChooserPanel |
| 76 | "ColorChooser.hsbBlueText", // NON-NLS: string key from DefaultHSBChooserPanel |
| 77 | |
| 78 | "ColorChooser.rgbNameText", // NON-NLS: string key from DefaultRGBChooserPanel |
| 79 | "ColorChooser.rgbMnemonic", // NON-NLS: int key from DefaultRGBChooserPanel |
| 80 | "ColorChooser.rgbDisplayedMnemonicIndex", // NON-NLS: int key from DefaultRGBChooserPanel |
| 81 | "ColorChooser.rgbRedText", // NON-NLS: string key from DefaultRGBChooserPanel |
| 82 | "ColorChooser.rgbGreenText", // NON-NLS: string key from DefaultRGBChooserPanel |
| 83 | "ColorChooser.rgbBlueText", // NON-NLS: string key from DefaultRGBChooserPanel |
| 84 | "ColorChooser.rgbRedMnemonic", // NON-NLS: int key from DefaultRGBChooserPanel |
| 85 | "ColorChooser.rgbGreenMnemonic", // NON-NLS: int key from DefaultRGBChooserPanel |
| 86 | "ColorChooser.rgbBlueMnemonic", // NON-NLS: int key from DefaultRGBChooserPanel |
| 87 | }; |
| 88 | private static final Object[] KOREAN = convert(Locale.KOREAN, KEYS); |
| 89 | private static final Object[] FRENCH = convert(Locale.FRENCH, KEYS); |
| 90 | |
| 91 | public static void main(String[] args) { |
| 92 | // it affects Swing because it is not initialized |
| 93 | Locale.setDefault(Locale.KOREAN); |
| 94 | Object[] korean = create(); |
| 95 | |
| 96 | // it does not affect Swing because it is initialized |
| 97 | Locale.setDefault(Locale.CANADA); |
| 98 | Object[] canada = create(); |
| 99 | |
| 100 | // it definitely should affect Swing |
| 101 | JComponent.setDefaultLocale(Locale.FRENCH); |
| 102 | Object[] french = create(); |
| 103 | |
| 104 | validate(KOREAN, korean); |
| 105 | validate(KOREAN, canada); |
| 106 | validate(FRENCH, french); |
| 107 | } |
| 108 | |
| 109 | private static void validate(Object[] expected, Object[] actual) { |
| 110 | int count = expected.length; |
| 111 | if (count != actual.length) { |
| 112 | throw new Error("different size: " + count + " <> " + actual.length); |
| 113 | } |
| 114 | for (int i = 0; i < count; i++) { |
| 115 | if (!expected[i].equals(actual[i])) { |
| 116 | throw new Error("unexpected value for key: " + KEYS[i]); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | private static Object[] convert(Locale locale, String[] keys) { |
| 122 | int count = keys.length; |
| 123 | Object[] array = new Object[count]; |
| 124 | for (int i = 0; i < count; i++) { |
| 125 | array[i] = convert(locale, keys[i]); |
| 126 | } |
| 127 | return array; |
| 128 | } |
| 129 | |
| 130 | private static Object convert(Locale locale, String key) { |
| 131 | if (key.endsWith("Text")) { // NON-NLS: suffix for text message |
| 132 | return UIManager.getString(key, locale); |
| 133 | } |
| 134 | if (key.endsWith("Size")) { // NON-NLS: suffix for dimension |
| 135 | return UIManager.getDimension(key, locale); |
| 136 | } |
| 137 | if (key.endsWith("Color")) { // NON-NLS: suffix for color |
| 138 | return UIManager.getColor(key, locale); |
| 139 | } |
| 140 | int value = SwingUtilities2.getUIDefaultsInt(key, locale, -1); |
| 141 | return Integer.valueOf(value); |
| 142 | } |
| 143 | |
| 144 | private static Object[] create() { |
| 145 | JFrame frame = new JFrame(); |
| 146 | frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); |
| 147 | frame.setVisible(true); |
| 148 | |
| 149 | // show color chooser |
| 150 | JColorChooser chooser = new JColorChooser(); |
| 151 | JDialog dialog = JColorChooser.createDialog(frame, null, false, chooser, null, null); |
| 152 | dialog.setVisible(true); |
| 153 | |
| 154 | // process all values |
| 155 | List<Object> list = new ArrayList<Object>(KEYS.length); |
| 156 | addMain(list, dialog); |
| 157 | addSwatch(list, chooser); |
| 158 | addHSB(list, chooser); |
| 159 | addRGB(list, chooser); |
| 160 | |
| 161 | // close dialog |
| 162 | dialog.setVisible(false); |
| 163 | dialog.dispose(); |
| 164 | |
| 165 | // close frame |
| 166 | frame.setVisible(false); |
| 167 | frame.dispose(); |
| 168 | |
| 169 | return list.toArray(); |
| 170 | } |
| 171 | |
| 172 | private static void addMain(List<Object> list, JDialog dialog) { |
| 173 | Component component = getC(getC(dialog.getLayeredPane(), 0), 1); |
| 174 | JButton ok = (JButton) getC(component, 0); |
| 175 | JButton cancel = (JButton) getC(component, 1); |
| 176 | JButton reset = (JButton) getC(component, 2); |
| 177 | list.add(ok.getText()); |
| 178 | list.add(cancel.getText()); |
| 179 | list.add(reset.getText()); |
| 180 | list.add(Integer.valueOf(reset.getMnemonic())); |
| 181 | } |
| 182 | |
| 183 | private static void addSwatch(List<Object> list, JColorChooser chooser) { |
| 184 | Component component = addPanel(list, chooser, 0); |
| 185 | JLabel label = (JLabel) getC(getC(component, 0), 1); |
| 186 | JPanel upper = (JPanel) getC(getC(getC(component, 0), 0), 0); |
| 187 | JPanel lower = (JPanel) getC(getC(getC(component, 0), 2), 0); |
| 188 | addSize(list, upper, 1, 1, 31, 9); |
| 189 | list.add(label.getText()); |
| 190 | addSize(list, lower, 1, 1, 5, 7); |
| 191 | } |
| 192 | |
| 193 | private static void addHSB(List<Object> list, JColorChooser chooser) { |
| 194 | Component component = addPanel(list, chooser, 1); |
| 195 | JRadioButton h = (JRadioButton) getC(getC(getC(component, 1), 0), 0); |
| 196 | JRadioButton s = (JRadioButton) getC(getC(getC(component, 1), 0), 2); |
| 197 | JRadioButton b = (JRadioButton) getC(getC(getC(component, 1), 0), 4); |
| 198 | list.add(h.getText()); |
| 199 | list.add(s.getText()); |
| 200 | list.add(b.getText()); |
| 201 | JLabel red = (JLabel) getC(getC(getC(component, 1), 2), 0); |
| 202 | JLabel green = (JLabel) getC(getC(getC(component, 1), 2), 2); |
| 203 | JLabel blue = (JLabel) getC(getC(getC(component, 1), 2), 4); |
| 204 | list.add(red.getText()); |
| 205 | list.add(green.getText()); |
| 206 | list.add(blue.getText()); |
| 207 | } |
| 208 | |
| 209 | private static void addRGB(List<Object> list, JColorChooser chooser) { |
| 210 | Component component = addPanel(list, chooser, 2); |
| 211 | JLabel red = (JLabel) getC(getC(component, 0), 0); |
| 212 | JLabel green = (JLabel) getC(getC(component, 0), 3); |
| 213 | JLabel blue = (JLabel) getC(getC(component, 0), 6); |
| 214 | list.add(red.getText()); |
| 215 | list.add(green.getText()); |
| 216 | list.add(blue.getText()); |
| 217 | list.add(Integer.valueOf(red.getDisplayedMnemonic())); |
| 218 | list.add(Integer.valueOf(green.getDisplayedMnemonic())); |
| 219 | list.add(Integer.valueOf(blue.getDisplayedMnemonic())); |
| 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 addPanel(List<Object> list, JColorChooser chooser, int index) { |
| 230 | AbstractColorChooserPanel panel = (AbstractColorChooserPanel) getC(getC(getC(chooser, 0), index), 0); |
| 231 | list.add(panel.getDisplayName()); |
| 232 | list.add(Integer.valueOf(panel.getMnemonic())); |
| 233 | list.add(Integer.valueOf(panel.getDisplayedMnemonicIndex())); |
| 234 | return panel; |
| 235 | } |
| 236 | |
| 237 | private static Component getC(Component component, int index) { |
| 238 | Container container = (Container) component; |
| 239 | return container.getComponent(index); |
| 240 | } |
| 241 | } |