KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > ui > nodes > editors > ModifierPanel


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.editors;
21
22 import java.awt.event.*;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.beans.PropertyChangeEvent JavaDoc;
25 import java.beans.FeatureDescriptor JavaDoc;
26 import java.lang.reflect.Modifier JavaDoc;
27
28 import javax.swing.*;
29
30 import org.openide.util.NbBundle;
31 import org.openide.util.WeakListeners;
32
33 /** JPanel extension containing components which allows visual
34  * editing Modifier object.
35  * This class has two main properties: mask (possible values mask)
36  * and modifier (current value).
37  *
38  * @author Petr Hamernik
39  */

40 final class ModifierPanel {
41
42     // ------------------------- Statics -------------------------------
43

44     /** Name of 'mask' property */
45     public static final String JavaDoc PROP_MASK = "mask"; // NOI18N
46

47     /** Name of 'modifier' property (current value) */
48     public static final String JavaDoc PROP_MODIFIER = "modifier"; // NOI18N
49

50     private static final int CHECK_ABSTRACT = 0;
51     private static final int CHECK_FINAL = 1;
52     private static final int CHECK_STATIC = 2;
53     private static final int CHECK_SYNCHRONIZED = 3;
54     private static final int CHECK_TRANSIENT = 4;
55     private static final int CHECK_VOLATILE = 5;
56     private static final int CHECK_NATIVE = 6;
57
58     /** Names of modifiers */
59     static final String JavaDoc MODIFIER_NAMES[] = {
60         "abstract", "final", "static", "synchronized", "transient", "volatile", "native" // NOI18N
61
};
62
63     private static final String JavaDoc[] MODIFIER_DESCRIPTION_KEYS = {
64         "ACSD_ModifierPanel_Modifier_Abstract", // NOI18N
65
"ACSD_ModifierPanel_Modifier_Final", // NOI18N
66
"ACSD_ModifierPanel_Modifier_Static", // NOI18N
67
"ACSD_ModifierPanel_Modifier_Synchronized", // NOI18N
68
"ACSD_ModifierPanel_Modifier_Transient", // NOI18N
69
"ACSD_ModifierPanel_Modifier_Volatile", // NOI18N
70
"ACSD_ModifierPanel_Modifier_Native" // NOI18N
71
};
72
73     private static final String JavaDoc[] MODIFIER_MNEMONICS_KEYS = {
74         "ModifierPanel_Modifier_Abstract_Mnemonic", // NOI18N
75
"ModifierPanel_Modifier_Final_Mnemonic", // NOI18N
76
"ModifierPanel_Modifier_Static_Mnemonic", // NOI18N
77
"ModifierPanel_Modifier_Synchronized_Mnemonic", // NOI18N
78
"ModifierPanel_Modifier_Transient_Mnemonic", // NOI18N
79
"ModifierPanel_Modifier_Volatile_Mnemonic", // NOI18N
80
"ModifierPanel_Modifier_Native_Mnemonic" // NOI18N
81
};
82
83     /** Values of modifiers */
84     static final int MODIFIER_VALUES[] = {
85         Modifier.ABSTRACT, Modifier.FINAL, Modifier.STATIC, Modifier.SYNCHRONIZED,
86         Modifier.TRANSIENT, Modifier.VOLATILE, Modifier.NATIVE
87     };
88
89     /** Count of the modifiers */
90     static final int MODIFIER_COUNT = MODIFIER_VALUES.length;
91
92     /** Names of accessibility */
93     static final String JavaDoc ACCESS_NAMES[] = {
94         "<default>", "private", "protected", "public" // NOI18N
95
};
96
97     /** Values of accessibility */
98     static final int ACCESS_VALUES[] = {
99         0, Modifier.PRIVATE, Modifier.PROTECTED, Modifier.PUBLIC
100     };
101
102     /** Mask of access modifiers */
103     static final int ACCESS_MASK = Modifier.PRIVATE | Modifier.PROTECTED | Modifier.PUBLIC;
104     
105     /** Mask of non-access modifiers */
106     private static final int OTHERS_MASK = Modifier.ABSTRACT |
107             Modifier.FINAL | Modifier.STATIC | Modifier.SYNCHRONIZED |
108             Modifier.TRANSIENT | Modifier.VOLATILE | Modifier.NATIVE | Modifier.STRICT;
109
110     /** Mask of all possible modifiers. */
111     static final int EDITABLE_MASK = ACCESS_MASK | OTHERS_MASK;
112
113     // ------------------ Instance Fields --------------------------
114

115     /** Reference back to the editor that created this panel. */
116     private ModifierEditor myEditor;
117     
118     /** Current access values shown in the combo box */
119     private int currentAccessValues[];
120
121     /** Current access names shown in the combo box */
122     private String JavaDoc currentAccessNames[];
123
124     /** JCheckBox array */
125     private JCheckBox[] checks;
126
127     /** listener for visual changes */
128     private ActionListener listener;
129
130     /** Ignored flag - used during firing change events */
131     private boolean ignored = false;
132
133     /** listener for ModifierEditor changes */
134     private PropertyChangeListener JavaDoc editorListener;
135
136     /** Creates new form ModifiersPanel */
137     public ModifierPanel(ModifierEditor ed) {
138         myEditor = ed;
139         currentAccessValues = ACCESS_VALUES;
140         currentAccessNames = ACCESS_NAMES;
141         
142         editorListener = new PropertyChangeListener JavaDoc() {
143             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
144                 if (PROP_MODIFIER.equals(evt.getPropertyName()) ||
145                     PROP_MASK.equals(evt.getPropertyName())) {
146                     updateAccess();
147                     ignored = true;
148                     updateComponents();
149                     ignored = false;
150                 }
151             }
152         };
153  
154         myEditor.addPropertyChangeListener(WeakListeners.propertyChange(editorListener, myEditor));
155
156         listener = new ActionListener() {
157             
158             public void actionPerformed(ActionEvent evt) {
159                 // remove abstract, if private access is being selected.
160

161                 int selIndex = accessCombo.getSelectedIndex();
162                 // disallow abstract, if private access is selected.
163
if (evt.getSource() == accessCombo && selIndex < 0) {
164                     // revert the combo to the previous value.
165
return;
166                 }
167                 if (checks[CHECK_ABSTRACT].isSelected() &&
168                         ((myEditor.getModifier() & MODIFIER_VALUES[CHECK_ABSTRACT]) == 0) &&
169                         ((myEditor.getModifier() & Modifier.PRIVATE) > 0)) {
170                     checks[CHECK_ABSTRACT].setSelected(false);
171                 }
172                 if (selIndex >= 0 &&
173                         (currentAccessValues[accessCombo.getSelectedIndex()] & Modifier.PRIVATE) > 0 &&
174                         (myEditor.getModifier() & Modifier.PRIVATE) == 0) {
175                     checks[CHECK_ABSTRACT].setSelected(false);
176                 }
177                    
178                 excludeChecks(CHECK_ABSTRACT, CHECK_FINAL);
179                 excludeChecks(CHECK_ABSTRACT, CHECK_NATIVE);
180                 excludeChecks(CHECK_ABSTRACT, CHECK_STATIC);
181                 excludeChecks(CHECK_ABSTRACT, CHECK_SYNCHRONIZED);
182                 excludeChecks(CHECK_VOLATILE, CHECK_FINAL);
183                 
184                 if (!ignored)
185                     updateValue();
186                 
187             }
188         };
189
190         ignored = true;
191         initBasicComponents();
192
193         updateAccess();
194         updateModifiers();
195         updateComponents();
196         ignored = false;
197     }
198
199     /** Makes sure that the specified two checkboxes are mutually exclusive by
200      * unselecting one if the other one becomes selected.
201      */

202     private void excludeChecks(int check1, int check2) {
203         if (checks[check1].isSelected() && ((myEditor.getModifier() & MODIFIER_VALUES[check1]) == 0))
204             checks[check2].setSelected(false);
205         else if (checks[check2].isSelected() && ((myEditor.getModifier() & MODIFIER_VALUES[check2]) == 0))
206             checks[check1].setSelected(false);
207     }
208
209     private void initBasicComponents() {
210         accessCombo = new JComboBox();
211         accessCombo.addActionListener(listener);
212         accessCombo.getAccessibleContext().setAccessibleName("ACS_AccessRights"); // NOI18N
213
accessCombo.getAccessibleContext().setAccessibleDescription("ACSD_AccessRights"); // NOI18N
214

215         modifPanel = new JPanel();
216         modifPanel.setLayout(new java.awt.GridLayout JavaDoc(4, 2, 4, 4));
217         modifPanel.getAccessibleContext().setAccessibleName("ACSN_OtherModifiers"); // NOI18N
218
modifPanel.getAccessibleContext().setAccessibleDescription("ACSD_OtherModifiers"); // NOI18N
219

220         checks = new JCheckBox[MODIFIER_COUNT];
221         for (int i = 0; i < MODIFIER_COUNT; i++) {
222             checks[i] = new JCheckBox(MODIFIER_NAMES[i]);
223             checks[i].setMnemonic(getModifierMnemonics(i));
224             checks[i].getAccessibleContext().setAccessibleDescription(getModifierDescription(i));
225             modifPanel.add(checks[i]);
226             checks[i].setEnabled((myEditor.getMask() & MODIFIER_VALUES[i]) != 0);
227             checks[i].addActionListener(listener);
228         }
229     }
230     
231     private void initComponents() {
232         jLabel1 = new JLabel();
233         jLabel1.setText(getString("LAB_AccessRights")); // NOI18N
234
jLabel1.setLabelFor(accessCombo);
235         jLabel1.setDisplayedMnemonic(getString("LAB_AccessRights_Mnemonic").charAt(0)); // NOI18N
236

237         jPanel2 = new JPanel();
238         jPanel2.setLayout(new java.awt.BorderLayout JavaDoc(8, 8));
239         jPanel2.setBorder(new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(5, 5, 5, 5)));
240         jPanel2.add(jLabel1, java.awt.BorderLayout.WEST);
241         jPanel2.add(accessCombo, java.awt.BorderLayout.CENTER);
242         modifPanel.setBorder (new javax.swing.border.CompoundBorder JavaDoc(
243                                   new javax.swing.border.TitledBorder JavaDoc(getString("LAB_Modifiers")), // NOI18N
244
new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(3, 3, 3, 3))
245                               ));
246         
247         commpactP = new JPanel();
248         commpactP.setLayout(new java.awt.BorderLayout JavaDoc());
249         commpactP.setBorder(new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(6, 7, 6, 7)));
250         commpactP.add(modifPanel, java.awt.BorderLayout.CENTER);
251         commpactP.add(jPanel2, java.awt.BorderLayout.NORTH);
252         commpactP.getAccessibleContext().setAccessibleName("ACSN_ModifierPanel"); // NOI18N
253
commpactP.getAccessibleContext().setAccessibleDescription(getString("ACSD_ModifierPanel")); // NOI18N
254
}
255
256
257     private JComboBox accessCombo;
258     private JLabel jLabel1;
259     private JPanel jPanel2;
260     private JPanel modifPanel;
261     private JPanel commpactP;
262     private boolean isCompact = false;
263     private boolean isPartial = false;
264     
265     public JComboBox getAccessComponent() {
266         if (isCompact)
267             throw new IllegalStateException JavaDoc("cannot use both getAccessComponent/getCompactComponent"); // NOI18N
268
isPartial = true;
269         return accessCombo;
270     }
271     
272     public JComponent getModifiersComponent() {
273         if (isCompact)
274             throw new IllegalStateException JavaDoc("cannot use both getModifiersComponent/getCompactComponent"); // NOI18N
275
isPartial = true;
276         return modifPanel;
277     }
278     
279     public JPanel getCompactComponent() {
280         if (isPartial)
281             throw new IllegalStateException JavaDoc(
282                     "cannot use getAccessComponent/getModifiersComponent/getCompactComponent together"); // NOI18N
283
isCompact = true;
284         if (commpactP == null) {
285             initComponents();
286         }
287         return commpactP;
288     }
289
290     /** Update access ComboBox values depending on new 'mask' property
291      */

292     private void updateAccess() {
293         int selValue = myEditor.getModifier() & ACCESS_MASK;
294         int selIndex = -1;
295
296         int counter = 1;
297         for (int i = 1; i < ACCESS_VALUES.length; i++) {
298             if ((ACCESS_VALUES[i] & myEditor.getMask()) != 0)
299                 counter++;
300         }
301         currentAccessValues = new int[counter];
302         currentAccessNames = new String JavaDoc[counter];
303
304         currentAccessValues[0] = ACCESS_VALUES[0];
305         currentAccessNames[0] = ACCESS_NAMES[0];
306         counter = 1;
307
308         for (int i = 1; i < ACCESS_VALUES.length; i++) {
309             if ((ACCESS_VALUES[i] & myEditor.getMask()) != 0) {
310                 currentAccessValues[counter] = ACCESS_VALUES[i];
311                 currentAccessNames[counter] = ACCESS_NAMES[i];
312                 if (ACCESS_VALUES[i] == selValue) {
313                     selIndex = counter;
314                 }
315                 counter++;
316             }
317         }
318         if (selIndex == -1 && selValue == 0)
319             selIndex = 0;
320
321         ignored = true;
322         accessCombo.setModel(new DefaultComboBoxModel(currentAccessNames));
323         accessCombo.setSelectedIndex(selIndex);
324         ignored = false;
325     }
326
327     /** Update enable status of all modifiers check boxes
328      */

329     private void updateModifiers() {
330         for (int i = 0; i < MODIFIER_COUNT; i++) {
331             checks[i].setEnabled((myEditor.getMask() & MODIFIER_VALUES[i]) != 0);
332         }
333     }
334
335     /** Update the components inside the ModifierPanel depending on new value
336      * of 'modifier' property.
337      */

338     private void updateComponents() {
339     updateAccessCombo();
340     updateModifiers();
341         for (int i = 0; i < MODIFIER_COUNT; i++) {
342             checks[i].setSelected((myEditor.getModifier() & MODIFIER_VALUES[i]) != 0);
343         }
344     }
345     
346     private void updateAccessCombo() {
347         int selIndex = -1;
348         if (myEditor.getModifier() == 0) {
349             selIndex = 0;
350         } else {
351             for (int i = 1; i < currentAccessValues.length; i++) {
352                 if ((currentAccessValues[i] & myEditor.getModifier()) != 0) {
353                     if (selIndex != -1) {
354                         selIndex = -1;
355                         break;
356                     }
357                     selIndex = i;
358                 }
359             }
360         }
361         if (accessCombo.getSelectedIndex() != selIndex) {
362             accessCombo.setSelectedIndex(selIndex);
363         }
364     }
365
366     /** Updates the value depending on the status of the components. */
367     private void updateValue() {
368         int newValue = 0;
369         int comboIndex = accessCombo.getSelectedIndex();
370         Object JavaDoc type = myEditor.getType();
371         int mask = 0;
372         if (ModifierEditor.FULL_CUSTOM_EDITOR == type || ModifierEditor.ACCESS_MODIFIERS_CUSTOM_EDITOR == type) {
373             mask |= ACCESS_MASK;
374             if (comboIndex == -1) {
375                 newValue = myEditor.getModifier() & ACCESS_MASK;
376             } else {
377                 newValue |= currentAccessValues[comboIndex];
378             }
379         }
380         if (ModifierEditor.FULL_CUSTOM_EDITOR == type || ModifierEditor.OTHERS_MODIFIERS_CUSTOM_EDITOR == type) {
381             mask |= OTHERS_MASK;
382             for (int i = 0; i < MODIFIER_COUNT; i++) {
383                 if (checks[i].isSelected() & checks[i].isEnabled())
384                     newValue |= MODIFIER_VALUES[i];
385             }
386         }
387         
388         int oldValue = myEditor.getModifier();
389         if ((oldValue & mask) != newValue) {
390             if (ModifierEditor.ACCESS_MODIFIERS_CUSTOM_EDITOR == type) {
391                 newValue |= (oldValue & ~ACCESS_MASK);
392             } else if (ModifierEditor.OTHERS_MODIFIERS_CUSTOM_EDITOR == type) {
393                 newValue |= (oldValue & ~OTHERS_MASK);
394             }
395             myEditor.setModifier(newValue);
396         }
397     }
398     
399     private static String JavaDoc getString(String JavaDoc key) {
400         return NbBundle.getMessage(ModifierPanel.class, key);
401     }
402     
403     static String JavaDoc getModifierDescription(int i) {
404         return getString(MODIFIER_DESCRIPTION_KEYS[i]);
405     }
406     
407     static char getModifierMnemonics(int i) {
408         return getString(MODIFIER_MNEMONICS_KEYS[i]).charAt(0);
409     }
410 }
411
Popular Tags