KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > actions > EventsAction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.form.actions;
21
22 import java.awt.event.ActionListener JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.util.ResourceBundle JavaDoc;
25 import java.text.MessageFormat JavaDoc;
26 import javax.swing.*;
27 import javax.swing.event.MenuListener JavaDoc;
28 import javax.swing.event.MenuEvent JavaDoc;
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 /**
39  * Action class providing popup menu presenter for events of one component.
40  *
41  * @author Tomas Pavek
42  */

43
44 public class EventsAction extends CookieAction {
45
46     protected int mode() {
47         return MODE_EXACTLY_ONE; // can be invoked on just one node
48
}
49
50     protected Class JavaDoc[] cookieClasses() {
51         return new Class JavaDoc[] { RADComponentCookie.class };
52     }
53
54     public String JavaDoc getName() {
55         return NbBundle.getBundle(EventsAction.class).getString("ACT_Events"); // NOI18N
56
}
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     /**
70      * Returns a JMenuItem that presents this action in a Popup Menu.
71      * @return the JMenuItem representation for the action
72      */

73     public JMenuItem getPopupPresenter() {
74         JMenu popupMenu = new JMenuPlus(
75             NbBundle.getBundle(EventsAction.class).getString("ACT_Events")); // NOI18N
76

77         popupMenu.setEnabled(isEnabled());
78         HelpCtx.setHelpIDString(popupMenu, EventsAction.class.getName());
79         
80         popupMenu.addMenuListener(new MenuListener JavaDoc() {
81             public void menuSelected(MenuEvent JavaDoc e) {
82                 JMenu menu = (JMenu) e.getSource();
83                 createEventSubmenu(menu);
84             }
85             
86             public void menuDeselected(MenuEvent JavaDoc e) {}
87             
88             public void menuCanceled(MenuEvent JavaDoc 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 JavaDoc bundle = NbBundle.getBundle(EventsAction.class);
111
112         boolean readOnly = metacomp.isReadOnly();
113         Event JavaDoc[] events = readOnly ?
114                          metacomp.getKnownEvents() : metacomp.getAllEvents();
115
116         java.beans.EventSetDescriptor JavaDoc lastEventSetDesc = null;
117         JMenu eventSetMenu = null;
118         boolean eventSetHasHandlers = false;
119
120         for (int i=0; i < events.length; i++) {
121             Event JavaDoc event = events[i];
122             String JavaDoc[] 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"), // NOI18N
130
new Object JavaDoc[] { 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"), // NOI18N
138
new Object JavaDoc[] { event.getName(), handlers[0] }),
139                     event,
140                     handlers[0]);
141             }
142             else {
143                 jmi = new JMenuPlus(MessageFormat.format(
144                     bundle.getString("FMT_CTL_EventMultipleHandlers"), // NOI18N
145
new Object JavaDoc[] { 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"), // NOI18N
151
new Object JavaDoc[] { 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 JavaDoc 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 JavaDoc 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 JavaDoc text = menuItem.getText();
205         for (int i=0; i < n; i++) {
206             String JavaDoc 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 JavaDoc getMenuItemListener() {
216         if (menuItemListener == null)
217             menuItemListener = new EventMenuItemListener();
218         return menuItemListener;
219     }
220
221     // --------
222

223     private static class EventMenuItem extends JMenuItem {
224         private Event JavaDoc event;
225         private String JavaDoc handlerName;
226
227         EventMenuItem(String JavaDoc text, Event JavaDoc event, String JavaDoc handlerName) {
228             super(text);
229             this.event = event;
230             this.handlerName = handlerName;
231         }
232
233         Event JavaDoc getEvent() {
234             return event;
235         }
236
237         String JavaDoc getHandlerName() {
238             return handlerName;
239         }
240     }
241
242     private static class EventMenuItemListener implements ActionListener JavaDoc {
243         public void actionPerformed(ActionEvent JavaDoc evt) {
244             Object JavaDoc source = evt.getSource();
245             if (!(source instanceof EventMenuItem))
246                     return;
247
248             EventMenuItem mi = (EventMenuItem) source;
249             Event JavaDoc event = ((EventMenuItem)source).getEvent();
250             Node.Property prop = event.getComponent()
251                                            .getPropertyByName(event.getId());
252             if (prop != null) {
253                 String JavaDoc handlerName = mi.getHandlerName();
254                 event.getComponent().getFormModel().getFormEvents()
255                     .attachEvent(event, handlerName, null);
256
257                 try { // hack to update the property sheet
258
if (handlerName == null)
259                         handlerName = (String JavaDoc) prop.getValue();
260                     prop.setValue(handlerName);
261                 }
262                 catch (Exception JavaDoc ex) {}
263             }
264         }
265     }
266
267     private ActionListener JavaDoc menuItemListener;
268 }
269
Popular Tags