Freitag, 11. Mai 2007

A small experiment in diagram-layout-from-AST


If the previous post sounded too vaporware to you, this time you'll see code snippets for a view that auto-layouts the EPackage contents of a selected EClass, for example in the Ecore Sample Editor. OK, right away I want to mention that this code is my HelloWorld 0.1 attempt at layouting. If you're already into programming diagram editors, stop reading this post!!!



Most of the setup code for the view in question comes from a plugin to visualize OCL Abstract Syntax Trees, an article about which you can find here.

Instead of using a TreeViewer, this time I'm using a GraphViewer, courtesy of Zest, the Eclipse Visualization Toolkit. Again, I'm using Zest in a most basic manner, you'll see the moment you visit Zest's homepage that it's way more powerful than what my example conveys.

In a lucid moment I realized that in order to customize the figure to display (a class figure) an IStylingGraphModelFactory should be provided, like this one:













public class EcoreGraphModelFactory extends GraphModelFactory {

  public EcoreGraphModelFactory(AbstractStructuredGraphViewer viewer) {
    super(viewer);
    // TODO Auto-generated constructor stub
  }

  @Override
  public GraphNode createNode(Graph graph, Object element) {
    GraphNode gn = super.createNode(graph, element);
    if (element instanceof ENamedElement) {
      IFigure cf = createFigure(element);
      Dimension s = cf.getSize();
      gn.setCustomFigure(cf);
      gn.setSize(s.width, s.height);
    }
    return gn;
  }

  private IFigure createFigure(Object element) {
    IFigure res = null;
    if (element instanceof EClass) {
      res = createEClassFigure((EClass)element);
      // res = UMLExample.createClassFigure1();
    }
    return res;
  }
  
  private IFigure createEClassFigure(EClass eC) {
    Font classFont = new Font(null, "Arial"12, SWT.BOLD);
    Image eCImage = Activator.getImage(eC);
    Label classLabel1 = new Label(eC.getName(), eCImage);
    classLabel1.setFont(classFont);

    UMLClassFigure classFigure = new UMLClassFigure(classLabel1);
    
    for (EAttribute eA : eC.getEAttributes()) {
      String str = eA.getName() " : " + GenericsUtil.getText(eA.getEGenericType());
      Label attributeL = new Label(str, Activator.getImage(eA));
      classFigure.getAttributesCompartment().add(attributeL);
    }
    
    for (EReference eR : eC.getEReferences()) {
      String str = eR.getName() " : " + GenericsUtil.getText(eR.getEGenericType());
      Label attributeL = new Label(str, Activator.getImage(eR));
      classFigure.getMethodsCompartment().add(attributeL);
    }
    
    classFigure.setSize(-1, -1);
    return classFigure;
  }


}







All in all, the plugin structure is as follows:


You can find (at your own risk, no support, thanks) the Eclipse project in question in this zip file.

I don't even dare call it a "proof of concept", but anyway I felt some code should be thrown in to support my argument.

The IDE generator I'm working on should provide an extension point to let querying the current AST, to support this and other use cases. Yes. I think so.

Keine Kommentare: