1 19 20 package org.netbeans.modules.form.actions; 21 22 import javax.swing.*; 23 24 import org.openide.util.HelpCtx; 25 import org.openide.util.actions.CallableSystemAction; 26 import org.openide.nodes.Node; 27 import org.openide.nodes.NodeAcceptor; 28 29 import org.netbeans.modules.form.palette.PaletteItem; 30 import org.netbeans.modules.form.palette.PaletteMenuView; 31 import org.netbeans.modules.form.*; 32 33 40 41 public class AddAction extends CallableSystemAction { 42 43 private static String name; 44 45 public String getName() { 46 if (name == null) 47 name = org.openide.util.NbBundle.getBundle(AddAction.class) 48 .getString("ACT_Add"); return name; 50 } 51 52 public HelpCtx getHelpCtx() { 53 return new HelpCtx(AddAction.class); 54 } 55 56 public boolean isEnabled() { 57 Node[] nodes = getNodes(); 58 for (int i=0; i < nodes.length; i++) { 59 FormCookie formCookie = 60 (FormCookie) nodes[i].getCookie(FormCookie.class); 61 if (formCookie == null) 62 return false; 63 64 RADComponentCookie radCookie = 65 (RADComponentCookie) nodes[i].getCookie(RADComponentCookie.class); 66 if (radCookie != null 67 && !(radCookie.getRADComponent() instanceof ComponentContainer)) 68 return false; 69 } 70 return true; 71 } 72 73 public JMenuItem getMenuPresenter() { 74 return getPopupPresenter(); 75 } 76 77 public JMenuItem getPopupPresenter() { 78 JMenuItem menu = new PaletteMenuView( 79 new NodeAcceptor() { 80 public boolean acceptNodes(Node[] nodes) { 81 if (nodes.length != 1) 82 return false; 83 84 PaletteItem paletteItem = 85 (PaletteItem) nodes[0].getCookie(PaletteItem.class); 86 if (paletteItem == null) 87 return false; 88 89 nodes = getNodes(); 90 if (nodes.length == 0) 91 return false; 92 93 boolean added = false; 94 95 for (int i=0; i < nodes.length; i++) { 96 FormCookie formCookie = 97 (FormCookie) nodes[i].getCookie(FormCookie.class); 98 if (formCookie == null) 99 continue; 100 101 RADComponentCookie radCookie = (RADComponentCookie) 102 nodes[i].getCookie(RADComponentCookie.class); 103 RADComponent targetComponent; 104 if (radCookie != null) { 105 targetComponent = radCookie.getRADComponent(); 106 if (!(targetComponent instanceof ComponentContainer)) 107 continue; 108 } 109 else targetComponent = null; 110 111 if (formCookie.getFormModel().getComponentCreator() 112 .createComponent(paletteItem.getComponentClassSource(), 113 targetComponent, 114 null) != null) 115 added = true; 116 } 117 118 return added; 119 } 120 } 121 ); 122 123 menu.setText(getName()); 124 menu.setEnabled(isEnabled()); 125 HelpCtx.setHelpIDString(menu, AddAction.class.getName()); 126 return menu; 127 } 128 129 protected boolean asynchronous() { 130 return false; 131 } 132 133 public void performAction() { 134 } 135 136 138 private static Node[] getNodes() { 139 return ComponentInspector.getInstance().getExplorerManager().getSelectedNodes(); 143 } 144 } 145 | Popular Tags |