KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > jsf > navigation > graph > NavigationGraphScene


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 package org.netbeans.modules.web.jsf.navigation.graph;
20
21 import org.netbeans.modules.web.jsf.navigation.vwmodel.Page;
22 import org.netbeans.modules.web.jsf.navigation.vwmodel.Link;
23 import org.netbeans.modules.web.jsf.navigation.vwmodel.Pin;
24 import org.netbeans.modules.web.jsf.navigation.graph.actions.DeleteAction;
25 import org.netbeans.modules.web.jsf.navigation.graph.actions.GraphPopupProvider;
26 import org.netbeans.modules.web.jsf.navigation.graph.actions.LinkCreateProvider;
27 import org.netbeans.api.visual.action.ActionFactory;
28 import org.netbeans.api.visual.action.PopupMenuProvider;
29 import org.netbeans.api.visual.action.WidgetAction;
30 import org.netbeans.api.visual.anchor.Anchor;
31 import org.netbeans.api.visual.anchor.AnchorFactory;
32 import org.netbeans.api.visual.graph.GraphPinScene;
33 import org.netbeans.api.visual.graph.layout.GridGraphLayout;
34 import org.netbeans.api.visual.layout.LayoutFactory;
35 import org.netbeans.api.visual.layout.SceneLayout;
36 import org.netbeans.api.visual.router.Router;
37 import org.netbeans.api.visual.router.RouterFactory;
38 import org.netbeans.api.visual.widget.ConnectionWidget;
39 import org.netbeans.api.visual.widget.LayerWidget;
40 import org.netbeans.api.visual.widget.Widget;
41 import org.netbeans.api.visual.widget.EventProcessingType;
42 import javax.swing.*;
43 import java.awt.*;
44 import java.util.Collection JavaDoc;
45 import org.netbeans.api.visual.action.WidgetAction.Chain;
46 import org.netbeans.api.visual.layout.Layout;
47 import org.netbeans.api.visual.vmd.VMDNodeWidget;
48 import org.netbeans.api.visual.vmd.VMDConnectionWidget;
49 import org.netbeans.api.visual.vmd.VMDPinWidget;
50 import org.openide.util.Utilities;
51
52 /**
53  * This class represents a GraphPinScene for the Navigation Editor which is soon to be the Page Flow Editor.
54  * Nodes are represented by a Page, Edges by a Link, and components by a Pin.
55  * Graphics were taken from the VMDGraphScene designed by David Kaspar for mobility pack.
56  * The visualization is done by: VMDNodeWidget for nodes, VMDPinWidget for pins, ConnectionWidget fro edges.
57  * <p>
58  * The scene has 4 layers: background, main, connection, upper.
59  * <p>
60  * The scene has following actions: zoom, panning, rectangular selection.
61  *
62  * @author Joelle Lam joelle.lam@sun.com
63  */

64 // TODO - remove popup menu action
65
public class NavigationGraphScene extends GraphPinScene<Page, Link, Pin> {
66     
67     public static final String JavaDoc PIN_ID_DEFAULT_SUFFIX = "#default"; // NOI18N
68

69     private LayerWidget backgroundLayer = new LayerWidget(this);
70     private LayerWidget mainLayer = new LayerWidget(this);
71     private LayerWidget connectionLayer = new LayerWidget(this);
72     private LayerWidget upperLayer = new LayerWidget(this);
73     
74     private Router router;
75     
76     private WidgetAction moveControlPointAction = ActionFactory.createOrthogonalMoveControlPointAction();
77     // private WidgetAction popupNodeAction = ActionFactory.createPopupMenuAction (new NodePopupMenuProvider(this));
78
private WidgetAction popupGraphAction = ActionFactory.createPopupMenuAction(new GraphPopupProvider(this));
79     private WidgetAction moveAction = ActionFactory.createMoveAction();
80 // private WidgetAction connectAction = ActionFactory.createConnectAction(connectionLayer, new LinkCreateProvider(this));
81
private WidgetAction deleteAction = new DeleteAction(this);
82     
83     private SceneLayout sceneLayout;
84     
85     /**
86      * Creates a VMD graph scene.
87      */

88     public NavigationGraphScene() {
89         setKeyEventProcessingType(EventProcessingType.FOCUSED_WIDGET_AND_ITS_PARENTS);
90         
91         addChild(backgroundLayer);
92         addChild(mainLayer);
93         addChild(connectionLayer);
94         addChild(upperLayer);
95         
96         router = RouterFactory.createOrthogonalSearchRouter(mainLayer, connectionLayer);
97         
98         Chain actions = getActions();
99         actions.addAction(ActionFactory.createZoomAction());
100         actions.addAction(ActionFactory.createPanAction());
101         actions.addAction(ActionFactory.createRectangularSelectAction(this, backgroundLayer));
102         actions.addAction(popupGraphAction);
103         // getActions ().addAction (deleteAction);
104

105         sceneLayout = LayoutFactory.createSceneGraphLayout(this, new GridGraphLayout<Page, Link> ().setChecker(true));
106     }
107     
108     private static final Image POINT_SHAPE_IMAGE = Utilities.loadImage("org/netbeans/modules/visual/resources/vmd-pin.png"); // NOI18N
109

110     private static final int PAGE_WIDGET_INDEX = 0;
111     private static final int DEFAULT_PIN_WIDGET_INDEX = 1;
112     
113     
114     /**
115      * Implements attaching a widget to a node. The widget is VMDNodeWidget and has object-hover, select, popup-menu and move actions.
116      * @param node the node
117      * @return the widget attached to the node, will return null if
118      */

119     protected Widget attachNodeWidget(Page node) {
120         assert node != null;
121         VMDNodeWidget nodeWidget = new NavigationNodeWidget(this);
122         
123         
124 // Widget widget = new Widget(this);
125
// ImageWidget imageWidget = new ImageWidget(this, POINT_SHAPE_IMAGE);
126
// widget.setLayout(new NodePinLayout(0) );
127
//
128
// widget.addChild(nodeWidget);
129
// widget.addChild(imageWidget);
130

131         mainLayer.addChild(nodeWidget);
132         
133         
134         nodeWidget.getActions().addAction(deleteAction);
135         nodeWidget.getHeader ().getActions().addAction(createObjectHoverAction());
136         nodeWidget.getActions().addAction(createSelectAction());
137         nodeWidget.getActions ().addAction (popupGraphAction);
138         nodeWidget.getActions().addAction(moveAction);
139 // imageWidget.getActions().addAction(connectAction);
140

141         return nodeWidget;
142     }
143     
144     /**
145      * Implements attaching a widget to a pin. The widget is VMDPinWidget and has object-hover and select action.
146      * The the node id ends with "#default" then the pin is the default pin of a node and therefore it is non-visual.
147      * @param node the node
148      * @param pin the pin
149      * @return the widget attached to the pin, null, if it is a default pin
150      */

151     protected Widget attachPinWidget(Page node, Pin pin) {
152         assert node != null;
153         
154         VMDPinWidget widget = new VMDPinWidget(this);
155         // this.addObject(pin, widget);
156
((VMDNodeWidget) findWidget(node)).attachPinWidget(widget);
157         
158         widget.getActions().addAction(deleteAction);
159         widget.getActions().addAction(createObjectHoverAction());
160         widget.getActions().addAction(createSelectAction());
161 // widget.getActions().addAction(connectAction);
162

163         return widget;
164     }
165     
166     /**
167      * Implements attaching a widget to an edge. the widget is ConnectionWidget and has object-hover, select and move-control-point actions.
168      * @param edge the edge
169      * @return the widget attached to the edge
170      */

171     protected Widget attachEdgeWidget(Link link) {
172         assert link != null;
173         
174         VMDConnectionWidget connectionWidget = new VMDConnectionWidget(this, router);
175         connectionLayer.addChild(connectionWidget);
176         // this.addObject(link);
177
String JavaDoc navCase = link.getFromOutcome();
178         
179         connectionWidget.getActions().addAction(deleteAction);
180         connectionWidget.getActions().addAction(createObjectHoverAction());
181         connectionWidget.getActions().addAction(createSelectAction());
182         connectionWidget.getActions().addAction(moveControlPointAction);
183         // connectionWidget.getActions ().addAction (deleteAction);
184

185         return connectionWidget;
186     }
187     
188     /**
189      * Attaches an anchor of a source pin an edge.
190      * The anchor is a ProxyAnchor that switches between the anchor attached to the pin widget directly and
191      * the anchor attached to the pin node widget based on the minimize-state of the node.
192      * @param edge the edge
193      * @param oldSourcePin the old source pin
194      * @param sourcePin the new source pin
195      */

196     protected void attachEdgeSourceAnchor(Link edge, Pin oldSourcePin, Pin sourcePin) {
197         ((ConnectionWidget) findWidget(edge)).setSourceAnchor(getPinAnchor(sourcePin));
198     }
199     
200     /**
201      * Attaches an anchor of a target pin an edge.
202      * The anchor is a ProxyAnchor that switches between the anchor attached to the pin widget directly and
203      * the anchor attached to the pin node widget based on the minimize-state of the node.
204      * @param edge the edge
205      * @param oldTargetPin the old target pin
206      * @param targetPin the new target pin
207      */

208     protected void attachEdgeTargetAnchor(Link edge, Pin oldTargetPin, Pin targetPin) {
209         ((ConnectionWidget) findWidget(edge)).setTargetAnchor(getPinAnchor(targetPin));
210     }
211     
212     /*
213      * Returns the Anchor for a given pin
214      * @param pin The Pin
215      * @return Anchor the anchor location
216      */

217     private Anchor getPinAnchor(Pin pin) {
218         if( pin == null ) {
219             return null;
220         }
221         VMDNodeWidget nodeWidget = (VMDNodeWidget) findWidget(getPinNode(pin));
222         Widget pinMainWidget = findWidget(pin);
223         Anchor anchor;
224         if (pinMainWidget != null) {
225             anchor = AnchorFactory.createDirectionalAnchor(pinMainWidget, AnchorFactory.DirectionalAnchorKind.HORIZONTAL, 8);
226             anchor = nodeWidget.createAnchorPin(anchor);
227         } else
228             anchor = nodeWidget.getNodeAnchor();
229         return anchor;
230     }
231     
232     /**
233      * Invokes layout of the scene.
234      */

235     public void layoutScene() {
236         sceneLayout.invokeLayout();
237     }
238     
239     private static class MyPopupMenuProvider implements PopupMenuProvider {
240         
241         public JPopupMenu getPopupMenu(Widget widget, Point localLocation) {
242             JPopupMenu popupMenu = new JPopupMenu();
243             popupMenu.add(new JMenuItem("Open " + ((VMDNodeWidget) widget).getNodeName()));
244             return popupMenu;
245         }
246         
247     }
248     
249     private static class NodePinLayout implements Layout {
250         int gap = 0;
251         int displacepin = 0;
252         
253         public NodePinLayout(int gap ){
254             this.gap = gap;
255             this.displacepin = displacepin;
256         }
257         
258         public void layout(Widget widget) {
259             
260             Collection JavaDoc<Widget> children = widget.getChildren();
261             int pos = 0;
262             for( Widget child : children ){
263                 Rectangle preferredBounds = child.getPreferredBounds();
264                 int x = preferredBounds.x;
265                 int y = preferredBounds.y;
266                 int width = preferredBounds.width;
267                 int height = preferredBounds.height;
268                 int lx = pos - x;
269                 int ly = - y;
270                 if ( child.isVisible() ) {
271                     if(child instanceof VMDNodeWidget ) {
272                         child.resolveBounds(new Point(lx, ly), new Rectangle(x, y, width, height));
273                     } else {
274                         child.resolveBounds(new Point(lx , ly + 5), new Rectangle(x, y, width, height));
275                     }
276                     pos += width + gap;
277                 } else {
278                     child.resolveBounds(new Point(lx, ly), new Rectangle(x, y, 0, 0));
279                 }
280             }
281         }
282         
283         public boolean requiresJustification(Widget arg0) {
284             return false;
285         }
286         
287         public void justify(Widget arg0) {
288             
289         }
290     }
291 }
292
293
294
Popular Tags