1 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 34 35 public class RADMenuComponent extends RADMenuItemComponent implements ComponentContainer { 36 37 40 static Map supportedMenus; 41 42 43 static { 44 supportedMenus = new HashMap(); 45 supportedMenus.put(new Integer (T_MENUBAR), 46 new Class [] { Menu.class }); 47 supportedMenus.put(new Integer (T_MENU), 48 new Class [] { MenuItem.class, 49 CheckboxMenuItem.class, 50 Menu.class, 51 Separator.class }); 52 supportedMenus.put(new Integer (T_POPUPMENU), 53 new Class [] { MenuItem.class, 54 CheckboxMenuItem.class, 55 Menu.class, 56 Separator.class }); 57 supportedMenus.put(new Integer (T_JMENUBAR), 58 new Class [] { JMenu.class }); 59 supportedMenus.put(new Integer (T_JMENU), 60 new Class [] { JMenuItem.class, 61 JCheckBoxMenuItem.class, 62 JRadioButtonMenuItem.class, 63 JMenu.class, 64 JSeparator.class }); 65 supportedMenus.put(new Integer (T_JPOPUPMENU), 66 new Class [] { JMenuItem.class, 67 JCheckBoxMenuItem.class, 68 JRadioButtonMenuItem.class, 69 JMenu.class, 70 JSeparator.class }); 71 } 72 73 76 private ArrayList subComponents; 77 78 81 84 public NewType[] getNewTypes() { 85 if (isReadOnly()) 86 return RADComponent.NO_NEW_TYPES; 87 88 Class [] classes = (Class []) 89 supportedMenus.get(new Integer (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 itemType) { 102 Class [] classes = (Class []) 103 supportedMenus.get(new Integer (getMenuItemType())); 104 105 if (classes != null) 106 for (int i=0; i < classes.length; i++) 107 if (classes[i] == itemType) return true; 109 110 return false; 111 } 112 113 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 } 154 } 155 156 public void remove(RADComponent comp) { 157 if (subComponents.remove(comp)) 158 comp.setParentComponent(null); 159 } 161 162 public int getIndexOf(RADComponent comp) { 163 return subComponents.indexOf(comp); 164 } 165 166 169 170 class NewMenuType extends NewType { 171 172 Class item; 173 174 175 public NewMenuType(Class item) { 176 this.item = item; 177 } 178 179 184 public String getName() { 185 String 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 197 public void create() throws java.io.IOException { 198 getFormModel().getComponentCreator() 199 .createComponent(new ClassSource(item.getName(), null, null), 200 RADMenuComponent.this, 201 null); 202 } 203 } 204 } 205 | Popular Tags |