1 19 20 package org.netbeans.modules.form; 21 22 import java.util.*; 23 import javax.swing.DefaultListModel ; 24 25 import org.openide.DialogDisplayer; 26 import org.openide.NotifyDescriptor; 27 28 32 public class EventCustomEditor extends javax.swing.JPanel { 33 34 static final long serialVersionUID =-4825059521634962952L; 35 36 37 public EventCustomEditor(EventProperty eventProperty) { 38 this.eventProperty = eventProperty; 39 40 initComponents(); 41 enableButtons(); 42 43 addButton.setMnemonic( 44 FormUtils.getBundleString("CTL_EE_ADD_Mnemonic").charAt(0)); removeButton.setMnemonic( 46 FormUtils.getBundleString("CTL_EE_REMOVE_Mnemonic").charAt(0)); editButton.setMnemonic( 48 FormUtils.getBundleString("CTL_EE_RENAME_Mnemonic").charAt(0)); handlersListLabel.setDisplayedMnemonic( 50 FormUtils.getBundleString("CTL_EE_Handlers_Mnemonic").charAt(0)); 52 handlersList.getAccessibleContext().setAccessibleDescription( 53 FormUtils.getBundleString("ACSD_CTL_EE_Handlers")); addButton.getAccessibleContext().setAccessibleDescription( 55 FormUtils.getBundleString("ACSD_CTL_EE_ADD")); removeButton.getAccessibleContext().setAccessibleDescription( 57 FormUtils.getBundleString("ACSD_CTL_EE_REMOVE")); editButton.getAccessibleContext().setAccessibleDescription( 59 FormUtils.getBundleString("ACSD_CTL_EE_RENAME")); getAccessibleContext().setAccessibleDescription( 61 FormUtils.getBundleString("ACSD_EventCustomEditor")); } 63 64 69 private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; 71 72 handlersListLabel = new javax.swing.JLabel (); 73 jScrollPane1 = new javax.swing.JScrollPane (); 74 String [] handlers = eventProperty.getEventHandlers(); 75 for (int i=0; i < handlers.length; i++) { 76 handlersModel.addElement(handlers[i]); 77 } 78 handlersList = new javax.swing.JList (); 79 handlersList.setModel(handlersModel); 80 if (handlers.length > 0) { 81 handlersList.setSelectedIndex(0); 82 } 83 addButton = new javax.swing.JButton (); 84 removeButton = new javax.swing.JButton (); 85 editButton = new javax.swing.JButton (); 86 87 setLayout(new java.awt.GridBagLayout ()); 88 89 setPreferredSize(new java.awt.Dimension (300, 300)); 90 handlersListLabel.setLabelFor(handlersList); 91 handlersListLabel.setText(FormUtils.getBundleString("CTL_EE_Handlers")); 92 gridBagConstraints = new java.awt.GridBagConstraints (); 93 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 94 gridBagConstraints.insets = new java.awt.Insets (12, 12, 2, 0); 95 add(handlersListLabel, gridBagConstraints); 96 97 handlersList.addListSelectionListener(new javax.swing.event.ListSelectionListener () { 98 public void valueChanged(javax.swing.event.ListSelectionEvent evt) { 99 handlersListValueChanged(evt); 100 } 101 }); 102 103 jScrollPane1.setViewportView(handlersList); 104 105 gridBagConstraints = new java.awt.GridBagConstraints (); 106 gridBagConstraints.gridx = 0; 107 gridBagConstraints.gridy = 1; 108 gridBagConstraints.gridheight = 4; 109 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 110 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 111 gridBagConstraints.weightx = 0.9; 112 gridBagConstraints.weighty = 1.0; 113 gridBagConstraints.insets = new java.awt.Insets (0, 12, 0, 11); 114 add(jScrollPane1, gridBagConstraints); 115 116 addButton.setText(FormUtils.getBundleString("CTL_EE_ADD")); 117 addButton.addActionListener(new java.awt.event.ActionListener () { 118 public void actionPerformed(java.awt.event.ActionEvent evt) { 119 addButtonActionPerformed(evt); 120 } 121 }); 122 123 gridBagConstraints = new java.awt.GridBagConstraints (); 124 gridBagConstraints.gridx = 1; 125 gridBagConstraints.gridy = 1; 126 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 127 gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; 128 gridBagConstraints.weightx = 0.1; 129 gridBagConstraints.insets = new java.awt.Insets (0, 0, 5, 11); 130 add(addButton, gridBagConstraints); 131 132 removeButton.setText(FormUtils.getBundleString("CTL_EE_REMOVE")); 133 removeButton.addActionListener(new java.awt.event.ActionListener () { 134 public void actionPerformed(java.awt.event.ActionEvent evt) { 135 removeButtonActionPerformed(evt); 136 } 137 }); 138 139 gridBagConstraints = new java.awt.GridBagConstraints (); 140 gridBagConstraints.gridx = 1; 141 gridBagConstraints.gridy = 2; 142 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 143 gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; 144 gridBagConstraints.weightx = 0.1; 145 gridBagConstraints.insets = new java.awt.Insets (0, 0, 5, 11); 146 add(removeButton, gridBagConstraints); 147 148 editButton.setText(FormUtils.getBundleString("CTL_EE_RENAME")); 149 editButton.addActionListener(new java.awt.event.ActionListener () { 150 public void actionPerformed(java.awt.event.ActionEvent evt) { 151 editButtonActionPerformed(evt); 152 } 153 }); 154 155 gridBagConstraints = new java.awt.GridBagConstraints (); 156 gridBagConstraints.gridx = 1; 157 gridBagConstraints.gridy = 3; 158 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 159 gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; 160 gridBagConstraints.weightx = 0.1; 161 gridBagConstraints.insets = new java.awt.Insets (0, 0, 0, 11); 162 add(editButton, gridBagConstraints); 163 164 } 166 private void handlersListValueChanged(javax.swing.event.ListSelectionEvent evt) { enableButtons(); 168 } 170 private void enableButtons() { 171 if (handlersList.isSelectionEmpty()) { 172 removeButton.setEnabled(false); 173 } else { 174 removeButton.setEnabled(true); 175 } 176 editButton.setEnabled(handlersList.getSelectedIndices().length == 1); 177 } 178 179 private void editButtonActionPerformed(java.awt.event.ActionEvent evt) { int i = handlersList.getSelectedIndex(); 182 if (i >= 0) { 183 String oldName = (String ) handlersModel.get(i); 184 NotifyDescriptor.InputLine nd = new NotifyDescriptor.InputLine( 185 FormUtils.getBundleString("CTL_EE_RENAME_LABEL"), FormUtils.getBundleString("CTL_EE_RENAME_CAPTION")); nd.setInputText(oldName); 188 189 if (DialogDisplayer.getDefault().notify(nd).equals(NotifyDescriptor.OK_OPTION)) { 190 String newName = nd.getInputText(); 191 if (newName.equals(oldName)) return; 193 if (!org.openide.util.Utilities.isJavaIdentifier(newName)) { NotifyDescriptor.Message msg = new NotifyDescriptor.Message( 195 FormUtils.getBundleString("CTL_EE_NOT_IDENTIFIER"), NotifyDescriptor.ERROR_MESSAGE); 197 DialogDisplayer.getDefault().notify(msg); 198 return; 199 } 200 201 if (handlersModel.indexOf(newName) >= 0) { NotifyDescriptor.Message msg = new NotifyDescriptor.Message( 203 FormUtils.getBundleString("CTL_EE_ALREADY_EXIST"), NotifyDescriptor.INFORMATION_MESSAGE); 205 DialogDisplayer.getDefault().notify(msg); 206 return; 207 } 208 209 int ii = changes.getAdded().indexOf(oldName); 210 if (ii >= 0) { changes.getAdded().set(ii,newName); 212 } 213 else { 214 ii = changes.getRenamedNewNames().indexOf(oldName); 215 if (ii >= 0) changes.getRenamedNewNames().set(ii, newName); 217 else { 218 changes.getRenamedOldNames().add(oldName); 219 changes.getRenamedNewNames().add(newName); 220 } 221 } 222 223 handlersModel.set(i,newName); 224 handlersList.setSelectedIndex(i); 225 enableButtons(); 226 } 227 } 228 } 230 private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) { Object [] handlers = handlersList.getSelectedValues(); 232 for (int i=0; i < handlers.length; i++) { 233 int ii = changes.getAdded().indexOf(handlers[i]); 234 if (ii >= 0) { changes.getAdded().remove(ii); 236 } 237 else { 238 ii = changes.getRenamedNewNames().indexOf(handlers[i]); 239 String toRemove; 240 if (ii >= 0) { changes.getRenamedNewNames().remove(ii); 242 toRemove = (String ) changes.getRenamedOldNames().get(ii); 243 changes.getRenamedOldNames().remove(ii); 244 } 245 else toRemove = (String ) handlers[i]; 246 247 changes.getRemoved().add(toRemove); 248 } 249 handlersModel.removeElement(handlers[i]); 250 enableButtons(); 251 } 252 } 254 private void addButtonActionPerformed(java.awt.event.ActionEvent evt) { NotifyDescriptor.InputLine nd = new NotifyDescriptor.InputLine( 256 FormUtils.getBundleString("CTL_EE_ADD_LABEL"), FormUtils.getBundleString("CTL_EE_ADD_CAPTION")); if (DialogDisplayer.getDefault().notify(nd).equals(NotifyDescriptor.OK_OPTION)) { 259 String newHandler = nd.getInputText(); 260 if (!org.openide.util.Utilities.isJavaIdentifier(newHandler)) { 261 NotifyDescriptor.Message msg = new NotifyDescriptor.Message( 262 FormUtils.getBundleString("CTL_EE_NOT_IDENTIFIER"), NotifyDescriptor.ERROR_MESSAGE); 264 DialogDisplayer.getDefault().notify(msg); 265 return; 266 } 267 268 if (handlersModel.indexOf(newHandler) >= 0) { 269 NotifyDescriptor.Message msg = new NotifyDescriptor.Message( 270 FormUtils.getBundleString("CTL_EE_ALREADY_EXIST"), NotifyDescriptor.INFORMATION_MESSAGE); 272 DialogDisplayer.getDefault().notify(msg); 273 return; 274 } 275 276 int ir = changes.getRemoved().indexOf(newHandler); 277 if (ir >= 0) { 278 changes.getRemoved().remove(ir); 279 } 280 else { 281 changes.getAdded().add(newHandler); 282 } 283 handlersModel.addElement(newHandler); 284 handlersList.setSelectedIndex(handlersModel.size() - 1); 285 enableButtons(); 286 } 287 } 289 public void doChanges() { 290 try { 291 eventProperty.setValue(changes); 292 } catch (Exception e) { e.printStackTrace(); 294 } 295 } 296 297 private javax.swing.JButton addButton; 299 private javax.swing.JScrollPane jScrollPane1; 300 private javax.swing.JList handlersList; 301 private javax.swing.JButton editButton; 302 private javax.swing.JButton removeButton; 303 private javax.swing.JLabel handlersListLabel; 304 306 EventProperty eventProperty; 307 DefaultListModel handlersModel = new DefaultListModel (); 308 EventProperty.Change changes = new EventProperty.Change(); 309 } 310 | Popular Tags |