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() {
  super.configureGraphicalViewer();
  getGraphicalViewer().setEditPartFactory(new OPMEditPartFactory());
  getActionRegistry().registerAction(new ToggleGridAction(getGraphicalViewer()));
  getActionRegistry().registerAction(new ToggleSnapToGeometryAction(getGraphicalViewer()));
  getGraphicalViewer().setContextMenu(
    new OPMGraphicalEditorContextMenuProvider(getGraphicalViewer(), getActionRegistry()));
  configureKeyboardShortcuts();

  // D&D
  getGraphicalViewer().addDropTargetListener(new TemplateTransferDropTargetListener(getGraphicalViewer()));
  getEditDomain().getPaletteViewer().addDragSourceListener(
    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"));
  group.add(entry);

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

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

6 thoughts on “Creating an OPM GEF Editor – Part 23: Drag & Drop from the Palette

    • LabelFactory is not a method but a class, and it is located in the com.vainolo.phd.opm.gef.editor.factory package.

  1. 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

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.