001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.visual.action;
042:
043: import org.netbeans.api.visual.widget.Scene;
044: import org.netbeans.api.visual.widget.Widget;
045: import org.netbeans.api.visual.action.WidgetAction;
046: import org.netbeans.api.visual.action.PopupMenuProvider;
047:
048: import javax.swing.*;
049: import java.awt.*;
050: import java.awt.event.KeyEvent;
051: import java.awt.event.InputEvent;
052:
053: /**
054: * @author William Headrick, David Kaspar
055: */
056: public final class PopupMenuAction extends WidgetAction.Adapter {
057:
058: private PopupMenuProvider provider;
059:
060: public PopupMenuAction(PopupMenuProvider provider) {
061: this .provider = provider;
062: }
063:
064: /**
065: * Conditionally display a {@link JPopupMenu} for the given Widget if
066: * the WidgetMouseEvent is a popup trigger. Delegates check code
067: * to {@link #handleMouseEvent(Widget, WidgetMouseEvent)}.
068: * @param widget
069: * @param event
070: * @return {@link State#REJECTED} if no JPopupMenu is displayed,
071: * or {@link State#CONSUMED} if a JPopupMenu is displayed.
072: * @see #handleMouseEvent(Widget, WidgetMouseEvent)
073: */
074: public State mousePressed(Widget widget, WidgetMouseEvent event) {
075: return handleMouseEvent(widget, event);
076: }
077:
078: /**
079: * Conditionally display a {@link JPopupMenu} for the given Widget if
080: * the WidgetMouseEvent is a popup trigger. Delegates check code
081: * to {@link #handleMouseEvent(Widget, WidgetMouseEvent)}.
082: * @param widget
083: * @param event
084: * @return {@link State#REJECTED} if no JPopupMenu is displayed,
085: * or {@link State#CONSUMED} if a JPopupMenu is displayed.
086: * @see #handleMouseEvent(Widget, WidgetMouseEvent)
087: */
088: public State mouseReleased(Widget widget, WidgetMouseEvent event) {
089: return handleMouseEvent(widget, event);
090: }
091:
092: /**
093: * Conditionally display a {@link JPopupMenu} for the given Widget if
094: * the WidgetMouseEvent is a popup trigger. This method is called
095: * by both {@link #mousePressed(Widget, WidgetMouseEvent)} and
096: * {@link #mouseReleased(Widget, WidgetMouseEvent)} methods to handle
097: * displaying a popup menu for the given widget and event. Uses
098: * {@link WidgetMouseEvent#isPopupTrigger() event.isPopupTrigger()} to
099: * determine whether or not a popup menu should be displayed.
100: * @param widget
101: * @param event
102: * @return {@link State#REJECTED} if no JPopupMenu is displayed,
103: * or {@link State#CONSUMED} if a JPopupMenu is displayed.
104: * @see #mousePressed(Widget, WidgetMouseEvent)
105: * @see #mouseReleased(Widget, WidgetMouseEvent)
106: */
107: protected State handleMouseEvent(Widget widget,
108: WidgetMouseEvent event) {
109: // Different OSes use different MouseEvents (Pressed/Released) to
110: // signal that an event is a PopupTrigger. So, the mousePressed(...)
111: // and mouseReleased(...) methods delegate to this method to
112: // handle the MouseEvent.
113: if (event.isPopupTrigger()) {
114: JPopupMenu popupMenu = provider.getPopupMenu(widget, event
115: .getPoint());
116: if (popupMenu != null) {
117: Scene scene = widget.getScene();
118: Point point = scene.convertSceneToView(widget
119: .convertLocalToScene(event.getPoint()));
120: popupMenu.show(scene.getView(), point.x, point.y);
121: }
122: return State.CONSUMED;
123: }
124: return State.REJECTED;
125: }
126:
127: public State keyPressed(Widget widget, WidgetKeyEvent event) {
128: if (event.getKeyCode() == KeyEvent.VK_CONTEXT_MENU
129: || ((event.getModifiers() & InputEvent.SHIFT_MASK) == InputEvent.SHIFT_MASK && event
130: .getKeyCode() == KeyEvent.VK_F10)) {
131: JPopupMenu popupMenu = provider.getPopupMenu(widget, null);
132: if (popupMenu != null) {
133: JComponent view = widget.getScene().getView();
134: if (view != null) {
135: // Rectangle visibleRect = view.getVisibleRect ();
136: // popupMenu.show (view, visibleRect.x + 10, visibleRect.y + 10);
137: Rectangle bounds = widget.getBounds();
138: Point location = new Point(bounds.x + 5,
139: bounds.y + 5);
140: location = widget.convertLocalToScene(location);
141: location = widget.getScene().convertSceneToView(
142: location);
143: popupMenu.show(view, location.x, location.y);
144: }
145: }
146: return State.CONSUMED;
147: }
148: return State.REJECTED;
149: }
150:
151: }
|