1 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 ; 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 64 public class NavigationGraphScene extends GraphPinScene<Page, Link, Pin> { 66 67 public static final String PIN_ID_DEFAULT_SUFFIX = "#default"; 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 popupGraphAction = ActionFactory.createPopupMenuAction(new GraphPopupProvider(this)); 79 private WidgetAction moveAction = ActionFactory.createMoveAction(); 80 private WidgetAction deleteAction = new DeleteAction(this); 82 83 private SceneLayout sceneLayout; 84 85 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 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"); 110 private static final int PAGE_WIDGET_INDEX = 0; 111 private static final int DEFAULT_PIN_WIDGET_INDEX = 1; 112 113 114 119 protected Widget attachNodeWidget(Page node) { 120 assert node != null; 121 VMDNodeWidget nodeWidget = new NavigationNodeWidget(this); 122 123 124 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 141 return nodeWidget; 142 } 143 144 151 protected Widget attachPinWidget(Page node, Pin pin) { 152 assert node != null; 153 154 VMDPinWidget widget = new VMDPinWidget(this); 155 ((VMDNodeWidget) findWidget(node)).attachPinWidget(widget); 157 158 widget.getActions().addAction(deleteAction); 159 widget.getActions().addAction(createObjectHoverAction()); 160 widget.getActions().addAction(createSelectAction()); 161 163 return widget; 164 } 165 166 171 protected Widget attachEdgeWidget(Link link) { 172 assert link != null; 173 174 VMDConnectionWidget connectionWidget = new VMDConnectionWidget(this, router); 175 connectionLayer.addChild(connectionWidget); 176 String 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 185 return connectionWidget; 186 } 187 188 196 protected void attachEdgeSourceAnchor(Link edge, Pin oldSourcePin, Pin sourcePin) { 197 ((ConnectionWidget) findWidget(edge)).setSourceAnchor(getPinAnchor(sourcePin)); 198 } 199 200 208 protected void attachEdgeTargetAnchor(Link edge, Pin oldTargetPin, Pin targetPin) { 209 ((ConnectionWidget) findWidget(edge)).setTargetAnchor(getPinAnchor(targetPin)); 210 } 211 212 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 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 <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 |