2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.form.actions;
44
import java.awt.event.ActionListener;
45
import java.awt.event.ActionEvent;
46
import java.util.ResourceBundle;
47
import java.text.MessageFormat;
49
import javax.swing.event.MenuListener;
50
import javax.swing.event.MenuEvent;
52
import org.openide.util.HelpCtx;
53
import org.openide.util.actions.*;
54
import org.openide.nodes.Node;
55
import org.openide.awt.JMenuPlus;
56
import org.openide.util.NbBundle;
58
import org.netbeans.modules.form.*;
61
* Action class providing popup menu presenter for events of one component.
66
public class EventsAction extends CookieAction {
68
protected int mode() {
69
return MODE_EXACTLY_ONE; // can be invoked on just one node
72
protected Class[] cookieClasses() {
73
return new Class[] { RADComponentCookie.class };
76
public String getName() {
77
return NbBundle.getBundle(EventsAction.class).getString("ACT_Events"); // NOI18N
80
public HelpCtx getHelpCtx() {
81
return HelpCtx.DEFAULT_HELP;
84
protected void performAction(Node[] activatedNodes) {
88
public JMenuItem getMenuPresenter() {
89
return getPopupPresenter();
93
* Returns a JMenuItem that presents this action in a Popup Menu.
94
* @return the JMenuItem representation for the action
97
public JMenuItem getPopupPresenter() {
98
JMenu popupMenu = new JMenuPlus(
99
NbBundle.getBundle(EventsAction.class).getString("ACT_Events")); // NOI18N
101
popupMenu.setEnabled(isEnabled());
102
HelpCtx.setHelpIDString(popupMenu, EventsAction.class.getName());
104
popupMenu.addMenuListener(new MenuListener() {
105
public void menuSelected(MenuEvent e) {
106
JMenu menu = (JMenu) e.getSource();
107
createEventSubmenu(menu);
110
public void menuDeselected(MenuEvent e) {}
112
public void menuCanceled(MenuEvent e) {}
117
private void createEventSubmenu(JMenu menu) {
118
if (menu.getMenuComponentCount() > 0)
121
Node[] nodes = getActivatedNodes();
122
if (nodes.length == 0)
125
RADComponentCookie radCookie = nodes[0].getCookie(RADComponentCookie.class);
126
if (radCookie == null)
129
RADComponent metacomp = radCookie.getRADComponent();
130
if (metacomp == null)
133
ResourceBundle bundle = NbBundle.getBundle(EventsAction.class);
135
boolean readOnly = metacomp.isReadOnly();
136
Event[] events = readOnly ?
137
metacomp.getKnownEvents() : metacomp.getAllEvents();
139
java.beans.EventSetDescriptor lastEventSetDesc = null;
140
JMenu eventSetMenu = null;
141
boolean eventSetHasHandlers = false;
143
for (int i=0; i < events.length; i++) {
144
Event event = events[i];
145
String[] handlers = event.getEventHandlers();
146
JMenuItem jmi = null;
148
if (handlers.length == 0) {
150
jmi = new EventMenuItem(
151
MessageFormat.format(
152
bundle.getString("FMT_CTL_EventNoHandlers"), // NOI18N
153
new Object[] { event.getName() }),
157
else if (handlers.length == 1) {
158
jmi = new EventMenuItem(
159
MessageFormat.format(
160
bundle.getString("FMT_CTL_EventOneHandler"), // NOI18N
161
new Object[] { event.getName(), handlers[0] }),
166
jmi = new JMenuPlus(MessageFormat.format(
167
bundle.getString("FMT_CTL_EventMultipleHandlers"), // NOI18N
168
new Object[] { event.getName() }));
170
for (int j=0; j < handlers.length; j++) {
171
JMenuItem handlerItem = new EventMenuItem(
172
MessageFormat.format(
173
bundle.getString("FMT_CTL_HandlerFromMultiple"), // NOI18N
174
new Object[] { handlers[j] }),
178
handlerItem.addActionListener(getMenuItemListener());
180
HelpCtx.setHelpIDString(handlerItem, EventsAction.class.getName());
181
setBoldFontForMenuText(handlerItem);
183
((JMenu)jmi).add(handlerItem);
188
if (event.getEventSetDescriptor() != lastEventSetDesc) {
189
if (eventSetHasHandlers)
190
setBoldFontForMenuText(eventSetMenu);
192
String name = event.getEventSetDescriptor().getName();
193
eventSetMenu = new JMenuPlus(name.substring(0,1).toUpperCase()
194
+ name.substring(1));
195
HelpCtx.setHelpIDString(eventSetMenu,
196
EventsAction.class.getName());
197
addSortedMenuItem(menu, eventSetMenu);
198
eventSetHasHandlers = false;
199
lastEventSetDesc = event.getEventSetDescriptor();
202
if (!(jmi instanceof JMenu))
203
jmi.addActionListener(getMenuItemListener());
205
HelpCtx.setHelpIDString(jmi, EventsAction.class.getName());
207
if (handlers.length > 0 && !readOnly) {
208
eventSetHasHandlers = true;
209
setBoldFontForMenuText(jmi);
212
addSortedMenuItem(eventSetMenu, jmi);
216
if (eventSetHasHandlers)
217
setBoldFontForMenuText(eventSetMenu);
220
private static void setBoldFontForMenuText(JMenuItem mi) {
221
java.awt.Font font = mi.getFont();
222
mi.setFont(font.deriveFont(font.getStyle() | java.awt.Font.BOLD));
225
private static void addSortedMenuItem(JMenu menu, JMenuItem menuItem) {
226
int n = menu.getMenuComponentCount();
227
String text = menuItem.getText();
228
for (int i=0; i < n; i++) {
229
String tx = ((JMenuItem)menu.getMenuComponent(i)).getText();
230
if (text.compareTo(tx) < 0) {
231
menu.add(menuItem, i);
238
private ActionListener getMenuItemListener() {
239
if (menuItemListener == null)
240
menuItemListener = new EventMenuItemListener();
241
return menuItemListener;
246
private static class EventMenuItem extends JMenuItem {
248
private String handlerName;
250
EventMenuItem(String text, Event event, String handlerName) {
253
this.handlerName = handlerName;
260
String getHandlerName() {
265
private static class EventMenuItemListener implements ActionListener {
266
public void actionPerformed(ActionEvent evt) {
267
Object source = evt.getSource();
268
if (!(source instanceof EventMenuItem))
271
EventMenuItem mi = (EventMenuItem) source;
272
Event event = ((EventMenuItem)source).getEvent();
273
Node.Property prop = event.getComponent()
274
.getPropertyByName(event.getId());
276
String handlerName = mi.getHandlerName();
277
event.getComponent().getFormModel().getFormEvents()
278
.attachEvent(event, handlerName, null);
280
try { // hack to update the property sheet
281
if (handlerName == null)
282
handlerName = (String) prop.getValue();
283
prop.setValue(handlerName);
285
catch (Exception ex) {}
290
private ActionListener menuItemListener;