Tag Archives: tutorial

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: “Smart” Multi-line Text Figure

Enhanced by Zemanta

JJTV Presentation – Creating Graphical Editors with GEF

Last week I gave a short presentation in JJTV meetup on how to create graphical editors with Eclipse and GEF. This is a very short and fast tutorial that shows the basic concepts and how they are developed. The code that implements the examples is far from being complete, but for those who are just getting started with GEF, you may find this interesting.

The code and the presentation can be found here.

 

Enhanced by Zemanta

Welcome Korean Readers

This week I started receiving a relatively large number of hits from http://cafe.naver.com/eclipseplugin (relative to the number of hits I get every day). I couldn’t help and take a look at the source of the hits, which turned out to be a Korean eclipse developer forum, linking to some of my GEF tutorials.

So hello to all my new Korean visitors! come back any time :-)

Enhanced by Zemanta