KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > src > nodes > 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.openide.src.nodes;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.event.KeyEvent JavaDoc;
24 import java.beans.FeatureDescriptor JavaDoc;
25 import java.util.ResourceBundle JavaDoc;
26
27 import javax.swing.*;
28 import javax.swing.border.*;
29
30 import org.openide.src.*;
31 import org.openide.explorer.propertysheet.*;
32 import org.openide.*;
33 import org.openide.util.Utilities;
34 import org.openide.util.NbBundle;
35
36 import org.openide.src.nodes.SourceEditSupport.ExceptionalRunnable;
37
38 /** Customizer for MethodElement and ConstructorElement
39  *
40  * @author Petr Hamernik
41  */

42 public class MethodCustomizer extends JPanel {
43     /** Source of the localized human presentable strings. */
44     static ResourceBundle JavaDoc bundle = NbBundle.getBundle(MethodCustomizer.class);
45
46     /** Predefined types in the type combo */
47     private static final String JavaDoc[] COMMON_TYPES = {
48         "void", // NOI18N
49
"String", // NOI18N
50
"boolean", // NOI18N
51
"char", // NOI18N
52
"int", // NOI18N
53
"long", // NOI18N
54
"byte", // NOI18N
55
"short", // NOI18N
56
"float", // NOI18N
57
"double" // NOI18N
58
};
59
60     /** Edited constructor */
61     ConstructorElement element;
62
63     /** In case that method is edited - this field holds
64     * the reference to it. Otherwise (Constructor) this field
65     * is <CODE>null</CODE>.
66     */

67     MethodElement method;
68     
69     boolean isOK = true;
70
71     /** Create new MethodCustomizer component
72     * @param element The method or constructor to be customized
73     */

74     public MethodCustomizer(ConstructorElement element) {
75         this.element = element;
76         this.method = (element instanceof MethodElement) ?
77                       (MethodElement) element : null;
78
79         initComponents ();
80
81         // borders
82
methodPanel.setBorder (new CompoundBorder(
83                                    new TitledBorder(bundle.getString("CTL_MethodFrame")),
84                                    new EmptyBorder(new java.awt.Insets JavaDoc(5, 5, 5, 5)))
85                               );
86         modifierPanel.setBorder (new TitledBorder(bundle.getString("CTL_Modifiers")));
87         paramsPanel.setBorder (new TitledBorder(bundle.getString("CTL_Parameters")));
88         exceptionsPanel.setBorder (new TitledBorder(bundle.getString("CTL_Exceptions")));
89
90         // modifiers
91
PropertyPanel modifEditor = ElementBeanModel.createModifiersPanel(element);
92         FeatureDescriptor JavaDoc fd = modifEditor.getProperty();
93         final String JavaDoc mnc = String.valueOf(KeyEvent.CHAR_UNDEFINED);
94         fd.setValue("ModifierPanel_Modifier_Abstract_Mnemonic", mnc); // NOI18N
95
fd.setValue("ModifierPanel_Modifier_Final_Mnemonic", mnc); // NOI18N
96
fd.setValue("ModifierPanel_Modifier_Static_Mnemonic", mnc); // NOI18N
97
fd.setValue("ModifierPanel_Modifier_Synchronized_Mnemonic", mnc); // NOI18N
98
fd.setValue("ModifierPanel_Modifier_Transient_Mnemonic", mnc); // NOI18N
99
fd.setValue("ModifierPanel_Modifier_Volatile_Mnemonic", mnc); // NOI18N
100
fd.setValue("ModifierPanel_Modifier_Native_Mnemonic", mnc); // NOI18N
101
modifierPanel.add(modifEditor, BorderLayout.CENTER);
102
103         // name
104
nameTextField.setText(element.getName().toString());
105         if (method == null) {
106             nameTextField.setEnabled(false);
107             returnCombo.setEnabled(false);
108         }
109         else {
110             returnCombo.setSelectedItem(method.getReturn().toString());
111         }
112
113         // parameters
114
PropertyPanel paramsEditor = ElementBeanModel.createPropertyPanel(element, ElementProperties.PROP_PARAMETERS);
115         fd = paramsEditor.getProperty();
116         fd.setValue("mnemonic_Add", bundle.getString("CTL_Parameters_Mnemonic_Add")); // NOI18N
117
fd.setValue("mnemonic_Remove", bundle.getString("CTL_Parameters_Mnemonic_Remove")); // NOI18N
118
fd.setValue("mnemonic_Up", bundle.getString("CTL_Parameters_Mnemonic_Up")); // NOI18N
119
fd.setValue("mnemonic_Down", bundle.getString("CTL_Parameters_Mnemonic_Down")); // NOI18N
120
fd.setValue("mnemonic_Edit", bundle.getString("CTL_Parameters_Mnemonic_Edit")); // NOI18N
121
paramsPanel.add(paramsEditor, BorderLayout.CENTER);
122         
123         // exceptions
124
PropertyPanel exceptionsEditor = ElementBeanModel.createPropertyPanel(element, ElementProperties.PROP_EXCEPTIONS);
125         fd = exceptionsEditor.getProperty();
126         fd.setValue("mnemonic_Add", bundle.getString("CTL_Exceptions_Mnemonic_Add"));
127         fd.setValue("mnemonic_Remove", bundle.getString("CTL_Exceptions_Mnemonic_Remove"));
128         fd.setValue("mnemonic_Up", bundle.getString("CTL_Exceptions_Mnemonic_Up"));
129         fd.setValue("mnemonic_Down", bundle.getString("CTL_Exceptions_Mnemonic_Down"));
130         fd.setValue("mnemonic_Edit", bundle.getString("CTL_Exceptions_Mnemonic_Edit"));
131         exceptionsPanel.add(exceptionsEditor, BorderLayout.CENTER);
132
133         //mnemonics
134
jLabel1.setDisplayedMnemonic(bundle.getString("CTL_Name_Mnemonic").charAt(0)); // NOI18N
135
jLabel2.setDisplayedMnemonic(bundle.getString("CTL_MethodType_Mnemonic").charAt(0)); // NOI18N
136
initAccessibility();
137     }
138     
139     public void addNotify() {
140         super.addNotify();
141         
142         int len = nameTextField.getText().length();
143         nameTextField.setCaretPosition(0);
144         nameTextField.moveCaretPosition(len);
145         SwingUtilities.invokeLater(new Runnable JavaDoc() {
146             public void run() {
147                 nameTextField.requestFocus();
148             }
149         });
150     }
151
152     /** This method is called from within the constructor to
153      * initialize the form.
154      * WARNING: Do NOT modify this code. The content of this method is
155      * always regenerated by the FormEditor.
156      */

157     private void initComponents() {//GEN-BEGIN:initComponents
158
methodPanel = new javax.swing.JPanel JavaDoc();
159         jLabel1 = new javax.swing.JLabel JavaDoc();
160         nameTextField = new javax.swing.JTextField JavaDoc();
161         jLabel2 = new javax.swing.JLabel JavaDoc();
162         returnCombo = new javax.swing.JComboBox JavaDoc(COMMON_TYPES);
163         jPanel1 = new javax.swing.JPanel JavaDoc();
164         modifierPanel = new javax.swing.JPanel JavaDoc();
165         paramsPanel = new javax.swing.JPanel JavaDoc();
166         exceptionsPanel = new javax.swing.JPanel JavaDoc();
167         
168         setLayout(new java.awt.GridBagLayout JavaDoc());
169         java.awt.GridBagConstraints JavaDoc gridBagConstraints1;
170         
171         setBorder(new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(6, 6, 6, 6)));
172         methodPanel.setLayout(new java.awt.GridBagLayout JavaDoc());
173         java.awt.GridBagConstraints JavaDoc gridBagConstraints2;
174         
175         jLabel1.setText(bundle.getString("CTL_Name"));
176         jLabel1.setLabelFor(nameTextField);
177         gridBagConstraints2 = new java.awt.GridBagConstraints JavaDoc();
178         gridBagConstraints2.insets = new java.awt.Insets JavaDoc(10, 0, 8, 8);
179         gridBagConstraints2.anchor = java.awt.GridBagConstraints.EAST;
180         methodPanel.add(jLabel1, gridBagConstraints2);
181         
182         nameTextField.addFocusListener(new java.awt.event.FocusAdapter JavaDoc() {
183             public void focusLost(java.awt.event.FocusEvent JavaDoc evt) {
184                 nameTextFieldFocusLost(evt);
185             }
186         });
187         
188         gridBagConstraints2 = new java.awt.GridBagConstraints JavaDoc();
189         gridBagConstraints2.gridwidth = java.awt.GridBagConstraints.REMAINDER;
190         gridBagConstraints2.fill = java.awt.GridBagConstraints.BOTH;
191         gridBagConstraints2.insets = new java.awt.Insets JavaDoc(10, 0, 8, 0);
192         gridBagConstraints2.weightx = 1.0;
193         methodPanel.add(nameTextField, gridBagConstraints2);
194         
195         jLabel2.setText(bundle.getString("CTL_ReturnType"));
196         jLabel2.setLabelFor(returnCombo);
197         gridBagConstraints2 = new java.awt.GridBagConstraints JavaDoc();
198         gridBagConstraints2.insets = new java.awt.Insets JavaDoc(0, 0, 0, 8);
199         gridBagConstraints2.anchor = java.awt.GridBagConstraints.EAST;
200         methodPanel.add(jLabel2, gridBagConstraints2);
201         
202         returnCombo.setEditable(true);
203         returnCombo.addActionListener(new java.awt.event.ActionListener JavaDoc() {
204             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
205                 returnComboActionPerformed(evt);
206             }
207         });
208         
209         gridBagConstraints2 = new java.awt.GridBagConstraints JavaDoc();
210         gridBagConstraints2.gridwidth = java.awt.GridBagConstraints.REMAINDER;
211         gridBagConstraints2.fill = java.awt.GridBagConstraints.BOTH;
212         gridBagConstraints2.weightx = 1.0;
213         methodPanel.add(returnCombo, gridBagConstraints2);
214         
215         gridBagConstraints2 = new java.awt.GridBagConstraints JavaDoc();
216         gridBagConstraints2.fill = java.awt.GridBagConstraints.VERTICAL;
217         gridBagConstraints2.weighty = 1.0;
218         methodPanel.add(jPanel1, gridBagConstraints2);
219         
220         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
221         gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
222         gridBagConstraints1.weightx = 1.0;
223         add(methodPanel, gridBagConstraints1);
224         
225         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
226         gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER;
227         gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
228         gridBagConstraints1.insets = new java.awt.Insets JavaDoc(0, 5, 0, 0);
229         add(modifierPanel, gridBagConstraints1);
230         
231         paramsPanel.setLayout(new java.awt.BorderLayout JavaDoc());
232         
233         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
234         gridBagConstraints1.gridx = 0;
235         gridBagConstraints1.gridy = 1;
236         gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER;
237         gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
238         gridBagConstraints1.insets = new java.awt.Insets JavaDoc(5, 0, 0, 0);
239         gridBagConstraints1.weightx = 1.0;
240         gridBagConstraints1.weighty = 1.0;
241         add(paramsPanel, gridBagConstraints1);
242         
243         exceptionsPanel.setLayout(new java.awt.BorderLayout JavaDoc());
244         
245         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
246         gridBagConstraints1.gridx = 0;
247         gridBagConstraints1.gridy = 2;
248         gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER;
249         gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
250         gridBagConstraints1.insets = new java.awt.Insets JavaDoc(5, 0, 0, 0);
251         gridBagConstraints1.weightx = 1.0;
252         gridBagConstraints1.weighty = 1.0;
253         add(exceptionsPanel, gridBagConstraints1);
254         
255     }//GEN-END:initComponents
256

257     private void returnComboActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_returnComboActionPerformed
258
if (method == null)
259                                return;
260
261                            Object JavaDoc selItem = returnCombo.getSelectedItem();
262                            Type oldValue = method.getReturn();
263                            boolean ok = false;
264                            
265                            if (selItem!=null) {
266                                try {
267                                    final Type newValue = Type.parse(selItem.toString());
268                                    if (!oldValue.equals(newValue)) {
269                                        try {
270                                            SourceEditSupport.runAsUser(method, new ExceptionalRunnable() {
271                                                public void run() throws SourceException {
272                                                    method.setReturn(newValue);
273                                                }
274                                            });
275                                            ok = true;
276                                        }
277                                        catch (SourceException e) {
278                                            ErrorManager.getDefault().notify(e);
279                                        }
280                                    } else
281                                        return;
282                                }
283                                catch (IllegalArgumentException JavaDoc e) {
284                                    ErrorManager.getDefault().annotate(
285                                    e, ErrorManager.USER, null,
286                                    bundle.getString("MSG_Not_Valid_Type"),
287                                    null, null);
288                                    ErrorManager.getDefault().notify(e);
289                                }
290                            }
291                            isOK = ok;
292                            if (!ok)
293                                returnCombo.setSelectedItem(oldValue.toString());
294                        }//GEN-LAST:event_returnComboActionPerformed
295

296                        private void nameTextFieldFocusLost (java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_nameTextFieldFocusLost
297
if ((evt != null && evt.isTemporary()) || (method == null))
298                                return;
299
300                            String JavaDoc newName = nameTextField.getText();
301                            String JavaDoc oldName = method.getName().toString();
302                            boolean ok = false;
303                            Exception JavaDoc x = null;
304                            if (Utilities.isJavaIdentifier(newName)) {
305                                if (!oldName.equals(newName)) {
306                                    final Identifier id = Identifier.create(newName);
307                                    try {
308                                        SourceEditSupport.runAsUser(method, new ExceptionalRunnable() {
309                                            public void run() throws SourceException {
310                                                method.setName(id);
311                                            }
312                                        });
313                                        ok = true;
314                                    }
315                                    catch (SourceException e) {
316                                        ErrorManager.getDefault().notify(e);
317                                    }
318                                } else
319                                    return;
320                            } else {
321                                x = new IllegalArgumentException JavaDoc("Invalid name"); // NOI18N
322
ErrorManager.getDefault().annotate(
323                                x, ErrorManager.USER, null,
324                                bundle.getString("MSG_Not_Valid_Identifier"),
325                                null, null);
326                            }
327                            isOK = ok;
328                            if (!ok) {
329                                nameTextField.setText(oldName);
330                            }
331                            if (x != null) {
332                                ErrorManager.getDefault().notify(x);
333                            }
334                        }//GEN-LAST:event_nameTextFieldFocusLost
335

336
337                        // Variables declaration - do not modify//GEN-BEGIN:variables
338
private javax.swing.JPanel JavaDoc methodPanel;
339                        private javax.swing.JLabel JavaDoc jLabel1;
340                        private javax.swing.JTextField JavaDoc nameTextField;
341                        private javax.swing.JLabel JavaDoc jLabel2;
342                        private javax.swing.JComboBox JavaDoc returnCombo;
343                        private javax.swing.JPanel JavaDoc jPanel1;
344                        private javax.swing.JPanel JavaDoc modifierPanel;
345                        private javax.swing.JPanel JavaDoc paramsPanel;
346                        private javax.swing.JPanel JavaDoc exceptionsPanel;
347                        // End of variables declaration//GEN-END:variables
348

349     private void initAccessibility() {
350         nameTextField.getAccessibleContext().setAccessibleName(bundle.getString("ACS_MethodNameTextField"));
351         nameTextField.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_MethodNameTextField"));
352         this.getAccessibleContext().setAccessibleDescription("ACSD_MethodCustomizerDialog"); // NOI18N
353
}
354     
355     public boolean isOK() {
356         nameTextFieldFocusLost(null);
357         returnComboActionPerformed(null);
358         return isOK;
359     }
360 }
361
Popular Tags