PDA

View Full Version : Check valid targets during DnD



A.Rothe
10 Nov 2011, 7:22 AM
Hi,

how I can check, whether or not the user moves the cursor over a valid target during DnD? I would like forbid to use some ModelData as drop zones within a TreeGrid. I have set TreeGridDropTarget.setAllowDropOnLeaf(false), but I need such a behavior also on some nodes.

Thanks
Andre :-?

A.Rothe
17 Nov 2011, 12:41 AM
I have found the method DropTarget.showFeedback(DNDEvent event), which displays the feedback on a drop node. It is possible to override this method like:



@Override
protected void showFeedback(DNDEvent event) {
if (!isValidDropTarget(event)) {
Insert.get().hide();
event.getStatus().setStatus(false);
return;
}
super.showFeedback(event);
}


The new method isValidDropTarget() can be implemented like:



@SuppressWarnings("unchecked")
private boolean isValidDropTarget(DNDEvent event) {
TreeGrid<ModelData> target = (TreeGrid<ModelData>) event.getDropTarget().getComponent();
TreeGrid<ModelData>.TreeNode zone = target.findNode(event.getTarget());
if (zone == null) {
return true; // let it check from super-class
}

TreeGrid<ModelData> source = (TreeGrid<ModelData>) event.getDragSource().getComponent();
List<ModelData> selection = source.getSelectionModel().getSelection();

for (ModelData model : selection) {
// check the "model" against "zone" and return false
// if "zone" is not a valid drop target for "model", otherwise check the next "model"
// example:
// if (source.getTreeStore().getParent(model) == zone.getModel()) return false;
}

return true;
}


~Andre :D