1 19 20 package org.netbeans.modules.java.ui.nodes.elements; 21 22 import java.awt.BorderLayout ; 23 import java.awt.event.KeyEvent ; 24 import java.beans.FeatureDescriptor ; 25 import java.lang.reflect.Modifier ; 26 import java.util.List ; 27 import java.util.Iterator ; 28 import java.util.ArrayList ; 29 30 import javax.swing.*; 31 import javax.jmi.reflect.JmiException; 32 import org.netbeans.modules.javacore.jmiimpl.javamodel.TypeClassImpl; 33 34 import org.openide.*; 35 import org.openide.explorer.propertysheet.PropertyPanel; 36 import org.openide.util.Utilities; 37 import org.openide.util.NbBundle; 38 39 import org.netbeans.modules.javacore.internalapi.JavaMetamodel; 40 import org.netbeans.modules.java.ui.nodes.editors.TypeEditor; 41 import org.netbeans.jmi.javamodel.*; 42 43 47 public final class MethodCustomizer extends JPanel { 48 51 private static final String [] COMMON_TYPES = TypeEditor.RVALUE_TYPES; 52 53 56 private final JavaClass jclass; 57 58 59 private final CallableFeature element; 60 61 65 private final Method method; 66 67 private final boolean isConstructor; 68 69 private boolean isOK = true; 70 71 75 public MethodCustomizer(JavaClass jclass, CallableFeature element) { 76 this.jclass = jclass; 77 this.element = element; 78 this.isConstructor = !(element instanceof Method); 79 this.method = this.isConstructor? null: (Method) element; 80 81 initComponents (); 82 83 int mask; 85 if (jclass.isInterface()) { 86 mask = Modifier.PUBLIC | Modifier.ABSTRACT; 87 } else { 88 mask = Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE; 89 if (!isConstructor) { 90 mask |= Modifier.ABSTRACT | Modifier.STATIC | Modifier.SYNCHRONIZED | 91 Modifier.FINAL | Modifier.NATIVE; 92 } 93 } 94 accessPanel.add(SourceEditSupport.createAccessModifiersPanel(element, mask), BorderLayout.CENTER); 95 96 PropertyPanel modifEditor = SourceEditSupport.createOtherModifiersPanel(element, mask); 97 FeatureDescriptor fd = modifEditor.getProperty(); 98 final String mnc = String.valueOf(KeyEvent.CHAR_UNDEFINED); 99 fd.setValue("ModifierPanel_Modifier_Abstract_Mnemonic", mnc); fd.setValue("ModifierPanel_Modifier_Final_Mnemonic", mnc); fd.setValue("ModifierPanel_Modifier_Static_Mnemonic", mnc); fd.setValue("ModifierPanel_Modifier_Synchronized_Mnemonic", mnc); fd.setValue("ModifierPanel_Modifier_Transient_Mnemonic", mnc); fd.setValue("ModifierPanel_Modifier_Volatile_Mnemonic", mnc); fd.setValue("ModifierPanel_Modifier_Native_Mnemonic", mnc); modifierPanel.add(modifEditor, BorderLayout.CENTER); 107 108 if (this.isConstructor) { 110 nameTextField.setText(jclass.getSimpleName()); 111 nameTextField.setEnabled(false); 112 returnCombo.setEnabled(false); 113 } 114 else { 115 nameTextField.setText(element.getName()); 116 returnCombo.setSelectedItem(ElementFormat.elementName(method.getType())); 117 } 118 119 PropertyPanel paramsEditor = new PropertyPanel( 121 ElementNode.createParametersProperty(element, true), 122 PropertyPanel.PREF_CUSTOM_EDITOR 123 ); 124 fd = paramsEditor.getProperty(); 125 fd.setValue("mnemonic_Add", getString("CTL_Parameters_Mnemonic_Add")); fd.setValue("mnemonic_Remove", getString("CTL_Parameters_Mnemonic_Remove")); fd.setValue("mnemonic_Up", getString("CTL_Parameters_Mnemonic_Up")); fd.setValue("mnemonic_Down", getString("CTL_Parameters_Mnemonic_Down")); fd.setValue("mnemonic_Edit", getString("CTL_Parameters_Mnemonic_Edit")); paramsPanel.add(paramsEditor, BorderLayout.CENTER); 131 132 PropertyPanel exceptionsEditor = new PropertyPanel( 134 ElementNode.createExceptionsProperty(element, true), 135 PropertyPanel.PREF_CUSTOM_EDITOR 136 ); 137 fd = exceptionsEditor.getProperty(); 138 fd.setValue("mnemonic_Add", getString("CTL_Parameters_Mnemonic_Add")); fd.setValue("mnemonic_Remove", getString("CTL_Parameters_Mnemonic_Remove")); fd.setValue("mnemonic_Up", getString("CTL_Parameters_Mnemonic_Up")); fd.setValue("mnemonic_Down", getString("CTL_Parameters_Mnemonic_Down")); fd.setValue("mnemonic_Edit", getString("CTL_Parameters_Mnemonic_Edit")); exceptionsPanel.add(exceptionsEditor, BorderLayout.CENTER); 144 145 jLabel1.setDisplayedMnemonic(getString("CTL_Name_Mnemonic").charAt(0)); jLabel2.setDisplayedMnemonic(getString("CTL_MethodType_Mnemonic").charAt(0)); 149 jLabel3.setVisible(false); 151 typeTextField.setVisible(false); 152 153 initAccessibility(); 154 } 155 156 public void addNotify() { 157 super.addNotify(); 158 159 int len = nameTextField.getText().length(); 160 nameTextField.setCaretPosition(0); 161 nameTextField.moveCaretPosition(len); 162 SwingUtilities.invokeLater(new Runnable () { 163 public void run() { 164 nameTextField.requestFocus(); 165 } 166 }); 167 } 168 169 174 private void initComponents() { 176 java.awt.GridBagConstraints gridBagConstraints; 177 178 jLabel1 = new javax.swing.JLabel (); 179 nameTextField = new javax.swing.JTextField (); 180 jLabel3 = new javax.swing.JLabel (); 181 typeTextField = new javax.swing.JTextField (); 182 jLabel2 = new javax.swing.JLabel (); 183 returnCombo = new javax.swing.JComboBox (COMMON_TYPES); 184 jLabel4 = new javax.swing.JLabel (); 185 accessPanel = new javax.swing.JPanel (); 186 jLabel5 = new javax.swing.JLabel (); 187 modifierPanel = new javax.swing.JPanel (); 188 jTabbedPane1 = new javax.swing.JTabbedPane (); 189 paramsPanel = new javax.swing.JPanel (); 190 exceptionsPanel = new javax.swing.JPanel (); 191 192 setLayout(new java.awt.GridBagLayout ()); 193 194 setBorder(new javax.swing.border.EmptyBorder (new java.awt.Insets (6, 6, 6, 6))); 195 jLabel1.setLabelFor(nameTextField); 196 org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(MethodCustomizer.class, "CTL_Name")); 197 gridBagConstraints = new java.awt.GridBagConstraints (); 198 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 199 gridBagConstraints.insets = new java.awt.Insets (12, 0, 0, 0); 200 add(jLabel1, gridBagConstraints); 201 202 nameTextField.addFocusListener(new java.awt.event.FocusAdapter () { 203 public void focusLost(java.awt.event.FocusEvent evt) { 204 nameTextFieldFocusLost(evt); 205 } 206 }); 207 208 gridBagConstraints = new java.awt.GridBagConstraints (); 209 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 210 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 211 gridBagConstraints.weightx = 1.0; 212 gridBagConstraints.insets = new java.awt.Insets (12, 8, 0, 0); 213 add(nameTextField, gridBagConstraints); 214 nameTextField.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(MethodCustomizer.class, "ACSD_MethodNameTextField")); 215 216 jLabel3.setLabelFor(typeTextField); 217 org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(MethodCustomizer.class, "CTL_Generic_Type")); 218 gridBagConstraints = new java.awt.GridBagConstraints (); 219 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 220 gridBagConstraints.insets = new java.awt.Insets (8, 0, 0, 0); 221 add(jLabel3, gridBagConstraints); 222 223 gridBagConstraints = new java.awt.GridBagConstraints (); 224 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 225 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 226 gridBagConstraints.weightx = 1.0; 227 gridBagConstraints.insets = new java.awt.Insets (8, 8, 0, 0); 228 add(typeTextField, gridBagConstraints); 229 typeTextField.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(MethodCustomizer.class, "ACSD_MethodType")); 230 231 jLabel2.setLabelFor(returnCombo); 232 org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(MethodCustomizer.class, "CTL_ReturnType")); 233 gridBagConstraints = new java.awt.GridBagConstraints (); 234 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 235 gridBagConstraints.insets = new java.awt.Insets (8, 0, 0, 0); 236 add(jLabel2, gridBagConstraints); 237 238 returnCombo.setEditable(true); 239 returnCombo.addActionListener(new java.awt.event.ActionListener () { 240 public void actionPerformed(java.awt.event.ActionEvent evt) { 241 returnComboActionPerformed(evt); 242 } 243 }); 244 245 gridBagConstraints = new java.awt.GridBagConstraints (); 246 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 247 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 248 gridBagConstraints.weightx = 1.0; 249 gridBagConstraints.insets = new java.awt.Insets (8, 8, 0, 0); 250 add(returnCombo, gridBagConstraints); 251 returnCombo.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(MethodCustomizer.class, "ACSD_MethodReturnType")); 252 253 jLabel4.setLabelFor(accessPanel); 254 org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getBundle(MethodCustomizer.class).getString("CTL_AccessRights")); 255 gridBagConstraints = new java.awt.GridBagConstraints (); 256 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 257 gridBagConstraints.insets = new java.awt.Insets (8, 0, 0, 0); 258 add(jLabel4, gridBagConstraints); 259 260 accessPanel.setLayout(new java.awt.BorderLayout ()); 261 262 gridBagConstraints = new java.awt.GridBagConstraints (); 263 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 264 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 265 gridBagConstraints.insets = new java.awt.Insets (8, 8, 0, 0); 266 add(accessPanel, gridBagConstraints); 267 268 jLabel5.setLabelFor(modifierPanel); 269 org.openide.awt.Mnemonics.setLocalizedText(jLabel5, org.openide.util.NbBundle.getMessage(MethodCustomizer.class, "CTL_Modifiers")); 270 gridBagConstraints = new java.awt.GridBagConstraints (); 271 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 272 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 273 gridBagConstraints.insets = new java.awt.Insets (8, 0, 0, 0); 274 add(jLabel5, gridBagConstraints); 275 276 modifierPanel.setLayout(new java.awt.BorderLayout ()); 277 278 gridBagConstraints = new java.awt.GridBagConstraints (); 279 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 280 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 281 gridBagConstraints.insets = new java.awt.Insets (8, 8, 0, 0); 282 add(modifierPanel, gridBagConstraints); 283 284 paramsPanel.setLayout(new java.awt.BorderLayout ()); 285 286 paramsPanel.setBorder(new javax.swing.border.EmptyBorder (new java.awt.Insets (0, 0, 10, 0))); 287 jTabbedPane1.addTab(org.openide.util.NbBundle.getMessage(MethodCustomizer.class, "CTL_Parameters"), paramsPanel); 288 289 exceptionsPanel.setLayout(new java.awt.BorderLayout ()); 290 291 exceptionsPanel.setBorder(new javax.swing.border.EmptyBorder (new java.awt.Insets (0, 0, 10, 0))); 292 jTabbedPane1.addTab(org.openide.util.NbBundle.getMessage(MethodCustomizer.class, "CTL_Exceptions"), exceptionsPanel); 293 294 gridBagConstraints = new java.awt.GridBagConstraints (); 295 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 296 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 297 gridBagConstraints.weightx = 1.0; 298 gridBagConstraints.weighty = 1.0; 299 gridBagConstraints.insets = new java.awt.Insets (5, 0, 0, 0); 300 add(jTabbedPane1, gridBagConstraints); 301 302 } 303 305 private void returnComboActionPerformed (java.awt.event.ActionEvent evt) { Object selItem = returnCombo.getSelectedItem(); 307 if (isConstructor || selItem == null) 308 return; 309 310 String selItemTxt = selItem.toString().trim(); 311 Type oldValue = method.getType(); 312 boolean ok = false; 313 314 try { 315 Type newValue = getJModel().getType().resolve(selItemTxt); 316 if (oldValue.equals(newValue)) { 317 return; 318 } else if (!TypeEditor.isValidTypeSyntax(selItemTxt)) { 319 notifyUserWarning("MSG_Not_Valid_Type", "invalid type"); } else { 321 method.setType(newValue); 322 ok = true; 323 } 324 } catch (JmiException e) { 325 ErrorManager.getDefault().notify(e); 326 } 327 isOK = ok; 328 if (!ok) 329 returnCombo.setSelectedItem(ElementFormat.elementName(oldValue)); 330 } 332 private void nameTextFieldFocusLost (java.awt.event.FocusEvent evt) { if ((evt != null && (evt.isTemporary() || !this.isAncestorOf(evt.getOppositeComponent()))) || isConstructor) 334 return; 335 336 String newName = nameTextField.getText().trim(); 337 String oldName = method.getName(); 338 boolean ok = false; 339 340 try { 341 if (evt == null) { if (oldName.equals(newName)) { 343 } else if (!Utilities.isJavaIdentifier(newName)) { 345 notifyUserWarning("MSG_Not_Valid_Identifier", "invalid name"); isOK = false; 347 nameTextField.setText(oldName); 348 return; 349 } else { 350 method.setName(newName); 351 } 352 353 354 Method met = this.jclass.getMethod(newName, params2Types(element.getParameters()), true); 355 if (met != null) { 356 Type retType = TypeClassImpl.getRawType(element.getType()); 357 if (retType instanceof UnresolvedClass) { Type type = getJModel().getType().resolve("java.lang." + retType.getName()); if (!(type instanceof UnresolvedClass)) 360 retType = type; 361 } 362 if (met.getDeclaringClass() == this.jclass) { 363 notifyUserWarning("MSG_Used_Identifier", newName, "invalid name"); } else if (TypeClassImpl.getRawType(met.getType()) != retType) { 365 notifyUserWarning("MSG_Another_Return_Type", newName, "invalid name"); } else if (Modifier.isFinal(met.getModifiers())) { 367 notifyUserWarning("MSG_Final_Method_Overriden", met.getDeclaringClass().getName(), "invalid name"); } else { 369 ok = true; 370 } 371 } else { 372 ok = true; 373 } 374 } else if (!Utilities.isJavaIdentifier(newName)) { 375 notifyUserWarning("MSG_Not_Valid_Identifier", "invalid name"); } else if (oldName.equals(newName)) { 377 return; } else { 379 method.setName(newName); 380 ok = true; 381 } 382 } catch (JmiException e) { 383 ErrorManager.getDefault().notify(e); 384 } 385 isOK = ok; 386 if (!ok) { 387 nameTextField.setText(oldName); 388 } 389 } 391 392 private javax.swing.JPanel accessPanel; 394 private javax.swing.JPanel exceptionsPanel; 395 private javax.swing.JLabel jLabel1; 396 private javax.swing.JLabel jLabel2; 397 private javax.swing.JLabel jLabel3; 398 private javax.swing.JLabel jLabel4; 399 private javax.swing.JLabel jLabel5; 400 private javax.swing.JTabbedPane jTabbedPane1; 401 private javax.swing.JPanel modifierPanel; 402 private javax.swing.JTextField nameTextField; 403 private javax.swing.JPanel paramsPanel; 404 private javax.swing.JComboBox returnCombo; 405 private javax.swing.JTextField typeTextField; 406 408 private void initAccessibility() { 409 nameTextField.getAccessibleContext().setAccessibleName(getString("ACS_MethodNameTextField")); nameTextField.getAccessibleContext().setAccessibleDescription(getString("ACS_MethodNameTextField")); this.getAccessibleContext().setAccessibleDescription(getString("ACSD_MethodCustomizerDialog")); } 413 414 public boolean isOK() { 415 nameTextFieldFocusLost(null); 416 if (isOK) { 417 returnComboActionPerformed(null); 418 } 419 return isOK; 420 } 421 422 private JavaModelPackage getJModel() { 423 return JavaMetamodel.getManager().getJavaExtent(this.jclass); 424 } 425 426 private static String getString(String key) { 427 return NbBundle.getMessage(MethodCustomizer.class, key); 428 } 429 430 private static void notifyUserWarning(String bundleKey, String msg) { 431 IllegalArgumentException e = new IllegalArgumentException (msg); 432 ErrorManager.getDefault().annotate( 433 e, ErrorManager.USER, null, 434 getString(bundleKey), 435 null, null); 436 ErrorManager.getDefault().notify(e); 437 } 438 439 private static void notifyUserWarning(String bundleKey, String param, String msg) { 440 IllegalArgumentException e = new IllegalArgumentException (msg); 441 ErrorManager.getDefault().annotate( 442 e, ErrorManager.USER, null, 443 NbBundle.getMessage(MethodCustomizer.class, bundleKey, param), 444 null, null); 445 ErrorManager.getDefault().notify(e); 446 } 447 448 private static List params2Types(List params) { 449 List types = new ArrayList (params.size()); 450 for (Iterator it = params.iterator(); it.hasNext();) { 451 Parameter p = (Parameter) it.next(); 452 Type t = p.getType(); 453 types.add(t); 454 } 455 return types; 456 } 457 } 458 | Popular Tags |