KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Modifier JavaDoc;
23 import java.awt.*;
24
25 import javax.swing.*;
26 import javax.jmi.reflect.JmiException;
27
28 import org.openide.*;
29 import org.openide.util.Utilities;
30 import org.openide.util.NbBundle;
31
32 import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
33 import org.netbeans.modules.java.ui.nodes.editors.TypeEditor;
34 import org.netbeans.jmi.javamodel.*;
35
36 /** Customizer for FieldElement
37  *
38  * @author Petr Hamernik
39  */

40 public final class FieldCustomizer extends JPanel {
41     /** The edited field */
42     private final Field element;
43     private final JavaClass jclass;
44
45     /** Predefined types in the type combo;
46      * duplicate of {@link org.netbeans.modules.java.ui.nodes.editors.TypeEditor#LVALUE_TYPES}
47      */

48     private static final String JavaDoc[] COMMON_TYPES = TypeEditor.LVALUE_TYPES;
49     
50     private boolean isOK = true;
51
52     /** Create new FieldCustomizer component
53     * @param element The field to be customized
54     */

55     public FieldCustomizer(JavaClass jclass, Field element) {
56         this.element = element;
57         this.jclass = jclass;
58
59         initComponents ();
60         
61         // modifiers
62
int mask;
63         if (jclass.isInterface()) {
64             mask = Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL;
65         } else {
66             mask = Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
67                    Modifier.STATIC | Modifier.FINAL | Modifier.TRANSIENT |
68                    Modifier.VOLATILE;
69         }
70         accessPanel.add(SourceEditSupport.createAccessModifiersPanel(element, mask), BorderLayout.CENTER);
71         modifierPanel.add(SourceEditSupport.createOtherModifiersPanel(element, mask), BorderLayout.CENTER);
72
73         // name
74
nameTextField.setText(element.getName());
75
76         // type
77
typeCombo.setSelectedItem(ElementFormat.elementName(element.getType()));
78
79         // init value
80
initValueEditor.setText(element.getInitialValueText());
81
82         //mnemonics
83
jLabel1.setDisplayedMnemonic(NbBundle.getMessage(FieldCustomizer.class, "CTL_Name_Mnemonic").charAt(0)); // NOI18N
84
jLabel2.setDisplayedMnemonic(NbBundle.getMessage(FieldCustomizer.class, "CTL_Type_Mnemonic").charAt(0)); // NOI18N
85
jLabel5.setDisplayedMnemonic(NbBundle.getMessage(FieldCustomizer.class, "CTL_InitValue_Mnemonic").charAt(0)); // NOI18N
86
initAccessibility();
87     }
88
89     public void addNotify() {
90         super.addNotify();
91
92         // select the name
93
int l = nameTextField.getText().length();
94         nameTextField.setCaretPosition(0);
95         nameTextField.moveCaretPosition(l);
96         nameTextField.requestFocus();
97         SwingUtilities.invokeLater(new Runnable JavaDoc() {
98             public void run() {
99                 nameTextField.requestFocus();
100             }
101         });
102     }
103     
104     private JavaModelPackage getJModel() {
105         return JavaMetamodel.getManager().getJavaExtent(this.jclass);
106     }
107     
108     /** This method is called from within the constructor to
109      * initialize the form.
110      * WARNING: Do NOT modify this code. The content of this method is
111      * always regenerated by the FormEditor.
112      */

113     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
114
private void initComponents() {
115         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
116
117         jLabel1 = new javax.swing.JLabel JavaDoc();
118         nameTextField = new javax.swing.JTextField JavaDoc();
119         jLabel2 = new javax.swing.JLabel JavaDoc();
120         typeCombo = new javax.swing.JComboBox JavaDoc(COMMON_TYPES);
121         jLabel3 = new javax.swing.JLabel JavaDoc();
122         accessPanel = new javax.swing.JPanel JavaDoc();
123         jLabel4 = new javax.swing.JLabel JavaDoc();
124         modifierPanel = new javax.swing.JPanel JavaDoc();
125         jLabel5 = new javax.swing.JLabel JavaDoc();
126         initValuePanel = new javax.swing.JPanel JavaDoc();
127         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
128         initValueEditor = new javax.swing.JEditorPane JavaDoc();
129
130         setLayout(new java.awt.GridBagLayout JavaDoc());
131
132         setBorder(new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(6, 6, 6, 6)));
133         jLabel1.setLabelFor(nameTextField);
134         org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(FieldCustomizer.class, "CTL_Name"));
135         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
136         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
137         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 0, 0);
138         add(jLabel1, gridBagConstraints);
139
140         nameTextField.addFocusListener(new java.awt.event.FocusAdapter JavaDoc() {
141             public void focusLost(java.awt.event.FocusEvent JavaDoc evt) {
142                 nameTextFieldFocusLost(evt);
143             }
144         });
145
146         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
147         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
148         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
149         gridBagConstraints.weightx = 1.0;
150         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 8, 0, 0);
151         add(nameTextField, gridBagConstraints);
152         nameTextField.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(FieldCustomizer.class, "ACSD_FieldNameTextField"));
153
154         jLabel2.setLabelFor(typeCombo);
155         org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(FieldCustomizer.class, "CTL_Type"));
156         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
157         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
158         gridBagConstraints.insets = new java.awt.Insets JavaDoc(8, 0, 0, 0);
159         add(jLabel2, gridBagConstraints);
160
161         typeCombo.setEditable(true);
162         typeCombo.addActionListener(new java.awt.event.ActionListener JavaDoc() {
163             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
164                 jComboBox1ActionPerformed(evt);
165             }
166         });
167
168         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
169         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
170         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
171         gridBagConstraints.weightx = 1.0;
172         gridBagConstraints.insets = new java.awt.Insets JavaDoc(8, 8, 0, 0);
173         add(typeCombo, gridBagConstraints);
174         typeCombo.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(FieldCustomizer.class, "ACSD_FieldType"));
175
176         jLabel3.setLabelFor(accessPanel);
177         org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(FieldCustomizer.class, "CTL_AccessRights"));
178         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
179         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
180         gridBagConstraints.insets = new java.awt.Insets JavaDoc(8, 0, 0, 0);
181         add(jLabel3, gridBagConstraints);
182
183         accessPanel.setLayout(new java.awt.BorderLayout JavaDoc());
184
185         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
186         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
187         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
188         gridBagConstraints.insets = new java.awt.Insets JavaDoc(8, 8, 0, 0);
189         add(accessPanel, gridBagConstraints);
190
191         jLabel4.setLabelFor(modifierPanel);
192         org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getMessage(FieldCustomizer.class, "CTL_Modifiers"));
193         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
194         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
195         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
196         gridBagConstraints.insets = new java.awt.Insets JavaDoc(8, 0, 0, 0);
197         add(jLabel4, gridBagConstraints);
198
199         modifierPanel.setLayout(new java.awt.BorderLayout JavaDoc());
200
201         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
202         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
203         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
204         gridBagConstraints.insets = new java.awt.Insets JavaDoc(8, 8, 0, 0);
205         add(modifierPanel, gridBagConstraints);
206
207         jLabel5.setLabelFor(initValueEditor);
208         org.openide.awt.Mnemonics.setLocalizedText(jLabel5, org.openide.util.NbBundle.getMessage(FieldCustomizer.class, "CTL_InitValue"));
209         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
210         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
211         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
212         gridBagConstraints.insets = new java.awt.Insets JavaDoc(8, 0, 0, 0);
213         add(jLabel5, gridBagConstraints);
214
215         initValuePanel.setLayout(new java.awt.BorderLayout JavaDoc());
216
217         initValueEditor.setContentType("text/x-java");
218         initValueEditor.setPreferredSize(new java.awt.Dimension JavaDoc(302, 200));
219         initValueEditor.addFocusListener(new java.awt.event.FocusAdapter JavaDoc() {
220             public void focusLost(java.awt.event.FocusEvent JavaDoc evt) {
221                 initValueEditorFocusLost(evt);
222             }
223         });
224
225         jScrollPane1.setViewportView(initValueEditor);
226
227         initValuePanel.add(jScrollPane1, java.awt.BorderLayout.CENTER);
228
229         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
230         gridBagConstraints.gridwidth = 2;
231         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
232         gridBagConstraints.weightx = 1.0;
233         gridBagConstraints.weighty = 1.0;
234         gridBagConstraints.insets = new java.awt.Insets JavaDoc(2, 0, 0, 0);
235         add(initValuePanel, gridBagConstraints);
236
237     }
238     // </editor-fold>//GEN-END:initComponents
239

240     private void jComboBox1ActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
241
Object JavaDoc selItem = typeCombo.getSelectedItem();
242         if (selItem == null)
243            return;
244         
245         String JavaDoc selItemTxt = selItem.toString().trim();
246         Type oldValue = element.getType();
247         boolean ok = false;
248         
249         try {
250             final Type newValue = getJModel().getType().resolve(selItemTxt);
251             if (oldValue.equals(newValue)) {
252                 return; // nothing to change
253
} else if (!TypeEditor.isValidTypeSyntax(selItemTxt)) {
254                 notifyUserWarning("MSG_Not_Valid_Type", "invalid type"); // NOI18N
255
} else if (newValue instanceof PrimitiveType &&
256                     PrimitiveTypeKindEnum.VOID.equals(((PrimitiveType) newValue).getKind())) {
257                 notifyUserWarning("MSG_Not_Valid_Type", "invalid type"); // NOI18N
258
} else {
259                 element.setType(newValue);
260                 ok = true;
261             }
262         } catch (JmiException e) {
263             ErrorManager.getDefault().notify(e);
264         }
265         isOK = ok;
266         if (!ok)
267             typeCombo.setSelectedItem(ElementFormat.elementName(oldValue));
268     }//GEN-LAST:event_jComboBox1ActionPerformed
269

270     private void initValueEditorFocusLost (java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_initValueEditorFocusLost
271
if (evt != null && (evt.isTemporary() || !this.isAncestorOf(evt.getOppositeComponent())))
272             return;
273
274         String JavaDoc newText = initValueEditor.getText();
275         String JavaDoc oldText = element.getInitialValueText();
276         boolean ok = false;
277         if (!newText.equals(oldText)) {
278             try {
279                 element.setInitialValueText(newText);
280                 ok = true;
281             } catch (JmiException e) {
282                 ErrorManager.getDefault().notify(e);
283             }
284         } else
285             return;
286         isOK = ok;
287         if (!ok)
288             initValueEditor.setText(oldText);
289     }//GEN-LAST:event_initValueEditorFocusLost
290

291     private void nameTextFieldFocusLost (java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_nameTextFieldFocusLost
292
if (evt != null && (evt.isTemporary() || !this.isAncestorOf(evt.getOppositeComponent())))
293             return;
294
295         String JavaDoc newName = nameTextField.getText().trim();
296         String JavaDoc oldName = element.getName();
297         boolean ok = false;
298
299         try {
300             if (!Utilities.isJavaIdentifier(newName)) {
301                 notifyUserWarning("MSG_Not_Valid_Identifier", "invalid name"); // NOI18N);
302
} else if (oldName.equals(newName)) {
303                 return; // nothing to change
304
} else if (this.jclass.getField(newName, true) != null) {
305                 notifyUserWarning("MSG_Used_Identifier", newName, "invalid name"); // NOI18N);
306
} else {
307                 element.setName(newName);
308                 ok = true;
309             }
310         } catch (JmiException e) {
311             ErrorManager.getDefault().notify(e);
312         }
313         isOK = ok;
314         if (!ok) {
315             nameTextField.setText(oldName);
316         }
317     }//GEN-LAST:event_nameTextFieldFocusLost
318

319
320     // Variables declaration - do not modify//GEN-BEGIN:variables
321
private javax.swing.JPanel JavaDoc accessPanel;
322     private javax.swing.JEditorPane JavaDoc initValueEditor;
323     private javax.swing.JPanel JavaDoc initValuePanel;
324     private javax.swing.JLabel JavaDoc jLabel1;
325     private javax.swing.JLabel JavaDoc jLabel2;
326     private javax.swing.JLabel JavaDoc jLabel3;
327     private javax.swing.JLabel JavaDoc jLabel4;
328     private javax.swing.JLabel JavaDoc jLabel5;
329     private javax.swing.JScrollPane JavaDoc jScrollPane1;
330     private javax.swing.JPanel JavaDoc modifierPanel;
331     private javax.swing.JTextField JavaDoc nameTextField;
332     private javax.swing.JComboBox JavaDoc typeCombo;
333     // End of variables declaration//GEN-END:variables
334

335     private void initAccessibility() {
336         nameTextField.getAccessibleContext().setAccessibleName(NbBundle.getMessage(FieldCustomizer.class, "ACS_FieldNameTextField")); // NOI18N
337
nameTextField.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(FieldCustomizer.class, "ACS_FieldNameTextField")); // NOI18N
338
initValueEditor.getAccessibleContext().setAccessibleName(NbBundle.getMessage(FieldCustomizer.class, "ACSN_InitValueEditorPane")); // NOI18N
339
initValueEditor.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(FieldCustomizer.class, "ACSD_InitValueEditorPane")); // NOI18N
340
this.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(FieldCustomizer.class, "ACSD_FieldCustomizerDialog")); // NOI18N
341
}
342     
343     public boolean isOK() {
344         nameTextFieldFocusLost(null);
345         if (isOK) {
346             initValueEditorFocusLost(null);
347         }
348         if (isOK) {
349             jComboBox1ActionPerformed(null);
350         }
351         return isOK;
352     }
353     
354     private static void notifyUserWarning(String JavaDoc bundleKey, String JavaDoc msg) {
355         IllegalArgumentException JavaDoc e = new IllegalArgumentException JavaDoc(msg); // NOI18N
356
ErrorManager.getDefault().annotate(
357             e, ErrorManager.USER, null,
358             NbBundle.getMessage(FieldCustomizer.class, bundleKey),
359             null, null);
360         ErrorManager.getDefault().notify(e);
361
362     }
363     
364     private static void notifyUserWarning(String JavaDoc bundleKey, String JavaDoc param, String JavaDoc msg) {
365         IllegalArgumentException JavaDoc e = new IllegalArgumentException JavaDoc(msg);
366         ErrorManager.getDefault().annotate(
367             e, ErrorManager.USER, null,
368             NbBundle.getMessage(FieldCustomizer.class, bundleKey, param),
369             null, null);
370         ErrorManager.getDefault().notify(e);
371     }
372     
373 }
374
Popular Tags