1 19 20 package org.netbeans.modules.form.actions; 21 22 import java.awt.event.ActionListener ; 23 import java.awt.event.ActionEvent ; 24 import java.util.ResourceBundle ; 25 import java.text.MessageFormat ; 26 import javax.swing.*; 27 import javax.swing.event.MenuListener ; 28 import javax.swing.event.MenuEvent ; 29 30 import org.openide.util.HelpCtx; 31 import org.openide.util.actions.*; 32 import org.openide.nodes.Node; 33 import org.openide.awt.JMenuPlus; 34 import org.openide.util.NbBundle; 35 36 import org.netbeans.modules.form.*; 37 38 43 44 public class EventsAction extends CookieAction { 45 46 protected int mode() { 47 return MODE_EXACTLY_ONE; } 49 50 protected Class [] cookieClasses() { 51 return new Class [] { RADComponentCookie.class }; 52 } 53 54 public String getName() { 55 return NbBundle.getBundle(EventsAction.class).getString("ACT_Events"); } 57 58 public HelpCtx getHelpCtx() { 59 return HelpCtx.DEFAULT_HELP; 60 } 61 62 protected void performAction(Node[] activatedNodes) { 63 } 64 65 public JMenuItem getMenuPresenter() { 66 return getPopupPresenter(); 67 } 68 69 73 public JMenuItem getPopupPresenter() { 74 JMenu popupMenu = new JMenuPlus( 75 NbBundle.getBundle(EventsAction.class).getString("ACT_Events")); 77 popupMenu.setEnabled(isEnabled()); 78 HelpCtx.setHelpIDString(popupMenu, EventsAction.class.getName()); 79 80 popupMenu.addMenuListener(new MenuListener () { 81 public void menuSelected(MenuEvent e) { 82 JMenu menu = (JMenu) e.getSource(); 83 createEventSubmenu(menu); 84 } 85 86 public void menuDeselected(MenuEvent e) {} 87 88 public void menuCanceled(MenuEvent e) {} 89 }); 90 return popupMenu; 91 } 92 93 private void createEventSubmenu(JMenu menu) { 94 if (menu.getMenuComponentCount() > 0) 95 menu.removeAll(); 96 97 Node[] nodes = getActivatedNodes(); 98 if (nodes.length == 0) 99 return; 100 101 RADComponentCookie radCookie = 102 (RADComponentCookie) nodes[0].getCookie(RADComponentCookie.class); 103 if (radCookie == null) 104 return; 105 106 RADComponent metacomp = radCookie.getRADComponent(); 107 if (metacomp == null) 108 return; 109 110 ResourceBundle bundle = NbBundle.getBundle(EventsAction.class); 111 112 boolean readOnly = metacomp.isReadOnly(); 113 Event [] events = readOnly ? 114 metacomp.getKnownEvents() : metacomp.getAllEvents(); 115 116 java.beans.EventSetDescriptor lastEventSetDesc = null; 117 JMenu eventSetMenu = null; 118 boolean eventSetHasHandlers = false; 119 120 for (int i=0; i < events.length; i++) { 121 Event event = events[i]; 122 String [] handlers = event.getEventHandlers(); 123 JMenuItem jmi = null; 124 125 if (handlers.length == 0) { 126 if (!readOnly) 127 jmi = new EventMenuItem( 128 MessageFormat.format( 129 bundle.getString("FMT_CTL_EventNoHandlers"), new Object [] { event.getName() }), 131 event, 132 null); 133 } 134 else if (handlers.length == 1) { 135 jmi = new EventMenuItem( 136 MessageFormat.format( 137 bundle.getString("FMT_CTL_EventOneHandler"), new Object [] { event.getName(), handlers[0] }), 139 event, 140 handlers[0]); 141 } 142 else { 143 jmi = new JMenuPlus(MessageFormat.format( 144 bundle.getString("FMT_CTL_EventMultipleHandlers"), new Object [] { event.getName() })); 146 147 for (int j=0; j < handlers.length; j++) { 148 JMenuItem handlerItem = new EventMenuItem( 149 MessageFormat.format( 150 bundle.getString("FMT_CTL_HandlerFromMultiple"), new Object [] { handlers[j] }), 152 event, 153 handlers[j]); 154 155 handlerItem.addActionListener(getMenuItemListener()); 156 157 HelpCtx.setHelpIDString(handlerItem, EventsAction.class.getName()); 158 setBoldFontForMenuText(handlerItem); 159 160 ((JMenu)jmi).add(handlerItem); 161 } 162 } 163 164 if (jmi != null) { 165 if (event.getEventSetDescriptor() != lastEventSetDesc) { 166 if (eventSetHasHandlers) 167 setBoldFontForMenuText(eventSetMenu); 168 169 String name = event.getEventSetDescriptor().getName(); 170 eventSetMenu = new JMenuPlus(name.substring(0,1).toUpperCase() 171 + name.substring(1)); 172 HelpCtx.setHelpIDString(eventSetMenu, 173 EventsAction.class.getName()); 174 addSortedMenuItem(menu, eventSetMenu); 175 eventSetHasHandlers = false; 176 lastEventSetDesc = event.getEventSetDescriptor(); 177 } 178 179 if (!(jmi instanceof JMenu)) 180 jmi.addActionListener(getMenuItemListener()); 181 182 HelpCtx.setHelpIDString(jmi, EventsAction.class.getName()); 183 184 if (handlers.length > 0 && !readOnly) { 185 eventSetHasHandlers = true; 186 setBoldFontForMenuText(jmi); 187 } 188 189 addSortedMenuItem(eventSetMenu, jmi); 190 } 191 } 192 193 if (eventSetHasHandlers) 194 setBoldFontForMenuText(eventSetMenu); 195 } 196 197 private static void setBoldFontForMenuText(JMenuItem mi) { 198 java.awt.Font font = mi.getFont(); 199 mi.setFont(font.deriveFont(font.getStyle() | java.awt.Font.BOLD)); 200 } 201 202 private static void addSortedMenuItem(JMenu menu, JMenuItem menuItem) { 203 int n = menu.getMenuComponentCount(); 204 String text = menuItem.getText(); 205 for (int i=0; i < n; i++) { 206 String tx = ((JMenuItem)menu.getMenuComponent(i)).getText(); 207 if (text.compareTo(tx) < 0) { 208 menu.add(menuItem, i); 209 return; 210 } 211 } 212 menu.add(menuItem); 213 } 214 215 private ActionListener getMenuItemListener() { 216 if (menuItemListener == null) 217 menuItemListener = new EventMenuItemListener(); 218 return menuItemListener; 219 } 220 221 223 private static class EventMenuItem extends JMenuItem { 224 private Event event; 225 private String handlerName; 226 227 EventMenuItem(String text, Event event, String handlerName) { 228 super(text); 229 this.event = event; 230 this.handlerName = handlerName; 231 } 232 233 Event getEvent() { 234 return event; 235 } 236 237 String getHandlerName() { 238 return handlerName; 239 } 240 } 241 242 private static class EventMenuItemListener implements ActionListener { 243 public void actionPerformed(ActionEvent evt) { 244 Object source = evt.getSource(); 245 if (!(source instanceof EventMenuItem)) 246 return; 247 248 EventMenuItem mi = (EventMenuItem) source; 249 Event event = ((EventMenuItem)source).getEvent(); 250 Node.Property prop = event.getComponent() 251 .getPropertyByName(event.getId()); 252 if (prop != null) { 253 String handlerName = mi.getHandlerName(); 254 event.getComponent().getFormModel().getFormEvents() 255 .attachEvent(event, handlerName, null); 256 257 try { if (handlerName == null) 259 handlerName = (String ) prop.getValue(); 260 prop.setValue(handlerName); 261 } 262 catch (Exception ex) {} 263 } 264 } 265 } 266 267 private ActionListener menuItemListener; 268 } 269 | Popular Tags |