Minimal Example HowTo
Version 5.1 by cmot on 2013/11/09 02:00
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 data structure is visualized with KLighD.
The Data
The data structure is defined like follows:
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;
}
}
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;
}
}
The method getTestData() will build a small instance of the MyData class that is used in this diagram synthesis example for data visualization.
Step-by-step guide
In order to get this example working, please follow the instructions below:
- Download Eclipse Kepler (Modeling Edition):
http://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/kepler/SR1/eclipse-modeling-kepler-SR1-win32-x86_64.zip&mirror_id=546 - Add the KIELER Pragmatics Nightly update site to your Eclipse (Help -> Install New Software ... -> Add):
http://rtsys.informatik.uni-kiel.de/~kieler/updatesite/nightly/pragmatics/ - Select
[x] Layout,
[x] Lightweight Diagrams, and
[x] Sources
to be installed into your Eclipse. You may need to re-start Eclipse after finishing the installation process. - Download the following file, unzip it and import it as an existing project into your Eclipse workspace:
- You may now create a new run configuration and run everything as an Eclipse Application.
- Once you click on the red-circled button, you should see the visualization as follows:

The Details
Mainly there a three classes involved:
- MyData.java : It contains a trivial hierarchical data model, in reality you can use your own data structures or models instead.
- MyDataDiagrammSynthesisAction.java : This is an Eclipse Action implementation for triggering the diagram synthesis, in reality you can use your own code in order to synthesize or update your data/model visualization.
- MyDataDiagramSynthesis.xtend : This ist 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
MyDataDiagrammSynthesisAction.java
public class MyDataDiagrammSynthesisAction implements
IWorkbenchWindowActionDelegate {
public MyDataDiagrammSynthesisAction() {
}
public void run(IAction action) {
// 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();
}
public void selectionChanged(IAction action, ISelection selection) {
}
public void dispose() {
}
public void init(IWorkbenchWindow window) {
}
}
IWorkbenchWindowActionDelegate {
public MyDataDiagrammSynthesisAction() {
}
public void run(IAction action) {
// 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();
}
public void selectionChanged(IAction action, ISelection selection) {
}
public void dispose() {
}
public void init(IWorkbenchWindow window) {
}
}
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 TransformationOption SHOW_SHADOW = TransformationOption::createCheckOption("Shadow", true);
// Add all transformation options (comma separated)
override public getTransformationOptions() {
return ImmutableSet::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.optionBooleanValue) {
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;
}
}
@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 TransformationOption SHOW_SHADOW = TransformationOption::createCheckOption("Shadow", true);
// Add all transformation options (comma separated)
override public getTransformationOptions() {
return ImmutableSet::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.optionBooleanValue) {
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;
}
}