Using support for persistence in your GMF application

To manage persistent layout configurations in a GMF application:
  1. You need to make your GMF editor project depend on the ilog.views.eclipse.graphlayout.gmf.properties plug-in.
  2. Use a ilog.views.eclipse.graphlayout.gmf.edit.source.PersistentGMFLayoutSource and make it listen to the notation model.
  3. Set up edit policies.
  4. Set up style controllers. They have the same purpose as the configuration controllers for EMF: to listen to the model in order to update the layout algorithms.
After these steps, the property sheets will be completely integrated with your GMF editor.
Example of a layout source and edit policy setup
The following code sample shows a layout source that listens to the GMF notation model.
import ilog.views.eclipse.graphlayout.IGrapherEditPart;
import ilog.views.eclipse.graphlayout.source.ILayoutSource;
import ilog.views.eclipse.graphlayout.edit.editpolicies.LayoutEditPolicyRoles;
import ilog.views.eclipse.graphlayout.gmf.edit.notation.LayoutNotationPackage;
import ilog.views.eclipse.graphlayout.gmf.edit.source.PersistentGMFLayoutSource;
import ilog.views.eclipse.graphlayout.gmf.edit.editpolicies.DefaultGMFLayoutEditPolicy;
import ilog.views.eclipse.graphlayout.gmf.edit.editpolicies.DefaultGMFLayoutPropertyEditPolicy;


import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
import org.eclipse.gmf.runtime.notation.NotationPackage;

public class MyDiagramEditPart extends DiagramEditPart implements
    IGrapherEditPart {

  // reference on the layout source implementation
  private PersistentGMFLayoutSource myLayoutSource;

  ...

  @Override
  public void activate() {
    // we choose to instantiate the layout source when the EditPart
    // is activated. It is important that it is
    // instantiated before its children are activated so that
    // children can behave consequently
    myLayoutSource = new PersistentGMFLayoutSource(this);
    super.activate();
  }

  @Override
  public void deactivate() {
    super.deactivate();
    // cleanup
    myLayoutSource.dispose();
    myLayoutSource = null;
  }

  @Override
  protected void addNotationalListeners() {
    super.addNotationalListeners();
    // listen to notation model changes to update the layout
    addListenerFilter(PersistentGMFLayoutSource.STYLE_NOTATION_FILTER_ID,
        myLayoutSource, getDiagramView(), NotationPackage.eINSTANCE
            .getView_Styles());
    addListenerFilter(
        PersistentGMFLayoutSource.STYLE_GRAPH_LAYOUT_NOTATION_FILTER_ID,
        myLayoutSource, getDiagramView(), LayoutNotationPackage.eINSTANCE
            .getLayoutStyle_GraphLayoutConfiguration());
    addListenerFilter(
        PersistentGMFLayoutSource.STYLE_LINK_LAYOUT_NOTATION_FILTER_ID,
        myLayoutSource, getDiagramView(), LayoutNotationPackage.eINSTANCE
            .getLayoutStyle_LinkLayoutConfiguration());
    addListenerFilter(
        PersistentGMFLayoutSource.STYLE_LABEL_LAYOUT_NOTATION_FILTER_ID,
        myLayoutSource, getDiagramView(), LayoutNotationPackage.eINSTANCE
            .getLayoutStyle_LabelLayoutConfiguration());
  }

  @Override
  protected void removeNotationalListeners() {
    removeListenerFilter(PersistentGMFLayoutSource.STYLE_NOTATION_FILTER_ID);
    removeListenerFilter(
      PersistentGMFLayoutSource.STYLE_GRAPH_LAYOUT_NOTATION_FILTER_ID);
    removeListenerFilter(
      PersistentGMFLayoutSource.STYLE_LINK_LAYOUT_NOTATION_FILTER_ID);
    removeListenerFilter(
      PersistentGMFLayoutSource.STYLE_LABEL_LAYOUT_NOTATION_FILTER_ID);
    super.removeNotationalListeners();
  }

  @Override
  protected void createDefaultEditPolicies() {
    super.createDefaultEditPolicies();
    ...
    installEditPolicy(LayoutEditPolicyRoles.LAYOUT_EDIT_ROLE,
      new DefaultGMFLayoutEditPolicy());
    installEditPolicy(LayoutEditPolicyRoles.LAYOUT_PROPERTY_EDIT_ROLE,
      new DefaultGMFLayoutPropertyEditPolicy());
  }

  @Override
  public Object getAdapter(Class adapter) {
    if (adapter.equals(ILayoutSource.class)) {
      return myLayoutSource;
    }
    return super.getAdapter(adapter);
  }

  public boolean isTopLevel() {
    return true;
  }

}
In this code sample, the dedicated edit policies for GMF are set up as well as the layout source.
Example of style controllers
The following code sample shows how to use a ilog.views.eclipse.graphlayout.gmf.edit.source.NodeOrConnectionStyleController for a node or connection.
import ilog.views.eclipse.graphlayout.edit.editpolicies.LayoutEditPolicyRoles;
import ilog.views.eclipse.graphlayout.gmf.edit.notation.LayoutNotationPackage;
import ilog.views.eclipse.graphlayout.gmf.edit.editpolicies.DefaultGMFLayoutPropertyEditPolicy;
import ilog.views.eclipse.graphlayout.gmf.edit.source.NodeOrConnectionStyleController;

import org.eclipse.gmf.runtime.diagram.ui.editparts.ShapeNodeEditPart;
import org.eclipse.gmf.runtime.notation.NotationPackage;

public class MyNodeEditPart extends ShapeNodeEditPart {

  // reference on the style controller
  private NodeOrConnectionStyleController layoutStyleController;

  ...

  @Override
  protected void addNotationalListeners() {
    super.addNotationalListeners();
    layoutStyleController = new NodeOrConnectionStyleController(this);
    addListenerFilter(       
      NodeOrConnectionStyleController.
        NODE_CONNECTION_STYLE_NOTATION_FILTER_ID,
      layoutStyleController, (View) getAdapter(View.class),
      NotationPackage.eINSTANCE.getView_Styles());
    addListenerFilter(
      NodeOrConnectionStyleController.
        NODE_CONNECTION_STYLE_GRAPH_LAYOUT_NOTATION_FILTER_ID,
      layoutStyleController, (View) getAdapter(View.class),
      LayoutNotationPackage.eINSTANCE.
        getNodeOrConnectionStyle_GraphLayoutConfiguration());
    addListenerFilter(
        NodeOrConnectionStyleController.
          NODE_CONNECTION_STYLE_LINK_LAYOUT_NOTATION_FILTER_ID,
      layoutStyleController, (View) getAdapter(View.class),
      LayoutNotationPackage.eINSTANCE.
        getNodeOrConnectionStyle_LinkLayoutConfiguration());
  }

  @Override
  protected void removeNotationalListeners() {
    removeListenerFilter(NodeOrConnectionStyleController.
      NODE_CONNECTION_STYLE_LINK_LAYOUT_NOTATION_FILTER_ID);
    removeListenerFilter(NodeOrConnectionStyleController.
      NODE_CONNECTION_STYLE_GRAPH_LAYOUT_NOTATION_FILTER_ID);
    removeListenerFilter(NodeOrConnectionStyleController.
      NODE_CONNECTION_STYLE_NOTATION_FILTER_ID);
    layoutStyleController.dispose();
    layoutStyleController = null;
    super.removeNotationalListeners();
  }

  @Override
  protected void createDefaultEditPolicies() {
    super.createDefaultEditPolicies();
    ...
    installEditPolicy(LayoutEditPolicyRoles.LAYOUT_PROPERTY_EDIT_ROLE,
        new DefaultGMFLayoutPropertyEditPolicy());
  }
}
The following code sample shows how to use a ilog.views.eclipse.graphlayout.gmf.edit.source.LabelStyleController for a label.
import ilog.views.eclipse.graphlayout.edit.editpolicies. LayoutEditPolicyRoles;
import ilog.views.eclipse.graphlayout.gmf.edit.notation.LayoutNotationPackage;
import ilog.views.eclipse.graphlayout.gmf.edit.editpolicies. DefaultGMFLayoutPropertyEditPolicy;
import ilog.views.eclipse.graphlayout.gmf.edit.source.LabelStyleController;
import ilog.views.eclipse.graphlayout.runtime.labellayout.ILabelEditPart;

import org.eclipse.gmf.runtime.diagram.ui.editparts.LabelEditPart;
import org.eclipse.gmf.runtime.notation.NotationPackage;

public class MyLabelEditPart extends LabelEditPart implements ILabelEditPart {

  // reference on the style controller
  private LabelStyleController labelStyleController;

  ...

  @Override
  protected void addNotationalListeners() {
    super.addNotationalListeners();
    labelStyleController = new LabelStyleController(this);
    addListenerFilter(LabelStyleController.LABEL_STYLE_NOTATION_FILTER_ID,
        labelStyleController, (View) getAdapter(View.class),
        NotationPackage.eINSTANCE.getView_Styles());
    addListenerFilter(
        LabelStyleController.LABEL_STYLE_LABEL_LAYOUT_NOTATION_FILTER_ID,
        labelStyleController, (View) getAdapter(View.class),
        LayoutNotationPackage.eINSTANCE.
          getLabelStyle_LabelLayoutConfiguration());
  }

  @Override
  protected void removeNotationalListeners() {
    removeListenerFilter(LabelStyleController.
      LABEL_STYLE_LABEL_LAYOUT_NOTATION_FILTER_ID);
    removeListenerFilter(LabelStyleController.LABEL_STYLE_NOTATION_FILTER_ID);
    labelStyleController.dispose();
    labelStyleController = null;
    super.removeNotationalListeners();
  }

  @Override
  protected void createDefaultEditPolicies() {
    super.createDefaultEditPolicies();
    ...
    installEditPolicy(LayoutEditPolicyRoles.LAYOUT_PROPERTY_EDIT_ROLE,
        new DefaultGMFLayoutPropertyEditPolicy());
  }
}