Vainolo's Blog

Creating an OPM GEF Editor – Part 23: Drag & Drop from the Palette


Previous tutorial: Creating an OPM GEF Editor – Part 22: Enabling Select-All Action in a GEF Editor

Doing D&D from the palette never seemed to me an important feature, but after seeing this question on SO, I thought to myself that this shouldn’t be so complicated. So I took the challenge, and after 1 hour of shoveling through the GEF examples, I found how this is done. Someday, someone must write good documentation for GEF… but in the meantime, this is how it is done.

First, you need to add two lines to the graphical editor’s configuration (lines between D&D comments):

protected void configureGraphicalViewer() {
  getGraphicalViewer().setEditPartFactory(new OPMEditPartFactory());
  getActionRegistry().registerAction(new ToggleGridAction(getGraphicalViewer()));
  getActionRegistry().registerAction(new ToggleSnapToGeometryAction(getGraphicalViewer()));
    new OPMGraphicalEditorContextMenuProvider(getGraphicalViewer(), getActionRegistry()));

  // D&D
  getGraphicalViewer().addDropTargetListener(new TemplateTransferDropTargetListener(getGraphicalViewer()));
    new TemplateTransferDragSourceListener(getEditDomain().getPaletteViewer()));
  // end D&D

I executed the new editor, and D&D still didn’t work… I was missing something. So I went and checked the Shapes
example and found that the palette entries created there are of type CombinedTemplateCreationEntry and
not the regular CreationToolEntry which I am using. This seemed in the right direction, so I changed one
of my palette entries:

private void addNodeTools() {
  CreationToolEntry entry =
    new CreationToolEntry("Label", "Create new Label", new LabelFactory(), ImageDescriptor.createFromFile(
      this.getClass(), "icons/label.ico"), ImageDescriptor.createFromFile(this.getClass(), "icons/label.ico"));

  entry =
    new CombinedTemplateCreationEntry("OPMObject", "Create a new Object", new OPMObjectFactory(),
      ImageDescriptor.createFromFile(this.getClass(), "icons/object.ico"), ImageDescriptor.createFromFile(
        this.getClass(), "icons/object.ico"));

I also had to alter the OPMObjectFactory so that the OPMObject so that it comes
with default constraints. Now fire up the editor… And there it goes! it works! it works!!!

Enjoy your D&D. As usual, you can browse the source code at github.

Next Turorial: Creating an OPM GEF Editor – Part 24: Showing Feedback to the User

Enhanced by Zemanta

Written by vainolo

February 6th, 2013 at 1:17 am

6 Responses to 'Creating an OPM GEF Editor – Part 23: Drag & Drop from the Palette'

Subscribe to comments with RSS or TrackBack to 'Creating an OPM GEF Editor – Part 23: Drag & Drop from the Palette'.

  1. Another very helpful article! Thanks a bunch, again :)!


    19 Sep 14 at 02:35

  2. the method LabelFactory() is not available. kindly please help.


    11 Nov 14 at 15:07

  3. LabelFactory is not a method but a class, and it is located in the package.


    13 Nov 14 at 18:23

  4. hey! great tutorial, thanks a lot!
    I just wanna to tell you that you have some little problems with the hyperlinks between the different parts of the tutorial:

    next of 23 is 25
    prev of 25 is 25


    19 Feb 15 at 16:08

  5. Thanks! I updated the links with the missing tutorial.


    21 Feb 15 at 22:19

  6. Great tutorial thanks a lot 🙂


    23 Aug 16 at 07:29

Leave a Reply

%d bloggers like this: