KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > RADMenuItemComponent


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.form;
21
22 import java.awt.*;
23 import javax.swing.*;
24 import javax.accessibility.*;
25 import java.beans.PropertyEditor JavaDoc;
26
27 import org.openide.nodes.Node;
28 import org.openide.util.NbBundle;
29
30 /**
31  * RADMenuItemComponent represents one menu item component in the Form.
32  *
33  * @author Petr Hamernik, Ian Formanek
34  */

35
36 public class RADMenuItemComponent extends RADComponent {
37
38     private MetaAccessibleContext accessibilityData;
39     private FormProperty[] accessibilityProperties;
40
41     /** Type of menu */
42     private int type;
43
44     /** Possible constants for type variable */
45     static final int T_MENUBAR = 0x01110;
46     static final int T_MENUITEM = 0x00011;
47     static final int T_CHECKBOXMENUITEM = 0x00012;
48     static final int T_MENU = 0x00113;
49     static final int T_POPUPMENU = 0x01114;
50
51     static final int T_JPOPUPMENU = 0x01125;
52     static final int T_JMENUBAR = 0x01126;
53     static final int T_JMENUITEM = 0x00027;
54     static final int T_JCHECKBOXMENUITEM = 0x00028;
55     static final int T_JMENU = 0x00129;
56     static final int T_JRADIOBUTTONMENUITEM = 0x0002A;
57
58     static final int T_SEPARATOR = 0x1001B;
59     static final int T_JSEPARATOR = 0x1002C;
60
61     /** Masks for the T_XXX constants */
62     static final int MASK_AWT = 0x00010;
63     static final int MASK_SWING = 0x00020;
64     static final int MASK_CONTAINER = 0x00100;
65     static final int MASK_ROOT = 0x01000;
66     static final int MASK_SEPARATOR = 0x10000;
67
68
69     public Object JavaDoc initInstance(Class JavaDoc beanClass) throws Exception JavaDoc {
70         type = recognizeType(beanClass);
71         return super.initInstance(beanClass);
72     }
73
74     protected org.openide.nodes.Node.Property[] createSyntheticProperties() {
75         // no synthetic properties for AWT Separator
76
if (type == T_SEPARATOR)
77             return RADComponent.NO_PROPERTIES;
78         else
79             return super.createSyntheticProperties();
80     }
81
82     int getMenuItemType() {
83         return type;
84     }
85
86     /** Recognizes type of the menu from its class.
87      * @return adequate T_XXX constant
88      */

89     static int recognizeType(Class JavaDoc cl) {
90         if (JSeparator.class.isAssignableFrom(cl))
91             return T_JSEPARATOR;
92         if (org.netbeans.modules.form.Separator.class.isAssignableFrom(cl))
93             return T_SEPARATOR;
94         if (PopupMenu.class.isAssignableFrom(cl))
95             return T_POPUPMENU;
96         if (Menu.class.isAssignableFrom(cl))
97             return T_MENU;
98         if (CheckboxMenuItem.class.isAssignableFrom(cl))
99             return T_CHECKBOXMENUITEM;
100         if (MenuItem.class.isAssignableFrom(cl))
101             return T_MENUITEM;
102         if (MenuBar.class.isAssignableFrom(cl))
103             return T_MENUBAR;
104         if (JRadioButtonMenuItem.class.isAssignableFrom(cl))
105             return T_JRADIOBUTTONMENUITEM;
106         if (JMenu.class.isAssignableFrom(cl))
107             return T_JMENU;
108         if (JCheckBoxMenuItem.class.isAssignableFrom(cl))
109             return T_JCHECKBOXMENUITEM;
110         if (JMenuItem.class.isAssignableFrom(cl))
111             return T_JMENUITEM;
112         if (JMenuBar.class.isAssignableFrom(cl))
113             return T_JMENUBAR;
114         if (JPopupMenu.class.isAssignableFrom(cl))
115             return T_JPOPUPMENU;
116
117         throw new IllegalArgumentException JavaDoc("Cannot create RADMenuItemComponent for class: "+cl.getName()); // NOI18N
118
}
119
120     public Object JavaDoc cloneBeanInstance(java.util.Collection JavaDoc relativeProperties) {
121         if (type == T_SEPARATOR)
122             return null; // don't clone artificial org.netbeans.modules.form.Separator
123
return super.cloneBeanInstance(relativeProperties);
124     }
125
126     // ----------
127
// accessibility properties
128

129     // [The following code duplicates lots of code from RADVisualComponent.
130
// It appears not only here that the metacomponents need to be reworked...]
131

132     protected void createPropertySets(java.util.List JavaDoc propSets) {
133         super.createPropertySets(propSets);
134
135         if (accessibilityProperties == null)
136             createAccessibilityProperties();
137
138         if (accessibilityProperties.length > 0)
139             propSets.add(new Node.PropertySet(
140                 "accessibility", // NOI18N
141
FormUtils.getBundleString("CTL_AccessibilityTab"), // NOI18N
142
FormUtils.getBundleString("CTL_AccessibilityTabHint")) // NOI18N
143
{
144                 public Node.Property[] getProperties() {
145                     return getAccessibilityProperties();
146                 }
147             });
148     }
149
150     public Node.Property getPropertyByName(String JavaDoc name,
151                                            Class JavaDoc propertyType,
152                                            boolean fromAll)
153     {
154         if (fromAll && accessibilityProperties == null)
155             createAccessibilityProperties();
156         return super.getPropertyByName(name, propertyType, fromAll);
157     }
158
159     protected void clearProperties() {
160         super.clearProperties();
161         accessibilityData = null;
162         accessibilityProperties = null;
163     }
164
165     public FormProperty[] getAccessibilityProperties() {
166         if (accessibilityProperties == null)
167             createAccessibilityProperties();
168         return accessibilityProperties;
169     }
170
171     private void createAccessibilityProperties() {
172         Object JavaDoc comp = getBeanInstance();
173         if (comp instanceof Accessible
174             && ((Accessible)comp).getAccessibleContext() != null)
175         {
176             if (accessibilityData == null)
177                 accessibilityData = new MetaAccessibleContext();
178             accessibilityProperties = accessibilityData.getProperties();
179
180             for (int i=0; i < accessibilityProperties.length; i++) {
181                 FormProperty prop = accessibilityProperties[i];
182                 setPropertyListener(prop);
183                 prop.setPropertyContext(
184                     new RADProperty.RADPropertyContext(this));
185                 nameToProperty.put(prop.getName(), prop);
186             }
187         }
188         else {
189             accessibilityData = null;
190             accessibilityProperties = NO_PROPERTIES;
191         }
192     }
193
194     private class MetaAccessibleContext {
195         private Object JavaDoc accName = BeanSupport.NO_VALUE;
196         private Object JavaDoc accDescription = BeanSupport.NO_VALUE;
197         private Object JavaDoc accParent = BeanSupport.NO_VALUE;
198
199         private FormProperty[] properties;
200
201         FormProperty[] getProperties() {
202             if (properties == null) {
203                 properties = new FormProperty[] {
204                     new FormProperty(
205                         "AccessibleContext.accessibleName", // NOI18N
206
String JavaDoc.class,
207                         FormUtils.getBundleString("PROP_AccessibleName"), // NOI18N
208
FormUtils.getBundleString("PROP_AccessibleName")) // NOI18N
209
{
210                         public Object JavaDoc getTargetValue() {
211                             return accName != BeanSupport.NO_VALUE ?
212                                        accName : getDefaultValue();
213                         }
214                         public void setTargetValue(Object JavaDoc value) {
215                             accName = (String JavaDoc) value;
216                         }
217                         public boolean supportsDefaultValue () {
218                             return true;
219                         }
220                         public Object JavaDoc getDefaultValue() {
221                             return getAccessibleContext().getAccessibleName();
222                         }
223                         public void restoreDefaultValue()
224                             throws IllegalAccessException JavaDoc,
225                                    java.lang.reflect.InvocationTargetException JavaDoc
226                         {
227                             super.restoreDefaultValue();
228                             accName = BeanSupport.NO_VALUE;
229                         }
230                         String JavaDoc getPartialSetterCode(String JavaDoc javaInitStr) {
231                             return "getAccessibleContext().setAccessibleName(" // NOI18N
232
+ javaInitStr + ")"; // NOI18N
233
}
234                     },
235
236                     new FormProperty(
237                         "AccessibleContext.accessibleDescription", // NOI18N
238
String JavaDoc.class,
239                         FormUtils.getBundleString("PROP_AccessibleDescription"), // NOI18N
240
FormUtils.getBundleString("PROP_AccessibleDescription")) // NOI18N
241
{
242                         public Object JavaDoc getTargetValue() {
243                             return accDescription != BeanSupport.NO_VALUE ?
244                                        accDescription : getDefaultValue();
245                         }
246                         public void setTargetValue(Object JavaDoc value) {
247                             accDescription = (String JavaDoc) value;
248                         }
249                         public boolean supportsDefaultValue () {
250                             return true;
251                         }
252                         public Object JavaDoc getDefaultValue() {
253                             return getAccessibleContext().getAccessibleDescription();
254                         }
255                         public void restoreDefaultValue()
256                             throws IllegalAccessException JavaDoc,
257                                    java.lang.reflect.InvocationTargetException JavaDoc
258                         {
259                             super.restoreDefaultValue();
260                             accDescription = BeanSupport.NO_VALUE;
261                         }
262                         String JavaDoc getPartialSetterCode(String JavaDoc javaInitStr) {
263                             return
264                               "getAccessibleContext().setAccessibleDescription(" // NOI18N
265
+ javaInitStr + ")"; // NOI18N
266
}
267                     },
268
269                     new FormProperty(
270                         "AccessibleContext.accessibleParent", // NOI18N
271
Accessible.class,
272                         FormUtils.getBundleString("PROP_AccessibleParent"), // NOI18N
273
FormUtils.getBundleString("PROP_AccessibleParent")) // NOI18N
274
{
275                         public Object JavaDoc getTargetValue() {
276                             return accParent != BeanSupport.NO_VALUE ?
277                                        accParent : getDefaultValue();
278                         }
279                         public void setTargetValue(Object JavaDoc value) {
280                             accParent = value;
281                         }
282                         public boolean supportsDefaultValue () {
283                             return true;
284                         }
285                         public Object JavaDoc getDefaultValue() {
286                             Object JavaDoc acP = getAccessibleContext()
287                                              .getAccessibleParent();
288                             if (acP != null) {
289                                 RADComponent metacont = getParentComponent();
290                                 if (metacont != null
291                                         && metacont.getBeanInstance() == acP)
292                                     return metacont;
293                             }
294                             return acP;
295                         }
296                         public void restoreDefaultValue()
297                             throws IllegalAccessException JavaDoc,
298                                    java.lang.reflect.InvocationTargetException JavaDoc
299                         {
300                             super.restoreDefaultValue();
301                             accParent = BeanSupport.NO_VALUE;
302                         }
303                         public PropertyEditor JavaDoc getExpliciteEditor() {
304                             return new AccessibleParentEditor();
305                         }
306                         String JavaDoc getPartialSetterCode(String JavaDoc javaInitStr) {
307                             return javaInitStr == null ? null :
308                                 "getAccessibleContext().setAccessibleParent(" // NOI18N
309
+ javaInitStr + ")"; // NOI18N
310
}
311                     }
312                 };
313             }
314             return properties;
315         }
316
317         private AccessibleContext getAccessibleContext() {
318             return ((Accessible)getBeanInstance()).getAccessibleContext();
319         }
320     }
321
322     public static class AccessibleParentEditor extends ComponentChooserEditor {
323         public AccessibleParentEditor() {
324             super();
325             setBeanTypes(new Class JavaDoc[] { Accessible.class });
326         }
327
328         public String JavaDoc getDisplayName() {
329             return NbBundle.getBundle(getClass()).getString("CTL_AccessibleParentEditor_DisplayName"); // NOI18N
330
}
331     }
332 }
333
Popular Tags