setting initial location of editpart from a context menu [message #201956] |
Fri, 04 November 2005 17:57 |
Eclipse User |
|
|
|
Originally posted by: pbeagan.yahoo_dontspamme_.com
Below is my rough code for setting the initial location of a figure on a
diagram part from a context menu. IE: Right click, choose Add Item, item
appears at the mouse location. I know this has been discussed and is in
the FAQ and is basically ignored because someone could do a shift+F10 and
can't know that the cursor will be in a relevant spot. Whatever - my
requirement was a right click -> add. The user won't even get the correct
context menu if they have not selected the diagram, so _likely_ the mouse
will be in a good spot. So here is my code if anyone want an example,
improvement/critcism welcomed. Repository is the diagram, Item is the
child.
In RepositoryEditPolicy:
public Command getCommand(Request request) {
if ((request.getType()).equals(Util.ADDITEM_ACTION) )
return getAddItemCommand(request);
return super.getCommand(request);
}
protected Command getAddItemCommand(Request request){
// absolute location
org.eclipse.swt.graphics.Point absoluteLocation =
Display.getCurrent().getCursorLocation();
// relative location to editor control
ScalableFreeformRootEditPart rootEditPart =
(ScalableFreeformRootEditPart)getHost().getRoot();
Point location = new
Point(rootEditPart.getViewer().getControl().toControl(absolu teLocation));
// transform the scroll
Point viewLocation =
((Viewport)rootEditPart.getFigure()).getViewLocation();
location.translate(viewLocation.x, viewLocation.y);
IRepositoryPart host = (IRepositoryPart)getHost();
Repository rep = host.getRepository();
// construct the create item command
Item item = new Item(rep);
ItemCreateCommand command = new ItemCreateCommand();
command.setItem(item);
command.setRepository(rep);
command.setLocation(location);
return command;
}
In ItemCreateCommand :
public void execute()
{
this.item.setName("item" + (rep.getChildren().size() + 1));
this.item.setRepository(rep);
// Order is important here. Must add to rep, THEN set bounds
// otherwise the Item will be put in the top left corner of editor due
to events firing and layout manager
rep.addChild(item);
// Set initial location/bounds of Item to where the mouse was clicked
Rectangle bounds = new Rectangle();
bounds.setSize(-1, -1);
bounds.setLocation(location);
item.modifyBounds(bounds); // this fires off event
}
|
|
|
Powered by
FUDForum. Page generated in 0.03177 seconds