Using yFiles Viewer within Eclipse

Tips & Tricks

Summary

Describes how to embed the yFiles graph view within the Eclipse workbench.
For a better user experience, please go to the integrated documentation viewer to read this article.

Description

For the implementation of new applications the technique shown here is strongly discouraged! There is a much cleaner and better supported way of getting yFiles technology into an Eclipse application: With yFiles for JavaFX there is a mature solution available that integrates perfectly with the Eclipse/SWT/RCP environment. The bundle contains a complete RCP graph editor applicaiton that can be used for creating advanced RCP based graph drawing applications.
There is another article which describes how to use only the yFiles layout algorithms with Eclipse and GEF, the Graphical Editing Framework.
The example described in this article uses Albireo, a tool which improves the handling of Swing components in an SWT-based GUI. In January 2011 Albireo moved from the Eclipse Incubator to the Eclipse Archive, so it is no longer an active project. For convenience we attached the latest distribution of Albireo to this article. Albireo is not part of yFiles for Java.

Embedding the yFiles graph viewer component Graph2DViewused to be complicated since Eclipse is based on the widget toolkit SWT and the graph view on the standard Java toolkits AWT and Swing. Fortunately, there is a SWT-AWT bridge since Eclipse 3.0 that makes it possible to embed Swing-based components in the Eclipse workbench. Since the intergration using the bridge is quite low level, some tasks remain untackled. Albireo aims at providing a higher-level integration.

The example below shows how to embed an editable Graph2DView as part control of an eclipse editor component using the SwingControl class provided by Albireo.

public class GraphEditor extends EditorPart {

  ...

  private SwingControl swingControl;

  public void createPartControl(final Composite parent) {
    swingControl = new SwingControl(parent, SWT.NONE) {
      protected JComponent createSwingComponent() {
        Graph2DView view = new Graph2DView();
        view.addViewMode(new EditMode());
        return view;
      }

      public Composite getLayoutAncestor() {
        return parent;
      }
    };
  }

  public void setFocus() {
    swingControl.setFocus();
  }
}

Graph Editor Plugin

We have prepared a comprehensive sample plugin that demonstrates how yFiles can be used to integrate a diagram editor in the eclipse workbench. To try out the plugin and inspect its source code, please download the attached Zip archive com.yworks.eclipse.sample3.zip and follow the instructions given below.

yFiles Graph Editor Plugin

Technical Requirements

  • Eclipse 3.4 (Ganymede), 3.5 (Galileo), 3.6 (Helios), 3.7 (Indigo), or 3.8/4.2 (Juno) with PDE and Albireo 0.0.3 (attached)
  • JDK 1.5 or higher
  • yFiles for Java Complete 2.7, 2.8, 2.9, or 2.10

Installation Instructions

  1. Unzip the attached Albireo archive into the dropins directory of your eclipse installation directory.
  2. Unzip the attached Graph Editor Plugin archive into the dropins directory of your eclipse installation directory. This will create a new directory
    com.yworks.eclipse.sample3.
  3. Copy the jar-file y.jar that is part of your yFiles distribution to the subdirectory
    com.yworks.eclipse.sample3/plugins/com.yworks.eclipse.sample3_<version>/lib.
  4. Start Eclipse.

Using the Plugin

  1. The creation wizard [File->New->Other...] can be used to create a new yFiles diagram inside your project.
  2. Opening a yFiles diagram of type .gml, .graphml or .ygf will open the yFiles graph editor for that diagram.
  3. A Properties View will allow you to view and edit the properties of selected graph elements. Choose [Window->Show View->Other...->Basic->Properties] from the menu bar.
  4. Standard operations like undo/redo and clipboard can be used from the eclipse edit menu when the graph editor is active.
  5. When the graph editor has focus, the menu item "Layout Diagram" provides access to some of the yFiles layout algorithms.

Setting up an Eclipse Project for the Plugin

  1. Once the graph editor has been installed in the eclipse plugin directory as described above, the plugin including its source code can be imported as an eclipse plugin project.
  2. Choose [File->Import...] from the menu bar. Next, choose "Plug-Ins and Fragments" from the list. On the next page choose Import As "Projects with source folders". On the next page "Add" the yFiles plugin name com.yworks.eclipse.sample3 from the list and "Finish" the dialog.

Troubleshooting

  • If eclipse reports the error Plug-in com.yworks.eclipse.sample3 was unable to load class com.yworks.eclipse.sample.GraphEditorPart., make sure y.jar is located in the lib mentioned above.

Resources

Categories this article belongs to:
yFiles for Java > yFiles Viewer
yFiles for Java > Deployment
Applies to:
yFiles for Java 2: 2.7, 2.8, 2.9, 2.10, 2.11, 2.12, 2.13, 2.14, 2.15, 2.16, 2.17, 2.18
Keywords:
Eclipse - SWT - Graph2DView