1 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 ; 42 import javax.swing.border.Border ; 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 73 public class PageFlowScene extends GraphPinScene<AbstractNode, NavigationCase, String > { 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 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 selectAction = ActionFactory.createSelectAction(new PageFlowSelectProvider()); 91 92 private SceneLayout sceneLayout; 93 private PageFlowView tc; 94 95 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 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 137 public PageFlowView getPageFlowView(){ 138 return tc; 139 } 140 141 private static final Image POINT_SHAPE_IMAGE = Utilities.loadImage("org/netbeans/modules/visual/resources/vmd-pin.png"); 147 private static final int PAGE_WIDGET_INDEX = 0; 148 private static final int DEFAULT_PIN_WIDGET_INDEX = 1; 149 150 151 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(createWidgetHoverAction()); 166 header.addChild(imageWidget); 167 168 169 176 mainLayer.addChild(nodeWidget); 177 178 179 nodeWidget.getActions().addAction(selectAction); 182 nodeWidget.getActions().addAction(moveAction); 185 nodeWidget.setMinimized(true); 186 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 BORDER_HOVERED = (Border ) javax.swing.BorderFactory.createLineBorder(java.awt.Color.BLACK); 200 Border 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 218 protected Widget attachPinWidget(AbstractNode node, String pin) { 219 assert node != null; 220 221 if( node.equals(pin)){ 222 return null; 223 } 224 225 VMDPinWidget widget = new VMDPinWidget(this); 226 VMDNodeWidget nodeWidget = ((VMDNodeWidget) findWidget(node)); 228 nodeWidget.attachPinWidget(widget); 229 230 widget.getActions().addAction(createObjectHoverAction()); 232 widget.getActions().addAction(createSelectAction()); 233 235 return widget; 236 } 237 238 243 protected Widget attachEdgeWidget(NavigationCase edge) { 244 assert edge != null; 245 246 VMDConnectionWidget connectionWidget = new VMDConnectionWidget(this, router); 247 connectionLayer.addChild(connectionWidget); 248 249 251 connectionWidget.getActions().addAction(createObjectHoverAction()); 253 connectionWidget.getActions().addAction(createSelectAction()); 254 connectionWidget.getActions().addAction(moveControlPointAction); 255 257 return connectionWidget; 258 } 259 260 268 protected void attachEdgeSourceAnchor(NavigationCase edge, String oldSourcePin, String sourcePin) { 269 ((ConnectionWidget) findWidget(edge)).setSourceAnchor(getPinAnchor(sourcePin)); 270 } 271 272 280 protected void attachEdgeTargetAnchor(NavigationCase edge, String oldTargetPin, String targetPin) { 281 ((ConnectionWidget) findWidget(edge)).setTargetAnchor(getPinAnchor(targetPin)); 282 } 283 284 289 private Anchor getPinAnchor(String 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 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 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 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 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 |