Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[gmf-dev] Implementing tool that aggregates multiple tools

Hello,

I have 2 separate tools in my palette.
     1) Page tool - that would create a page node
     2) Control tool - that would create a control node inside a page node

Now, I need a tool 'Page-Control', which would create a page and also a
control inside it. I have tried options like
     a) Have the Page-Control tool the same as the Page tool. Then override
the handleButtonUp() method. Here instead of simply returning
super.handleButtonUp(), I call the super.handleButtonUp(), then try to
create the Control tool instance. I also try to get the target edit part for
the control(which is pageControlCompartmentEditPart
). Then I create a
MouseEvent instance and set its x, y values from the bounds of
pageControlCompartmentEditPart. Then I programatically call the mouseMove()
and mouseUp() to simulate the control creation inside the page. But this did
not work.

The first issue in the above approach is that, the bounds of the
PageControlCompartmentEditPart from which I set the x,y values for the mouse
event, is always 0.

Thanks in advance.

Here is the my customization to the code.

public class PageControlTool extends UnspecifiedTypeCreationTool {
       //This tool is the same as the PageTool to create a page
       //The elementTypes of this tool, is the page element type only.

       private MouseEvent me;
       private EditPartViewer viewer;

       ...
       ...

       //record the mouse event. This mouse event would be tweaked to
programmatically create the Control.
       public void mouseMove(MouseEvent me, EditPartViewer viewer) {
               super.mouseMove();
               this.me = me;
               this.viewer = viewer;
       }

       //once the mouse button is released
       public void handleButtonUp(int button) {
               boolean returnValue = super.handleButtonUp(button);

               //find and instantiate the data control tool
               ApplicationDiagramEditor editor = (ApplicationDiagramEditor)
PlatformUI.getWorkbench()
                       .getActiveWorkbenchWindow().getActivePage().getActiveEditor();
               Tool tool = null;
               PaletteViewer paletteViewer = (PaletteViewer)
editor.getAdapter(PaletteViewer.class);

               PaletteContainer container = (PaletteContainer)
paletteViewer.getPaletteRoot().getChildren().get(1);

               //find the ControlToolEntry from the palette
               for(Iterator iterator = container.getChildren().iterator();
iterator.hasNext();) {
                       ToolEntry entry = (ToolEntry) iterator.next();
                       if(entry instanceof ControlToolEntry) {
                               paletteViewer.setActiveTool(entry);
                               GraphicalViewer graphicalViewer = (GraphicalViewer)
editor.getAdapter(GraphicalViewer.class);
                               if(graphicalViewer != null) {
                                       tool = graphicalViewer.getEditDomain().getActiveTool();
                               }
                       }
               }

               PageEditPart newPageEditPart = (PageEditPart)
editor.getDiagramGraphicalViewer().getSelectedEditParts().get(0);
               newPageEditPart.deactivate(); //to manullay reset the active flag.
               newPageEditPart.activate();
               List childrenEditParts = newPageEditPart.getChildren();
               GraphicalEditPart pageCompartmentEditPart = null;
               if(childrenEditParts.size() == 2) {
                       pageCompartmentEditPart = (GraphicalEditPart) childrenEditParts.get(1);
               }

               Rectangle bounds = pageCompartmentEditPart.getFigure().getBounds();

//This mouse event instance is got from the last call the mouseMove of the
Page-Control tool.
               me.x = bounds.x;
               me.y = bounds.y;
               tool.mouseMove(me, viewer);
               tool.mouseUp(me, viewer);

               return returnValue;
       }
}


Back to the top