KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Modifier JavaDoc;
24
25 import javax.swing.*;
26 import javax.swing.border.*;
27 import javax.jmi.reflect.JmiException;
28
29 import org.openide.*;
30 import org.openide.explorer.propertysheet.PropertyPanel;
31 import org.openide.util.Utilities;
32 import org.openide.util.NbBundle;
33 import org.netbeans.jmi.javamodel.JavaClass;
34 import org.netbeans.jmi.javamodel.JavaModelPackage;
35 import org.netbeans.jmi.javamodel.MultipartId;
36 import org.netbeans.jmi.javamodel.Resource;
37 import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
38
39 /** Customizer for ClassElement
40  *
41  * @author Petr Hamernik
42  */

43 final class ClassCustomizer extends JPanel {
44     /** Predefined types in the type combo */
45     private static final String JavaDoc[] COMMON_TYPES = {
46         "java.lang.Object", // NOI18N
47
"java.awt.Component", // NOI18N
48
"javax.swing.JComponent", // NOI18N
49
"javax.swing.JPanel" // NOI18N
50
};
51
52     /** The edited class */
53     private final JavaClass element;
54     /**
55      * class where the customized class or interface should be added; can be null
56      */

57     private final JavaClass jclass;
58     /** java resource where the customized class or interface should be added; can be null */
59     private final Resource resource;
60     
61     private boolean isOK = true;
62     
63     /** Create new ClassCustomizer component for inner classes
64     * @param jclass class where the customized inner class or inner interface should be added
65     * @param element The class to be customized
66     */

67     public ClassCustomizer(JavaClass jclass, JavaClass element) {
68         this(null, jclass, element);
69     }
70     
71     /** Create new ClassCustomizer component for top-level classes
72     * @param resource java resource where the customized class or interface should be added
73     * @param element The class to be customized
74     */

75     public ClassCustomizer(Resource resource, JavaClass element) {
76         this(resource, null, element);
77     }
78     
79     private ClassCustomizer(Resource resource, JavaClass jclass, JavaClass element) {
80         this.resource = resource;
81         this.jclass = jclass;
82         this.element = element;
83         initComponents ();
84         
85         //borders
86
interfacesPanel.setBorder(new TitledBorder(NbBundle.getMessage(ClassCustomizer.class, "CTL_Interfaces"))); // NOI18N
87

88         // modifiers
89
int mask = Modifier.PUBLIC | Modifier.ABSTRACT;
90         if (resource == null) { // inner class
91
mask |= Modifier.PROTECTED | Modifier.PRIVATE | Modifier.STATIC;
92         }
93         if (!element.isInterface()) {
94             mask |= Modifier.FINAL;
95         }
96         accessPanel.add(SourceEditSupport.createAccessModifiersPanel(element, mask), BorderLayout.CENTER);
97         modifierPanel.add(SourceEditSupport.createOtherModifiersPanel(element, mask), BorderLayout.CENTER);
98
99         if (element.isInterface()) {
100             superClassCombo.setEnabled(false);
101             jLabel2.setVisible(false);
102             superClassCombo.setVisible(false);
103         } else {
104             JavaClass superclassId = element.getSuperClass();
105             superClassCombo.setSelectedItem(
106                     (superclassId == null) ? "" : superclassId.getName()); // NOI18N
107

108         }
109
110         // name
111
nameTextField.setText(element.getSimpleName());
112
113         // interfaces
114
interfacesPanel.add(
115                 new PropertyPanel(ClassNode.createInterfacesProperty(element, true), PropertyPanel.PREF_CUSTOM_EDITOR),
116                 BorderLayout.CENTER
117         );
118
119         //mnemonics
120
jLabel1.setDisplayedMnemonic(NbBundle.getMessage(ClassCustomizer.class, "CTL_Name_Mnemonic").charAt(0)); // NOI18N
121
jLabel2.setDisplayedMnemonic(NbBundle.getMessage(ClassCustomizer.class, "CTL_Type_Mnemonic").charAt(0)); // NOI18N
122

123         // XXX generics not yet implemented
124
jLabel3.setVisible(false);
125         typeTextField.setVisible(false);
126         
127         initAccessibility();
128     }
129
130     public void addNotify() {
131         super.addNotify();
132
133         // select the name
134
int l = nameTextField.getText().length();
135         nameTextField.setCaretPosition(0);
136         nameTextField.moveCaretPosition(l);
137         SwingUtilities.invokeLater(new Runnable JavaDoc() {
138             public void run() {
139                 nameTextField.requestFocus();
140             }
141         });
142     }
143     
144     /** This method is called from within the class to
145      * initialize the form.
146      * WARNING: Do NOT modify this code. The content of this method is
147      * always regenerated by the FormEditor.
148      */

149     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
150
private void initComponents() {
151         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
152
153         jLabel1 = new javax.swing.JLabel JavaDoc();
154         nameTextField = new javax.swing.JTextField JavaDoc();
155         jLabel3 = new javax.swing.JLabel JavaDoc();
156         typeTextField = new javax.swing.JTextField JavaDoc();
157         jLabel2 = new javax.swing.JLabel JavaDoc();
158         superClassCombo = new javax.swing.JComboBox JavaDoc(COMMON_TYPES);
159         jLabel4 = new javax.swing.JLabel JavaDoc();
160         accessPanel = new javax.swing.JPanel JavaDoc();
161         jLabel5 = new javax.swing.JLabel JavaDoc();
162         modifierPanel = new javax.swing.JPanel JavaDoc();
163         interfacesPanel = new javax.swing.JPanel JavaDoc();
164
165         setLayout(new java.awt.GridBagLayout JavaDoc());
166
167         setBorder(new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(6, 6, 6, 6)));
168         jLabel1.setLabelFor(nameTextField);
169         org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(ClassCustomizer.class, "CTL_Name"));
170         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
171         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
172         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 0, 0);
173         add(jLabel1, gridBagConstraints);
174
175         nameTextField.addFocusListener(new java.awt.event.FocusAdapter JavaDoc() {
176             public void focusLost(java.awt.event.FocusEvent JavaDoc evt) {
177                 nameTextFieldFocusLost(evt);
178             }
179         });
180
181         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
182         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
183         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
184         gridBagConstraints.weightx = 1.0;
185         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 8, 0, 0);
186         add(nameTextField, gridBagConstraints);
187         nameTextField.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ClassCustomizer.class, "ACSD_ClassNameTextField"));
188
189         jLabel3.setLabelFor(typeTextField);
190         org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(ClassCustomizer.class, "CTL_Generic_Type"));
191         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
192         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
193         gridBagConstraints.insets = new java.awt.Insets JavaDoc(8, 0, 0, 0);
194         add(jLabel3, gridBagConstraints);
195
196         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
197         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
198         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
199         gridBagConstraints.weightx = 1.0;
200         gridBagConstraints.insets = new java.awt.Insets JavaDoc(8, 8, 0, 0);
201         add(typeTextField, gridBagConstraints);
202         typeTextField.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ClassCustomizer.class, "ACSD_ClassType"));
203
204         jLabel2.setLabelFor(superClassCombo);
205         org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(ClassCustomizer.class, "CTL_Superclass"));
206         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
207         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
208         gridBagConstraints.insets = new java.awt.Insets JavaDoc(8, 0, 0, 0);
209         add(jLabel2, gridBagConstraints);
210
211         superClassCombo.setEditable(true);
212         superClassCombo.addActionListener(new java.awt.event.ActionListener JavaDoc() {
213             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
214                 superClassComboActionPerformed(evt);
215             }
216         });
217
218         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
219         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
220         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
221         gridBagConstraints.weightx = 1.0;
222         gridBagConstraints.insets = new java.awt.Insets JavaDoc(8, 8, 0, 0);
223         add(superClassCombo, gridBagConstraints);
224         superClassCombo.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ClassCustomizer.class, "ACSD_SuperClass"));
225
226         jLabel4.setLabelFor(accessPanel);
227         org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getMessage(ClassCustomizer.class, "CTL_AccessRights"));
228         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
229         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
230         gridBagConstraints.insets = new java.awt.Insets JavaDoc(8, 0, 0, 0);
231         add(jLabel4, gridBagConstraints);
232
233         accessPanel.setLayout(new java.awt.BorderLayout JavaDoc());
234
235         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
236         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
237         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
238         gridBagConstraints.insets = new java.awt.Insets JavaDoc(8, 8, 0, 0);
239         add(accessPanel, gridBagConstraints);
240
241         jLabel5.setLabelFor(modifierPanel);
242         org.openide.awt.Mnemonics.setLocalizedText(jLabel5, org.openide.util.NbBundle.getMessage(ClassCustomizer.class, "CTL_Modifiers"));
243         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
244         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
245         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
246         gridBagConstraints.insets = new java.awt.Insets JavaDoc(8, 0, 0, 0);
247         add(jLabel5, gridBagConstraints);
248
249         modifierPanel.setLayout(new java.awt.BorderLayout JavaDoc());
250
251         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
252         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
253         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
254         gridBagConstraints.insets = new java.awt.Insets JavaDoc(8, 8, 0, 0);
255         add(modifierPanel, gridBagConstraints);
256
257         interfacesPanel.setLayout(new java.awt.BorderLayout JavaDoc());
258
259         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
260         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
261         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
262         gridBagConstraints.weightx = 1.0;
263         gridBagConstraints.weighty = 1.0;
264         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 0, 0, 0);
265         add(interfacesPanel, gridBagConstraints);
266
267     }
268     // </editor-fold>//GEN-END:initComponents
269

270     private void superClassComboActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_superClassComboActionPerformed
271
if (!superClassCombo.isEnabled()) return;
272         
273         JavaClass oldValue = element.getSuperClass();
274         String JavaDoc oldValueStr = (oldValue == null) ? "" : oldValue.getName(); // NOI18N
275
Object JavaDoc item = superClassCombo.getSelectedItem();
276
277         if (item == null) {
278             // user held a CTRL -- bug in Swing ?!?
279
superClassCombo.setSelectedItem(oldValueStr);
280             return;
281         }
282         String JavaDoc newValueStr = item.toString().trim();
283         boolean ok = false;
284         if (!oldValueStr.equals(newValueStr)) {
285             try {
286                 MultipartId mid = getJModel().getMultipartId().createMultipartId(newValueStr, null, null);
287                 element.setSuperClassName(mid);
288                 ok = true;
289             } catch (JmiException e) {
290                 ErrorManager.getDefault().notify(e);
291             }
292         } else {
293             return; // nothing to change
294
}
295         isOK = ok;
296         if (!ok)
297             superClassCombo.setSelectedItem(oldValueStr);
298     }//GEN-LAST:event_superClassComboActionPerformed
299

300     private void nameTextFieldFocusLost (java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_nameTextFieldFocusLost
301
if (evt != null && (evt.isTemporary() || !this.isAncestorOf(evt.getOppositeComponent())))
302             return;
303
304         String JavaDoc newName = nameTextField.getText().trim();
305         String JavaDoc oldName = element.getSimpleName();
306         boolean ok = false;
307         
308         if (!Utilities.isJavaIdentifier(newName)) {
309             IllegalArgumentException JavaDoc x = new IllegalArgumentException JavaDoc("Invalid name: " + newName); // NOI18N
310
ErrorManager.getDefault().annotate(
311                 x, ErrorManager.USER, null,
312                 NbBundle.getMessage(ClassCustomizer.class, "MSG_Not_Valid_Identifier"), // NOI18N
313
null, null);
314             ErrorManager.getDefault().notify (x);
315         } else if (oldName.equals(newName)) {
316             return; // nothing to change
317
} else if (this.resource == null && jclass.getInnerClass(newName, true) != null) {
318             IllegalArgumentException JavaDoc x = new IllegalArgumentException JavaDoc("Invalid name: " + newName); // NOI18N
319
ErrorManager.getDefault().annotate(
320                 x, ErrorManager.USER, null,
321                 NbBundle.getMessage(ClassCustomizer.class, "MSG_Used_Identifier"), // NOI18N
322
null, null);
323             ErrorManager.getDefault().notify (x);
324         } else if (this.jclass == null && SourceEditSupport.findTopLevelClass(this.resource, newName) != null) {
325             IllegalArgumentException JavaDoc x = new IllegalArgumentException JavaDoc("Invalid name: " + newName); // NOI18N
326
ErrorManager.getDefault().annotate(
327                 x, ErrorManager.USER, null,
328                 NbBundle.getMessage(ClassCustomizer.class, "MSG_Used_Identifier"), // NOI18N
329
null, null);
330             ErrorManager.getDefault().notify (x);
331         } else {
332             try {
333                 element.setSimpleName(newName);
334                 ok = true;
335             } catch (JmiException e) {
336                 ErrorManager.getDefault().notify(e);
337             }
338         }
339         isOK = ok;
340         if (!ok) {
341             nameTextField.setText(oldName);
342         }
343     }//GEN-LAST:event_nameTextFieldFocusLost
344

345
346     // Variables declaration - do not modify//GEN-BEGIN:variables
347
private javax.swing.JPanel JavaDoc accessPanel;
348     private javax.swing.JPanel JavaDoc interfacesPanel;
349     private javax.swing.JLabel JavaDoc jLabel1;
350     private javax.swing.JLabel JavaDoc jLabel2;
351     private javax.swing.JLabel JavaDoc jLabel3;
352     private javax.swing.JLabel JavaDoc jLabel4;
353     private javax.swing.JLabel JavaDoc jLabel5;
354     private javax.swing.JPanel JavaDoc modifierPanel;
355     private javax.swing.JTextField JavaDoc nameTextField;
356     private javax.swing.JComboBox JavaDoc superClassCombo;
357     private javax.swing.JTextField JavaDoc typeTextField;
358     // End of variables declaration//GEN-END:variables
359

360     
361     private void initAccessibility() {
362         nameTextField.getAccessibleContext().setAccessibleName(NbBundle.getMessage(ClassCustomizer.class, "ACS_ClassNameTextField")); // NOI18N
363
nameTextField.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(ClassCustomizer.class, "ACS_ClassNameTextField")); // NOI18N
364
this.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(ClassCustomizer.class, "ACSD_ClassCustomizerDialog")); // NOI18N
365
}
366     
367     public boolean isOK() {
368         superClassComboActionPerformed(null);
369         if (isOK) {
370             nameTextFieldFocusLost(null);
371         }
372         return isOK;
373     }
374     
375     private JavaModelPackage getJModel() {
376         return jclass != null?
377                 JavaMetamodel.getManager().getJavaExtent(jclass):
378                 JavaMetamodel.getManager().getJavaExtent(resource);
379     }
380 }
381
Popular Tags