KickJava   Java API By Example, From Geeks To Geeks.

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


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

73 // TODO - remove popup menu action
74
public class PageFlowScene extends GraphPinScene<AbstractNode, NavigationCase, String JavaDoc> {
75     
76     private LayerWidget backgroundLayer = new LayerWidget(this);
77     private LayerWidget mainLayer = new LayerWidget(this);
78     private LayerWidget connectionLayer = new LayerWidget(this);
79     private LayerWidget upperLayer = new LayerWidget(this);
80     
81     private Router router;
82     
83     private WidgetAction moveControlPointAction = ActionFactory.createOrthogonalMoveControlPointAction();
84     // private WidgetAction popupNodeAction = ActionFactory.createPopupMenuAction (new NodePopupMenuProvider(this));
85
private WidgetAction popupGraphAction = ActionFactory.createPopupMenuAction(new PageFlowPopupProvider(this));
86     private WidgetAction moveAction = ActionFactory.createMoveAction();
87     private WidgetAction dragNdropAction = ActionFactory.createAcceptAction(new PageFlowAcceptProvider());
88     private WidgetAction connectAction = ActionFactory.createConnectAction(connectionLayer, new LinkCreateProvider(this));
89     // private WidgetAction deleteAction = new DeleteAction(this);
90
private WidgetAction selectAction = ActionFactory.createSelectAction(new PageFlowSelectProvider());
91     
92     private SceneLayout sceneLayout;
93     private PageFlowView tc;
94     
95     /**
96      * Creates a VMD graph scene.
97      * @param tc or TopComponent/container.
98      */

99     public PageFlowScene(PageFlowView tc) {
100         this.tc = tc;
101         
102         setKeyEventProcessingType(EventProcessingType.FOCUSED_WIDGET_AND_ITS_PARENTS);
103         
104         addChild(backgroundLayer);
105         addChild(mainLayer);
106         addChild(connectionLayer);
107         addChild(upperLayer);
108         
109         router = RouterFactory.createOrthogonalSearchRouter(mainLayer, connectionLayer);
110         
111         Chain actions = getActions();
112         actions.addAction(ActionFactory.createZoomAction());
113         actions.addAction(ActionFactory.createPanAction());
114         actions.addAction(ActionFactory.createRectangularSelectAction(this, backgroundLayer));
115         
116         actions.addAction(popupGraphAction);
117         actions.addAction(dragNdropAction);
118         // getActions ().addAction (deleteAction);
119

120         
121         GridGraphLayout gglayout = new GridGraphLayout<AbstractNode, NavigationCase> ();
122         gglayout.setChecker(true);
123         
124         sceneLayout = LayoutFactory.createSceneGraphLayout(this, gglayout);
125         
126         getActions().addAction(ActionFactory.createEditAction(new EditProvider() {
127             public void edit(Widget widget) {
128                 sceneLayout.invokeLayout();
129             }
130         }));
131     }
132     
133     /**
134      *
135      * @return
136      */

137     public PageFlowView getPageFlowView(){
138         return tc;
139     }
140     
141     // protected PageFlowController getPageFlowController() {
142
// return tc.getPageFlowController();
143
// }
144
//
145
private static final Image POINT_SHAPE_IMAGE = Utilities.loadImage("org/netbeans/modules/visual/resources/vmd-pin.png"); // NOI18N
146

147     private static final int PAGE_WIDGET_INDEX = 0;
148     private static final int DEFAULT_PIN_WIDGET_INDEX = 1;
149     
150     
151     /**
152      * Implements attaching a widget to a node. The widget is VMDNodeWidget and has object-hover, select, popup-menu and move actions.
153      * @param node the node
154      * @return the widget attached to the node, will return null if
155      */

156     protected Widget attachNodeWidget(AbstractNode node) {
157         assert node != null;
158         VMDNodeWidget nodeWidget = new VMDNodeWidget(this);
159         nodeWidget.setNodeName(node.getName());
160         
161         Widget header = nodeWidget.getHeader();
162         ImageWidget imageWidget = new DefaultAnchorWidget(this, POINT_SHAPE_IMAGE);
163         imageWidget.getActions().addAction(connectAction);
164         // imageWidget.getActions().addAction(createSelectAction());
165
imageWidget.getActions().addAction(createWidgetHoverAction());
166         header.addChild(imageWidget);
167         
168         
169         // Widget widget = new Widget(this);
170
// ImageWidget imageWidget = new ImageWidget(this, POINT_SHAPE_IMAGE);
171
// widget.setLayout(new NodePinLayout(0) );
172
//
173
// widget.addChild(nodeWidget);
174
// widget.addChild(imageWidget);
175

176         mainLayer.addChild(nodeWidget);
177         
178         
179         // nodeWidget.getActions().addAction(deleteAction);
180
// nodeWidget.getHeader ().getActions().addAction(createObjectHoverAction());
181
nodeWidget.getActions().addAction(selectAction);
182         // nodeWidget.getActions().addAction(createSelectAction());
183
// nodeWidget.getActions ().addAction (popupGraphAction);
184
nodeWidget.getActions().addAction(moveAction);
185         nodeWidget.setMinimized(true);
186         // imageWidget.getActions().addAction(connectAction);
187

188         return nodeWidget;
189     }
190     
191     
192     private static class DefaultAnchorWidget extends ImageWidget{
193         
194         
195         public DefaultAnchorWidget( PageFlowScene scene, Image image ){
196             super(scene, image);
197         }
198         protected void notifyStateChanged(ObjectState previousState, ObjectState state) {
199             Border JavaDoc BORDER_HOVERED = (Border JavaDoc) javax.swing.BorderFactory.createLineBorder(java.awt.Color.BLACK);
200             Border JavaDoc BORDER = BorderFactory.createEmptyBorder();
201             if (previousState.isHovered() == state.isHovered())
202                 return;
203             if (state.isHovered())
204             setBorder(state.isHovered() ? BORDER_HOVERED : BORDER );
205             System.out.println("Hovering.");
206         }
207         
208         
209     }
210     
211     /**
212      * Implements attaching a widget to a pin. The widget is VMDPinWidget and has object-hover and select action.
213      * The the node id ends with "#default" then the pin is the default pin of a node and therefore it is non-visual.
214      * @param node the node
215      * @param pin the pin
216      * @return the widget attached to the pin, null, if it is a default pin
217      */

218     protected Widget attachPinWidget(AbstractNode node, String JavaDoc pin) {
219         assert node != null;
220         
221         if( node.equals(pin)){
222             return null;
223         }
224         
225         VMDPinWidget widget = new VMDPinWidget(this);
226         // this.addObject(pin, widget);
227
VMDNodeWidget nodeWidget = ((VMDNodeWidget) findWidget(node));
228         nodeWidget.attachPinWidget(widget);
229         
230         // widget.getActions().addAction(deleteAction);
231
widget.getActions().addAction(createObjectHoverAction());
232         widget.getActions().addAction(createSelectAction());
233         // widget.getActions().addAction(connectAction);
234

235         return widget;
236     }
237     
238     /**
239      * Implements attaching a widget to an edge. the widget is ConnectionWidget and has object-hover, select and move-control-point actions.
240      * @param edge
241      * @return the widget attached to the edge
242      */

243     protected Widget attachEdgeWidget(NavigationCase edge) {
244         assert edge != null;
245         
246         VMDConnectionWidget connectionWidget = new VMDConnectionWidget(this, router);
247         connectionLayer.addChild(connectionWidget);
248         
249         // String navCaseName = link.getFromOutcome();
250

251         // connectionWidget.getActions().addAction(deleteAction);
252
connectionWidget.getActions().addAction(createObjectHoverAction());
253         connectionWidget.getActions().addAction(createSelectAction());
254         connectionWidget.getActions().addAction(moveControlPointAction);
255         // connectionWidget.getActions ().addAction (deleteAction);
256

257         return connectionWidget;
258     }
259     
260     /**
261      * Attaches an anchor of a source pin an edge.
262      * The anchor is a ProxyAnchor that switches between the anchor attached to the pin widget directly and
263      * the anchor attached to the pin node widget based on the minimize-state of the node.
264      * @param edge the edge
265      * @param oldSourcePin the old source pin
266      * @param sourcePin the new source pin
267      */

268     protected void attachEdgeSourceAnchor(NavigationCase edge, String JavaDoc oldSourcePin, String JavaDoc sourcePin) {
269         ((ConnectionWidget) findWidget(edge)).setSourceAnchor(getPinAnchor(sourcePin));
270     }
271     
272     /**
273      * Attaches an anchor of a target pin an edge.
274      * The anchor is a ProxyAnchor that switches between the anchor attached to the pin widget directly and
275      * the anchor attached to the pin node widget based on the minimize-state of the node.
276      * @param edge the edge
277      * @param oldTargetPin the old target pin
278      * @param targetPin the new target pin
279      */

280     protected void attachEdgeTargetAnchor(NavigationCase edge, String JavaDoc oldTargetPin, String JavaDoc targetPin) {
281         ((ConnectionWidget) findWidget(edge)).setTargetAnchor(getPinAnchor(targetPin));
282     }
283     
284     /*
285      * Returns the Anchor for a given pin
286      * @param pin The Pin
287      * @return Anchor the anchor location
288      */

289     private Anchor getPinAnchor(String JavaDoc pin) {
290         if( pin == null ) {
291             return null;
292         }
293         VMDNodeWidget nodeWidget = (VMDNodeWidget) findWidget(getPinNode(pin));
294         Widget pinMainWidget = findWidget(pin);
295         Anchor anchor;
296         if (pinMainWidget != null) {
297             anchor = AnchorFactory.createDirectionalAnchor(pinMainWidget, AnchorFactory.DirectionalAnchorKind.HORIZONTAL, 8);
298             anchor = nodeWidget.createAnchorPin(anchor);
299         } else
300             anchor = nodeWidget.getNodeAnchor();
301         return anchor;
302     }
303     
304     /**
305      * Invokes layout of the scene.
306      */

307     public void layoutScene() {
308         sceneLayout.invokeLayout();
309     }
310     
311     private static class MyPopupMenuProvider implements PopupMenuProvider {
312         
313         public JPopupMenu getPopupMenu(Widget widget, Point localLocation) {
314             JPopupMenu popupMenu = new JPopupMenu();
315             popupMenu.add(new JMenuItem("Open " + ((VMDNodeWidget) widget).getNodeName()));
316             return popupMenu;
317         }
318         
319     }
320     
321     // private static class NodePinLayout implements Layout {
322
// int gap = 0;
323
// int displacepin = 0;
324
//
325
// public NodePinLayout(int gap ){
326
// this.gap = gap;
327
// this.displacepin = displacepin;
328
// }
329
//
330
// public void layout(Widget widget) {
331
//
332
// Collection<Widget> children = widget.getChildren();
333
// int pos = 0;
334
// for( Widget child : children ){
335
// Rectangle preferredBounds = child.getPreferredBounds();
336
// int x = preferredBounds.x;
337
// int y = preferredBounds.y;
338
// int width = preferredBounds.width;
339
// int height = preferredBounds.height;
340
// int lx = pos - x;
341
// int ly = - y;
342
// if ( child.isVisible() ) {
343
// if(child instanceof VMDNodeWidget ) {
344
// child.resolveBounds(new Point(lx, ly), new Rectangle(x, y, width, height));
345
// } else {
346
// child.resolveBounds(new Point(lx , ly + 5), new Rectangle(x, y, width, height));
347
// }
348
// pos += width + gap;
349
// } else {
350
// child.resolveBounds(new Point(lx, ly), new Rectangle(x, y, 0, 0));
351
// }
352
// }
353
// }
354
//
355
// public boolean requiresJustification(Widget arg0) {
356
// return false;
357
// }
358
//
359
// public void justify(Widget arg0) {
360
//
361
// }
362
// }
363

364     
365     private final class PageFlowSelectProvider implements SelectProvider {
366         
367         public boolean isAimingAllowed(Widget widget, Point localLocation, boolean invertSelection) {
368             return false;
369         }
370         
371         public boolean isSelectionAllowed(Widget widget, Point localLocation, boolean invertSelection) {
372             Object JavaDoc object = findObject(widget);
373             return object != null && (invertSelection || ! getSelectedObjects().contains(object));
374         }
375         
376         public void select(Widget widget, Point localLocation, boolean invertSelection) {
377             Object JavaDoc object = findObject(widget);
378             
379             setFocusedObject(object);
380             if (object != null) {
381                 if (getSelectedObjects().contains(object))
382                     return;
383                 userSelectionSuggested(Collections.singleton(object), invertSelection);
384                 tc.setActivatedNodes(new Node[]{(Node)object});
385             } else
386                 userSelectionSuggested(Collections.emptySet(), invertSelection);
387         }
388     }
389     
390     
391 }
392
393
394
Popular Tags