KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > explorer > propertysheet > 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.openide.explorer.propertysheet.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 import java.util.Locale JavaDoc;
28
29 import javax.swing.*;
30
31 import org.openide.util.NbBundle;
32 import org.openide.util.WeakListeners;
33 import org.openide.explorer.propertysheet.PropertyEnv;
34
35 /** JPanel extension containing components which allows visual
36  * editing Modifier object.
37  * This class has two main properties: mask (possible values mask)
38  * and modifier (current value).
39  *
40  * @author Petr Hamernik
41  */

42 class ModifierPanel extends javax.swing.JPanel JavaDoc {
43
44     // ------------------------- Statics -------------------------------
45

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

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

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

114     /** Reference back to the editor that created this panel. */
115     private ModifierEditor myEditor;
116     
117     /** Current access values shown in the combo box */
118     private int currentAccessValues[];
119
120     /** Current access names shown in the combo box */
121     private String JavaDoc currentAccessNames[];
122
123     /** JCheckBox array */
124     private JCheckBox[] checks;
125
126     /** listener for visual changes */
127     private ActionListener listener;
128
129     /** Ignored flag - used during firing change events */
130     private boolean ignored = false;
131
132
133     static final long serialVersionUID =6884758007403225916L;
134     /** Creates new form ModifiersPanel */
135     public ModifierPanel(ModifierEditor ed) {
136         myEditor = ed;
137         currentAccessValues = ACCESS_VALUES;
138         currentAccessNames = ACCESS_NAMES;
139         
140         myEditor.addPropertyChangeListener(
141             WeakListeners.propertyChange(
142                 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                 myEditor
154             )
155         );
156
157         listener = new ActionListener() {
158                        public void actionPerformed(ActionEvent evt) {
159                            // remove abstract, if private access is being selected.
160
int selIndex = accessCombo.getSelectedIndex();
161                            // disallow abstract, if private access is selected.
162
if (evt.getSource() == accessCombo && selIndex < 0) {
163                    // revert the combo to the previous value.
164
return;
165                }
166                            if (checks[CHECK_ABSTRACT].isSelected() &&
167                             ((myEditor.getModifier() & MODIFIER_VALUES[CHECK_ABSTRACT]) == 0) &&
168                             ((myEditor.getModifier() & Modifier.PRIVATE) > 0)) {
169                                checks[CHECK_ABSTRACT].setSelected(false);
170                            }
171                            if (selIndex >= 0 &&
172                    (currentAccessValues[accessCombo.getSelectedIndex()] & Modifier.PRIVATE) > 0 &&
173                         (myEditor.getModifier() & Modifier.PRIVATE) == 0)
174                                checks[CHECK_ABSTRACT].setSelected(false);
175                    
176                            excludeChecks(CHECK_ABSTRACT, CHECK_FINAL);
177                            excludeChecks(CHECK_ABSTRACT, CHECK_NATIVE);
178                            excludeChecks(CHECK_ABSTRACT, CHECK_STATIC);
179                            excludeChecks(CHECK_ABSTRACT, CHECK_SYNCHRONIZED);
180                            excludeChecks(CHECK_VOLATILE, CHECK_FINAL);
181                            
182                            if (!ignored)
183                                updateValue();
184                        }
185                    };
186
187         ignored = true;
188         initComponents();
189
190         modifPanel.setBorder (new javax.swing.border.CompoundBorder JavaDoc(
191                                   new javax.swing.border.TitledBorder JavaDoc(getString("LAB_Modifiers")),
192                                   new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(3, 3, 3, 3))
193                               ));
194
195         updateAccess();
196         updateModifiers();
197         updateComponents();
198         ignored = false;
199
200         jLabel1.setDisplayedMnemonic(getString("LAB_AccessRights_Mnemonic").charAt(0));
201
202         jLabel1.getAccessibleContext().setAccessibleDescription(getString("ACSD_AccessRights"));
203     }
204
205     /** Makes sure that the specified two checkboxes are mutually exclusive by
206      * unselecting one if the other one becomes selected.
207      */

208     private void excludeChecks(int check1, int check2) {
209         if (checks[check1].isSelected() && ((myEditor.getModifier() & MODIFIER_VALUES[check1]) == 0))
210             checks[check2].setSelected(false);
211         else if (checks[check2].isSelected() && ((myEditor.getModifier() & MODIFIER_VALUES[check2]) == 0))
212             checks[check1].setSelected(false);
213     }
214
215     /** This method is called from within the constructor to
216      * initialize the form.
217      * WARNING: Do NOT modify this code. The content of this method is
218      * always regenerated by the FormEditor.
219      */

220     private void initComponents() {//GEN-BEGIN:initComponents
221
modifPanel = new javax.swing.JPanel JavaDoc();
222         jPanel2 = new javax.swing.JPanel JavaDoc();
223         jLabel1 = new javax.swing.JLabel JavaDoc();
224         accessCombo = new javax.swing.JComboBox JavaDoc();
225
226         setLayout(new java.awt.BorderLayout JavaDoc());
227
228         setBorder(new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(6, 7, 6, 7)));
229         modifPanel.setLayout(new java.awt.GridLayout JavaDoc(4, 2, 4, 4));
230
231         checks = new JCheckBox[MODIFIER_COUNT];
232         for (int i = 0; i < MODIFIER_COUNT; i++) {
233             checks[i] = new JCheckBox(MODIFIER_NAMES[i]);
234             checks[i].getAccessibleContext().setAccessibleDescription(getModifierDescription(i));
235             modifPanel.add(checks[i]);
236             checks[i].setEnabled((myEditor.getMask() & MODIFIER_VALUES[i]) != 0);
237             checks[i].addActionListener(listener);
238         }
239
240         add(modifPanel, java.awt.BorderLayout.CENTER);
241
242         jPanel2.setLayout(new java.awt.BorderLayout JavaDoc(8, 8));
243
244         jPanel2.setBorder(new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(5, 5, 5, 5)));
245         jLabel1.setText(getString("LAB_AccessRights"));
246         jLabel1.setLabelFor(accessCombo);
247         jPanel2.add(jLabel1, java.awt.BorderLayout.WEST);
248
249         accessCombo.addActionListener(listener);
250         jPanel2.add(accessCombo, java.awt.BorderLayout.CENTER);
251
252         add(jPanel2, java.awt.BorderLayout.NORTH);
253
254     }//GEN-END:initComponents
255

256
257     // Variables declaration - do not modify//GEN-BEGIN:variables
258
private javax.swing.JComboBox JavaDoc accessCombo;
259     private javax.swing.JLabel JavaDoc jLabel1;
260     private javax.swing.JPanel JavaDoc jPanel2;
261     private javax.swing.JPanel JavaDoc modifPanel;
262     // End of variables declaration//GEN-END:variables
263

264     /** Update access ComboBox values depending on new 'mask' property
265      */

266     private void updateAccess() {
267         int selValue = myEditor.getModifier() & ACCESS_MASK;
268         int selIndex = -1;
269
270         int counter = 1;
271         for (int i = 1; i < ACCESS_VALUES.length; i++) {
272             if ((ACCESS_VALUES[i] & myEditor.getMask()) != 0)
273                 counter++;
274         }
275         currentAccessValues = new int[counter];
276         currentAccessNames = new String JavaDoc[counter];
277
278         currentAccessValues[0] = ACCESS_VALUES[0];
279         currentAccessNames[0] = ACCESS_NAMES[0];
280         counter = 1;
281
282         for (int i = 1; i < ACCESS_VALUES.length; i++) {
283             if ((ACCESS_VALUES[i] & myEditor.getMask()) != 0) {
284                 currentAccessValues[counter] = ACCESS_VALUES[i];
285                 currentAccessNames[counter] = ACCESS_NAMES[i];
286                 if (ACCESS_VALUES[i] == selValue) {
287                     selIndex = counter;
288                 }
289                 counter++;
290             }
291         }
292         if (selIndex == -1 && selValue == 0)
293             selIndex = 0;
294
295         ignored = true;
296         accessCombo.setModel(new DefaultComboBoxModel(currentAccessNames));
297         accessCombo.setSelectedIndex(selIndex);
298         ignored = false;
299     }
300
301     /** Update enable status of all modifiers check boxes
302      */

303     private void updateModifiers() {
304         for (int i = 0; i < MODIFIER_COUNT; i++) {
305             checks[i].setEnabled((myEditor.getMask() & MODIFIER_VALUES[i]) != 0);
306         }
307     }
308
309     /** Update the components inside the ModifierPanel depending on new value
310      * of 'modifier' property.
311      */

312     private void updateComponents() {
313     updateAccessCombo();
314     updateModifiers();
315         for (int i = 0; i < MODIFIER_COUNT; i++) {
316             checks[i].setSelected((myEditor.getModifier() & MODIFIER_VALUES[i]) != 0);
317         }
318     }
319     
320     private void updateAccessCombo() {
321         int selIndex = -1;
322         if (myEditor.getModifier() == 0) {
323             selIndex = 0;
324         } else {
325             for (int i = 1; i < currentAccessValues.length; i++) {
326                 if ((currentAccessValues[i] & myEditor.getModifier()) != 0) {
327                     if (selIndex != -1) {
328                         selIndex = -1;
329                         break;
330                     }
331                     selIndex = i;
332                 }
333             }
334         }
335         if (accessCombo.getSelectedIndex() != selIndex) {
336             accessCombo.setSelectedIndex(selIndex);
337         }
338     }
339
340     /** Updates the value depending on the status of the components. */
341     private void updateValue() {
342         int newValue = 0;
343     int comboIndex = accessCombo.getSelectedIndex();
344     
345     if (comboIndex == -1) {
346         newValue = myEditor.getModifier() & ACCESS_MASK;
347     } else {
348             newValue |= currentAccessValues[comboIndex];
349     }
350
351         for (int i = 0; i < MODIFIER_COUNT; i++) {
352             if (checks[i].isSelected() & checks[i].isEnabled())
353                 newValue |= MODIFIER_VALUES[i];
354         }
355         if (myEditor.getModifier() != newValue) {
356             myEditor.setModifier(newValue);
357         }
358     }
359     
360     /* package private */void setMnemonics(PropertyEnv env) {
361         FeatureDescriptor JavaDoc desc = env.getFeatureDescriptor();
362
363         for (int i = 0; i < MODIFIER_COUNT; i++) {
364             checks[i].setMnemonic(getModifierMnemonics(i));
365         
366             Object JavaDoc o = desc.getValue(MODIFIER_MNEMONICS_KEYS[i]);
367             if (o instanceof String JavaDoc) {
368                 checks[i].setMnemonic(((String JavaDoc)o).charAt(0));
369             } else {
370                 checks[i].setMnemonic(getModifierMnemonics(i));
371             }
372         }
373     }
374     
375     private static String JavaDoc getString(String JavaDoc key) {
376         return NbBundle.getBundle("org.openide.explorer.propertysheet.editors.Bundle2", Locale.getDefault(), ModifierPanel.class.getClassLoader()).getString(key);
377     }
378     
379     static String JavaDoc getModifierDescription(int i) {
380         return getString(MODIFIER_DESCRIPTION_KEYS[i]);
381     }
382     
383     static char getModifierMnemonics(int i) {
384         return getString(MODIFIER_MNEMONICS_KEYS[i]).charAt(0);
385     }
386 }
387
Popular Tags