1 19 20 package org.netbeans.modules.form.editors; 21 22 import java.util.Vector ; 23 import java.util.ResourceBundle ; 24 25 import javax.swing.*; 26 import javax.swing.border.*; 27 28 import org.openide.util.NbBundle; 29 import org.openide.awt.Mnemonics; 30 31 32 36 public class StringArrayCustomEditor extends javax.swing.JPanel { 37 38 private ResourceBundle bundle = NbBundle.getBundle ( 40 StringArrayCustomEditor.class); 41 42 private Vector itemsVector; 43 private StringArrayCustomizable editor; 44 45 private final static int DEFAULT_WIDTH = 400; 46 47 static final long serialVersionUID =-4347656479280614636L; 48 49 50 public StringArrayCustomEditor(StringArrayCustomizable sac) { 51 editor = sac; 52 itemsVector = new Vector (); 53 String [] array = editor.getStringArray (); 54 if (array != null) 55 for (int i = 0; i < array.length; i++) 56 itemsVector.addElement (array[i]); 57 initComponents (); 58 itemList.setCellRenderer (new EmptyStringListCellRenderer ()); 59 itemList.setListData (itemsVector); 60 itemList.setSelectionMode (ListSelectionModel.SINGLE_SELECTION); 61 62 setBorder (new javax.swing.border.EmptyBorder (new java.awt.Insets (16, 8, 8, 0))); 63 buttonsPanel.setBorder (new javax.swing.border.EmptyBorder (new java.awt.Insets (0, 5, 5, 5))); 64 65 Mnemonics.setLocalizedText(itemLabel, bundle.getString("CTL_Item")); Mnemonics.setLocalizedText(itemListLabel, bundle.getString("CTL_ItemList")); Mnemonics.setLocalizedText(addButton, bundle.getString("CTL_Add_StringArrayCustomEditor")); Mnemonics.setLocalizedText(changeButton, bundle.getString("CTL_Change_StringArrayCustomEditor")); Mnemonics.setLocalizedText(removeButton, bundle.getString("CTL_Remove")); Mnemonics.setLocalizedText(moveUpButton, bundle.getString("CTL_MoveUp")); Mnemonics.setLocalizedText(moveDownButton, bundle.getString("CTL_MoveDown")); 73 getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_StringArrayCustomEditor")); itemField.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CTL_Item")); itemList.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CTL_ItemList")); addButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CTL_Add_StringArrayCustomEditor")); changeButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CTL_Change_StringArrayCustomEditor")); removeButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CTL_Remove")); moveUpButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CTL_MoveUp")); moveDownButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CTL_MoveDown")); 82 updateButtons (); 83 } 84 85 public java.awt.Dimension getPreferredSize () { 86 java.awt.Dimension sup = super.getPreferredSize (); 88 return new java.awt.Dimension (Math.max (sup.width, DEFAULT_WIDTH), sup.height); 89 } 90 91 96 private void initComponents() { editPanel = new javax.swing.JPanel (); 98 itemListScroll = new javax.swing.JScrollPane (); 99 itemList = new javax.swing.JList (); 100 itemLabel = new javax.swing.JLabel (); 101 itemField = new javax.swing.JTextField (); 102 itemListLabel = new javax.swing.JLabel (); 103 buttonsPanel = new javax.swing.JPanel (); 104 addButton = new javax.swing.JButton (); 105 changeButton = new javax.swing.JButton (); 106 removeButton = new javax.swing.JButton (); 107 jSeparator1 = new javax.swing.JSeparator (); 108 moveUpButton = new javax.swing.JButton (); 109 moveDownButton = new javax.swing.JButton (); 110 paddingPanel = new javax.swing.JPanel (); 111 112 setLayout(new java.awt.BorderLayout ()); 113 114 editPanel.setLayout(new java.awt.GridBagLayout ()); 115 java.awt.GridBagConstraints gridBagConstraints2; 116 117 itemList.addListSelectionListener(new javax.swing.event.ListSelectionListener () { 118 public void valueChanged(javax.swing.event.ListSelectionEvent evt) { 119 itemListValueChanged(evt); 120 } 121 }); 122 123 itemListScroll.setViewportView(itemList); 124 125 gridBagConstraints2 = new java.awt.GridBagConstraints (); 126 gridBagConstraints2.gridx = 0; 127 gridBagConstraints2.gridy = 2; 128 gridBagConstraints2.gridwidth = 2; 129 gridBagConstraints2.fill = java.awt.GridBagConstraints.BOTH; 130 gridBagConstraints2.weightx = 1.0; 131 gridBagConstraints2.weighty = 1.0; 132 editPanel.add(itemListScroll, gridBagConstraints2); 133 134 itemLabel.setText("item"); 135 itemLabel.setLabelFor(itemField); 136 gridBagConstraints2 = new java.awt.GridBagConstraints (); 137 gridBagConstraints2.insets = new java.awt.Insets (0, 0, 11, 12); 138 gridBagConstraints2.anchor = java.awt.GridBagConstraints.WEST; 139 editPanel.add(itemLabel, gridBagConstraints2); 140 141 gridBagConstraints2 = new java.awt.GridBagConstraints (); 142 gridBagConstraints2.fill = java.awt.GridBagConstraints.HORIZONTAL; 143 gridBagConstraints2.insets = new java.awt.Insets (0, 0, 11, 0); 144 editPanel.add(itemField, gridBagConstraints2); 145 146 itemListLabel.setText("jLabel1"); 147 itemListLabel.setLabelFor(itemList); 148 gridBagConstraints2 = new java.awt.GridBagConstraints (); 149 gridBagConstraints2.gridx = 0; 150 gridBagConstraints2.gridy = 1; 151 gridBagConstraints2.gridwidth = 2; 152 gridBagConstraints2.insets = new java.awt.Insets (0, 0, 2, 0); 153 gridBagConstraints2.anchor = java.awt.GridBagConstraints.WEST; 154 editPanel.add(itemListLabel, gridBagConstraints2); 155 156 add(editPanel, java.awt.BorderLayout.CENTER); 157 158 buttonsPanel.setLayout(new java.awt.GridBagLayout ()); 159 java.awt.GridBagConstraints gridBagConstraints1; 160 161 addButton.addActionListener(new java.awt.event.ActionListener () { 162 public void actionPerformed(java.awt.event.ActionEvent evt) { 163 addButtonActionPerformed(evt); 164 } 165 }); 166 167 gridBagConstraints1 = new java.awt.GridBagConstraints (); 168 gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER; 169 gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH; 170 gridBagConstraints1.insets = new java.awt.Insets (0, 8, 0, 8); 171 gridBagConstraints1.weightx = 1.0; 172 buttonsPanel.add(addButton, gridBagConstraints1); 173 174 changeButton.addActionListener(new java.awt.event.ActionListener () { 175 public void actionPerformed(java.awt.event.ActionEvent evt) { 176 changeButtonActionPerformed(evt); 177 } 178 }); 179 180 gridBagConstraints1 = new java.awt.GridBagConstraints (); 181 gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER; 182 gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH; 183 gridBagConstraints1.insets = new java.awt.Insets (8, 8, 0, 8); 184 gridBagConstraints1.weightx = 1.0; 185 buttonsPanel.add(changeButton, gridBagConstraints1); 186 187 removeButton.addActionListener(new java.awt.event.ActionListener () { 188 public void actionPerformed(java.awt.event.ActionEvent evt) { 189 removeButtonActionPerformed(evt); 190 } 191 }); 192 193 gridBagConstraints1 = new java.awt.GridBagConstraints (); 194 gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER; 195 gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH; 196 gridBagConstraints1.insets = new java.awt.Insets (8, 8, 8, 8); 197 gridBagConstraints1.weightx = 1.0; 198 buttonsPanel.add(removeButton, gridBagConstraints1); 199 200 gridBagConstraints1 = new java.awt.GridBagConstraints (); 201 gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER; 202 gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL; 203 gridBagConstraints1.insets = new java.awt.Insets (0, 4, 0, 4); 204 buttonsPanel.add(jSeparator1, gridBagConstraints1); 205 206 moveUpButton.setEnabled(false); 207 moveUpButton.addActionListener(new java.awt.event.ActionListener () { 208 public void actionPerformed(java.awt.event.ActionEvent evt) { 209 moveUpButtonActionPerformed(evt); 210 } 211 }); 212 213 gridBagConstraints1 = new java.awt.GridBagConstraints (); 214 gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER; 215 gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH; 216 gridBagConstraints1.insets = new java.awt.Insets (8, 8, 0, 8); 217 gridBagConstraints1.weightx = 1.0; 218 buttonsPanel.add(moveUpButton, gridBagConstraints1); 219 220 moveDownButton.setEnabled(false); 221 moveDownButton.addActionListener(new java.awt.event.ActionListener () { 222 public void actionPerformed(java.awt.event.ActionEvent evt) { 223 moveDownButtonActionPerformed(evt); 224 } 225 }); 226 227 gridBagConstraints1 = new java.awt.GridBagConstraints (); 228 gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER; 229 gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH; 230 gridBagConstraints1.insets = new java.awt.Insets (8, 8, 0, 8); 231 gridBagConstraints1.weightx = 1.0; 232 buttonsPanel.add(moveDownButton, gridBagConstraints1); 233 234 gridBagConstraints1 = new java.awt.GridBagConstraints (); 235 gridBagConstraints1.weighty = 1.0; 236 buttonsPanel.add(paddingPanel, gridBagConstraints1); 237 238 add(buttonsPanel, java.awt.BorderLayout.EAST); 239 240 } 242 private void changeButtonActionPerformed (java.awt.event.ActionEvent evt) { int sel = itemList.getSelectedIndex (); 244 itemsVector.removeElementAt (sel); 245 itemsVector.insertElementAt (itemField.getText (), sel); 246 itemList.setListData (itemsVector); 247 itemList.setSelectedIndex (sel); 248 itemList.repaint (); 249 updateValue (); 250 } 252 private void moveDownButtonActionPerformed (java.awt.event.ActionEvent evt) { int sel = itemList.getSelectedIndex (); 254 String s = (String ) itemsVector.elementAt (sel); 255 itemsVector.removeElementAt (sel); 256 itemsVector.insertElementAt (s, sel + 1); 257 itemList.setListData (itemsVector); 258 itemList.setSelectedIndex (sel + 1); 259 itemList.repaint (); 260 updateValue (); 261 } 263 private void moveUpButtonActionPerformed (java.awt.event.ActionEvent evt) { int sel = itemList.getSelectedIndex (); 265 String s = (String ) itemsVector.elementAt (sel); 266 itemsVector.removeElementAt (sel); 267 itemsVector.insertElementAt (s, sel - 1); 268 itemList.setListData (itemsVector); 269 itemList.setSelectedIndex (sel - 1); 270 itemList.repaint (); 271 updateValue (); 272 } 274 private void removeButtonActionPerformed (java.awt.event.ActionEvent evt) { int currentIndex = itemList.getSelectedIndex (); 277 itemsVector.removeElementAt (currentIndex); 278 itemList.setListData (itemsVector); 279 280 if (itemsVector.size () != 0) { 282 if (currentIndex >= itemsVector.size ()) 283 currentIndex = itemsVector.size () - 1; 284 itemList.setSelectedIndex (currentIndex); 285 } 286 287 itemList.repaint (); 288 289 updateValue (); 290 } 292 private void itemListValueChanged (javax.swing.event.ListSelectionEvent evt) { updateButtons (); 295 int sel = itemList.getSelectedIndex (); 296 if (sel != -1) { 297 itemField.setText ((String ) itemsVector.elementAt (sel)); 298 } 299 } 301 private void addButtonActionPerformed (java.awt.event.ActionEvent evt) { itemsVector.addElement (itemField.getText ()); 304 itemList.setListData (itemsVector); 305 itemList.setSelectedIndex (itemsVector.size () - 1); 306 itemList.repaint (); 307 updateValue (); 308 } 310 private void updateButtons () { 311 int sel = itemList.getSelectedIndex (); 312 if (sel == -1) { 313 removeButton.setEnabled (false); 314 moveUpButton.setEnabled (false); 315 moveDownButton.setEnabled (false); 316 changeButton.setEnabled (false); 317 } else { 318 removeButton.setEnabled (true); 319 moveUpButton.setEnabled (sel != 0); 320 moveDownButton.setEnabled (sel != itemsVector.size () - 1); 321 changeButton.setEnabled (true); 322 } 323 } 324 325 private void updateValue () { 326 String [] value = new String [itemsVector.size()]; 327 itemsVector.copyInto (value); 328 editor.setStringArray (value); 329 } 330 331 private javax.swing.JPanel editPanel; 333 private javax.swing.JScrollPane itemListScroll; 334 private javax.swing.JList itemList; 335 private javax.swing.JLabel itemLabel; 336 private javax.swing.JTextField itemField; 337 private javax.swing.JLabel itemListLabel; 338 private javax.swing.JPanel buttonsPanel; 339 private javax.swing.JButton addButton; 340 private javax.swing.JButton changeButton; 341 private javax.swing.JButton removeButton; 342 private javax.swing.JSeparator jSeparator1; 343 private javax.swing.JButton moveUpButton; 344 private javax.swing.JButton moveDownButton; 345 private javax.swing.JPanel paddingPanel; 346 348 static class EmptyStringListCellRenderer extends JLabel implements ListCellRenderer { 349 350 protected static Border hasFocusBorder; 351 protected static Border noFocusBorder; 352 353 static { 354 hasFocusBorder = new LineBorder(UIManager.getColor("List.focusCellHighlight")); noFocusBorder = new EmptyBorder(1, 1, 1, 1); 356 } 357 358 static final long serialVersionUID =487512296465844339L; 359 360 public EmptyStringListCellRenderer () { 361 setOpaque (true); 362 setBorder (noFocusBorder); 363 } 364 365 368 public java.awt.Component getListCellRendererComponent( 369 JList list, 370 Object value, int index, boolean isSelected, boolean cellHasFocus) { 375 if (!(value instanceof String )) return this; 376 String text = (String )value; 377 if ("".equals (text)) text = NbBundle.getMessage (EmptyStringListCellRenderer.class, "CTL_Empty"); 379 setText(text); 380 if (isSelected){ 381 setBackground(UIManager.getColor("List.selectionBackground")); setForeground(UIManager.getColor("List.selectionForeground")); } 384 else { 385 setBackground(list.getBackground()); 386 setForeground(list.getForeground()); 387 } 388 389 setBorder(cellHasFocus ? hasFocusBorder : noFocusBorder); 390 391 return this; 392 } 393 } 394 } 395 | Popular Tags |