KLighD old Minimal Example HowTo - Minimal Example (Java Model) 

Page / - last modified by Niklas Rentz on 2023/09/12 13:04 

Minimal Example (Java Model) 
last modified by Niklas Rentz
on 2023/09/12 13:04

Table of Contents
Step-By-Step Guide
The Details
The Files

These instructions should guide you if you'd like to try out using KLighD for visualizing your own models or data structures. A minimal example is shown where a trivial java data structure is visualized with KLighD.
Step-By-Step Guide
In order to get this example working, please follow the instructions below:
 1.	Download Eclipse Kepler (Modeling Edition) and install KIELER Pragmatics 0.10.0 (according to these instructions)
[Note that there is an updated version with connections/transitions here]
 2.	Download the following file, unzip it and import it as an existing project into your Eclipse workspace using File->Import...->General->Existing projects into Workspace (see help for details):
de.cau.cs.kieler.klighd.example.mydata.zip
 3.	You may now create a new run-configuration (according to these instructions) and run the project as an Eclipse Application.
 4.	Once you click on the red-circled button, you should see the synthesized diagram as follows:
 
The Details
Mainly there are three classes involved (found in the src folder of the imported project in your Eclipse workspace):
 1.	MyData.java : It contains a trivial hierarchical data model, in reality you can use your own data structures or models instead.
 2.	MyDataDiagrammSynthesisHandler.java : This is an Eclipse command handler implementation for triggering the diagram synthesis with a button, in reality you can use your own code in order to synthesize or update your data/model visualization.
 3.	MyDataDiagramSynthesis.xtend : This is the most essential part, it is the transformation from your data/model to the view model (the diagram). You can use this as a bare bone example and extend it as you like. Note that it already uses several KLighD extension libraries that are included. These libraries can be inspected also if you are searching for further features that you want to use, they contain the most common features already.
The Files
MyData.java
public class MyData {
   
   public String name;
   public List<MyData> subData = new LinkedList<MyData>();
   
   public static MyData getTestData() {
        MyData d1 = new MyData();
        d1.name = "all data";
        MyData d2 = new MyData();
        d2.name = "outer data";
        MyData d3 = new MyData();
        d3.name = "inner 1";
        MyData d4 = new MyData();
        d4.name = "inner 2";
        MyData d5 = new MyData();
        d5.name = "inner 5";
        MyData d6 = new MyData();
        d6.name = "most inner 6";
        d1.subData.add(d2);
        d2.subData.add(d3); 
        d2.subData.add(d4); 
        d2.subData.add(d5); 
        d4.subData.add(d6); 
       return d1;
   }
   
}
MyDataDiagrammSynthesisHandler.java
public class MyDataDiagrammSynthesisHandler extends
    WorkbenchWindowHandlerDelegate { //IWorkbenchWindowActionDelegate {
   @Override
   public Object execute(ExecutionEvent event) throws ExecutionException {
       // Get some static dummy test data
       MyData data = MyData.getTestData();
       // Prepare the diagram synthesis: Create a new klighdUpdateDiagramEffect
       KlighdUpdateDiagramEffect klighdEffect = new KlighdUpdateDiagramEffect(
               "de.cau.cs.kieler.klighd.example.mydata.MyDataDiagramSynthesis",
               "KLighD MyData Diagram", data);
       // FIXME: Note that the incremental update currently is under re-development
       // for Eclipse Kepler after large API changes to EMF Compare had been made.
       // At this time we cannot use the incremental update strategy (commented out).
       // As soon as it is fixed, uncomment the line below and delete the 
       // SimpleUpdateStrategy line:
       
       // klighdEffect.setProperty(LightDiagramServices.REQUESTED_UPDATE_STRATEGY,
       // UpdateStrategy.ID);
       klighdEffect.setProperty(
                LightDiagramServices.REQUESTED_UPDATE_STRATEGY,
                SimpleUpdateStrategy.ID);
       // Do the diagram synthesis
       klighdEffect.execute();
       return null;
   }
}
MyDataDiagramSynthesis.xtend
class MyDataDiagramSynthesis extends AbstractDiagramSynthesis<MyData> {
    @Inject    extension KNodeExtensions
    @Inject    extension KEdgeExtensions
    @Inject    extension KPortExtensions
    @Inject    extension KLabelExtensions
    @Inject    extension KRenderingExtensions
    @Inject    extension KContainerRenderingExtensions
    @Inject    extension KPolylineExtensions
    @Inject    extension KColorExtensions
   // Some self-defined colors
   private static val KColor BLUE1 = RENDERING_FACTORY.createKColor() =>
        [it.red = 248; it.green = 249; it.blue = 253];
   private static val KColor BLUE2 = RENDERING_FACTORY.createKColor() =>
        [it.red = 205; it.green = 220; it.blue = 243];
   // Additional transformation option to hide or show a shadow    
   private static val SynthesisOption SHOW_SHADOW = SynthesisOption::createCheckOption("Shadow", true);
   // Add all transformation options (comma separated)
   override getDisplayedSynthesisOptions() {
       return ImmutableList::of(
           SHOW_SHADOW
       );
    }
   override KNode transform(MyData data) {
       val root = data.createNode() 
       root.putToLookUpWith(data) => [
           // Optional Layout parameters can be set
           //it.addLayoutParam(LayoutOptions::ALGORITHM, "de.cau.cs.kieler.kiml.ogdf.planarization");
           //it.addLayoutParam(LayoutOptions::SPACING, 75f);
           //it.addLayoutParam(LayoutOptions::DIRECTION, Direction::UP);
           // A rounded rectangle is created for every MyData instance
           it.addRoundedRectangle(5, 5) => [
               // Set linewith, foreground color, and a fading background color
               it.lineWidth = 1;
               it.setForeground("darkGray".color)
               // We need a fresh copy of each color item, because it is contained by its element
               it.setBackgroundGradient(BLUE1.copy, BLUE2.copy, 90)
               // Here we see a how to use a boolean transformation/diagram option
               if (SHOW_SHADOW.booleanValue) {
                   it.shadow = "black".color;
                }
               // Set a text
               it.addText("  " + data.name + " ") => [
                   it.setFontSize(9)
                   it.setForeground("black".color)
                ]
               // If this is a hierarchical MyData instance, then create horizontal splitter, 
               // a child area, and add its children
               if (data.subData.length > 0) {
                   it.setGridPlacement(1);
                   it.addHorizontalSeperatorLine(1, 2).setForeground("darkGray".color)
                   it.addChildArea().setGridPlacementData() => [
                       from(LEFT, 3, 0, TOP, 3, 0).to(RIGHT, 3, 0, BOTTOM, 3, 0)
                       minCellHeight = 5;
                       minCellWidth = 5;
                    ];
                   for (subData : data.subData) {
                       // To the recursive call to transform for all children
                       val child = subData.transform
                       // It is important to add all children to the root!
                       root.children.add(child)
                    }
                }
            ]
        ]
       return root;
    }
}

Eclipse