KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > ui > nodes > elements > MethodCustomizer


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.java.ui.nodes.elements;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.event.KeyEvent JavaDoc;
24 import java.beans.FeatureDescriptor JavaDoc;
25 import java.lang.reflect.Modifier JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.ArrayList JavaDoc;
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 /** Customizer for MethodElement and ConstructorElement
44  *
45  * @author Petr Hamernik
46  */

47 public final class MethodCustomizer extends JPanel {
48     /** Predefined types in the type combo;
49      * duplicate of {@link org.netbeans.modules.java.ui.nodes.editors.TypeEditor#RVALUE_TYPES}
50      */

51     private static final String JavaDoc[] COMMON_TYPES = TypeEditor.RVALUE_TYPES;
52
53     /**
54      * class where the customized method should be added
55      */

56     private final JavaClass jclass;
57     
58     /** Edited constructor */
59     private final CallableFeature element;
60
61     /** In case that method is edited - this field holds
62     * the reference to it. Otherwise (Constructor) this field
63     * is <CODE>null</CODE>.
64     */

65     private final Method method;
66     
67     private final boolean isConstructor;
68     
69     private boolean isOK = true;
70
71     /** Create new MethodCustomizer component
72     * @param jclass class where the customized method should be added
73     * @param element The method or constructor to be customized
74     */

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         // modifiers
84
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 JavaDoc fd = modifEditor.getProperty();
98         final String JavaDoc mnc = String.valueOf(KeyEvent.CHAR_UNDEFINED);
99         fd.setValue("ModifierPanel_Modifier_Abstract_Mnemonic", mnc); // NOI18N
100
fd.setValue("ModifierPanel_Modifier_Final_Mnemonic", mnc); // NOI18N
101
fd.setValue("ModifierPanel_Modifier_Static_Mnemonic", mnc); // NOI18N
102
fd.setValue("ModifierPanel_Modifier_Synchronized_Mnemonic", mnc); // NOI18N
103
fd.setValue("ModifierPanel_Modifier_Transient_Mnemonic", mnc); // NOI18N
104
fd.setValue("ModifierPanel_Modifier_Volatile_Mnemonic", mnc); // NOI18N
105
fd.setValue("ModifierPanel_Modifier_Native_Mnemonic", mnc); // NOI18N
106
modifierPanel.add(modifEditor, BorderLayout.CENTER);
107
108         // name
109
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         // parameters
120
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")); // NOI18N
126
fd.setValue("mnemonic_Remove", getString("CTL_Parameters_Mnemonic_Remove")); // NOI18N
127
fd.setValue("mnemonic_Up", getString("CTL_Parameters_Mnemonic_Up")); // NOI18N
128
fd.setValue("mnemonic_Down", getString("CTL_Parameters_Mnemonic_Down")); // NOI18N
129
fd.setValue("mnemonic_Edit", getString("CTL_Parameters_Mnemonic_Edit")); // NOI18N
130
paramsPanel.add(paramsEditor, BorderLayout.CENTER);
131         
132         // exceptions
133
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")); // NOI18N
139
fd.setValue("mnemonic_Remove", getString("CTL_Parameters_Mnemonic_Remove")); // NOI18N
140
fd.setValue("mnemonic_Up", getString("CTL_Parameters_Mnemonic_Up")); // NOI18N
141
fd.setValue("mnemonic_Down", getString("CTL_Parameters_Mnemonic_Down")); // NOI18N
142
fd.setValue("mnemonic_Edit", getString("CTL_Parameters_Mnemonic_Edit")); // NOI18N
143
exceptionsPanel.add(exceptionsEditor, BorderLayout.CENTER);
144
145         //mnemonics
146
jLabel1.setDisplayedMnemonic(getString("CTL_Name_Mnemonic").charAt(0)); // NOI18N
147
jLabel2.setDisplayedMnemonic(getString("CTL_MethodType_Mnemonic").charAt(0)); // NOI18N
148

149         // XXX generics not yet implemented
150
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 JavaDoc() {
163             public void run() {
164                 nameTextField.requestFocus();
165             }
166         });
167     }
168
169     /** This method is called from within the constructor to
170      * initialize the form.
171      * WARNING: Do NOT modify this code. The content of this method is
172      * always regenerated by the FormEditor.
173      */

174     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
175
private void initComponents() {
176         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
177
178         jLabel1 = new javax.swing.JLabel JavaDoc();
179         nameTextField = new javax.swing.JTextField JavaDoc();
180         jLabel3 = new javax.swing.JLabel JavaDoc();
181         typeTextField = new javax.swing.JTextField JavaDoc();
182         jLabel2 = new javax.swing.JLabel JavaDoc();
183         returnCombo = new javax.swing.JComboBox JavaDoc(COMMON_TYPES);
184         jLabel4 = new javax.swing.JLabel JavaDoc();
185         accessPanel = new javax.swing.JPanel JavaDoc();
186         jLabel5 = new javax.swing.JLabel JavaDoc();
187         modifierPanel = new javax.swing.JPanel JavaDoc();
188         jTabbedPane1 = new javax.swing.JTabbedPane JavaDoc();
189         paramsPanel = new javax.swing.JPanel JavaDoc();
190         exceptionsPanel = new javax.swing.JPanel JavaDoc();
191
192         setLayout(new java.awt.GridBagLayout JavaDoc());
193
194         setBorder(new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(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 JavaDoc();
198         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
199         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 0, 0);
200         add(jLabel1, gridBagConstraints);
201
202         nameTextField.addFocusListener(new java.awt.event.FocusAdapter JavaDoc() {
203             public void focusLost(java.awt.event.FocusEvent JavaDoc evt) {
204                 nameTextFieldFocusLost(evt);
205             }
206         });
207
208         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc(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 JavaDoc();
219         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
220         gridBagConstraints.insets = new java.awt.Insets JavaDoc(8, 0, 0, 0);
221         add(jLabel3, gridBagConstraints);
222
223         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc(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 JavaDoc();
234         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
235         gridBagConstraints.insets = new java.awt.Insets JavaDoc(8, 0, 0, 0);
236         add(jLabel2, gridBagConstraints);
237
238         returnCombo.setEditable(true);
239         returnCombo.addActionListener(new java.awt.event.ActionListener JavaDoc() {
240             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
241                 returnComboActionPerformed(evt);
242             }
243         });
244
245         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc(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 JavaDoc();
256         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
257         gridBagConstraints.insets = new java.awt.Insets JavaDoc(8, 0, 0, 0);
258         add(jLabel4, gridBagConstraints);
259
260         accessPanel.setLayout(new java.awt.BorderLayout JavaDoc());
261
262         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
263         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
264         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
265         gridBagConstraints.insets = new java.awt.Insets JavaDoc(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 JavaDoc();
271         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
272         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
273         gridBagConstraints.insets = new java.awt.Insets JavaDoc(8, 0, 0, 0);
274         add(jLabel5, gridBagConstraints);
275
276         modifierPanel.setLayout(new java.awt.BorderLayout JavaDoc());
277
278         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
279         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
280         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
281         gridBagConstraints.insets = new java.awt.Insets JavaDoc(8, 8, 0, 0);
282         add(modifierPanel, gridBagConstraints);
283
284         paramsPanel.setLayout(new java.awt.BorderLayout JavaDoc());
285
286         paramsPanel.setBorder(new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(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 JavaDoc());
290
291         exceptionsPanel.setBorder(new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(0, 0, 10, 0)));
292         jTabbedPane1.addTab(org.openide.util.NbBundle.getMessage(MethodCustomizer.class, "CTL_Exceptions"), exceptionsPanel);
293
294         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc(5, 0, 0, 0);
300         add(jTabbedPane1, gridBagConstraints);
301
302     }
303     // </editor-fold>//GEN-END:initComponents
304

305     private void returnComboActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_returnComboActionPerformed
306
Object JavaDoc selItem = returnCombo.getSelectedItem();
307         if (isConstructor || selItem == null)
308            return;
309         
310         String JavaDoc 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"); // NOI18N
320
} 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     }//GEN-LAST:event_returnComboActionPerformed
331

332     private void nameTextFieldFocusLost (java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_nameTextFieldFocusLost
333
if ((evt != null && (evt.isTemporary() || !this.isAncestorOf(evt.getOppositeComponent()))) || isConstructor)
334             return;
335         
336         String JavaDoc newName = nameTextField.getText().trim();
337         String JavaDoc oldName = method.getName();
338         boolean ok = false;
339         
340         try {
341             if (evt == null) { // final check for OK button
342
if (oldName.equals(newName)) {
343                     // no name change on OK button
344
} else if (!Utilities.isJavaIdentifier(newName)) {
345                     notifyUserWarning("MSG_Not_Valid_Identifier", "invalid name"); // NOI18N
346
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) { // [PENDING]
358
Type type = getJModel().getType().resolve("java.lang." + retType.getName()); // NOI18N
359
if (!(type instanceof UnresolvedClass))
360                             retType = type;
361                     }
362                     if (met.getDeclaringClass() == this.jclass) {
363                         notifyUserWarning("MSG_Used_Identifier", newName, "invalid name"); // NOI18N
364
} else if (TypeClassImpl.getRawType(met.getType()) != retType) {
365                         notifyUserWarning("MSG_Another_Return_Type", newName, "invalid name"); // NOI18N
366
} else if (Modifier.isFinal(met.getModifiers())) {
367                         notifyUserWarning("MSG_Final_Method_Overriden", met.getDeclaringClass().getName(), "invalid name"); // NOI18N
368
} 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"); // NOI18N
376
} else if (oldName.equals(newName)) {
377                 return; // nothing to change
378
} 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     }//GEN-LAST:event_nameTextFieldFocusLost
390

391     
392     // Variables declaration - do not modify//GEN-BEGIN:variables
393
private javax.swing.JPanel JavaDoc accessPanel;
394     private javax.swing.JPanel JavaDoc exceptionsPanel;
395     private javax.swing.JLabel JavaDoc jLabel1;
396     private javax.swing.JLabel JavaDoc jLabel2;
397     private javax.swing.JLabel JavaDoc jLabel3;
398     private javax.swing.JLabel JavaDoc jLabel4;
399     private javax.swing.JLabel JavaDoc jLabel5;
400     private javax.swing.JTabbedPane JavaDoc jTabbedPane1;
401     private javax.swing.JPanel JavaDoc modifierPanel;
402     private javax.swing.JTextField JavaDoc nameTextField;
403     private javax.swing.JPanel JavaDoc paramsPanel;
404     private javax.swing.JComboBox JavaDoc returnCombo;
405     private javax.swing.JTextField JavaDoc typeTextField;
406     // End of variables declaration//GEN-END:variables
407

408     private void initAccessibility() {
409         nameTextField.getAccessibleContext().setAccessibleName(getString("ACS_MethodNameTextField")); // NOI18N
410
nameTextField.getAccessibleContext().setAccessibleDescription(getString("ACS_MethodNameTextField")); // NOI18N
411
this.getAccessibleContext().setAccessibleDescription(getString("ACSD_MethodCustomizerDialog")); // NOI18N
412
}
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 JavaDoc getString(String JavaDoc key) {
427         return NbBundle.getMessage(MethodCustomizer.class, key);
428     }
429     
430     private static void notifyUserWarning(String JavaDoc bundleKey, String JavaDoc msg) {
431         IllegalArgumentException JavaDoc e = new IllegalArgumentException JavaDoc(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 JavaDoc bundleKey, String JavaDoc param, String JavaDoc msg) {
440         IllegalArgumentException JavaDoc e = new IllegalArgumentException JavaDoc(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 JavaDoc/*<Type>*/ params2Types(List JavaDoc/*<Parameter>*/ params) {
449         List JavaDoc/*<Type>*/ types = new ArrayList JavaDoc/*<Type>*/(params.size());
450         for (Iterator JavaDoc 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