KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.netbeans.jmi.javamodel.JavaClass;
23 import org.netbeans.jmi.javamodel.AnnotationType;
24 import org.netbeans.jmi.javamodel.Resource;
25 import org.openide.util.HelpCtx;
26 import org.openide.util.NbBundle;
27 import org.openide.util.Utilities;
28 import org.openide.ErrorManager;
29
30 import javax.jmi.reflect.JmiException;
31 import javax.swing.*;
32 import java.awt.*;
33 import java.lang.reflect.Modifier JavaDoc;
34
35 /** Customizer for AnnotationType
36  *
37  * @author Jan Pokorsky
38  */

39 public class AnnotationTypeCustomizer extends javax.swing.JPanel JavaDoc {
40     /** The edited class */
41     private final JavaClass element;
42     /**
43      * class where the customized annotation type should be added; can be null
44      */

45     private final JavaClass jclass;
46     /** java resource where the annotation type should be added; can be null */
47     private final Resource resource;
48     
49     private boolean isOK = true;
50     
51     /** Create new AnnotationTypeCustomizer component for inner annotation types
52     * @param jclass class where the customized inner class or inner interface should be added
53     * @param element The annotation type to be customized
54     */

55     public AnnotationTypeCustomizer(JavaClass jclass, AnnotationType element) {
56         this(null, jclass, element);
57     }
58     
59     /** Create new ClassCustomizer component for top-level annotation types
60     * @param resource java resource where the customized annotation type should be added
61     * @param element The annotation type to be customized
62     */

63     public AnnotationTypeCustomizer(Resource resource, AnnotationType element) {
64         this(resource, null, element);
65     }
66     
67     private AnnotationTypeCustomizer(Resource resource, JavaClass jclass, AnnotationType element) {
68         this.resource = resource;
69         this.jclass = jclass;
70         this.element = element;
71         
72 // modifiersPP = SourceEditSupport.createModifiersPanel(element);
73
// me = (ModifierEditor) modifiersPP.getProperty().getPropertyEditor();
74

75         initComponents();
76         
77         nameTextField.setText(element.getSimpleName());
78         
79         int mask = Modifier.PUBLIC | Modifier.ABSTRACT;
80         if (resource == null) { // inner class
81
mask |= Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE | Modifier.STATIC;
82         }
83         accessPanel.add(SourceEditSupport.createAccessModifiersPanel(this.element, mask), BorderLayout.CENTER);
84         modifiersPanel.add(SourceEditSupport.createOtherModifiersPanel(this.element, mask), BorderLayout.CENTER);
85         
86         HelpCtx.setHelpIDString (this, "java.annotationType.customizer"); // NOI18N
87

88         nameLabel.setDisplayedMnemonic(NbBundle.getMessage(AnnotationTypeCustomizer.class, "CTL_Name_Mnemonic").charAt(0)); // NOI18N
89
nameTextField.getAccessibleContext().setAccessibleName(NbBundle.getMessage(AnnotationTypeCustomizer.class, "ACSN_AnnotationTypeNameTextField")); // NOI18N
90
nameTextField.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(AnnotationTypeCustomizer.class, "ACSD_AnnotationTypeNameTextField")); // NOI18N
91

92         accessLabel.setDisplayedMnemonic(NbBundle.getMessage(AnnotationTypeCustomizer.class, "CTL_AccessRights_Mnemonic").charAt(0)); // NOI18N
93
this.getAccessibleContext().setAccessibleDescription("ACSD_AnnotationTypeCustomizer"); // NOI18N
94
}
95     
96     public void addNotify() {
97         super.addNotify();
98
99         // select the name
100
int l = nameTextField.getText().length();
101         nameTextField.setCaretPosition(0);
102         nameTextField.moveCaretPosition(l);
103         SwingUtilities.invokeLater(new Runnable JavaDoc() {
104             public void run() {
105                 nameTextField.requestFocus();
106             }
107         });
108     }
109     
110     /** This method is called from within the constructor to
111      * initialize the form.
112      * WARNING: Do NOT modify this code. The content of this method is
113      * always regenerated by the Form Editor.
114      */

115     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
116
private void initComponents() {
117         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
118
119         nameLabel = new javax.swing.JLabel JavaDoc();
120         nameTextField = new javax.swing.JTextField JavaDoc();
121         accessLabel = new javax.swing.JLabel JavaDoc();
122         accessPanel = new javax.swing.JPanel JavaDoc();
123         modifiersLabel = new javax.swing.JLabel JavaDoc();
124         modifiersPanel = new javax.swing.JPanel JavaDoc();
125         jPanel1 = new javax.swing.JPanel JavaDoc();
126
127         setLayout(new java.awt.GridBagLayout JavaDoc());
128
129         setBorder(new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(5, 5, 5, 5)));
130         nameLabel.setLabelFor(nameTextField);
131         org.openide.awt.Mnemonics.setLocalizedText(nameLabel, org.openide.util.NbBundle.getMessage(AnnotationTypeCustomizer.class, "CTL_Name"));
132         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
133         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
134         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 0, 0);
135         add(nameLabel, gridBagConstraints);
136
137         nameTextField.setColumns(25);
138         nameTextField.addFocusListener(new java.awt.event.FocusAdapter JavaDoc() {
139             public void focusLost(java.awt.event.FocusEvent JavaDoc evt) {
140                 nameTextFieldFocusLost(evt);
141             }
142         });
143
144         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
145         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
146         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
147         gridBagConstraints.weightx = 1.0;
148         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 8, 0, 0);
149         add(nameTextField, gridBagConstraints);
150
151         accessLabel.setLabelFor(accessPanel);
152         org.openide.awt.Mnemonics.setLocalizedText(accessLabel, org.openide.util.NbBundle.getMessage(AnnotationTypeCustomizer.class, "CTL_AccessRights"));
153         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
154         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
155         gridBagConstraints.insets = new java.awt.Insets JavaDoc(8, 0, 0, 0);
156         add(accessLabel, gridBagConstraints);
157
158         accessPanel.setLayout(new java.awt.BorderLayout JavaDoc());
159
160         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
161         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
162         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
163         gridBagConstraints.insets = new java.awt.Insets JavaDoc(8, 8, 0, 0);
164         add(accessPanel, gridBagConstraints);
165
166         modifiersLabel.setLabelFor(modifiersPanel);
167         org.openide.awt.Mnemonics.setLocalizedText(modifiersLabel, org.openide.util.NbBundle.getMessage(AnnotationTypeCustomizer.class, "CTL_Modifiers"));
168         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
169         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
170         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
171         gridBagConstraints.insets = new java.awt.Insets JavaDoc(8, 0, 0, 0);
172         add(modifiersLabel, gridBagConstraints);
173
174         modifiersPanel.setLayout(new java.awt.BorderLayout JavaDoc());
175
176         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
177         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
178         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
179         gridBagConstraints.insets = new java.awt.Insets JavaDoc(8, 8, 0, 8);
180         add(modifiersPanel, gridBagConstraints);
181
182         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
183         gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
184         gridBagConstraints.weighty = 1.0;
185         add(jPanel1, gridBagConstraints);
186
187     }
188     // </editor-fold>//GEN-END:initComponents
189

190     private void nameTextFieldFocusLost(java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_nameTextFieldFocusLost
191
if (evt != null && evt.isTemporary())
192             return;
193
194         String JavaDoc newName = nameTextField.getText().trim();
195         String JavaDoc oldName = element.getSimpleName();
196         boolean ok = false;
197         
198         if (!Utilities.isJavaIdentifier(newName)) {
199             IllegalArgumentException JavaDoc x = new IllegalArgumentException JavaDoc("Invalid name: " + newName); // NOI18N
200
ErrorManager.getDefault().annotate(
201                 x, ErrorManager.USER, null,
202                 NbBundle.getMessage(AnnotationTypeCustomizer.class, "MSG_Not_Valid_Identifier"), // NOI18N
203
null, null);
204             ErrorManager.getDefault().notify (x);
205         } else if (oldName.equals(newName)) {
206             return; // nothing to change
207
} else if (this.resource == null && jclass.getInnerClass(newName, true) != null) {
208             IllegalArgumentException JavaDoc x = new IllegalArgumentException JavaDoc("Invalid name: " + newName); // NOI18N
209
ErrorManager.getDefault().annotate(
210                 x, ErrorManager.USER, null,
211                 NbBundle.getMessage(AnnotationTypeCustomizer.class, "MSG_Not_Valid_Identifier"), // NOI18N
212
null, null);
213             ErrorManager.getDefault().notify (x);
214         } else if (this.jclass == null && SourceEditSupport.findTopLevelClass(this.resource, newName) != null) {
215             IllegalArgumentException JavaDoc x = new IllegalArgumentException JavaDoc("Invalid name: " + newName); // NOI18N
216
ErrorManager.getDefault().annotate(
217                 x, ErrorManager.USER, null,
218                 NbBundle.getMessage(AnnotationTypeCustomizer.class, "MSG_Not_Valid_Identifier"), // NOI18N
219
null, null);
220             ErrorManager.getDefault().notify (x);
221         } else {
222             try {
223                 element.setSimpleName(newName);
224                 ok = true;
225             } catch (JmiException e) {
226                 ErrorManager.getDefault().notify(e);
227             }
228         }
229         isOK = ok;
230         if (!ok) {
231             nameTextField.setText(oldName);
232         }
233     }//GEN-LAST:event_nameTextFieldFocusLost
234

235     
236     // Variables declaration - do not modify//GEN-BEGIN:variables
237
private javax.swing.JLabel JavaDoc accessLabel;
238     private javax.swing.JPanel JavaDoc accessPanel;
239     private javax.swing.JPanel JavaDoc jPanel1;
240     private javax.swing.JLabel JavaDoc modifiersLabel;
241     private javax.swing.JPanel JavaDoc modifiersPanel;
242     private javax.swing.JLabel JavaDoc nameLabel;
243     private javax.swing.JTextField JavaDoc nameTextField;
244     // End of variables declaration//GEN-END:variables
245

246     public boolean isOK() {
247         nameTextFieldFocusLost(null);
248         return isOK;
249     }
250     
251 }
252
Popular Tags