1 19 20 21 package org.netbeans.modules.properties; 22 23 24 import java.awt.Component ; 25 import java.awt.Dialog ; 26 import java.awt.event.ActionEvent ; 27 import java.awt.event.ActionListener ; 28 import java.io.IOException ; 29 import java.util.ArrayList ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Locale ; 33 import javax.swing.DefaultListCellRenderer ; 34 import javax.swing.Icon ; 35 import javax.swing.ImageIcon ; 36 import javax.swing.JLabel ; 37 import javax.swing.JList ; 38 import javax.swing.JPanel ; 39 import javax.swing.SwingUtilities ; 40 import org.openide.DialogDescriptor; 41 import org.openide.DialogDisplayer; 42 import org.openide.util.HelpCtx; 43 import org.openide.util.NbBundle; 44 import org.openide.util.RequestProcessor; 45 import org.openide.util.Utilities; 46 47 48 54 public class LocaleNodeCustomizer extends JPanel { 55 56 57 private PropertiesFileEntry entry; 58 59 60 public LocaleNodeCustomizer(PropertiesFileEntry entry) { 61 this.entry = entry; 62 63 initComponents(); 64 initAccessibility(); 65 66 Locale locale = getLocale(entry); 67 68 if(new Locale ("", "").equals(locale)) { changeNameButton.setEnabled(false); 70 nameText.setText(NbBundle.getBundle(LocaleNodeCustomizer.class).getString("LAB_defaultLanguage")); } else 72 nameText.setText(locale.toString()); 73 74 removeKeyButton.setEnabled(false); 75 76 HelpCtx.setHelpIDString(this, Util.HELP_ID_EDITLOCALE); 77 } 78 79 80 private void updateName(Locale locale) { 81 if(locale.equals(new Locale ("", "")) || locale.equals(getLocale(entry)) ) 83 return; 84 85 String newName = Util.assembleName( 86 entry.getDataObject().getPrimaryFile().getName(), 87 locale.toString() 88 ); 89 90 entry.getNodeDelegate().setName(newName); 91 92 nameText.setText(locale.toString()); 93 } 94 95 96 private static Icon getKeyIcon() { 97 return new ImageIcon (Utilities.loadImage("org/netbeans/modules/properties/propertiesKey.gif")); } 99 100 102 static Locale getLocale(PropertiesFileEntry entry) { 103 String localeSuffix = Util.getLocaleSuffix(entry); 104 String languageCode = Util.getLanguage(localeSuffix); 105 106 if(languageCode == null) 107 return new Locale ("", ""); 109 String countryCode = Util.getCountry(localeSuffix); 110 111 if(countryCode == null) 112 return new Locale (languageCode, ""); 114 String variant = Util.getVariant(localeSuffix); 115 116 if(variant == null) 117 return new Locale (languageCode, countryCode); 118 119 return new Locale (languageCode, countryCode, variant); 120 } 121 122 124 private static String [] retrieveKeys(PropertiesFileEntry entry) { 125 List <String > keysList = new ArrayList <String >(); 126 127 if(entry == null) { 128 return new String [0]; 129 } 130 131 for (Iterator <Element.ItemElem> it = entry.getHandler().getStructure().allItems(); it.hasNext(); ) { 132 String key = it.next().getKey(); 133 if (key != null && !(keysList.contains(key))) { 134 keysList.add(key); 135 } 136 } 137 138 String [] keys = new String [keysList.size()]; 139 keysList.toArray(keys); 140 return keys; 141 } 142 143 144 private void initAccessibility() { 145 this.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(LocaleNodeCustomizer.class).getString("ACS_LocaleNodeCustomizer")); 146 147 nameText.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(LocaleNodeCustomizer.class).getString("ACS_CTL_NameText")); 148 addKeyButton.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(LocaleNodeCustomizer.class).getString("ACS_CTL_AddKey")); 149 changeNameButton.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(LocaleNodeCustomizer.class).getString("ACS_CTL_ChangeNameButton")); 150 removeKeyButton.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(LocaleNodeCustomizer.class).getString("ACS_CTL_RemoveKey")); 151 keyList.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(LocaleNodeCustomizer.class).getString("ACS_CTL_KeyList")); 152 } 153 154 159 private void initComponents() { 161 java.awt.GridBagConstraints gridBagConstraints; 162 163 nameLabel = new javax.swing.JLabel (); 164 keyLabel = new javax.swing.JLabel (); 165 jScrollPane1 = new javax.swing.JScrollPane (); 166 keyList = new JList (retrieveKeys(entry)); 167 addKeyButton = new javax.swing.JButton (); 168 removeKeyButton = new javax.swing.JButton (); 169 jPanel1 = new javax.swing.JPanel (); 170 nameText = new javax.swing.JTextField (); 171 changeNameButton = new javax.swing.JButton (); 172 173 setLayout(new java.awt.GridBagLayout ()); 174 175 nameLabel.setLabelFor(nameText); 176 org.openide.awt.Mnemonics.setLocalizedText(nameLabel, NbBundle.getBundle(LocaleNodeCustomizer.class).getString("LBL_Name")); gridBagConstraints = new java.awt.GridBagConstraints (); 178 gridBagConstraints.gridx = 0; 179 gridBagConstraints.gridy = 0; 180 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 181 gridBagConstraints.insets = new java.awt.Insets (12, 12, 0, 0); 182 add(nameLabel, gridBagConstraints); 183 184 keyLabel.setLabelFor(keyList); 185 org.openide.awt.Mnemonics.setLocalizedText(keyLabel, NbBundle.getBundle(LocaleNodeCustomizer.class).getString("LBL_Keys")); gridBagConstraints = new java.awt.GridBagConstraints (); 187 gridBagConstraints.gridx = 0; 188 gridBagConstraints.gridy = 1; 189 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 190 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 191 gridBagConstraints.insets = new java.awt.Insets (11, 12, 0, 0); 192 add(keyLabel, gridBagConstraints); 193 194 keyList.setCellRenderer(new DefaultListCellRenderer () { 195 public Component getListCellRendererComponent( 196 JList list, 197 Object value, int index, boolean isSelected, boolean cellHasFocus) { 202 JLabel label = (JLabel )super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); 203 204 label.setText(value.toString()); 205 206 label.setIcon(getKeyIcon()); 207 208 return label; 209 } 210 }); 211 keyList.setPrototypeCellValue("0123456789012345678901234567890123456789"); 212 keyList.addListSelectionListener(new javax.swing.event.ListSelectionListener () { 213 public void valueChanged(javax.swing.event.ListSelectionEvent evt) { 214 keyListValueChanged(evt); 215 } 216 }); 217 jScrollPane1.setViewportView(keyList); 218 219 gridBagConstraints = new java.awt.GridBagConstraints (); 220 gridBagConstraints.gridx = 1; 221 gridBagConstraints.gridy = 1; 222 gridBagConstraints.gridheight = 2; 223 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 224 gridBagConstraints.weightx = 1.0; 225 gridBagConstraints.weighty = 1.0; 226 gridBagConstraints.insets = new java.awt.Insets (11, 11, 5, 0); 227 add(jScrollPane1, gridBagConstraints); 228 229 org.openide.awt.Mnemonics.setLocalizedText(addKeyButton, NbBundle.getBundle(LocaleNodeCustomizer.class).getString("CTL_AddKey")); addKeyButton.addActionListener(new java.awt.event.ActionListener () { 231 public void actionPerformed(java.awt.event.ActionEvent evt) { 232 addKeyButtonActionPerformed(evt); 233 } 234 }); 235 gridBagConstraints = new java.awt.GridBagConstraints (); 236 gridBagConstraints.gridx = 2; 237 gridBagConstraints.gridy = 1; 238 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 239 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST; 240 gridBagConstraints.insets = new java.awt.Insets (11, 11, 0, 11); 241 add(addKeyButton, gridBagConstraints); 242 243 org.openide.awt.Mnemonics.setLocalizedText(removeKeyButton, NbBundle.getBundle(LocaleNodeCustomizer.class).getString("CTL_RemoveKey")); removeKeyButton.addActionListener(new java.awt.event.ActionListener () { 245 public void actionPerformed(java.awt.event.ActionEvent evt) { 246 removeKeyButtonActionPerformed(evt); 247 } 248 }); 249 gridBagConstraints = new java.awt.GridBagConstraints (); 250 gridBagConstraints.gridx = 2; 251 gridBagConstraints.gridy = 2; 252 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 253 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST; 254 gridBagConstraints.insets = new java.awt.Insets (5, 11, 5, 11); 255 add(removeKeyButton, gridBagConstraints); 256 257 jPanel1.setLayout(new java.awt.GridBagLayout ()); 258 259 nameText.setEditable(false); 260 nameText.selectAll(); 261 nameText.addFocusListener(new java.awt.event.FocusAdapter () { 262 public void focusGained(java.awt.event.FocusEvent evt) { 263 nameTextFocusGained(evt); 264 } 265 }); 266 gridBagConstraints = new java.awt.GridBagConstraints (); 267 gridBagConstraints.gridx = 0; 268 gridBagConstraints.gridy = 0; 269 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 270 gridBagConstraints.weightx = 1.0; 271 jPanel1.add(nameText, gridBagConstraints); 272 273 changeNameButton.setText("..."); 274 changeNameButton.addActionListener(new java.awt.event.ActionListener () { 275 public void actionPerformed(java.awt.event.ActionEvent evt) { 276 changeNameButtonActionPerformed(evt); 277 } 278 }); 279 gridBagConstraints = new java.awt.GridBagConstraints (); 280 gridBagConstraints.gridx = 1; 281 gridBagConstraints.gridy = 0; 282 gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; 283 gridBagConstraints.insets = new java.awt.Insets (0, 5, 0, 0); 284 jPanel1.add(changeNameButton, gridBagConstraints); 285 286 gridBagConstraints = new java.awt.GridBagConstraints (); 287 gridBagConstraints.gridwidth = 2; 288 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 289 gridBagConstraints.insets = new java.awt.Insets (12, 12, 0, 11); 290 add(jPanel1, gridBagConstraints); 291 } 293 private void nameTextFocusGained(java.awt.event.FocusEvent evt) { nameText.selectAll(); 296 } 298 private void changeNameButtonActionPerformed(java.awt.event.ActionEvent evt) { final Dialog [] dialog = new Dialog [1]; 300 final LocalePanel panel = new LocalePanel(getLocale(entry)); 301 302 DialogDescriptor dd = new DialogDescriptor( 303 panel, 304 NbBundle.getBundle(PropertiesDataNode.class).getString("CTL_NewLocaleTitle"), 305 true, 306 DialogDescriptor.OK_CANCEL_OPTION, 307 DialogDescriptor.OK_OPTION, 308 new ActionListener () { 309 public void actionPerformed(ActionEvent evt2) { 310 if (evt2.getSource() == DialogDescriptor.OK_OPTION) { 312 dialog[0].setVisible(false); 313 dialog[0].dispose(); 314 315 updateName(panel.getLocale()); 316 } else if (evt2.getSource() == DialogDescriptor.CANCEL_OPTION) { 318 dialog[0].setVisible(false); 319 dialog[0].dispose(); 320 } 321 } 322 } 323 ); 324 dialog[0] = DialogDisplayer.getDefault().createDialog(dd); 325 dialog[0].setVisible(true); 326 } 328 private void keyListValueChanged(javax.swing.event.ListSelectionEvent evt) { if(keyList.isSelectionEmpty()) 330 removeKeyButton.setEnabled(false); 331 else 332 removeKeyButton.setEnabled(true); 333 } 335 private void removeKeyButtonActionPerformed(java.awt.event.ActionEvent evt) { Object [] selectedValues = keyList.getSelectedValues(); 337 338 PropertiesStructure ps = entry.getHandler().getStructure(); 339 340 for(int i=0; i<selectedValues.length; i++) { 341 ps.deleteItem((String )selectedValues[i]); 342 } 343 344 updateKeyList(); 345 } 347 private void addKeyButtonActionPerformed(java.awt.event.ActionEvent evt) { try { 349 entry.getNodeDelegate().getNewTypes()[0].create(); 350 351 updateKeyList(); 352 } catch(IOException ioe) { 353 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ioe); 354 } 355 } 357 358 private void updateKeyList() { 359 PropertiesRequestProcessor.getInstance().post(new Runnable () { 362 public void run() { 363 SwingUtilities.invokeLater(new Runnable () { 364 public void run() { 365 keyList.setListData(retrieveKeys(entry)); 367 } 368 }); 369 } 370 }); 371 } 372 373 374 private javax.swing.JButton addKeyButton; 376 private javax.swing.JButton changeNameButton; 377 private javax.swing.JPanel jPanel1; 378 private javax.swing.JScrollPane jScrollPane1; 379 private javax.swing.JLabel keyLabel; 380 private javax.swing.JList keyList; 381 private javax.swing.JLabel nameLabel; 382 private javax.swing.JTextField nameText; 383 private javax.swing.JButton removeKeyButton; 384 386 } 387 | Popular Tags |