1 19 20 package org.netbeans.modules.java.ui.nodes.editors; 21 22 import java.beans.*; 23 import java.util.ResourceBundle ; 24 import javax.swing.*; 25 26 import org.openide.explorer.propertysheet.PropertyEnv; 27 import org.openide.util.NbBundle; 28 29 30 39 abstract class ObjectArrayPanel2 extends javax.swing.JPanel { 40 static final String PROP_MNEMONIC_ADD = "mnemonic_Add"; static final String PROP_MNEMONIC_REMOVE = "mnemonic_Remove"; static final String PROP_MNEMONIC_UP = "mnemonic_Up"; static final String PROP_MNEMONIC_DOWN = "mnemonic_Down"; static final String PROP_MNEMONIC_EDIT = "mnemonic_Edit"; static final String PROP_MNEMONIC_LIST = "mnemonic_List"; static final String PROP_LABEL_LIST = "label_List"; 48 49 static final ResourceBundle bundle = NbBundle.getBundle(ObjectArrayPanel2.class); 50 51 52 protected DefaultListModel model; 53 54 protected final boolean isWritable; 55 56 57 public ObjectArrayPanel2() { 58 this(true); 59 } 60 61 public ObjectArrayPanel2(boolean isWritable) { 62 this.isWritable = isWritable; 63 initComponents (); 64 65 model = new DefaultListModel(); 66 list.setModel(model); 67 updateButtons(); 68 69 getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_ObjectArrayPanel")); list.getAccessibleContext().setAccessibleName(bundle.getString("ACS_ObjectArrayPanel_List")); list.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_ObjectArrayPanel_List")); upButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CTL_MoveUp")); downButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CTL_MoveDown")); addButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_Add")); removeButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CTL_Remove")); changeButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_Change")); } 78 79 void setMnemonics(PropertyEnv env) { 80 FeatureDescriptor desc = env.getFeatureDescriptor(); 81 Object o = desc.getValue(PROP_MNEMONIC_ADD); 82 if (o instanceof String ) { 83 addButton.setMnemonic(((String )o).charAt(0)); 84 } else { 85 addButton.setMnemonic(bundle.getString("CTL_Add_Mnemonic").charAt(0)); } 87 o = desc.getValue(PROP_MNEMONIC_REMOVE); 88 if (o instanceof String ) { 89 removeButton.setMnemonic(((String )o).charAt(0)); 90 } else { 91 removeButton.setMnemonic(bundle.getString("CTL_Remove_Mnemonic").charAt(0)); } 93 o = desc.getValue(PROP_MNEMONIC_EDIT); 94 if (o instanceof String ) { 95 changeButton.setMnemonic(((String )o).charAt(0)); 96 } else { 97 changeButton.setMnemonic(bundle.getString("CTL_Change_Mnemonic").charAt(0)); } 99 o = desc.getValue(PROP_MNEMONIC_UP); 100 if (o instanceof String ) { 101 upButton.setMnemonic(((String )o).charAt(0)); 102 } else { 103 upButton.setMnemonic(bundle.getString("CTL_MoveUp_Mnemonic").charAt(0)); } 105 o = desc.getValue(PROP_MNEMONIC_DOWN); 106 if (o instanceof String ) { 107 downButton.setMnemonic(((String )o).charAt(0)); 108 } else { 109 downButton.setMnemonic(bundle.getString("CTL_MoveDown_Mnemonic").charAt(0)); } 111 o = desc.getValue(PROP_LABEL_LIST); 112 if (o instanceof String ) { 113 jLabel1.setText((String )o); 114 o = desc.getValue(PROP_MNEMONIC_LIST); 115 if (o instanceof String ) { 116 jLabel1.setDisplayedMnemonic(((String )o).charAt(0)); 117 } 118 } else { 119 jLabel1.setText(bundle.getString("LAB_ObjectList")); jLabel1.setDisplayedMnemonic(bundle.getString("MNEMO_ObjectList").charAt(0)); } 122 } 123 124 129 private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; 131 132 jLabel1 = new javax.swing.JLabel (); 133 jScrollPane1 = new javax.swing.JScrollPane (); 134 list = new javax.swing.JList (); 135 upButton = new javax.swing.JButton (); 136 downButton = new javax.swing.JButton (); 137 addButton = new javax.swing.JButton (); 138 removeButton = new javax.swing.JButton (); 139 changeButton = new javax.swing.JButton (); 140 141 setLayout(new java.awt.GridBagLayout ()); 142 143 jLabel1.setLabelFor(list); 144 gridBagConstraints = new java.awt.GridBagConstraints (); 145 gridBagConstraints.gridx = 0; 146 gridBagConstraints.gridy = 0; 147 gridBagConstraints.insets = new java.awt.Insets (12, 12, 5, 0); 148 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 149 gridBagConstraints.weighty = 1.0; 150 add(jLabel1, gridBagConstraints); 151 152 jScrollPane1.setPreferredSize(new java.awt.Dimension (200, 3)); 153 list.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); 154 list.setVisibleRowCount(3); 155 list.addListSelectionListener(new javax.swing.event.ListSelectionListener () { 156 public void valueChanged(javax.swing.event.ListSelectionEvent evt) { 157 ObjectArrayPanel2.this.listValueChanged(evt); 158 } 159 }); 160 list.addMouseListener(new java.awt.event.MouseAdapter () { 161 public void mouseClicked(java.awt.event.MouseEvent evt) { 162 ObjectArrayPanel2.this.listMouseClicked(evt); 163 } 164 }); 165 166 jScrollPane1.setViewportView(list); 167 168 gridBagConstraints = new java.awt.GridBagConstraints (); 169 gridBagConstraints.gridx = 0; 170 gridBagConstraints.gridy = 1; 171 gridBagConstraints.gridheight = 6; 172 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 173 gridBagConstraints.insets = new java.awt.Insets (0, 12, 0, 11); 174 gridBagConstraints.weightx = 1.0; 175 gridBagConstraints.weighty = 1.0; 176 add(jScrollPane1, gridBagConstraints); 177 178 upButton.setText(bundle.getString("CTL_MoveUp")); 179 upButton.addActionListener(new java.awt.event.ActionListener () { 180 public void actionPerformed(java.awt.event.ActionEvent evt) { 181 ObjectArrayPanel2.this.upButtonActionPerformed(evt); 182 } 183 }); 184 185 gridBagConstraints = new java.awt.GridBagConstraints (); 186 gridBagConstraints.gridx = 1; 187 gridBagConstraints.gridy = 4; 188 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 189 gridBagConstraints.insets = new java.awt.Insets (0, 0, 5, 11); 190 gridBagConstraints.weighty = 1.0; 191 add(upButton, gridBagConstraints); 192 193 downButton.setText(bundle.getString("CTL_MoveDown")); 194 downButton.addActionListener(new java.awt.event.ActionListener () { 195 public void actionPerformed(java.awt.event.ActionEvent evt) { 196 ObjectArrayPanel2.this.downButtonActionPerformed(evt); 197 } 198 }); 199 200 gridBagConstraints = new java.awt.GridBagConstraints (); 201 gridBagConstraints.gridx = 1; 202 gridBagConstraints.gridy = 5; 203 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 204 gridBagConstraints.insets = new java.awt.Insets (0, 0, 0, 11); 205 gridBagConstraints.weighty = 1.0; 206 add(downButton, gridBagConstraints); 207 208 addButton.setText(bundle.getString("CTL_Add")); 209 addButton.addActionListener(new java.awt.event.ActionListener () { 210 public void actionPerformed(java.awt.event.ActionEvent evt) { 211 ObjectArrayPanel2.this.addButtonActionPerformed(evt); 212 } 213 }); 214 215 gridBagConstraints = new java.awt.GridBagConstraints (); 216 gridBagConstraints.gridx = 1; 217 gridBagConstraints.gridy = 1; 218 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 219 gridBagConstraints.insets = new java.awt.Insets (12, 0, 5, 11); 220 gridBagConstraints.weighty = 1.0; 221 add(addButton, gridBagConstraints); 222 223 removeButton.setText(bundle.getString("CTL_Remove")); 224 removeButton.addActionListener(new java.awt.event.ActionListener () { 225 public void actionPerformed(java.awt.event.ActionEvent evt) { 226 ObjectArrayPanel2.this.removeButtonActionPerformed(evt); 227 } 228 }); 229 230 gridBagConstraints = new java.awt.GridBagConstraints (); 231 gridBagConstraints.gridx = 1; 232 gridBagConstraints.gridy = 3; 233 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 234 gridBagConstraints.insets = new java.awt.Insets (0, 0, 11, 11); 235 gridBagConstraints.weighty = 1.0; 236 add(removeButton, gridBagConstraints); 237 238 changeButton.setText(bundle.getString("CTL_Change")); 239 changeButton.addActionListener(new java.awt.event.ActionListener () { 240 public void actionPerformed(java.awt.event.ActionEvent evt) { 241 ObjectArrayPanel2.this.changeButtonActionPerformed(evt); 242 } 243 }); 244 245 gridBagConstraints = new java.awt.GridBagConstraints (); 246 gridBagConstraints.gridx = 1; 247 gridBagConstraints.gridy = 2; 248 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 249 gridBagConstraints.insets = new java.awt.Insets (0, 0, 5, 11); 250 gridBagConstraints.weighty = 1.0; 251 add(changeButton, gridBagConstraints); 252 253 } 255 private void listMouseClicked (java.awt.event.MouseEvent evt) { if (org.openide.awt.MouseUtils.isDoubleClick(evt) && this.isWritable) { 257 int selIndex = list.getSelectedIndex(); 258 if (selIndex != -1) { 259 Object newValue = editValue(model.getElementAt(selIndex)); 260 if (newValue != null) { 261 model.setElementAt(newValue,selIndex); 262 modelChanged(); 263 updateButtons(); 264 } 265 } 266 } 267 } 269 private void listValueChanged (javax.swing.event.ListSelectionEvent evt) { updateButtons(); 271 } 273 private void downButtonActionPerformed (java.awt.event.ActionEvent evt) { int selIndex = list.getSelectedIndex(); 275 model.set(selIndex + 1, model.set(selIndex, model.getElementAt(selIndex + 1))); 276 list.setSelectedIndex(selIndex + 1); 277 modelChanged(); 278 updateButtons(); 279 } 281 private void upButtonActionPerformed (java.awt.event.ActionEvent evt) { int selIndex = list.getSelectedIndex(); 283 model.set(selIndex - 1, model.set(selIndex, model.getElementAt(selIndex - 1))); 284 list.setSelectedIndex(selIndex - 1); 285 modelChanged(); 286 updateButtons(); 287 } 289 private void changeButtonActionPerformed (java.awt.event.ActionEvent evt) { int selIndex = list.getSelectedIndex(); 291 Object newValue = editValue(model.getElementAt(selIndex)); 292 if (newValue != null) { 293 model.setElementAt(newValue,selIndex); 294 modelChanged(); 295 updateButtons(); 296 } 297 } 299 private void removeButtonActionPerformed (java.awt.event.ActionEvent evt) { int selIndex = list.getSelectedIndex(); 301 model.remove(selIndex); 302 int size = model.size(); 303 if (size > 0) { 304 selIndex = Math.min(size - 1, selIndex); 305 list.setSelectedIndex(selIndex); 306 } 307 308 modelChanged(); 309 updateButtons(); 310 } 312 private void addButtonActionPerformed (java.awt.event.ActionEvent evt) { Object newValue = insertNewValue(); 314 if (newValue != null) { 315 model.addElement(newValue); 316 list.setSelectedIndex(model.size() - 1); 317 modelChanged(); 318 updateButtons(); 319 } 320 } 322 private javax.swing.JButton addButton; 324 private javax.swing.JButton downButton; 325 private javax.swing.JList list; 326 private javax.swing.JScrollPane jScrollPane1; 327 private javax.swing.JLabel jLabel1; 328 private javax.swing.JButton changeButton; 329 private javax.swing.JButton upButton; 330 private javax.swing.JButton removeButton; 331 333 336 protected JList getListComponent() { 337 return list; 338 } 339 340 342 private void updateButtons() { 343 boolean isEmpty = model.isEmpty(); 344 int selIndex = list.getSelectedIndex(); 345 boolean isSelected = (selIndex != -1) && this.isWritable; 346 347 addButton.setEnabled(this.isWritable); 348 removeButton.setEnabled(isSelected); 349 changeButton.setEnabled(isSelected); 350 upButton.setEnabled(isSelected && (selIndex > 0)); 351 downButton.setEnabled(isSelected && (selIndex < model.size() - 1)); 352 } 353 354 358 protected abstract Object insertNewValue(); 359 360 365 protected abstract Object editValue(Object oldValue); 366 367 369 protected void modelChanged() { 370 } 371 } 372 | Popular Tags |