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