|
Re: Moving child part from one parent part to other [message #1735144 is a reply to message #1734838] |
Wed, 15 June 2016 19:35 |
|
First of all, there's two basic types of policy, interaction policies and transaction policies. An interaction policy handles the user interaction by mouse (click and/or drag) or keyboard (type), and so implements one of more of the IFXOnXxxPolicy interfaces. A transaction policy is responsible for creating and invoking operations which will update the model and/or visual parts. The general pattern is that the interaction policy will work out what is happening (i.e. what is the new parent node), and update the transaction policy with the details, invoking the transaction policy when the user completes the action.
So for example, you would have two policies called, say RelocateChildOnDragPolicy and RelocateChildTxPolicy, and in your module you would install both those policies on your child part.
RelocateChildOnDragPolicy would extend AbstractFXInteractionPolicy and implement IFXOnDragPolicy. This will give it three methods to handle the user interaction, press(), drag() and release(). In press(), as the user clicks on the child part, you would lookup and initialize the transaction policy from the host, like this:
RelocateChildTxPolicy txPolicy = getHost().getAdapter(RelocateChildTxPolicy.class);
txPolicy.init();
This would create an operation, say RelocateChildOperation - at this stage, the transaction policy only knows the host, which is the child that you are moving. That's okay, we'll tell it later about the new parent.
The drag() method is called as you move the child node around. Use the corrdinates in the mouse event to determine the new parent for the node. I generally do this in model code, as all my model objects keep track of their location and dimensions within the overall scene, but you can also do this through the visual parts as well. When you have identified the new parent for the child, you update the transaction policy with that information:
@Override
public void drag(MouseEvent e, Dimension delta) {
...
ParentPart newParent = ... // look up using the mouse location
RelocateChildTxPolicy txPolicy = getHost().getAdapter(RelocateChildTxPolicy.class);
txPolicy.setNewParent(newParent);
...
}
The setNewParent method on your transaction policy would update the operation with the new parent.
The release() method is where you commit the changes:
RelocateChildTxPolicy txPolicy = getHost().getAdapter(RelocateChildTxPolicy.class);
commit(txPolicy);
This will invoke your operation, which will up date the model to change the child's parent, and then refresh the visuals of the old parent, new parent and child. The operation will be in the undo history, so make sure that the undo() method puts everything back to the way it was before, and refreshed the visuals again.
Colin Sharples
CTG Games Ltd
Wellington, New Zealand
|
|
|
Powered by
FUDForum. Page generated in 0.04530 seconds