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-2006 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: * NodePopupMenuProvider.java
042: *
043: * Created on February 2, 2007, 6:46 PM
044: *
045: * To change this template, choose Tools | Template Manager
046: * and open the template in the editor.
047: */
048:
049: package org.netbeans.modules.web.jsf.navigation.graph.actions;
050:
051: import java.awt.Point;
052: import java.lang.ref.WeakReference;
053: import java.util.HashSet;
054: import java.util.Set;
055: import javax.swing.JPopupMenu;
056: import org.netbeans.api.visual.action.PopupMenuProvider;
057: import org.netbeans.api.visual.widget.Widget;
058: import org.netbeans.modules.web.jsf.navigation.Page;
059: import org.netbeans.modules.web.jsf.navigation.graph.PageFlowScene;
060: import org.openide.util.Lookup;
061: import org.openide.util.Utilities;
062: import org.openide.util.lookup.AbstractLookup;
063: import org.openide.util.lookup.InstanceContent;
064: import org.openide.windows.TopComponent;
065: import javax.swing.Action;
066: import org.netbeans.modules.web.jsf.navigation.PageFlowView;
067: import org.netbeans.modules.web.jsf.navigation.Pin;
068:
069: /**
070: *
071: * @author joelle
072: */
073: public class PageFlowPopupProvider implements PopupMenuProvider {
074:
075: /**
076: * Creates a Popup for any right click on Page Flow Editor
077: * @param graphScene The related PageFlow Scene.
078: * @param tc
079: */
080: // public PageFlowPopupProvider(PageFlowScene scene) {
081: // setGraphScene( scene);
082: // initialize();
083: // }
084: // <actions from layers>
085: private static final String PATH_PAGEFLOW_NODE_ACTIONS = "PageFlowEditor/PopupActions/PageFlowSceneElement"; // NOI18N
086: private static final String PATH_PAGEFLOW_SCENE_ACTIONS = "PageFlowEditor/PopupActions/Scene"; // NOI18N
087:
088: // private void initialize() {
089: // InstanceContent ic = new InstanceContent();
090: // ic.add(getGraphScene());
091: // }
092:
093: /* Point and widget are actually not needed. */
094: public JPopupMenu getPopupMenu(Widget widget, Point point) {
095:
096: PageFlowScene scene = (PageFlowScene) widget.getScene();
097: setGraphScene(scene);
098: Object obj = scene.getHoveredObject();
099: PageFlowView view = scene.getPageFlowView();
100:
101: if (obj != null) {
102:
103: Set elements = scene.getSelectedObjects();
104: if (!elements.contains(obj)) {
105: Set<Object> set = new HashSet<Object>();
106: set.add(obj);
107: scene.setSelectedObjects(set);
108: }
109:
110: // Node nodes[] = tc.getActivatedNodes();
111: if (obj instanceof Page) {
112: Page pageNode = (Page) obj;
113: Action[] actions;
114: Action[] pageNodeActions = pageNode.getActions(true);
115: Action[] fileSystemActions = SystemFileSystemSupport
116: .getActions(PATH_PAGEFLOW_NODE_ACTIONS);
117: if (pageNodeActions == null
118: || pageNodeActions.length == 0) {
119: actions = fileSystemActions;
120: } else if (fileSystemActions == null
121: || fileSystemActions.length == 0) {
122: actions = pageNodeActions;
123: } else {
124: actions = new Action[pageNodeActions.length
125: + fileSystemActions.length];
126: System.arraycopy(fileSystemActions, 0, actions, 0,
127: fileSystemActions.length);
128: System.arraycopy(pageNodeActions, 0, actions,
129: fileSystemActions.length,
130: pageNodeActions.length);
131: }
132: return Utilities.actionsToPopup(actions, view
133: .getLookup());
134: } else if (obj instanceof Pin) {
135: Pin pinNode = (Pin) obj;
136: Action[] actions = pinNode.getActions();
137: return Utilities.actionsToPopup(actions, view
138: .getLookup());
139: }
140: return Utilities.actionsToPopup(SystemFileSystemSupport
141: .getActions(PATH_PAGEFLOW_NODE_ACTIONS), view
142: .getLookup());
143: }
144: return Utilities.actionsToPopup(SystemFileSystemSupport
145: .getActions(PATH_PAGEFLOW_SCENE_ACTIONS), view
146: .getLookup());
147: }
148:
149: /** Weak reference to the lookup. */
150: private WeakReference<Lookup> lookupWRef = new WeakReference<Lookup>(
151: null);
152:
153: /** Adds <code>NavigatorLookupHint</code> into the original lookup,
154: * for the navigator. */
155: private Lookup getLookup() {
156: Lookup lookup = lookupWRef.get();
157:
158: if (lookup == null) {
159: InstanceContent ic = new InstanceContent();
160: // ic.add(firstObject);
161: ic.add(getGraphScene());
162: lookup = new AbstractLookup(ic);
163: lookupWRef = new WeakReference<Lookup>(lookup);
164: }
165:
166: return lookup;
167: }
168:
169: private WeakReference<PageFlowScene> refPageFlowScene;
170:
171: public PageFlowScene getGraphScene() {
172: PageFlowScene scene = null;
173: if (refPageFlowScene != null) {
174: scene = refPageFlowScene.get();
175: }
176: return scene;
177:
178: }
179:
180: public void setGraphScene(PageFlowScene graphScene) {
181: refPageFlowScene = new WeakReference<PageFlowScene>(graphScene);
182: }
183:
184: }
|