1 19 20 21 package org.netbeans.modules.properties; 22 23 24 import java.awt.Component ; 25 import java.io.IOException ; 26 import java.util.ArrayList ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Locale ; 30 import javax.swing.DefaultListCellRenderer ; 31 import javax.swing.JLabel ; 32 import javax.swing.JList ; 33 import javax.swing.JPanel ; 34 import javax.swing.Icon ; 35 import javax.swing.ImageIcon ; 36 import org.openide.util.HelpCtx; 37 import org.openide.util.NbBundle; 38 import org.openide.util.Utilities; 39 40 41 49 public class BundleNodeCustomizer extends JPanel { 50 51 52 private PropertiesDataObject propDataObject; 53 54 55 public BundleNodeCustomizer(PropertiesDataObject propDataObject) { 56 this.propDataObject = propDataObject; 57 58 initComponents(); 59 initAccessibility(); 60 61 nameText.setText(propDataObject.getNodeDelegate().getName()); 62 63 localesList.setListData(retrieveLocales(propDataObject)); 64 65 removeLocales.setEnabled(false); 66 67 HelpCtx.setHelpIDString(this, Util.HELP_ID_ADDLOCALE); 68 } 69 70 71 private static Icon getLocaleIcon() { 72 return new ImageIcon (Utilities.loadImage("org/netbeans/modules/properties/propertiesLocale.gif")); } 74 75 77 private static Locale [] retrieveLocales(PropertiesDataObject propDataObject) { 78 List <Locale > entryList = new ArrayList <Locale >(); 79 80 entryList.add(LocaleNodeCustomizer.getLocale((PropertiesFileEntry)propDataObject.getPrimaryEntry())); 81 82 for (Iterator it = propDataObject.secondaryEntries().iterator(); it.hasNext(); ) { 83 entryList.add(LocaleNodeCustomizer.getLocale((PropertiesFileEntry)it.next())); 84 } 85 86 Locale [] entryLocales = new Locale [entryList.size()]; 87 entryList.toArray(entryLocales); 88 89 return entryLocales; 90 } 91 92 93 private void initAccessibility() { 94 this.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(LocaleNodeCustomizer.class).getString("ACS_BundleNodeCustomizer")); 95 96 nameText.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(LocaleNodeCustomizer.class).getString("ACS_CTL_NameText")); 97 localesList.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(LocaleNodeCustomizer.class).getString("ACS_CTL_LocalesList")); 98 addLocale.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(LocaleNodeCustomizer.class).getString("ACS_CTL_AddLocale")); 99 removeLocales.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(LocaleNodeCustomizer.class).getString("ACS_CTL_RemoveLocale")); 100 } 101 102 107 private void initComponents() { 109 java.awt.GridBagConstraints gridBagConstraints; 110 111 nameLabel = new javax.swing.JLabel (); 112 nameText = new javax.swing.JTextField (); 113 localesLabel = new javax.swing.JLabel (); 114 jScrollPane1 = new javax.swing.JScrollPane (); 115 localesList = new javax.swing.JList (); 116 addLocale = new javax.swing.JButton (); 117 removeLocales = new javax.swing.JButton (); 118 119 setLayout(new java.awt.GridBagLayout ()); 120 121 nameLabel.setLabelFor(nameText); 122 org.openide.awt.Mnemonics.setLocalizedText(nameLabel, NbBundle.getBundle(LocaleNodeCustomizer.class).getString("LBL_Name")); gridBagConstraints = new java.awt.GridBagConstraints (); 124 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 125 gridBagConstraints.insets = new java.awt.Insets (12, 12, 0, 0); 126 add(nameLabel, gridBagConstraints); 127 128 nameText.addActionListener(new java.awt.event.ActionListener () { 129 public void actionPerformed(java.awt.event.ActionEvent evt) { 130 nameTextActionPerformed(evt); 131 } 132 }); 133 nameText.addFocusListener(new java.awt.event.FocusAdapter () { 134 public void focusLost(java.awt.event.FocusEvent evt) { 135 nameTextFocusLost(evt); 136 } 137 }); 138 gridBagConstraints = new java.awt.GridBagConstraints (); 139 gridBagConstraints.gridwidth = 2; 140 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 141 gridBagConstraints.weightx = 1.0; 142 gridBagConstraints.insets = new java.awt.Insets (12, 12, 0, 11); 143 add(nameText, gridBagConstraints); 144 145 localesLabel.setLabelFor(localesList); 146 org.openide.awt.Mnemonics.setLocalizedText(localesLabel, NbBundle.getBundle(LocaleNodeCustomizer.class).getString("LBL_Locales")); gridBagConstraints = new java.awt.GridBagConstraints (); 148 gridBagConstraints.gridx = 0; 149 gridBagConstraints.gridy = 1; 150 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 151 gridBagConstraints.insets = new java.awt.Insets (11, 12, 0, 0); 152 add(localesLabel, gridBagConstraints); 153 154 localesList.setCellRenderer(new DefaultListCellRenderer () { 155 public Component getListCellRendererComponent( 156 JList list, 157 Object value, int index, boolean isSelected, boolean cellHasFocus) { 162 JLabel label = (JLabel )super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); 163 164 if(value instanceof String ) 165 return label; 166 167 Locale locale = (Locale )value; 168 169 if(locale.equals(new Locale ("", ""))) label.setText(NbBundle.getBundle(BundleNodeCustomizer.class).getString("LAB_defaultLanguage")); else { 172 label.setText(locale.toString() + 173 (locale.getLanguage().equals("") ? "" : " - " + locale.getDisplayLanguage()) + (locale.getCountry().equals("") ? "" : " / " + locale.getDisplayCountry()) + (locale.getVariant().equals("") ? "" : " / " + locale.getDisplayVariant()) ); 177 } 178 179 label.setIcon(getLocaleIcon()); 180 181 return label; 182 } 183 }); 184 localesList.setPrototypeCellValue("0123456789012345678901234567890123456789"); 185 localesList.addListSelectionListener(new javax.swing.event.ListSelectionListener () { 186 public void valueChanged(javax.swing.event.ListSelectionEvent evt) { 187 localesListValueChanged(evt); 188 } 189 }); 190 jScrollPane1.setViewportView(localesList); 191 192 gridBagConstraints = new java.awt.GridBagConstraints (); 193 gridBagConstraints.gridx = 1; 194 gridBagConstraints.gridy = 1; 195 gridBagConstraints.gridheight = 2; 196 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 197 gridBagConstraints.weightx = 1.0; 198 gridBagConstraints.weighty = 1.0; 199 gridBagConstraints.insets = new java.awt.Insets (11, 12, 5, 0); 200 add(jScrollPane1, gridBagConstraints); 201 202 org.openide.awt.Mnemonics.setLocalizedText(addLocale, NbBundle.getBundle(LocaleNodeCustomizer.class).getString("CTL_AddLocale")); addLocale.addActionListener(new java.awt.event.ActionListener () { 204 public void actionPerformed(java.awt.event.ActionEvent evt) { 205 addLocaleActionPerformed(evt); 206 } 207 }); 208 gridBagConstraints = new java.awt.GridBagConstraints (); 209 gridBagConstraints.gridx = 2; 210 gridBagConstraints.gridy = 1; 211 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 212 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; 213 gridBagConstraints.insets = new java.awt.Insets (11, 11, 0, 11); 214 add(addLocale, gridBagConstraints); 215 216 org.openide.awt.Mnemonics.setLocalizedText(removeLocales, NbBundle.getBundle(LocaleNodeCustomizer.class).getString("CTL_RemoveLocale")); removeLocales.addActionListener(new java.awt.event.ActionListener () { 218 public void actionPerformed(java.awt.event.ActionEvent evt) { 219 removeLocalesActionPerformed(evt); 220 } 221 }); 222 gridBagConstraints = new java.awt.GridBagConstraints (); 223 gridBagConstraints.gridx = 2; 224 gridBagConstraints.gridy = 2; 225 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 226 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; 227 gridBagConstraints.weighty = 1.0; 228 gridBagConstraints.insets = new java.awt.Insets (5, 11, 5, 11); 229 add(removeLocales, gridBagConstraints); 230 } 232 private void localesListValueChanged(javax.swing.event.ListSelectionEvent evt) { if(localesList.isSelectionEmpty() 234 || new Locale ("", "").equals(localesList.getSelectedValue())) 235 236 removeLocales.setEnabled(false); 237 else 238 removeLocales.setEnabled(true); 239 } 241 private void removeLocalesActionPerformed(java.awt.event.ActionEvent evt) { Object [] selectedValues = localesList.getSelectedValues(); 243 244 String basicName = propDataObject.getPrimaryFile().getName(); 245 246 for(int i=0; i<selectedValues.length; i++) { 247 PropertiesFileEntry entry = propDataObject.getBundleStructure().getEntryByFileName(basicName + PropertiesDataLoader.PRB_SEPARATOR_CHAR + selectedValues[i].toString()); 248 try { 249 entry.delete(); 250 } catch(IOException ioe) { 251 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ioe); 252 } 253 } 254 255 localesList.setListData(retrieveLocales(propDataObject)); 256 } 258 private void addLocaleActionPerformed(java.awt.event.ActionEvent evt) { try { 260 propDataObject.getNodeDelegate().getNewTypes()[0].create(); 261 } catch(IOException ioe) { 262 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ioe); 263 } 264 265 localesList.setListData(retrieveLocales(propDataObject)); 266 } 268 private void nameTextFocusLost(java.awt.event.FocusEvent evt) { nameTextHandler(); 270 } 272 private void nameTextActionPerformed(java.awt.event.ActionEvent evt) { nameTextHandler(); 274 } 276 277 private void nameTextHandler() { 278 String newName = nameText.getText(); 279 280 if(newName == null || "".equals(newName)) return; 282 283 propDataObject.getNodeDelegate().setName(newName); 284 } 285 286 private javax.swing.JButton addLocale; 288 private javax.swing.JScrollPane jScrollPane1; 289 private javax.swing.JLabel localesLabel; 290 private javax.swing.JList localesList; 291 private javax.swing.JLabel nameLabel; 292 private javax.swing.JTextField nameText; 293 private javax.swing.JButton removeLocales; 294 296 } 297 | Popular Tags |