X

Geertjan's Blog

  • January 21, 2011

AcceptProvider & ConnectProvider

Geertjan Wielenga
Product Manager
Drop a Node from an explorer view into a Scene and then let the user connect the dropped widgets together:
public class StudentScene extends Scene {
private LayerWidget mainLayer;
private LayerWidget connectionLayer;
private LayerWidget interactionLayer;
public StudentScene() {
mainLayer = new LayerWidget(this);
connectionLayer = new LayerWidget(this);
interactionLayer = new LayerWidget(this);
addChild(mainLayer);
addChild(connectionLayer);
addChild(interactionLayer);
getActions().addAction(ActionFactory.createAcceptAction(new SceneAcceptProvider(this)));
}
private class SceneAcceptProvider implements AcceptProvider {
private final StudentScene scene;
private SceneAcceptProvider(StudentScene scene) {
this.scene = scene;
}
@Override
public ConnectorState isAcceptable(Widget widget, Point point, Transferable t) {
//Do some checking here before accepting.
return ConnectorState.ACCEPT;
}
@Override
public void accept(Widget widget, Point point, Transferable t) {
Node[] nodes = NodeTransfer.nodes(t, NodeTransfer.DND_COPY_OR_MOVE);
for (int i = 0; i < nodes.length; i++) {
Node node = nodes[i];
Student student = node.getLookup().lookup(Student.class);
LabelWidget labelWidget = new LabelWidget(scene, student.getName());
labelWidget.setPreferredLocation(point);
labelWidget.getActions().addAction(
ActionFactory.createExtendedConnectAction(
connectionLayer, new LabelWidgetConnectProvider(scene)));
labelWidget.getActions().addAction(
ActionFactory.createAlignWithMoveAction(
mainLayer, interactionLayer,
ActionFactory.createDefaultAlignWithMoveDecorator()));
mainLayer.addChild(labelWidget);
}
}
}
private class LabelWidgetConnectProvider implements ConnectProvider {
private final StudentScene scene;
private LabelWidgetConnectProvider(StudentScene scene) {
this.scene = scene;
}
@Override
public boolean isSourceWidget(Widget source) {
return source instanceof LabelWidget && source != null ? true : false;
}
@Override
public ConnectorState isTargetWidget(Widget src, Widget trg) {
return src != trg && trg
instanceof LabelWidget ? ConnectorState.ACCEPT : ConnectorState.REJECT;
}
@Override
public boolean hasCustomTargetWidgetResolver(Scene arg0) {
return false;
}
@Override
public Widget resolveTargetWidget(Scene arg0, Point arg1) {
return null;
}
@Override
public void createConnection(Widget source, Widget target) {
ConnectionWidget conn = new ConnectionWidget(scene);
conn.setTargetAnchorShape(AnchorShape.TRIANGLE_FILLED);
conn.setTargetAnchor(AnchorFactory.createRectangularAnchor(target));
conn.setSourceAnchor(AnchorFactory.createRectangularAnchor(source));
connectionLayer.addChild(conn);
}
}
}

Join the discussion

Comments ( 2 )
  • Tatarize Saturday, June 9, 2012

    return source instanceof LabelWidget && source != null ? true : false;

    That might better read,

    return source instanceof LabelWidget && source != null;

    Though, I can see how the readability might differ, and people like thinking about true and false as booleans rather than the boolean you just checked. Though, they should get out of that habit.


  • CT Friday, March 15, 2013

    Hi Geertjan,

    Why cant the edge get the actions work when I put

    conn.getActions ().addAction (createObjectHoverAction ());

    conn.getActions().addAction(createSelectAction());

    in the public void createConnection(Widget source, Widget target) class.

    Thanks for answering.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.