BookNBPlatformCookbookCH0703

Contents

NetBeans Platform Cookbook Chapter 07 03

Creating Connections

Connections are widgets showing edges connected nodes. Connection's ends are placed by Anchors and the path controlled by Router. The router resolves locations of control points creating the path.

The Anchor represents an anchor, end point of the connection. It is joined with widget and listens to its changes using Widget.Dependency interface. To one Anchor can be attached more connection entries that are represented by Anchor.Entry instances. The Entry is binding component between widget and connections.

You can set an AnchorShape to anchors and a PointShape to control points. There are prepared constants and factories for shapes. There are listed to not dip into the documentation:

AnchorShape constants NONE, TRIANGLE_FILLED, TRIANGLE_HOLLOW and TRIANGLE_OUT.

AnchorSpaheFactory methods: createImageAnchorShape(), createTriangleAnchorShape(), createArrowAnchorShape(), createAdjustableAnchorShape() with different parameters.

Warning: The AnchorShape has an attribute isLineOriented to rotate shape according to line direction. It is necessary for arrows and not needed for some images or circles.

PointShape constants NONE, SQUARE_FILLED_BIG and SQUARE_FILLED_SMALL.

PointShapeFactory methods createPointShape() and createImagePointShape().

AnchorFactory

  • createCenterAnchor() locates the anchor into center of the widget.
  • createCircularAnchor() locates the anchor to the point on circle around the widget nearest to the second anchor.
  • createDIrectionalAnchor() locates the anchor to the centre of nearest side of widget's bound rectangle.
  • createFixedAnchor() locates the anchor to the given Point.
  • createRectangularAnchor() and createFreeRectangularAnchor() locates the anchor to nearest point on widget's bounds.
  • createProxyAnchor() delegates location to one of several anchors according to state of given StateModel instance. The StateModel can have true/false or set of integer values states. You can switch the connection to outer/inner widget or to several widgets as a rail switch or electric commutator.

RouterFactory

  • createDirectRouter() - the router creates stright line between anchors. It is the default router.
  • createFreeRouter() - path is controlled by controll points created by user.
  • createOrthogonalSearchRouter() - computes the path by regions occupied by other widgets. You pass layers or collision collector as parameter.

If you want create loop or parallel connection widget (loop -target widget is the same as source, parallel - connection is created by both directions) use any orthogonal router and VMDNodeAnchor that computes different anchor location for source and target.

image:Nbpcook_07_04_connections.png

Figure 7.4 Connections


How to

When you are creating a connection widget find widgets associated with related objects. Then create anchors using AnchorFactory and pass the source or target widget as a parameter.

Then set anchor shape. It will be arrow or triangle arrow. Then set router. If you want distinguish kind of visualized relation set stroke or line color.

   private void createConnection(Relation relation, ObjectScene scene
            , LayerWidget mainLayer, LayerWidget connectionLayer) {

        Thing sourceTh = relation.getSource();
        Thing targetTh = relation.getTarget();

        // find widgets to domain objects
        Widget sourceWi = scene.findWidget(sourceTh);
        Widget targetWi = scene.findWidget(targetTh);

        ConnectionWidget conw = new ConnectionWidget(scene);
        conw.setSourceAnchor(
                          AnchorFactory.createRectangularAnchor(sourceWi));
        conw.setTargetAnchor(
                          AnchorFactory.createRectangularAnchor(targetWi));
        conw.setSourceAnchorShape(AnchorShape.NONE);
        conw.setTargetAnchorShape(AnchorShape.TRIANGLE_FILLED);

        if ("mine".equals(relation.getType().getName())) {
            conw.setStroke(dashed);
        }
        else if (sourceTh.getType().getName().equals("PC")) {
            conw.setRouter(
                   RouterFactory.createOrthogonalSearchRouter(
                                              mainLayer,  connectionLayer) );
        }

        connectionLayer.addChild(conw);
    }

To add labels to the edge do following:

   LabelWidget label = new LabelWidget (scene, "from");
   label.setOpaque (true);
   edge.addChild (label);
   edge.setConstraint( label
          , LayoutFactory.ConnectionWidgetLayoutAlignment.TOP_SOURCE
          , 10 );      // distance from anchor
      
   label = new LabelWidget (scene, "to");
   label.setOpaque (true);
   edge.addChild (label);
   edge.setConstraint( label
          , LayoutFactory.ConnectionWidgetLayoutAlignment.BOTTOM_LEFT
          , -10);      // distance from anchor
 
   label = new LabelWidget (scene, "relation name");
   label.setOpaque (true);
   edge.addChild (label);
   edge.setConstraint( label
          , LayoutFactory.ConnectionWidgetLayoutAlignment.CENTER_RIGHT
          , 0.7f );      // distance as percentage of path length

You can create other arrow as anchor shape. You can set if it can rotate according to line direction for image anchor shape.

   widget.setTargetAnchorShape(              
         AnchorShapeFactory.createImageAnchorShape(
                     imageArrow       // image
                   , true)                    // line oriented
                  );
    widget.setTargetAnchorShape(  
         AnchorShapeFactory.createTriangleAnchorShape(
                     18      // size
                   , true    // filled
                   , false)  // output
                  );
Examples are in the 07_Visual_Library / vl03_create_connections.
Visual Library 2.0 - Examples are stored on this page
http://platform.netbeans.org/graph/examples.html

Notes/Tips

Examples are in the 07_Visual_Library / vl03_create_connections. Visual Library 2.0 - Examples are stored on this page http://platform.netbeans.org/graph/examples.html


Text and sources were created under NB 6.8, 6.9, 7.0, 7.1.

Navigation

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo