KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.openide.util.datatransfer.NewType;
23
24 import java.awt.*;
25 import javax.swing.*;
26 import java.util.*;
27
28 import org.netbeans.modules.form.project.ClassSource;
29
30 /**
31  *
32  * @author Ian Formanek
33  */

34
35 public class RADMenuComponent extends RADMenuItemComponent implements ComponentContainer {
36
37     /** Map of possible combinations of menus in menus. Menu types (as Integer)
38      * are mapped to supported (sub)menu types (as Class[]).
39      */

40     static Map supportedMenus;
41
42     /** Initialization of supportedMenus map. */
43     static {
44         supportedMenus = new HashMap();
45         supportedMenus.put(new Integer JavaDoc(T_MENUBAR),
46                            new Class JavaDoc[] { Menu.class });
47         supportedMenus.put(new Integer JavaDoc(T_MENU),
48                            new Class JavaDoc[] { MenuItem.class,
49                                          CheckboxMenuItem.class,
50                                          Menu.class,
51                                          Separator.class });
52         supportedMenus.put(new Integer JavaDoc(T_POPUPMENU),
53                            new Class JavaDoc[] { MenuItem.class,
54                                          CheckboxMenuItem.class,
55                                          Menu.class,
56                                          Separator.class });
57         supportedMenus.put(new Integer JavaDoc(T_JMENUBAR),
58                            new Class JavaDoc[] { JMenu.class });
59         supportedMenus.put(new Integer JavaDoc(T_JMENU),
60                            new Class JavaDoc[] { JMenuItem.class,
61                                          JCheckBoxMenuItem.class,
62                                          JRadioButtonMenuItem.class,
63                                          JMenu.class,
64                                          JSeparator.class });
65         supportedMenus.put(new Integer JavaDoc(T_JPOPUPMENU),
66                            new Class JavaDoc[] { JMenuItem.class,
67                                          JCheckBoxMenuItem.class,
68                                          JRadioButtonMenuItem.class,
69                                          JMenu.class,
70                                          JSeparator.class });
71     }
72
73     // -----------------------------------------------------------------------------
74
// Private variables
75

76     private ArrayList subComponents;
77
78     // -----------------------------------------------------------------------------
79
// Initialization
80

81     /** Support for new types that can be created in this node.
82      * @return array of new type operations that are allowed
83      */

84     public NewType[] getNewTypes() {
85         if (isReadOnly())
86             return RADComponent.NO_NEW_TYPES;
87
88         Class JavaDoc[] classes = (Class JavaDoc[])
89                           supportedMenus.get(new Integer JavaDoc(getMenuItemType()));
90
91         if (classes == null)
92             return RADComponent.NO_NEW_TYPES;
93
94         NewType[] types = new NewType[classes.length];
95         for (int i = 0; i < types.length; i++)
96             types[i] = new NewMenuType(classes[i]);
97
98         return types;
99     }
100
101     public boolean canAddItem(Class JavaDoc itemType) {
102         Class JavaDoc[] classes = (Class JavaDoc[])
103                           supportedMenus.get(new Integer JavaDoc(getMenuItemType()));
104
105         if (classes != null)
106             for (int i=0; i < classes.length; i++)
107                 if (classes[i] == itemType) // or more general isAssignableFrom ??
108
return true;
109
110         return false;
111     }
112
113     // -----------------------------------------------------------------------------
114
// SubComponents Management
115

116     public RADComponent[] getSubBeans() {
117         RADComponent[] components = new RADComponent [subComponents.size()];
118         subComponents.toArray(components);
119         return components;
120     }
121
122     public void initSubComponents(RADComponent[] initComponents) {
123         if (subComponents == null)
124             subComponents = new ArrayList(initComponents.length);
125         else {
126             subComponents.clear();
127             subComponents.ensureCapacity(initComponents.length);
128         }
129
130         for (int i = 0; i < initComponents.length; i++) {
131             RADComponent comp = initComponents[i];
132             if (comp instanceof RADMenuItemComponent) {
133                 subComponents.add(comp);
134                 comp.setParentComponent(this);
135             }
136         }
137     }
138
139     public void reorderSubComponents(int[] perm) {
140         RADComponent[] components = new RADComponent[subComponents.size()];
141         for (int i=0; i < perm.length; i++)
142             components[perm[i]] = (RADComponent) subComponents.get(i);
143
144         subComponents.clear();
145         subComponents.addAll(Arrays.asList(components));
146     }
147
148     public void add(RADComponent comp) {
149         if (comp instanceof RADMenuItemComponent) {
150             subComponents.add(comp);
151             comp.setParentComponent(this);
152 // getNodeReference().updateChildren();
153
}
154     }
155
156     public void remove(RADComponent comp) {
157         if (subComponents.remove(comp))
158             comp.setParentComponent(null);
159 // getNodeReference().updateChildren();
160
}
161
162     public int getIndexOf(RADComponent comp) {
163         return subComponents.indexOf(comp);
164     }
165
166     // -------------
167
// Innerclasses
168

169     /** NewType for creating sub-MenuItem. */
170     class NewMenuType extends NewType {
171         /** Class which represents the menu class for this NewType */
172         Class JavaDoc item;
173
174         /** Constructs new NewType for the given menu class */
175         public NewMenuType(Class JavaDoc item) {
176             this.item = item;
177         }
178
179         /** Display name for the creation action. This should be
180          * presented as an item in a menu.
181          *
182          * @return the name of the action
183          */

184         public String JavaDoc getName() {
185             String JavaDoc s = item.getName();
186
187             int index = s.lastIndexOf('.');
188             if (index != -1)
189                 return s.substring(index + 1);
190             else
191                 return s;
192         }
193
194         /** Create the object.
195          * @exception IOException if something fails
196          */

197         public void create() throws java.io.IOException JavaDoc {
198             getFormModel().getComponentCreator()
199                 .createComponent(new ClassSource(item.getName(), null, null),
200                                  RADMenuComponent.this,
201                                  null);
202         }
203     }
204 }
205
Popular Tags