~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to form/src/org/netbeans/modules/form/RADMenuComponent.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.form;
 
43
 
 
44
import org.openide.util.datatransfer.NewType;
 
45
 
 
46
import java.awt.*;
 
47
import javax.swing.*;
 
48
import java.util.*;
 
49
 
 
50
import org.netbeans.modules.form.project.ClassSource;
 
51
 
 
52
/**
 
53
 *
 
54
 * @author Ian Formanek
 
55
 */
 
56
 
 
57
public class RADMenuComponent extends RADMenuItemComponent implements ComponentContainer {
 
58
 
 
59
    /** Map of possible combinations of menus in menus. Menu types (as Integer)
 
60
     * are mapped to supported (sub)menu types (as Class[]).
 
61
     */
 
62
    static Map supportedMenus;
 
63
 
 
64
    /** Initialization of supportedMenus map. */
 
65
    static {
 
66
        supportedMenus = new HashMap();
 
67
        supportedMenus.put(new Integer(T_MENUBAR),
 
68
                           new Class[] { Menu.class });
 
69
        supportedMenus.put(new Integer(T_MENU),
 
70
                           new Class[] { MenuItem.class,
 
71
                                         CheckboxMenuItem.class,
 
72
                                         Menu.class,
 
73
                                         Separator.class });
 
74
        supportedMenus.put(new Integer(T_POPUPMENU),
 
75
                           new Class[] { MenuItem.class,
 
76
                                         CheckboxMenuItem.class,
 
77
                                         Menu.class,
 
78
                                         Separator.class });
 
79
//        supportedMenus.put(new Integer(T_JMENUBAR),
 
80
//                           new Class[] { JMenu.class });
 
81
//        supportedMenus.put(new Integer(T_JMENU),
 
82
//                           new Class[] { JMenuItem.class,
 
83
//                                         JCheckBoxMenuItem.class,
 
84
//                                         JRadioButtonMenuItem.class,
 
85
//                                         JMenu.class,
 
86
//                                         JSeparator.class });
 
87
//        supportedMenus.put(new Integer(T_JPOPUPMENU),
 
88
//                           new Class[] { JMenuItem.class,
 
89
//                                         JCheckBoxMenuItem.class,
 
90
//                                         JRadioButtonMenuItem.class,
 
91
//                                         JMenu.class,
 
92
//                                         JSeparator.class });
 
93
    }
 
94
 
 
95
    // -----------------------------------------------------------------------------
 
96
    // Private variables
 
97
 
 
98
    private ArrayList subComponents;
 
99
 
 
100
    // -----------------------------------------------------------------------------
 
101
    // Initialization
 
102
 
 
103
    /** Support for new types that can be created in this node.
 
104
     * @return array of new type operations that are allowed
 
105
     */
 
106
    public NewType[] getNewTypes() {
 
107
        if (isReadOnly())
 
108
            return RADComponent.NO_NEW_TYPES;
 
109
 
 
110
        Class[] classes = (Class[])
 
111
                          supportedMenus.get(new Integer(getMenuItemType()));
 
112
 
 
113
        if (classes == null)
 
114
            return RADComponent.NO_NEW_TYPES;
 
115
 
 
116
        NewType[] types = new NewType[classes.length];
 
117
        for (int i = 0; i < types.length; i++)
 
118
            types[i] = new NewMenuType(classes[i]);
 
119
 
 
120
        return types;
 
121
    }
 
122
 
 
123
    public boolean canAddItem(Class itemType) {
 
124
        Class[] classes = (Class[])
 
125
                          supportedMenus.get(new Integer(getMenuItemType()));
 
126
 
 
127
        if (classes != null)
 
128
            for (int i=0; i < classes.length; i++)
 
129
                if (classes[i] == itemType) // or more general isAssignableFrom ??
 
130
                    return true;
 
131
 
 
132
        return false;
 
133
    }
 
134
 
 
135
    // -----------------------------------------------------------------------------
 
136
    // SubComponents Management
 
137
 
 
138
    public RADComponent[] getSubBeans() {
 
139
        RADComponent[] components = new RADComponent [subComponents.size()];
 
140
        subComponents.toArray(components);
 
141
        return components;
 
142
    }
 
143
 
 
144
    public void initSubComponents(RADComponent[] initComponents) {
 
145
        if (subComponents == null)
 
146
            subComponents = new ArrayList(initComponents.length);
 
147
        else {
 
148
            subComponents.clear();
 
149
            subComponents.ensureCapacity(initComponents.length);
 
150
        }
 
151
 
 
152
        for (int i = 0; i < initComponents.length; i++) {
 
153
            RADComponent comp = initComponents[i];
 
154
            if (comp instanceof RADMenuItemComponent) {
 
155
                subComponents.add(comp);
 
156
                comp.setParentComponent(this);
 
157
            }
 
158
        }
 
159
    }
 
160
 
 
161
    public void reorderSubComponents(int[] perm) {
 
162
        RADComponent[] components = new RADComponent[subComponents.size()];
 
163
        for (int i=0; i < perm.length; i++)
 
164
            components[perm[i]] = (RADComponent) subComponents.get(i);
 
165
 
 
166
        subComponents.clear();
 
167
        subComponents.addAll(Arrays.asList(components));
 
168
    }
 
169
 
 
170
    public void add(RADComponent comp) {
 
171
        if (comp instanceof RADMenuItemComponent) {
 
172
            subComponents.add(comp);
 
173
            comp.setParentComponent(this);
 
174
//            getNodeReference().updateChildren();
 
175
        }
 
176
    }
 
177
 
 
178
    public void remove(RADComponent comp) {
 
179
        if (subComponents.remove(comp))
 
180
            comp.setParentComponent(null);
 
181
//        getNodeReference().updateChildren();
 
182
    }
 
183
 
 
184
    public int getIndexOf(RADComponent comp) {
 
185
        return subComponents.indexOf(comp);
 
186
    }
 
187
 
 
188
    // -------------
 
189
    // Innerclasses
 
190
 
 
191
    /** NewType for creating sub-MenuItem. */
 
192
    class NewMenuType extends NewType {
 
193
        /** Class which represents the menu class for this NewType */
 
194
        Class item;
 
195
 
 
196
        /** Constructs new NewType for the given menu class */
 
197
        public NewMenuType(Class item) {
 
198
            this.item = item;
 
199
        }
 
200
 
 
201
        /** Display name for the creation action. This should be
 
202
         * presented as an item in a menu.
 
203
         *
 
204
         * @return the name of the action
 
205
         */
 
206
        public String getName() {
 
207
            String s = item.getName();
 
208
 
 
209
            int index = s.lastIndexOf('.');
 
210
            if (index != -1)
 
211
                return s.substring(index + 1);
 
212
            else
 
213
                return s;
 
214
        }
 
215
 
 
216
        /** Create the object.
 
217
         * @exception IOException if something fails
 
218
         */
 
219
        public void create() throws java.io.IOException {
 
220
            getFormModel().getComponentCreator()
 
221
                .createComponent(new ClassSource(item.getName(), null, null),
 
222
                                 RADMenuComponent.this,
 
223
                                 null);
 
224
        }
 
225
    }
 
226
}