Simplest possible Drag&Drop implementation for Visual Library
Two days ago I blogged about how to create a new ExplorerView by Combining ListView and ChoiceView. Today I’ll show you how to use this view as a palette for a Visual Library scene. It’s very simple, AbstractNodes already implement everything needed for this. In the NestedListView -as in any ListView- dragging is enabled with this line:
listView.setAllowedDragActions(DnDConstants.ACTION_COPY);
Now we need to create a scene and display it in a TopComponent. For adding the scene to the TopComponent check the NetBeans Visual Library Tutorial.
This is a minimal implementation for accepting Nodes:
public class ObjectSceneImpl extends ObjectScene {
public ObjectSceneImpl() {
getActions().addAction(ActionFactory.createAcceptAction(new AcceptProvider() {
@Override
public ConnectorState isAcceptable(Widget widget, Point point, Transferable transferable) {
return ConnectorState.ACCEPT;
}
@Override
public void accept(Widget widget, Point point, Transferable transferable) {
Node node = NodeTransfer.node(transferable, NodeTransfer.DND_COPY);
Widget w = new LabelWidget(ObjectSceneImpl.this, node.getDisplayName());
ObjectSceneImpl.this.addChild(w);
w.setPreferredLocation(widget.convertLocalToScene(point));
}
}));
}
}
If you run this code, you can drag & drop nodes from everywhere onto your TopComponent. If you only want to accept a certain kind of nodes, the easiest way is to create your own node class and check for it:
public ConnectorState isAcceptable(Widget widget, Point point, Transferable transferable) {
Node node = NodeTransfer.node(transferable, NodeTransfer.DND_COPY);
if (node == null | !(node instanceof OSGiRepositoryBundleNode)) {
return ConnectorState.REJECT_AND_STOP;
}
return ConnectorState.ACCEPT;
}
Obviously this is almost the simplest possible implementation and as such only a starting point. In VisualOsgi I’m checking the Nodes’ lookup for Bundle info and Install the bundle when everythings fine. If you want to make it look nicer you can check the above mentioned Tutorial for details.



[...] Epple posted Simplest possible Drag&Drop implementation for Visual Library: Two days ago I blogged about how to create a new ExplorerView by Combining ListView and [...]
Pingback by Toni Epple on the Simplest possible Drag&Drop implementation for Visual Library - Technology
— 28. August 2009 @ 18:03
[...] of the cases, the post is written by a member of the java.net community). First, Tony Epple posted Simplest possible Drag&Drop implementation for Visual Library: Two days ago I blogged about how to create a new ExplorerView by Combining ListView and [...]
Pingback by Poll Result: Java Store/Warehouse Are Finding Early Adopters - Technology
— 28. August 2009 @ 20:01