Hello,
I am facing a very weird problem. I dont know why the selection Policy of my EditPart stops working when I add to the associated figure of the EditPart a MouseListener.
Basically, I am installing for my EditPart a Selection Policy FeedBack to let the GraphicViewer highlight and select my EditPart, as well as to notify the property view with the new selection. So far so good, However, when I add to my Figure a Mouse listener to drag and move the figure. The GraphicViewer stops selecting the EditPart and notifying the Property View. I dont understand why. I try to install also a EditPolicy.Layout_Role to the RootEditPart to move my figures. It works, however the way of moving the figure using EditPolity.Layout_Role it is not what I am looking for. I dont like the way of moving the figures in this way because I cannot see the figure moving under the mouse, only the shadow of the moving figure. I dont get the same effect as using the Mouse Listener associated to the figure.
Could anyone tell me what I am doing wrong? I dont understand why the EditPart Selection stops working after addition the Mouse listener to the figure.
Thanks in advance.
Here you have the code that I am using.
MyRootEditPart
public class MyRootEditPart extends AbstractGraphicalEditPart
{
.... code here ....
protected void createEditPolicies()
{
// If this editpart is the root content of the viewer, then disallow
// removal
if (getParent() instanceof RootEditPart) {
installEditPolicy(EditPolicy.COMPONENT_ROLE, new RootComponentEditPolicy());
}
// Handle Resize and Move the Children
installEditPolicy(EditPolicy.LAYOUT_ROLE, new MoveEditLayoutPolicy());
}
.... code here ....
}
MyEditPart
public class MyEditPart extends AbstractGraphicalEditPart implements PropertyChangeListener
{
.... code here ....
@Override
protected void createEditPolicies()
{
// Policy to Highlight the Figure when the mouse is hovering or then the EditPart
// has been selected.
installEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE, new HighLightEditPolicy((BlockNodeFigure)this.getFigure()));
}
.... code here ....
}
MyBlockFigure
public class MyBlockFigure extends RoundedRectangle
{
.... code here ....
public MyBlockFigure(MyEditPart editpart)
{
.... code here ....
// Instantiate the Drag&Move Listener
new DragAndMoveFigureMouseListener(this);
}
.... code here ....
}
MoveEditLayoutPolicy
public class MoveEditLayoutPolicy extends XYLayoutEditPolicy
{
@Override
protected Command createChangeConstraintCommand(EditPart child, Object constraint)
{
AbstractLayoutCommand command = null;
if(child instanceof BlockModelEditPart)
{
command = new BlockModelLayoutCommand();
command.setModel((BlockModel)child.getModel());
command.setConstraint((Rectangle)constraint);
}
return command;
}
@Override
protected Command getCreateCommand(CreateRequest request)
{
return null;
}
}
HighLightEditPolicy
public class HighLightEditPolicy extends SelectionEditPolicy
{
/** BlockModel Figure to be handled for this policy to be
* highlighted or selected */
MyBlockFigure figBlock = null;
public HighLightEditPolicy(MyBlockFigure figure)
{
super();
this.figBlock = figure;
}
@Override
protected void showSelection()
{
figBlock.select();
}
@Override
protected void hideSelection()
{
figBlock.deSelect();
}
@Override
public void showTargetFeedback(Request request)
{
figBlock.activateHighLight();
}
@Override
public void eraseTargetFeedback(Request request)
{
figBlock.deactiveHighLight();
}
}
MyBlockFigure
/** This Listener implements drag and drop for Draw2D Figure */
public class DragAndMoveFigureMouseListener implements MouseListener, MouseMotionListener{
/** */
private Figure figure;
/** */
private Point location;
/** Instance the Logger */
private final static Logger LOGGER = Logger.getLogger(DragAndMoveFigureMouseListener.class.getName());
/**
* constructor save reference to figure, then add listeners
* @param figure
*/
public DragAndMoveFigureMouseListener(Figure figure)
{
this.figure = figure;
figure.addMouseListener(this);
figure.addMouseMotionListener(this);
}
@Override
public void mousePressed(MouseEvent me)
{
location = me.getLocation();
me.consume();
}
@Override
public void mouseReleased(MouseEvent me)
{
if( location==null )
return;
location = null;
me.consume();
}
public void mouseDragged(MouseEvent event)
{
if (location == null)
return;
Point newLocation = event.getLocation();
if (newLocation == null)
return;
Dimension offset = newLocation.getDifference(location);
if (offset.width == 0 && offset.height == 0)
return;
location = newLocation;
UpdateManager updateMgr = figure.getUpdateManager();
LayoutManager layoutMgr = figure.getParent().getLayoutManager();
Rectangle bounds = figure.getBounds();
updateMgr.addDirtyRegion(figure.getParent(), bounds);
bounds = bounds.getCopy().translate(offset.width, offset.height);
layoutMgr.setConstraint(figure, bounds);
figure.translate(offset.width, offset.height);
updateMgr.addDirtyRegion(figure.getParent(), bounds);
event.consume();
}
@Override
public void mouseEntered(MouseEvent me) { }
@Override
public void mouseExited(MouseEvent me) { }
@Override
public void mouseHover(MouseEvent me) { }
@Override
public void mouseMoved(MouseEvent me) { }
@Override
public void mouseDoubleClicked(MouseEvent me) { }
}