1 19 20 package org.netbeans.beaninfo.editors; 21 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.ActionListener ; 24 import java.awt.event.KeyAdapter ; 25 import java.awt.event.KeyEvent ; 26 import java.util.ArrayList ; 27 import java.util.Enumeration ; 28 import java.util.List ; 29 import java.util.ResourceBundle ; 30 import java.util.StringTokenizer ; 31 32 import javax.swing.*; 33 import javax.swing.border.*; 34 import org.openide.loaders.ExtensionList; 35 36 import org.openide.util.NbBundle; 37 38 42 public class ExtensionListCustomEditor extends javax.swing.JPanel { 43 44 private ExtensionList value; 45 private ExtensionListEditor editor; 46 47 static final long serialVersionUID =-4347656479280614636L; 48 49 @SuppressWarnings ("unchecked") 50 private String [] getStrings() { 51 List <String > l = new ArrayList <String > (); 52 if (value == null) return new String [0]; 53 54 Enumeration <String > e = (Enumeration <String >)value.extensions (); 55 while (e.hasMoreElements ()) l.add (e.nextElement ()); 56 57 e = (Enumeration <String >)value.mimeTypes (); 58 while (e.hasMoreElements ()) l.add (e.nextElement ()); 59 60 return l.toArray (new String [l.size ()]); 61 } 62 63 64 public ExtensionListCustomEditor(ExtensionListEditor ed) { 65 editor = ed; 66 value = (ExtensionList)((ExtensionList)editor.getValue()).clone (); 67 initComponents (); 68 itemList.setCellRenderer (new EmptyStringListCellRenderer ()); 69 itemList.setSelectionMode (ListSelectionModel.SINGLE_SELECTION); 70 itemList.setListData (getStrings()); 71 72 ResourceBundle bundle = NbBundle.getBundle ( 73 ExtensionListCustomEditor.class); 74 75 getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_ELCE")); 76 itemField.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_ELCE_Item")); 77 itemList.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_ELCE_ItemList")); 78 addButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_ELCE_Add")); 79 changeButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_ELCE_Change")); 80 removeButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_ELCE_Remove")); 81 82 if ( ! editor.isEditable() ) { 83 itemField.setEnabled( false ); 85 addButton.setEnabled( false ); 86 changeButton.setEnabled( false ); 87 removeButton.setEnabled( false ); 88 } 89 updateButtons (); 90 itemField.addKeyListener(new KeyAdapter () { 91 public void keyReleased(KeyEvent event) { 92 boolean containsCurrent = containsCurrent(); 93 String txt = itemField.getText().trim(); 94 boolean en = itemField.isEnabled() && 95 txt.length() > 0 && 96 !containsCurrent; 97 addButton.setEnabled(en); 98 changeButton.setEnabled(en && itemList.getSelectedIndex() != -1); 99 if (containsCurrent) { 100 itemList.setSelectedIndex(idxOfCurrent()); 101 } 102 } 103 }); 104 itemField.addActionListener(new ActionListener () { 105 public void actionPerformed (ActionEvent ae) { 106 if (addButton.isEnabled()) { 107 doAdd(); 108 } 109 } 110 }); 111 addButton.setEnabled(false); 112 changeButton.setEnabled(false); 113 } 114 115 117 private boolean containsCurrent() { 118 return idxOfCurrent() != -1; 119 } 120 121 private int idxOfCurrent() { 122 String txt = itemField.getText().trim(); 123 if (txt.length() > 0) { 124 int max = itemList.getModel().getSize(); 125 for (int i=0; i < max; i++) { 126 if (txt.equals(itemList.getModel().getElementAt(i))) return i; 127 } 128 } 129 return -1; 130 } 131 132 137 private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; 139 140 addButton = new javax.swing.JButton (); 141 changeButton = new javax.swing.JButton (); 142 removeButton = new javax.swing.JButton (); 143 itemListScroll = new javax.swing.JScrollPane (); 144 itemList = new javax.swing.JList (); 145 itemLabel = new javax.swing.JLabel (); 146 itemField = new javax.swing.JTextField (); 147 itemListLabel = new javax.swing.JLabel (); 148 149 setLayout(new java.awt.GridBagLayout ()); 150 151 org.openide.awt.Mnemonics.setLocalizedText(addButton, org.openide.util.NbBundle.getMessage(ExtensionListCustomEditor.class, "CTL_ELCE_Add", new Object [] {})); 152 addButton.addActionListener(new java.awt.event.ActionListener () { 153 public void actionPerformed(java.awt.event.ActionEvent evt) { 154 addButtonActionPerformed(evt); 155 } 156 }); 157 158 gridBagConstraints = new java.awt.GridBagConstraints (); 159 gridBagConstraints.gridx = 3; 160 gridBagConstraints.gridy = 0; 161 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 162 gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; 163 gridBagConstraints.insets = new java.awt.Insets (4, 8, 0, 8); 164 add(addButton, gridBagConstraints); 165 166 org.openide.awt.Mnemonics.setLocalizedText(changeButton, org.openide.util.NbBundle.getMessage(ExtensionListCustomEditor.class, "CTL_ELCE_Change", new Object [] {})); 167 changeButton.addActionListener(new java.awt.event.ActionListener () { 168 public void actionPerformed(java.awt.event.ActionEvent evt) { 169 changeButtonActionPerformed(evt); 170 } 171 }); 172 173 gridBagConstraints = new java.awt.GridBagConstraints (); 174 gridBagConstraints.gridx = 3; 175 gridBagConstraints.gridy = 1; 176 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 177 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; 178 gridBagConstraints.insets = new java.awt.Insets (8, 8, 0, 8); 179 add(changeButton, gridBagConstraints); 180 181 org.openide.awt.Mnemonics.setLocalizedText(removeButton, org.openide.util.NbBundle.getMessage(ExtensionListCustomEditor.class, "CTL_ELCE_Remove", new Object [] {})); 182 removeButton.addActionListener(new java.awt.event.ActionListener () { 183 public void actionPerformed(java.awt.event.ActionEvent evt) { 184 removeButtonActionPerformed(evt); 185 } 186 }); 187 188 gridBagConstraints = new java.awt.GridBagConstraints (); 189 gridBagConstraints.gridx = 3; 190 gridBagConstraints.gridy = 2; 191 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; 192 gridBagConstraints.insets = new java.awt.Insets (8, 8, 8, 8); 193 add(removeButton, gridBagConstraints); 194 195 itemList.addListSelectionListener(new javax.swing.event.ListSelectionListener () { 196 public void valueChanged(javax.swing.event.ListSelectionEvent evt) { 197 itemListValueChanged(evt); 198 } 199 }); 200 201 itemListScroll.setViewportView(itemList); 202 203 gridBagConstraints = new java.awt.GridBagConstraints (); 204 gridBagConstraints.gridx = 0; 205 gridBagConstraints.gridy = 2; 206 gridBagConstraints.gridwidth = 2; 207 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 208 gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHWEST; 209 gridBagConstraints.insets = new java.awt.Insets (0, 8, 0, 0); 210 add(itemListScroll, gridBagConstraints); 211 212 itemLabel.setLabelFor(itemField); 213 org.openide.awt.Mnemonics.setLocalizedText(itemLabel, org.openide.util.NbBundle.getMessage(ExtensionListCustomEditor.class, "CTL_ELCE_Item", new Object [] {})); 214 gridBagConstraints = new java.awt.GridBagConstraints (); 215 gridBagConstraints.gridx = 0; 216 gridBagConstraints.gridy = 0; 217 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 218 gridBagConstraints.insets = new java.awt.Insets (4, 8, 0, 8); 219 add(itemLabel, gridBagConstraints); 220 221 itemField.setColumns(10); 222 gridBagConstraints = new java.awt.GridBagConstraints (); 223 gridBagConstraints.gridx = 1; 224 gridBagConstraints.gridy = 0; 225 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 226 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 227 gridBagConstraints.weightx = 1.0; 228 gridBagConstraints.insets = new java.awt.Insets (4, 0, 0, 0); 229 add(itemField, gridBagConstraints); 230 231 itemListLabel.setLabelFor(itemList); 232 org.openide.awt.Mnemonics.setLocalizedText(itemListLabel, org.openide.util.NbBundle.getMessage(ExtensionListCustomEditor.class, "CTL_ItemList", new Object [] {})); 233 gridBagConstraints = new java.awt.GridBagConstraints (); 234 gridBagConstraints.gridx = 0; 235 gridBagConstraints.gridy = 1; 236 gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHWEST; 237 gridBagConstraints.insets = new java.awt.Insets (0, 8, 0, 0); 238 add(itemListLabel, gridBagConstraints); 239 240 } 242 private String addTexts() { 243 StringTokenizer st = new StringTokenizer (itemField.getText (), ",. \n\t"); String last = null; 245 while(st.hasMoreTokens()) { 246 last = st.nextToken(); 247 if (last.indexOf('/') >= 0) { value.addMimeType(last); 249 } else { 250 value.addExtension (last); 251 } 252 } 253 return last; 254 } 255 256 private void changeButtonActionPerformed (java.awt.event.ActionEvent evt) { int sel = itemList.getSelectedIndex (); 258 String s = (String ) itemList.getModel().getElementAt(sel); 259 if (s.indexOf('/') >= 0) { value.removeMimeType(s); 261 } else { 262 value.removeExtension(s); 263 } 264 doAdd(); 265 } 267 private int indexOf(String [] array, String item) { 268 for (int i = 0; i<array.length; i++) { 269 if (array[i].equals(item)) return i; 270 } 271 return -1; 272 } 273 274 private void removeButtonActionPerformed (java.awt.event.ActionEvent evt) { int sel = itemList.getSelectedIndex (); 276 String s = (String ) itemList.getModel().getElementAt(sel); 277 if (s.indexOf('/') >= 0) { value.removeMimeType(s); 279 } else { 280 value.removeExtension(s); 281 } 282 itemList.setListData (getStrings()); 283 284 int count = itemList.getModel().getSize(); 285 if (count != 0) { 287 if (sel >= count) sel = count - 1; 288 itemList.setSelectedIndex (sel); 289 } 290 291 itemList.repaint (); 292 updateValue (); 293 } 295 private void itemListValueChanged (javax.swing.event.ListSelectionEvent evt) { updateButtons (); 298 int sel = itemList.getSelectedIndex (); 299 if (sel != -1) { 300 itemField.setText ((String ) itemList.getModel().getElementAt(sel)); 301 changeButton.setEnabled(false); 302 addButton.setEnabled (false); 303 } 304 } 306 private void doAdd() { 307 String last = addTexts(); 308 String [] values = getStrings(); 309 int index = indexOf(values, last); 310 itemList.setListData (values); 311 if (index >= 0) itemList.setSelectedIndex (index); 312 itemList.repaint (); 313 updateValue (); 314 } 315 316 private void addButtonActionPerformed (java.awt.event.ActionEvent evt) { doAdd(); 318 } 320 private void updateButtons () { 321 int sel = itemList.getSelectedIndex (); 322 if (sel == -1 || !editor.isEditable()) { 323 removeButton.setEnabled (false); 324 changeButton.setEnabled (false); 325 } else { 326 removeButton.setEnabled (true); 327 changeButton.setEnabled (true); 328 } 329 boolean containsCurrent = containsCurrent(); 331 String txt = itemField.getText().trim(); 332 boolean en = itemField.isEnabled() && 333 txt.length() > 0 && 334 !containsCurrent; 335 addButton.setEnabled(en); 336 } 337 338 private void updateValue () { 339 editor.setValue(value); 340 } 341 342 private javax.swing.JButton addButton; 344 private javax.swing.JButton changeButton; 345 private javax.swing.JTextField itemField; 346 private javax.swing.JLabel itemLabel; 347 private javax.swing.JList itemList; 348 private javax.swing.JLabel itemListLabel; 349 private javax.swing.JScrollPane itemListScroll; 350 private javax.swing.JButton removeButton; 351 353 static class EmptyStringListCellRenderer extends JLabel implements ListCellRenderer { 354 355 protected static Border hasFocusBorder; 356 protected static Border noFocusBorder; 357 358 static { 359 hasFocusBorder = new LineBorder(UIManager.getColor("List.focusCellHighlight")); noFocusBorder = new EmptyBorder(1, 1, 1, 1); 361 } 362 363 static final long serialVersionUID =487512296465844339L; 364 365 public EmptyStringListCellRenderer () { 366 setOpaque (true); 367 setBorder (noFocusBorder); 368 } 369 370 373 public java.awt.Component getListCellRendererComponent( 374 JList list, 375 Object value, int index, boolean isSelected, boolean cellHasFocus) { 380 if (!(value instanceof String )) return this; 381 String text = (String )value; 382 if ("".equals (text)) text = NbBundle.getMessage ( 383 ExtensionListCustomEditor.class, "CTL_ELCE_Empty"); 384 385 setText(text); 386 if (isSelected){ 387 setBackground(UIManager.getColor("List.selectionBackground")); setForeground(UIManager.getColor("List.selectionForeground")); } 390 else { 391 setBackground(list.getBackground()); 392 setForeground(list.getForeground()); 393 } 394 395 setBorder(cellHasFocus ? hasFocusBorder : noFocusBorder); 396 397 return this; 398 } 399 } 400 } 401 | Popular Tags |