KickJava   Java API By Example, From Geeks To Geeks.

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


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.openide.util.HelpCtx;
34 import org.netbeans.jmi.javamodel.JavaClass;
35 import org.netbeans.jmi.javamodel.JavaEnum;
36 import org.netbeans.jmi.javamodel.Resource;
37
38 /** Customizer for {@link org.netbeans.jmi.javamodel.JavaEnum}
39  *
40  * @author Jan Pokorsky
41  */

42 final class EnumCustomizer extends JPanel {
43     /** The edited enumeration */
44     private final JavaEnum element;
45     /**
46      * class where the customized class or interface should be added; can be null
47      */

48     private final JavaClass jclass;
49     /** java resource where the customized class or interface should be added; can be null */
50     private final Resource resource;
51     
52     private boolean isOK = true;
53     
54     /** Create new EnumCustomizer component for inner classes
55     * @param jclass class where the customized inner class or inner interface should be added
56     * @param element The enumeration to be customized
57     */

58     public EnumCustomizer(JavaClass jclass, JavaEnum element) {
59         this(null, jclass, element);
60     }
61     
62     /** Create new EnumCustomizer component for top-level classes
63     * @param resource java resource where the customized class or interface should be added
64     * @param element The enumeration to be customized
65     */

66     public EnumCustomizer(Resource resource, JavaEnum element) {
67         this(resource, null, element);
68     }
69     
70     private EnumCustomizer(Resource resource, JavaClass jclass, JavaEnum element) {
71         this.resource = resource;
72         this.jclass = jclass;
73         this.element = element;
74         initComponents ();
75         
76         //borders
77
interfacesPanel.setBorder (new TitledBorder(NbBundle.getMessage(EnumCustomizer.class, "CTL_Interfaces"))); // NOI18N
78

79         // modifiers
80
int mask = Modifier.ABSTRACT;
81         if (resource == null) { // inner class
82
mask |= Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE | Modifier.STATIC;
83         }
84         if (!element.isInterface()) {
85             mask |= Modifier.FINAL;
86         }
87         accessPanel.add(SourceEditSupport.createAccessModifiersPanel(element, mask), BorderLayout.CENTER);
88         modifierPanel.add(SourceEditSupport.createOtherModifiersPanel(element, mask), BorderLayout.CENTER);
89
90         // name
91
nameTextField.setText(element.getSimpleName());
92
93         // interfaces
94
interfacesPanel.add(
95                 new PropertyPanel(ClassNode.createInterfacesProperty(element, true), PropertyPanel.PREF_CUSTOM_EDITOR),
96                 BorderLayout.CENTER
97         );
98
99         HelpCtx.setHelpIDString (this, "java.enum.customizer"); // NOI18N
100

101         //mnemonics
102
jLabel1.setDisplayedMnemonic(NbBundle.getMessage(EnumCustomizer.class, "CTL_Name_Mnemonic").charAt(0)); // NOI18N
103

104         initAccessibility();
105     }
106
107     public void addNotify() {
108         super.addNotify();
109
110         // select the name
111
int l = nameTextField.getText().length();
112         nameTextField.setCaretPosition(0);
113         nameTextField.moveCaretPosition(l);
114         SwingUtilities.invokeLater(new Runnable JavaDoc() {
115             public void run() {
116                 nameTextField.requestFocus();
117             }
118         });
119     }
120     
121     /** This method is called from within the class to
122      * initialize the form.
123      * WARNING: Do NOT modify this code. The content of this method is
124      * always regenerated by the FormEditor.
125      */

126     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
127
private void initComponents() {
128         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
129
130         jLabel1 = new javax.swing.JLabel JavaDoc();
131         nameTextField = new javax.swing.JTextField JavaDoc();
132         jLabel2 = new javax.swing.JLabel JavaDoc();
133         accessPanel = new javax.swing.JPanel JavaDoc();
134         jLabel3 = new javax.swing.JLabel JavaDoc();
135         modifierPanel = new javax.swing.JPanel JavaDoc();
136         interfacesPanel = new javax.swing.JPanel JavaDoc();
137
138         setLayout(new java.awt.GridBagLayout JavaDoc());
139
140         setBorder(new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(6, 6, 6, 6)));
141         jLabel1.setLabelFor(nameTextField);
142         org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(EnumCustomizer.class, "CTL_Name"));
143         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
144         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
145         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 0, 0);
146         add(jLabel1, gridBagConstraints);
147
148         nameTextField.addFocusListener(new java.awt.event.FocusAdapter JavaDoc() {
149             public void focusLost(java.awt.event.FocusEvent JavaDoc evt) {
150                 nameTextFieldFocusLost(evt);
151             }
152         });
153
154         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
155         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
156         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
157         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
158         gridBagConstraints.weightx = 1.0;
159         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 8, 0, 0);
160         add(nameTextField, gridBagConstraints);
161
162         jLabel2.setLabelFor(accessPanel);
163         org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(EnumCustomizer.class, "CTL_AccessRights"));
164         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
165         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
166         gridBagConstraints.insets = new java.awt.Insets JavaDoc(8, 0, 0, 0);
167         add(jLabel2, gridBagConstraints);
168
169         accessPanel.setLayout(new java.awt.BorderLayout JavaDoc());
170
171         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
172         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
173         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
174         gridBagConstraints.insets = new java.awt.Insets JavaDoc(8, 8, 0, 0);
175         add(accessPanel, gridBagConstraints);
176
177         jLabel3.setLabelFor(modifierPanel);
178         org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(EnumCustomizer.class, "CTL_Modifiers"));
179         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
180         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
181         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
182         gridBagConstraints.insets = new java.awt.Insets JavaDoc(8, 0, 0, 0);
183         add(jLabel3, gridBagConstraints);
184
185         modifierPanel.setLayout(new java.awt.BorderLayout JavaDoc());
186
187         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
188         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
189         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
190         gridBagConstraints.insets = new java.awt.Insets JavaDoc(8, 8, 0, 0);
191         add(modifierPanel, gridBagConstraints);
192
193         interfacesPanel.setLayout(new java.awt.BorderLayout JavaDoc());
194
195         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
196         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
197         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
198         gridBagConstraints.weightx = 1.0;
199         gridBagConstraints.weighty = 1.0;
200         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 0, 0, 0);
201         add(interfacesPanel, gridBagConstraints);
202
203     }
204     // </editor-fold>//GEN-END:initComponents
205

206     private void nameTextFieldFocusLost (java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_nameTextFieldFocusLost
207
if (evt != null && (evt.isTemporary() || !this.isAncestorOf(evt.getOppositeComponent())))
208             return;
209
210         String JavaDoc newName = nameTextField.getText().trim();
211         String JavaDoc oldName = element.getSimpleName();
212         boolean ok = false;
213         
214         if (!Utilities.isJavaIdentifier(newName)) {
215             IllegalArgumentException JavaDoc x = new IllegalArgumentException JavaDoc("Invalid name: " + newName); // NOI18N
216
ErrorManager.getDefault().annotate(
217                 x, ErrorManager.USER, null,
218                 NbBundle.getMessage(EnumCustomizer.class, "MSG_Not_Valid_Identifier"), // NOI18N
219
null, null);
220             ErrorManager.getDefault().notify (x);
221         } else if (oldName.equals(newName)) {
222             return; // nothing to change
223
} else if (this.resource == null && jclass.getInnerClass(newName, true) != null) {
224             IllegalArgumentException JavaDoc x = new IllegalArgumentException JavaDoc("Invalid name: " + newName); // NOI18N
225
ErrorManager.getDefault().annotate(
226                 x, ErrorManager.USER, null,
227                 NbBundle.getMessage(EnumCustomizer.class, "MSG_Not_Valid_Identifier"), // NOI18N
228
null, null);
229             ErrorManager.getDefault().notify (x);
230         } else if (this.jclass == null && SourceEditSupport.findTopLevelClass(this.resource, newName) != null) {
231             IllegalArgumentException JavaDoc x = new IllegalArgumentException JavaDoc("Invalid name: " + newName); // NOI18N
232
ErrorManager.getDefault().annotate(
233                 x, ErrorManager.USER, null,
234                 NbBundle.getMessage(EnumCustomizer.class, "MSG_Not_Valid_Identifier"), // NOI18N
235
null, null);
236             ErrorManager.getDefault().notify (x);
237         } else {
238             try {
239                 element.setSimpleName(newName);
240                 ok = true;
241             } catch (JmiException e) {
242                 ErrorManager.getDefault().notify(e);
243             }
244         }
245         isOK = ok;
246         if (!ok) {
247             nameTextField.setText(oldName);
248         }
249     }//GEN-LAST:event_nameTextFieldFocusLost
250

251
252     // Variables declaration - do not modify//GEN-BEGIN:variables
253
private javax.swing.JPanel JavaDoc accessPanel;
254     private javax.swing.JPanel JavaDoc interfacesPanel;
255     private javax.swing.JLabel JavaDoc jLabel1;
256     private javax.swing.JLabel JavaDoc jLabel2;
257     private javax.swing.JLabel JavaDoc jLabel3;
258     private javax.swing.JPanel JavaDoc modifierPanel;
259     private javax.swing.JTextField JavaDoc nameTextField;
260     // End of variables declaration//GEN-END:variables
261

262     
263     private void initAccessibility() {
264         nameTextField.getAccessibleContext().setAccessibleName(NbBundle.getMessage(EnumCustomizer.class, "ACS_EnumNameTextField")); // NOI18N
265
nameTextField.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(EnumCustomizer.class, "ACS_EnumNameTextField")); // NOI18N
266
this.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(EnumCustomizer.class, "ACSD_EnumCustomizerDialog")); // NOI18N
267
}
268     
269     public boolean isOK() {
270         nameTextFieldFocusLost(null);
271         return isOK;
272     }
273 }
274
Popular Tags