~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to form/src/org/netbeans/modules/form/actions/EventsAction.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.form.actions;
 
43
 
 
44
import java.awt.event.ActionListener;
 
45
import java.awt.event.ActionEvent;
 
46
import java.util.ResourceBundle;
 
47
import java.text.MessageFormat;
 
48
import javax.swing.*;
 
49
import javax.swing.event.MenuListener;
 
50
import javax.swing.event.MenuEvent;
 
51
 
 
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;
 
57
 
 
58
import org.netbeans.modules.form.*;
 
59
 
 
60
/**
 
61
 * Action class providing popup menu presenter for events of one component.
 
62
 *
 
63
 * @author Tomas Pavek
 
64
 */
 
65
 
 
66
public class EventsAction extends CookieAction {
 
67
 
 
68
    protected int mode() {
 
69
        return MODE_EXACTLY_ONE; // can be invoked on just one node
 
70
    }
 
71
 
 
72
    protected Class[] cookieClasses() {
 
73
        return new Class[] { RADComponentCookie.class };
 
74
    }
 
75
 
 
76
    public String getName() {
 
77
        return NbBundle.getBundle(EventsAction.class).getString("ACT_Events"); // NOI18N
 
78
    }
 
79
 
 
80
    public HelpCtx getHelpCtx() {
 
81
        return HelpCtx.DEFAULT_HELP;
 
82
    }
 
83
 
 
84
    protected void performAction(Node[] activatedNodes) {
 
85
    }
 
86
 
 
87
    @Override
 
88
    public JMenuItem getMenuPresenter() {
 
89
        return getPopupPresenter();
 
90
    }
 
91
 
 
92
    /**
 
93
     * Returns a JMenuItem that presents this action in a Popup Menu.
 
94
     * @return the JMenuItem representation for the action
 
95
     */
 
96
    @Override
 
97
    public JMenuItem getPopupPresenter() {
 
98
        JMenu popupMenu = new JMenuPlus(
 
99
            NbBundle.getBundle(EventsAction.class).getString("ACT_Events")); // NOI18N
 
100
        
 
101
        popupMenu.setEnabled(isEnabled());
 
102
        HelpCtx.setHelpIDString(popupMenu, EventsAction.class.getName());
 
103
        
 
104
        popupMenu.addMenuListener(new MenuListener() {
 
105
            public void menuSelected(MenuEvent e) {
 
106
                JMenu menu = (JMenu) e.getSource();
 
107
                createEventSubmenu(menu);
 
108
            }
 
109
            
 
110
            public void menuDeselected(MenuEvent e) {}
 
111
            
 
112
            public void menuCanceled(MenuEvent e) {}
 
113
        });
 
114
        return popupMenu;
 
115
    }
 
116
 
 
117
    private void createEventSubmenu(JMenu menu) {
 
118
        if (menu.getMenuComponentCount() > 0)
 
119
            menu.removeAll();
 
120
 
 
121
        Node[] nodes = getActivatedNodes();
 
122
        if (nodes.length == 0)
 
123
            return;
 
124
 
 
125
        RADComponentCookie radCookie = nodes[0].getCookie(RADComponentCookie.class);
 
126
        if (radCookie == null)
 
127
            return;
 
128
 
 
129
        RADComponent metacomp = radCookie.getRADComponent();
 
130
        if (metacomp == null)
 
131
            return;
 
132
 
 
133
        ResourceBundle bundle = NbBundle.getBundle(EventsAction.class);
 
134
 
 
135
        boolean readOnly = metacomp.isReadOnly();
 
136
        Event[] events = readOnly ?
 
137
                         metacomp.getKnownEvents() : metacomp.getAllEvents();
 
138
 
 
139
        java.beans.EventSetDescriptor lastEventSetDesc = null;
 
140
        JMenu eventSetMenu = null;
 
141
        boolean eventSetHasHandlers = false;
 
142
 
 
143
        for (int i=0; i < events.length; i++) {
 
144
            Event event = events[i];
 
145
            String[] handlers = event.getEventHandlers();
 
146
            JMenuItem jmi = null;
 
147
 
 
148
            if (handlers.length == 0) {
 
149
                if (!readOnly)
 
150
                    jmi = new EventMenuItem(
 
151
                        MessageFormat.format(
 
152
                            bundle.getString("FMT_CTL_EventNoHandlers"), // NOI18N
 
153
                            new Object[] { event.getName() }),
 
154
                        event,
 
155
                        null);
 
156
            }
 
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] }),
 
162
                    event,
 
163
                    handlers[0]);
 
164
            }
 
165
            else {
 
166
                jmi = new JMenuPlus(MessageFormat.format(
 
167
                    bundle.getString("FMT_CTL_EventMultipleHandlers"), // NOI18N
 
168
                    new Object[] { event.getName() }));
 
169
 
 
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] }),
 
175
                        event,
 
176
                        handlers[j]);
 
177
 
 
178
                    handlerItem.addActionListener(getMenuItemListener());
 
179
 
 
180
                    HelpCtx.setHelpIDString(handlerItem, EventsAction.class.getName());
 
181
                    setBoldFontForMenuText(handlerItem);
 
182
 
 
183
                    ((JMenu)jmi).add(handlerItem);
 
184
                }
 
185
            }
 
186
 
 
187
            if (jmi != null) {
 
188
                if (event.getEventSetDescriptor() != lastEventSetDesc) {
 
189
                    if (eventSetHasHandlers)
 
190
                        setBoldFontForMenuText(eventSetMenu);
 
191
 
 
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();
 
200
                }
 
201
 
 
202
                if (!(jmi instanceof JMenu))
 
203
                    jmi.addActionListener(getMenuItemListener());
 
204
 
 
205
                HelpCtx.setHelpIDString(jmi, EventsAction.class.getName());
 
206
 
 
207
                if (handlers.length > 0 && !readOnly) {
 
208
                    eventSetHasHandlers = true;
 
209
                    setBoldFontForMenuText(jmi);
 
210
                }
 
211
 
 
212
                addSortedMenuItem(eventSetMenu, jmi);
 
213
            }
 
214
        }
 
215
 
 
216
        if (eventSetHasHandlers)
 
217
            setBoldFontForMenuText(eventSetMenu);
 
218
    }
 
219
 
 
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));
 
223
    }
 
224
 
 
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);
 
232
                return;
 
233
            }
 
234
        }
 
235
        menu.add(menuItem);
 
236
    }
 
237
        
 
238
    private ActionListener getMenuItemListener() {
 
239
        if (menuItemListener == null)
 
240
            menuItemListener = new EventMenuItemListener();
 
241
        return menuItemListener;
 
242
    }
 
243
 
 
244
    // --------
 
245
 
 
246
    private static class EventMenuItem extends JMenuItem {
 
247
        private Event event;
 
248
        private String handlerName;
 
249
 
 
250
        EventMenuItem(String text, Event event, String handlerName) {
 
251
            super(text);
 
252
            this.event = event;
 
253
            this.handlerName = handlerName;
 
254
        }
 
255
 
 
256
        Event getEvent() {
 
257
            return event;
 
258
        }
 
259
 
 
260
        String getHandlerName() {
 
261
            return handlerName;
 
262
        }
 
263
    }
 
264
 
 
265
    private static class EventMenuItemListener implements ActionListener {
 
266
        public void actionPerformed(ActionEvent evt) {
 
267
            Object source = evt.getSource();
 
268
            if (!(source instanceof EventMenuItem))
 
269
                    return;
 
270
 
 
271
            EventMenuItem mi = (EventMenuItem) source;
 
272
            Event event = ((EventMenuItem)source).getEvent();
 
273
            Node.Property prop = event.getComponent()
 
274
                                           .getPropertyByName(event.getId());
 
275
            if (prop != null) {
 
276
                String handlerName = mi.getHandlerName();
 
277
                event.getComponent().getFormModel().getFormEvents()
 
278
                    .attachEvent(event, handlerName, null);
 
279
 
 
280
                try { // hack to update the property sheet
 
281
                    if (handlerName == null)
 
282
                        handlerName = (String) prop.getValue();
 
283
                    prop.setValue(handlerName);
 
284
                }
 
285
                catch (Exception ex) {}
 
286
            }
 
287
        }
 
288
    }
 
289
 
 
290
    private ActionListener menuItemListener;
 
291
}