KickJava   Java API By Example, From Geeks To Geeks.

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


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.JavaEnum;
23 import org.netbeans.jmi.javamodel.EnumConstant;
24 import org.openide.util.HelpCtx;
25 import org.openide.util.NbBundle;
26 import org.openide.util.Utilities;
27 import org.openide.ErrorManager;
28
29 import javax.jmi.reflect.JmiException;
30
31 /**
32  * Customizer for {@link org.netbeans.jmi.javamodel.EnumConstant}
33  * @author Jan Pokorsky
34  */

35 final class EnumConstantCustomizer extends javax.swing.JPanel JavaDoc {
36
37     /** Declaring enum */
38     private final JavaEnum jclass;
39     /** The edited constant */
40     private EnumConstant constant;
41     private boolean isOK = true;
42     
43     /** Creates new form EnumConstantCustomizer */
44     public EnumConstantCustomizer(JavaEnum jclass, EnumConstant constant) {
45         this.jclass = jclass;
46         this.constant = constant;
47         initComponents();
48         
49         nameTextField.setText(constant.getName());
50         HelpCtx.setHelpIDString (this, "java.enumconstant.customizer"); // NOI18N
51

52         initAccessibility();
53     }
54
55     public boolean isOK() {
56         nameTextFieldFocusLost(null);
57         return isOK;
58     }
59     
60     private void initAccessibility() {
61         nameTextField.getAccessibleContext().setAccessibleName(getString("ACSN_ConstantNameTextField")); // NOI18N
62
nameTextField.getAccessibleContext().setAccessibleDescription(getString("ACSD_ConstantNameTextField")); // NOI18N
63
this.getAccessibleContext().setAccessibleDescription("ACSD_ConstantCustomizerDialog"); // NOI18N
64
}
65     
66     /** I18N helper */
67     private static String JavaDoc getString(String JavaDoc key) {
68         return NbBundle.getMessage(EnumConstantCustomizer.class, key);
69     }
70     
71     /** This method is called from within the constructor to
72      * initialize the form.
73      * WARNING: Do NOT modify this code. The content of this method is
74      * always regenerated by the Form Editor.
75      */

76     private void initComponents() {//GEN-BEGIN:initComponents
77
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
78
79         nameLabel = new javax.swing.JLabel JavaDoc();
80         nameTextField = new javax.swing.JTextField JavaDoc();
81
82         setLayout(new java.awt.GridBagLayout JavaDoc());
83
84         nameLabel.setLabelFor(nameTextField);
85         nameLabel.setText(org.openide.util.NbBundle.getMessage(EnumConstantCustomizer.class, "CTL_Name"));
86         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
87         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 0, 8);
88         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
89         add(nameLabel, gridBagConstraints);
90
91         nameTextField.setColumns(25);
92         nameTextField.addFocusListener(new java.awt.event.FocusAdapter JavaDoc() {
93             public void focusLost(java.awt.event.FocusEvent JavaDoc evt) {
94                 nameTextFieldFocusLost(evt);
95             }
96         });
97
98         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
99         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
100         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
101         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 0, 11);
102         gridBagConstraints.weightx = 1.0;
103         add(nameTextField, gridBagConstraints);
104
105     }//GEN-END:initComponents
106

107     private void nameTextFieldFocusLost(java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_nameTextFieldFocusLost
108
if (evt != null && (evt.isTemporary() || !this.isAncestorOf(evt.getOppositeComponent())))
109             return;
110
111         String JavaDoc newName = nameTextField.getText().trim();
112         String JavaDoc oldName = constant.getName();
113         boolean ok = false;
114         Exception JavaDoc x = null;
115
116         if (!Utilities.isJavaIdentifier(newName)) {
117             x = new IllegalArgumentException JavaDoc("Invalid name"); // NOI18N
118
ErrorManager.getDefault().annotate(
119                     x, ErrorManager.USER, null,
120                     NbBundle.getMessage(FieldCustomizer.class, "MSG_Not_Valid_Identifier"), // NOI18N
121
null, null);
122         } else if (oldName.equals(newName)) {
123             return; // nothing to change
124
} else if (SourceEditSupport.findConstant(this.jclass, newName) != null) {
125             x = new IllegalArgumentException JavaDoc("Invalid name"); // NOI18N
126
ErrorManager.getDefault().annotate(
127                     x, ErrorManager.USER, null,
128                     NbBundle.getMessage(FieldCustomizer.class, "MSG_Used_Identifier", newName), // NOI18N
129
null, null);
130         } else {
131             try {
132                 constant.setName(newName);
133                 ok = true;
134             } catch (JmiException e) {
135                 ErrorManager.getDefault().notify(e);
136             }
137         }
138         isOK = ok;
139         if (!ok) {
140             nameTextField.setText(oldName);
141         }
142         if (x != null) {
143             ErrorManager.getDefault().notify(x);
144         }
145     }//GEN-LAST:event_nameTextFieldFocusLost
146

147     
148     // Variables declaration - do not modify//GEN-BEGIN:variables
149
private javax.swing.JLabel JavaDoc nameLabel;
150     private javax.swing.JTextField JavaDoc nameTextField;
151     // End of variables declaration//GEN-END:variables
152

153 }
154
Popular Tags