View Javadoc
1 /* 2 * $Header: /cvsroot/ebxmlrr/jaxr/src/com/sun/xml/registry/client/browser/LocaleSelectorDialog.java,v 1.5 2003/08/29 08:36:34 doballve Exp $ 3 * 4 * ==================================================================== 5 * 6 * This code is subject to the freebxml License, Version 1.1 7 * 8 * Copyright (c) 2003 freebxml.org. All rights reserved. 9 * 10 * ==================================================================== 11 */ 12 13 package com.sun.xml.registry.client.browser; 14 15 import java.awt.GridBagConstraints; 16 import java.awt.Insets; 17 import java.awt.event.ActionEvent; 18 import java.awt.event.ActionListener; 19 import java.util.Locale; 20 import javax.swing.JComboBox; 21 22 /*** 23 * A Dialog to allow selection of a Locale. The Locale can be selected from 24 * the available Locales or customized by selecting language and country. 25 * 26 * @author <a href="mailto:doballve@users.sorceforge.net">Diego Ballve</a> / Republica Corp. 27 */ 28 public class LocaleSelectorDialog extends JBDialog { 29 30 private JComboBox jcbAvailableLocales; 31 private JComboBox jcbCustomCountry; 32 private javax.swing.JLabel jlSelectLocale; 33 private javax.swing.JCheckBox jchbCountry; 34 private javax.swing.JCheckBox jchbVariant; 35 private javax.swing.JRadioButton jrbAvailableLocales; 36 private javax.swing.JRadioButton jrbCustom; 37 private JComboBox jcbCustomLanguage; 38 private javax.swing.JPanel jPanel1; 39 private javax.swing.ButtonGroup bgSelectionMode; 40 private javax.swing.JTextField jtfVariant; 41 42 /*** The selected Locale */ 43 private Locale selectedLocale = null; 44 45 /*** Creates new form LocaleSelectorDialog */ 46 public LocaleSelectorDialog(Locale initialLocale, javax.swing.JFrame parent, boolean modal) { 47 super(parent, modal); 48 LocaleSelectorDialog_initialize(initialLocale); 49 } 50 51 /*** Creates new form LocaleSelectorDialog */ 52 public LocaleSelectorDialog(Locale initialLocale, javax.swing.JDialog parent, boolean modal) { 53 super(parent, modal); 54 LocaleSelectorDialog_initialize(initialLocale); 55 } 56 57 private void LocaleSelectorDialog_initialize(Locale initialLocale) { 58 setEditable(true); 59 selectedLocale = initialLocale; 60 setTitle("Locale Selector Dialog"); 61 62 jPanel1 = getMainPanel(); 63 jPanel1.setLayout(new java.awt.GridBagLayout()); 64 65 GridBagConstraints gridBagConstraints; 66 67 bgSelectionMode = new javax.swing.ButtonGroup(); 68 jlSelectLocale = new javax.swing.JLabel(); 69 jrbAvailableLocales = new javax.swing.JRadioButton(); 70 jrbCustom = new javax.swing.JRadioButton(); 71 jcbAvailableLocales = new JComboBox(); 72 jcbCustomLanguage = new JComboBox(); 73 jcbCustomCountry = new JComboBox(); 74 jchbCountry = new javax.swing.JCheckBox(); 75 jchbVariant = new javax.swing.JCheckBox(); 76 jtfVariant = new javax.swing.JTextField(); 77 78 jlSelectLocale.setText("Select Locale : "); 79 gridBagConstraints = new GridBagConstraints(); 80 gridBagConstraints.gridy = 0; 81 gridBagConstraints.fill = GridBagConstraints.HORIZONTAL; 82 gridBagConstraints.insets = new Insets(4, 4, 0, 4); 83 jPanel1.add(jlSelectLocale, gridBagConstraints); 84 85 jrbAvailableLocales.setSelected(true); 86 jrbAvailableLocales.setText("Available Locales : "); 87 jrbAvailableLocales.setToolTipText("Select a Locale from the available Locales"); 88 bgSelectionMode.add(jrbAvailableLocales); 89 jrbAvailableLocales.addActionListener(new ActionListener() { 90 public void actionPerformed(ActionEvent evt) { 91 jrbAvailableLocalesActionPerformed(evt); 92 } 93 }); 94 95 gridBagConstraints = new GridBagConstraints(); 96 gridBagConstraints.gridy = 1; 97 gridBagConstraints.fill = GridBagConstraints.HORIZONTAL; 98 gridBagConstraints.insets = new Insets(4, 4, 0, 4); 99 jPanel1.add(jrbAvailableLocales, gridBagConstraints); 100 101 jrbCustom.setText("Custom Language:"); 102 jrbCustom.setToolTipText("Customize a Locale by selecting a language (and if needed, a country)"); 103 bgSelectionMode.add(jrbCustom); 104 jrbCustom.addActionListener(new ActionListener() { 105 public void actionPerformed(ActionEvent evt) { 106 jrbCustomActionPerformed(evt); 107 } 108 }); 109 110 gridBagConstraints = new GridBagConstraints(); 111 gridBagConstraints.gridy = 2; 112 gridBagConstraints.fill = GridBagConstraints.HORIZONTAL; 113 gridBagConstraints.insets = new Insets(4, 4, 0, 4); 114 jPanel1.add(jrbCustom, gridBagConstraints); 115 116 jcbAvailableLocales.setModel(getAvailableLocalesCBModel()); 117 //TO DO: fix case selectedLocale is not in Locale.getAvailableLocales() 118 jcbAvailableLocales.setSelectedItem(selectedLocale); 119 120 gridBagConstraints = new GridBagConstraints(); 121 gridBagConstraints.gridy = 1; 122 gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER; 123 gridBagConstraints.fill = GridBagConstraints.HORIZONTAL; 124 gridBagConstraints.insets = new Insets(4, 4, 0, 4); 125 jPanel1.add(jcbAvailableLocales, gridBagConstraints); 126 127 jcbCustomLanguage.setModel(getCustomLanguageCBModel()); 128 jcbCustomLanguage.setSelectedItem(selectedLocale.getLanguage()); 129 jcbCustomLanguage.setEnabled(false); 130 gridBagConstraints = new GridBagConstraints(); 131 gridBagConstraints.gridy = 2; 132 gridBagConstraints.fill = GridBagConstraints.HORIZONTAL; 133 gridBagConstraints.insets = new Insets(4, 4, 0, 4); 134 jPanel1.add(jcbCustomLanguage, gridBagConstraints); 135 136 jcbCustomCountry.setModel(getCustomCountryCBModel()); 137 jcbCustomCountry.setSelectedItem(selectedLocale.getCountry()); 138 jcbCustomCountry.setEnabled(false); 139 gridBagConstraints = new GridBagConstraints(); 140 gridBagConstraints.gridx = 1; 141 gridBagConstraints.gridy = 3; 142 gridBagConstraints.fill = GridBagConstraints.HORIZONTAL; 143 gridBagConstraints.insets = new Insets(4, 4, 0, 4); 144 jPanel1.add(jcbCustomCountry, gridBagConstraints); 145 146 jchbCountry.setText("Custom Country : "); 147 jchbCountry.setEnabled(false); 148 jchbCountry.addActionListener(new ActionListener() { 149 public void actionPerformed(ActionEvent evt) { 150 jchbCountryActionPerformed(evt); 151 } 152 }); 153 gridBagConstraints = new GridBagConstraints(); 154 gridBagConstraints.gridx = 0; 155 gridBagConstraints.gridy = 3; 156 gridBagConstraints.fill = GridBagConstraints.HORIZONTAL; 157 gridBagConstraints.insets = new Insets(4, 12, 0, 4); 158 jPanel1.add(jchbCountry, gridBagConstraints); 159 160 if (selectedLocale.getVariant() != null) { 161 jtfVariant.setText(selectedLocale.getVariant()); 162 } 163 jtfVariant.setEnabled(false); 164 gridBagConstraints = new GridBagConstraints(); 165 gridBagConstraints.gridx = 1; 166 gridBagConstraints.gridy = 4; 167 gridBagConstraints.fill = GridBagConstraints.HORIZONTAL; 168 gridBagConstraints.insets = new Insets(4, 4, 0, 4); 169 jPanel1.add(jtfVariant, gridBagConstraints); 170 171 jchbVariant.setText("Custom Variant : "); 172 jchbVariant.setEnabled(false); 173 jchbVariant.addActionListener(new ActionListener() { 174 public void actionPerformed(ActionEvent evt) { 175 jchbVariantActionPerformed(evt); 176 } 177 }); 178 gridBagConstraints = new GridBagConstraints(); 179 gridBagConstraints.gridx = 0; 180 gridBagConstraints.gridy = 4; 181 gridBagConstraints.fill = GridBagConstraints.HORIZONTAL; 182 gridBagConstraints.insets = new Insets(4, 12, 0, 4); 183 jPanel1.add(jchbVariant, gridBagConstraints); 184 185 186 pack(); 187 } 188 189 private void jchbCountryActionPerformed(ActionEvent evt) { 190 jcbCustomCountry.setEnabled(jchbCountry.isSelected()); 191 jchbVariant.setEnabled(jchbCountry.isSelected()); 192 jtfVariant.setEnabled(jchbCountry.isSelected() && jchbVariant.isSelected()); 193 } 194 195 private void jchbVariantActionPerformed(ActionEvent evt) { 196 jtfVariant.setEnabled(jchbVariant.isSelected()); 197 } 198 199 private void jrbCustomActionPerformed(ActionEvent evt) { 200 jcbAvailableLocales.setEnabled(false); 201 jcbCustomLanguage.setEnabled(true); 202 jchbCountry.setEnabled(true); 203 jcbCustomCountry.setEnabled(jchbCountry.isSelected()); 204 jchbVariant.setEnabled(jchbCountry.isSelected()); 205 jtfVariant.setEnabled(jchbVariant.isSelected()); 206 } 207 208 private void jrbAvailableLocalesActionPerformed(ActionEvent evt) { 209 jcbAvailableLocales.setEnabled(true); 210 jcbCustomLanguage.setEnabled(false); 211 jcbCustomCountry.setEnabled(false); 212 jchbCountry.setEnabled(false); 213 jchbVariant.setEnabled(false); 214 jtfVariant.setEnabled(false); 215 } 216 217 private javax.swing.ComboBoxModel getAvailableLocalesCBModel() { 218 return new javax.swing.DefaultComboBoxModel(Locale.getAvailableLocales()); 219 } 220 221 private javax.swing.ComboBoxModel getCustomLanguageCBModel() { 222 return new javax.swing.DefaultComboBoxModel(Locale.getISOLanguages()); 223 } 224 225 private javax.swing.ComboBoxModel getCustomCountryCBModel() { 226 return new javax.swing.DefaultComboBoxModel(Locale.getISOCountries()); 227 } 228 229 protected void okAction() { 230 super.okAction(); 231 if (jrbAvailableLocales.isSelected()) { 232 selectedLocale = (Locale)jcbAvailableLocales.getSelectedItem(); 233 } else if (!jchbCountry.isSelected()) { 234 selectedLocale = new Locale((String)jcbCustomLanguage.getSelectedItem()); 235 } else if (!jchbVariant.isSelected() || jtfVariant.getText().trim().length() == 0) { 236 selectedLocale = new Locale((String)jcbCustomLanguage.getSelectedItem(), 237 (String)jcbCustomCountry.getSelectedItem()); 238 } else { 239 selectedLocale = new Locale((String)jcbCustomLanguage.getSelectedItem(), 240 (String)jcbCustomCountry.getSelectedItem(), 241 jtfVariant.getText()); 242 } 243 Locale.setDefault(selectedLocale); 244 dispose(); 245 } 246 247 /*** 248 * Getter for property selectedLocale. 249 * 250 * @return Locale the selected Locale. 251 */ 252 public Locale getSelectedLocale() { 253 return selectedLocale; 254 } 255 256 /*** 257 * @param args the command line arguments 258 */ 259 public static void main(String args[]) { 260 javax.swing.JFrame frame = new javax.swing.JFrame(); 261 LocaleSelectorDialog localeSelector = 262 new LocaleSelectorDialog(Locale.getDefault(), frame, true); 263 localeSelector.show(); 264 System.out.println("Selected Locale was: " 265 + localeSelector.getSelectedLocale()); 266 frame.dispose(); 267 } 268 269 }

This page was automatically generated by Maven