001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.form;
043:
044: import org.openide.util.datatransfer.NewType;
045:
046: import java.awt.*;
047: import javax.swing.*;
048: import java.util.*;
049:
050: import org.netbeans.modules.form.project.ClassSource;
051:
052: /**
053: *
054: * @author Ian Formanek
055: */
056:
057: public class RADMenuComponent extends RADMenuItemComponent implements
058: ComponentContainer {
059:
060: /** Map of possible combinations of menus in menus. Menu types (as Integer)
061: * are mapped to supported (sub)menu types (as Class[]).
062: */
063: static Map<Integer, Class[]> supportedMenus;
064:
065: /** Initialization of supportedMenus map. */
066: static {
067: supportedMenus = new HashMap<Integer, Class[]>();
068: supportedMenus.put(new Integer(T_MENUBAR),
069: new Class[] { Menu.class });
070: supportedMenus.put(new Integer(T_MENU), new Class[] {
071: MenuItem.class, CheckboxMenuItem.class, Menu.class,
072: Separator.class });
073: supportedMenus.put(new Integer(T_POPUPMENU), new Class[] {
074: MenuItem.class, CheckboxMenuItem.class, Menu.class,
075: Separator.class });
076: // supportedMenus.put(new Integer(T_JMENUBAR),
077: // new Class[] { JMenu.class });
078: // supportedMenus.put(new Integer(T_JMENU),
079: // new Class[] { JMenuItem.class,
080: // JCheckBoxMenuItem.class,
081: // JRadioButtonMenuItem.class,
082: // JMenu.class,
083: // JSeparator.class });
084: // supportedMenus.put(new Integer(T_JPOPUPMENU),
085: // new Class[] { JMenuItem.class,
086: // JCheckBoxMenuItem.class,
087: // JRadioButtonMenuItem.class,
088: // JMenu.class,
089: // JSeparator.class });
090: }
091:
092: // -----------------------------------------------------------------------------
093: // Private variables
094:
095: private ArrayList<RADComponent> subComponents;
096:
097: // -----------------------------------------------------------------------------
098: // Initialization
099:
100: /** Support for new types that can be created in this node.
101: * @return array of new type operations that are allowed
102: */
103: @Override
104: public NewType[] getNewTypes() {
105: if (isReadOnly())
106: return RADComponent.NO_NEW_TYPES;
107:
108: Class[] classes = supportedMenus.get(new Integer(
109: getMenuItemType()));
110:
111: if (classes == null)
112: return RADComponent.NO_NEW_TYPES;
113:
114: NewType[] types = new NewType[classes.length];
115: for (int i = 0; i < types.length; i++)
116: types[i] = new NewMenuType(classes[i]);
117:
118: return types;
119: }
120:
121: public boolean canAddItem(Class itemType) {
122: Class[] classes = supportedMenus.get(new Integer(
123: getMenuItemType()));
124:
125: if (classes != null)
126: for (int i = 0; i < classes.length; i++)
127: if (classes[i] == itemType) // or more general isAssignableFrom ??
128: return true;
129:
130: return false;
131: }
132:
133: // -----------------------------------------------------------------------------
134: // SubComponents Management
135:
136: public RADComponent[] getSubBeans() {
137: RADComponent[] components = new RADComponent[subComponents
138: .size()];
139: subComponents.toArray(components);
140: return components;
141: }
142:
143: public void initSubComponents(RADComponent[] initComponents) {
144: if (subComponents == null)
145: subComponents = new ArrayList<RADComponent>(
146: 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
163: .size()];
164: for (int i = 0; i < perm.length; i++)
165: components[perm[i]] = subComponents.get(i);
166:
167: subComponents.clear();
168: subComponents.addAll(Arrays.asList(components));
169: }
170:
171: public void add(RADComponent comp) {
172: if (comp instanceof RADMenuItemComponent) {
173: subComponents.add(comp);
174: comp.setParentComponent(this );
175: // getNodeReference().updateChildren();
176: }
177: }
178:
179: public void remove(RADComponent comp) {
180: if (subComponents.remove(comp))
181: comp.setParentComponent(null);
182: // getNodeReference().updateChildren();
183: }
184:
185: public int getIndexOf(RADComponent comp) {
186: return subComponents.indexOf(comp);
187: }
188:
189: // -------------
190: // Innerclasses
191:
192: /** NewType for creating sub-MenuItem. */
193: class NewMenuType extends NewType {
194: /** Class which represents the menu class for this NewType */
195: Class item;
196:
197: /** Constructs new NewType for the given menu class */
198: public NewMenuType(Class item) {
199: this .item = item;
200: }
201:
202: /** Display name for the creation action. This should be
203: * presented as an item in a menu.
204: *
205: * @return the name of the action
206: */
207: @Override
208: public String getName() {
209: String s = item.getName();
210:
211: int index = s.lastIndexOf('.');
212: if (index != -1)
213: return s.substring(index + 1);
214: else
215: return s;
216: }
217:
218: /** Create the object.
219: * @exception IOException if something fails
220: */
221: public void create() throws java.io.IOException {
222: getFormModel().getComponentCreator().createComponent(
223: new ClassSource(item.getName()),
224: RADMenuComponent.this, null);
225: }
226: }
227: }
|