1 19 20 package org.netbeans.beaninfo.editors; 21 22 import java.awt.Component ; 23 import java.awt.Dimension ; 24 import java.awt.event.ActionEvent ; 25 import java.awt.event.ActionListener ; 26 import java.awt.event.KeyAdapter ; 27 import java.awt.event.KeyEvent ; 28 import java.util.Vector ; 29 import java.util.ResourceBundle ; 30 31 import javax.swing.*; 32 import javax.swing.border.*; 33 import org.openide.awt.Mnemonics; 34 35 import org.openide.util.NbBundle; 37 38 42 public class StringArrayCustomEditor extends javax.swing.JPanel { 43 private Vector <String > itemsVector; 44 private StringArrayCustomizable editor; 45 46 static final long serialVersionUID =-4347656479280614636L; 47 48 49 public StringArrayCustomEditor(StringArrayCustomizable sac) { 50 editor = sac; 51 itemsVector = new Vector <String > (); 52 String [] array = editor.getStringArray (); 53 if (array != null) 54 for (int i = 0; i < array.length; i++) 55 itemsVector.addElement (array[i]); 56 initComponents (); 57 itemList.setCellRenderer (new EmptyStringListCellRenderer ()); 58 itemList.setListData (itemsVector); 59 itemList.setSelectionMode (ListSelectionModel.SINGLE_SELECTION); 60 61 setBorder (new javax.swing.border.EmptyBorder (new java.awt.Insets (16, 8, 8, 0))); 62 buttonsPanel.setBorder (new javax.swing.border.EmptyBorder (new java.awt.Insets (0, 5, 5, 5))); 63 64 Mnemonics.setLocalizedText (itemLabel, NbBundle.getMessage( 65 StringArrayCustomEditor.class, "CTL_Item")); Mnemonics.setLocalizedText (itemListLabel, NbBundle.getMessage( 67 StringArrayCustomEditor.class, "CTL_ItemList")); Mnemonics.setLocalizedText (addButton, NbBundle.getMessage(StringArrayCustomEditor.class, 69 "CTL_Add_StringArrayCustomEditor")); Mnemonics.setLocalizedText (changeButton, NbBundle.getMessage(StringArrayCustomEditor.class, 71 "CTL_Change_StringArrayCustomEditor")); Mnemonics.setLocalizedText (removeButton, NbBundle.getMessage(StringArrayCustomEditor.class, 73 "CTL_Remove")); Mnemonics.setLocalizedText (moveUpButton, NbBundle.getMessage(StringArrayCustomEditor.class, 75 "CTL_MoveUp")); Mnemonics.setLocalizedText (moveDownButton, NbBundle.getMessage( 77 StringArrayCustomEditor.class, "CTL_MoveDown")); 79 getAccessibleContext().setAccessibleDescription(NbBundle.getMessage( 80 StringArrayCustomEditor.class, "ACSD_StringArrayCustomEditor")); itemField.getAccessibleContext().setAccessibleDescription( 82 NbBundle.getMessage( 83 StringArrayCustomEditor.class, "ACSD_CTL_Item")); itemList.getAccessibleContext().setAccessibleDescription( 85 NbBundle.getMessage( 86 StringArrayCustomEditor.class, "ACSD_CTL_ItemList")); addButton.getAccessibleContext().setAccessibleDescription( 88 NbBundle.getMessage(StringArrayCustomEditor.class, 89 "ACSD_CTL_Add_StringArrayCustomEditor")); changeButton.getAccessibleContext().setAccessibleDescription( 91 NbBundle.getMessage(StringArrayCustomEditor.class, 92 "ACSD_CTL_Change_StringArrayCustomEditor")); removeButton.getAccessibleContext().setAccessibleDescription( 94 NbBundle.getMessage(StringArrayCustomEditor.class, 95 "ACSD_CTL_Remove")); moveUpButton.getAccessibleContext().setAccessibleDescription( 97 NbBundle.getMessage(StringArrayCustomEditor.class, 98 "ACSD_CTL_MoveUp")); moveDownButton.getAccessibleContext().setAccessibleDescription( 100 NbBundle.getMessage(StringArrayCustomEditor.class, 101 "ACSD_CTL_MoveDown")); 103 updateButtons (); 104 itemField.addKeyListener(new KeyAdapter () { 105 public void keyReleased(KeyEvent event) { 106 boolean containsCurrent = containsCurrent(); 107 String txt = itemField.getText().trim(); 108 boolean en = itemField.isEnabled() && 109 txt.length() > 0 && 110 !containsCurrent; 111 addButton.setEnabled(en); 112 changeButton.setEnabled(en && itemList.getSelectedIndex() != -1); 113 if (containsCurrent) { 114 itemList.setSelectedIndex(idxOfCurrent()); 115 } 116 } 117 }); 118 itemField.addActionListener(new ActionListener () { 119 public void actionPerformed (ActionEvent ae) { 120 if (addButton.isEnabled()) { 121 addButtonActionPerformed(ae); 122 } 123 } 124 }); 125 addButton.setEnabled(false); 126 changeButton.setEnabled(false); 127 setMinimumSize(new Dimension (200, 400)); 129 } 130 131 133 private boolean containsCurrent() { 134 return idxOfCurrent() != -1; 135 } 136 137 private int idxOfCurrent() { 138 String txt = itemField.getText().trim(); 139 if (txt.length() > 0) { 140 int max = itemList.getModel().getSize(); 141 for (int i=0; i < max; i++) { 142 if (txt.equals(itemList.getModel().getElementAt(i))) return i; 143 } 144 } 145 return -1; 146 } 147 148 153 private void initComponents() { editPanel = new javax.swing.JPanel (); 155 itemListScroll = new javax.swing.JScrollPane (); 156 itemList = new javax.swing.JList (); 157 itemLabel = new javax.swing.JLabel (); 158 itemField = new javax.swing.JTextField (); 159 itemListLabel = new javax.swing.JLabel (); 160 buttonsPanel = new javax.swing.JPanel (); 161 addButton = new javax.swing.JButton (); 162 changeButton = new javax.swing.JButton (); 163 removeButton = new javax.swing.JButton (); 164 jSeparator1 = new javax.swing.JSeparator (); 165 moveUpButton = new javax.swing.JButton (); 166 moveDownButton = new javax.swing.JButton (); 167 paddingPanel = new javax.swing.JPanel (); 168 169 setLayout(new java.awt.BorderLayout ()); 170 171 editPanel.setLayout(new java.awt.GridBagLayout ()); 172 java.awt.GridBagConstraints gridBagConstraints2; 173 174 itemList.addListSelectionListener(new javax.swing.event.ListSelectionListener () { 175 public void valueChanged(javax.swing.event.ListSelectionEvent evt) { 176 itemListValueChanged(evt); 177 } 178 }); 179 180 itemListScroll.setViewportView(itemList); 181 182 gridBagConstraints2 = new java.awt.GridBagConstraints (); 183 gridBagConstraints2.gridx = 0; 184 gridBagConstraints2.gridy = 2; 185 gridBagConstraints2.gridwidth = 2; 186 gridBagConstraints2.fill = java.awt.GridBagConstraints.BOTH; 187 gridBagConstraints2.weightx = 1.0; 188 gridBagConstraints2.weighty = 1.0; 189 editPanel.add(itemListScroll, gridBagConstraints2); 190 191 itemLabel.setText("item"); 192 itemLabel.setLabelFor(itemField); 193 gridBagConstraints2 = new java.awt.GridBagConstraints (); 194 gridBagConstraints2.insets = new java.awt.Insets (0, 0, 11, 12); 195 gridBagConstraints2.anchor = java.awt.GridBagConstraints.WEST; 196 editPanel.add(itemLabel, gridBagConstraints2); 197 198 gridBagConstraints2 = new java.awt.GridBagConstraints (); 199 gridBagConstraints2.fill = java.awt.GridBagConstraints.HORIZONTAL; 200 gridBagConstraints2.insets = new java.awt.Insets (0, 0, 11, 0); 201 editPanel.add(itemField, gridBagConstraints2); 202 203 itemListLabel.setText("jLabel1"); 204 itemListLabel.setLabelFor(itemList); 205 gridBagConstraints2 = new java.awt.GridBagConstraints (); 206 gridBagConstraints2.gridx = 0; 207 gridBagConstraints2.gridy = 1; 208 gridBagConstraints2.gridwidth = 2; 209 gridBagConstraints2.insets = new java.awt.Insets (0, 0, 2, 0); 210 gridBagConstraints2.anchor = java.awt.GridBagConstraints.WEST; 211 editPanel.add(itemListLabel, gridBagConstraints2); 212 213 add(editPanel, java.awt.BorderLayout.CENTER); 214 215 buttonsPanel.setLayout(new java.awt.GridBagLayout ()); 216 java.awt.GridBagConstraints gridBagConstraints1; 217 218 addButton.addActionListener(new java.awt.event.ActionListener () { 219 public void actionPerformed(java.awt.event.ActionEvent evt) { 220 addButtonActionPerformed(evt); 221 } 222 }); 223 224 gridBagConstraints1 = new java.awt.GridBagConstraints (); 225 gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER; 226 gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH; 227 gridBagConstraints1.insets = new java.awt.Insets (0, 8, 0, 8); 228 gridBagConstraints1.weightx = 1.0; 229 buttonsPanel.add(addButton, gridBagConstraints1); 230 231 changeButton.addActionListener(new java.awt.event.ActionListener () { 232 public void actionPerformed(java.awt.event.ActionEvent evt) { 233 changeButtonActionPerformed(evt); 234 } 235 }); 236 237 gridBagConstraints1 = new java.awt.GridBagConstraints (); 238 gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER; 239 gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH; 240 gridBagConstraints1.insets = new java.awt.Insets (8, 8, 0, 8); 241 gridBagConstraints1.weightx = 1.0; 242 buttonsPanel.add(changeButton, gridBagConstraints1); 243 244 removeButton.addActionListener(new java.awt.event.ActionListener () { 245 public void actionPerformed(java.awt.event.ActionEvent evt) { 246 removeButtonActionPerformed(evt); 247 } 248 }); 249 250 gridBagConstraints1 = new java.awt.GridBagConstraints (); 251 gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER; 252 gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH; 253 gridBagConstraints1.insets = new java.awt.Insets (8, 8, 8, 8); 254 gridBagConstraints1.weightx = 1.0; 255 buttonsPanel.add(removeButton, gridBagConstraints1); 256 257 gridBagConstraints1 = new java.awt.GridBagConstraints (); 258 gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER; 259 gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL; 260 gridBagConstraints1.insets = new java.awt.Insets (0, 4, 0, 4); 261 buttonsPanel.add(jSeparator1, gridBagConstraints1); 262 263 moveUpButton.setEnabled(false); 264 moveUpButton.addActionListener(new java.awt.event.ActionListener () { 265 public void actionPerformed(java.awt.event.ActionEvent evt) { 266 moveUpButtonActionPerformed(evt); 267 } 268 }); 269 270 gridBagConstraints1 = new java.awt.GridBagConstraints (); 271 gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER; 272 gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH; 273 gridBagConstraints1.insets = new java.awt.Insets (8, 8, 0, 8); 274 gridBagConstraints1.weightx = 1.0; 275 buttonsPanel.add(moveUpButton, gridBagConstraints1); 276 277 moveDownButton.setEnabled(false); 278 moveDownButton.addActionListener(new java.awt.event.ActionListener () { 279 public void actionPerformed(java.awt.event.ActionEvent evt) { 280 moveDownButtonActionPerformed(evt); 281 } 282 }); 283 284 gridBagConstraints1 = new java.awt.GridBagConstraints (); 285 gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER; 286 gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH; 287 gridBagConstraints1.insets = new java.awt.Insets (8, 8, 0, 8); 288 gridBagConstraints1.weightx = 1.0; 289 buttonsPanel.add(moveDownButton, gridBagConstraints1); 290 291 gridBagConstraints1 = new java.awt.GridBagConstraints (); 292 gridBagConstraints1.weighty = 1.0; 293 buttonsPanel.add(paddingPanel, gridBagConstraints1); 294 295 add(buttonsPanel, java.awt.BorderLayout.EAST); 296 297 } 299 private void changeButtonActionPerformed (java.awt.event.ActionEvent evt) { int sel = itemList.getSelectedIndex (); 301 String s = itemsVector.elementAt(sel); 302 itemsVector.removeElementAt (sel); 303 itemsVector.insertElementAt (itemField.getText (), sel); 304 itemList.setListData (itemsVector); 305 itemList.setSelectedIndex (sel); 306 itemList.repaint (); 307 updateValue (); 308 } 310 private void moveDownButtonActionPerformed (java.awt.event.ActionEvent evt) { int sel = itemList.getSelectedIndex (); 312 String s = itemsVector.elementAt(sel); 313 itemsVector.removeElementAt (sel); 314 itemsVector.insertElementAt (s, sel + 1); 315 itemList.setListData (itemsVector); 316 itemList.setSelectedIndex (sel + 1); 317 itemList.repaint (); 318 updateValue (); 319 } 321 private void moveUpButtonActionPerformed (java.awt.event.ActionEvent evt) { int sel = itemList.getSelectedIndex (); 323 String s = itemsVector.elementAt(sel); 324 itemsVector.removeElementAt (sel); 325 itemsVector.insertElementAt (s, sel - 1); 326 itemList.setListData (itemsVector); 327 itemList.setSelectedIndex (sel - 1); 328 itemList.repaint (); 329 updateValue (); 330 } 332 private void removeButtonActionPerformed (java.awt.event.ActionEvent evt) { int currentIndex = itemList.getSelectedIndex (); 335 itemsVector.removeElementAt (currentIndex); 336 itemList.setListData (itemsVector); 337 338 if (itemsVector.size () != 0) { 340 if (currentIndex >= itemsVector.size ()) 341 currentIndex = itemsVector.size () - 1; 342 itemList.setSelectedIndex (currentIndex); 343 } 344 345 itemList.repaint (); 346 347 updateValue (); 348 } 350 private void itemListValueChanged (javax.swing.event.ListSelectionEvent evt) { updateButtons (); 353 int sel = itemList.getSelectedIndex (); 354 if (sel != -1) { 355 itemField.setText(itemsVector.elementAt(sel)); 356 } 357 } 359 private void addButtonActionPerformed (java.awt.event.ActionEvent evt) { itemsVector.addElement (itemField.getText ()); 362 itemList.setListData (itemsVector); 363 itemList.setSelectedIndex (itemsVector.size () - 1); 364 itemList.repaint (); 365 updateValue (); 366 } 368 public void setEnabled (boolean val) { 369 Component [] c = getComponents(); 370 super.setEnabled(val); 371 setChildrenEnabled (this, val); 372 } 373 374 private void setChildrenEnabled(JPanel parent, boolean val) { 375 Component [] c = parent.getComponents(); 376 for (int i=0; i < c.length; i++) { 377 c[i].setEnabled(val); 378 if (c[i] instanceof JPanel) { 379 setChildrenEnabled((JPanel) c[i], val); 380 } 381 } 382 } 383 384 private void updateButtons () { 385 int sel = itemList.getSelectedIndex (); 386 boolean enVal = isEnabled(); 387 if (sel == -1) { 388 removeButton.setEnabled (false); 389 moveUpButton.setEnabled (false); 390 moveDownButton.setEnabled (false); 391 changeButton.setEnabled (false); 392 } else { 393 removeButton.setEnabled (enVal && true); 394 moveUpButton.setEnabled (enVal && (sel != 0)); 395 moveDownButton.setEnabled (enVal && (sel != itemsVector.size () - 1)); 396 changeButton.setEnabled (enVal && true); 397 } 398 itemField.setEnabled(enVal); 399 boolean containsCurrent = containsCurrent(); 401 String txt = itemField.getText().trim(); 402 boolean en = itemField.isEnabled() && 403 txt.length() > 0 && 404 !containsCurrent; 405 addButton.setEnabled(en); 406 } 407 408 private void updateValue () { 409 String [] value = new String [itemsVector.size()]; 410 itemsVector.copyInto (value); 411 editor.setStringArray (value); 412 } 413 414 private javax.swing.JPanel editPanel; 416 private javax.swing.JScrollPane itemListScroll; 417 private javax.swing.JList itemList; 418 private javax.swing.JLabel itemLabel; 419 private javax.swing.JTextField itemField; 420 private javax.swing.JLabel itemListLabel; 421 private javax.swing.JPanel buttonsPanel; 422 private javax.swing.JButton addButton; 423 private javax.swing.JButton changeButton; 424 private javax.swing.JButton removeButton; 425 private javax.swing.JSeparator jSeparator1; 426 private javax.swing.JButton moveUpButton; 427 private javax.swing.JButton moveDownButton; 428 private javax.swing.JPanel paddingPanel; 429 431 static class EmptyStringListCellRenderer extends JLabel implements ListCellRenderer { 432 433 protected static Border hasFocusBorder; 434 protected static Border noFocusBorder; 435 436 static { 437 hasFocusBorder = new LineBorder(UIManager.getColor("List.focusCellHighlight")); noFocusBorder = new EmptyBorder(1, 1, 1, 1); 439 } 440 441 static final long serialVersionUID =487512296465844339L; 442 443 public EmptyStringListCellRenderer () { 444 setOpaque (true); 445 setBorder (noFocusBorder); 446 } 447 448 451 public java.awt.Component getListCellRendererComponent( 452 JList list, 453 Object value, int index, boolean isSelected, boolean cellHasFocus) { 458 if (!(value instanceof String )) return this; 459 String text = (String )value; 460 if ("".equals (text)) text = NbBundle.getMessage(StringArrayCustomEditor.class, "CTL_Empty"); 461 462 setText(text); 463 if (isSelected){ 464 setBackground(UIManager.getColor("List.selectionBackground")); setForeground(UIManager.getColor("List.selectionForeground")); } 467 else { 468 setBackground(list.getBackground()); 469 setForeground(list.getForeground()); 470 } 471 472 setBorder(cellHasFocus ? hasFocusBorder : noFocusBorder); 473 474 return this; 475 } 476 } 477 } 478 | Popular Tags |