Wiki source code of Minimal Example HowTo
Version 6.1 by cmot on 2013/11/09 02:07
Show last authors
| author | version | line-number | content |
|---|---|---|---|
| 1 | 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. | ||
| 2 | |||
| 3 | == The Data == | ||
| 4 | |||
| 5 | The data structure is defined like follows: | ||
| 6 | |||
| 7 | {{code title="MyData.java" language="java"}} | ||
| 8 | public class MyData { | ||
| 9 | |||
| 10 | public String name; | ||
| 11 | public List<MyData> subData = new LinkedList<MyData>(); | ||
| 12 | |||
| 13 | public static MyData getTestData() { | ||
| 14 | MyData d1 = new MyData(); | ||
| 15 | d1.name = "all data"; | ||
| 16 | MyData d2 = new MyData(); | ||
| 17 | d2.name = "outer data"; | ||
| 18 | MyData d3 = new MyData(); | ||
| 19 | d3.name = "inner 1"; | ||
| 20 | MyData d4 = new MyData(); | ||
| 21 | d4.name = "inner 2"; | ||
| 22 | MyData d5 = new MyData(); | ||
| 23 | d5.name = "inner 5"; | ||
| 24 | MyData d6 = new MyData(); | ||
| 25 | d6.name = "most inner 6"; | ||
| 26 | d1.subData.add(d2); | ||
| 27 | d2.subData.add(d3); | ||
| 28 | d2.subData.add(d4); | ||
| 29 | d2.subData.add(d5); | ||
| 30 | d4.subData.add(d6); | ||
| 31 | return d1; | ||
| 32 | } | ||
| 33 | |||
| 34 | } | ||
| 35 | {{/code}} | ||
| 36 | |||
| 37 | The method getTestData() will build a small instance of the MyData class that is used in this diagram synthesis example for data visualization. | ||
| 38 | |||
| 39 | == Step-By-Step Guide == | ||
| 40 | |||
| 41 | In order to get this example working, please follow the instructions below: | ||
| 42 | |||
| 43 | 1. Download Eclipse Kepler (Modeling Edition): | ||
| 44 | [[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>>url: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||shape="rect"]] | ||
| 45 | 1. Add the KIELER Pragmatics Nightly update site to your Eclipse (Help -> Install New Software ... -> Add)[[: | ||
| 46 | http:~~/~~/rtsys.informatik.uni-kiel.de/~~~~kieler/updatesite/nightly/pragmatics/>>url:http://rtsys.informatik.uni-kiel.de/~~kieler/updatesite/nightly/pragmatics/||shape="rect"]] | ||
| 47 | 1. Select | ||
| 48 | [x] Layout, | ||
| 49 | [x] Lightweight Diagrams, and | ||
| 50 | [x] Sources | ||
| 51 | to be installed into your Eclipse. You may need to re-start Eclipse after finishing the installation process. | ||
| 52 | 1. Download the following file, unzip it and import it as an existing project into your Eclipse workspace: | ||
| 53 | [[attach:de.cau.cs.kieler.klighd.example.mydata.zip]] | ||
| 54 | 1. You may now create a new run configuration and run everything as an Eclipse Application. | ||
| 55 | 1. Once you click on the red-circled button, you should see the visualization as follows: | ||
| 56 | [[image:attach:klighd-minimal-example.png]] | ||
| 57 | |||
| 58 | |||
| 59 | |||
| 60 | == The Details == | ||
| 61 | |||
| 62 | Mainly there a three classes involved: | ||
| 63 | |||
| 64 | 1. **MyData.java** : It contains a trivial hierarchical data model, in reality you can use your own data structures or models instead. | ||
| 65 | 1. **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. | ||
| 66 | 1. **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. | ||
| 67 | |||
| 68 | == The Files == | ||
| 69 | |||
| 70 | {{code title="MyDataDiagrammSynthesisAction.java" language="java"}} | ||
| 71 | public class MyDataDiagrammSynthesisAction implements | ||
| 72 | IWorkbenchWindowActionDelegate { | ||
| 73 | public MyDataDiagrammSynthesisAction() { | ||
| 74 | } | ||
| 75 | public void run(IAction action) { | ||
| 76 | |||
| 77 | // Get some static dummy test data | ||
| 78 | MyData data = MyData.getTestData(); | ||
| 79 | // Prepare the diagram synthesis: Create a new klighdUpdateDiagramEffect | ||
| 80 | KlighdUpdateDiagramEffect klighdEffect = new KlighdUpdateDiagramEffect( | ||
| 81 | "de.cau.cs.kieler.klighd.example.mydata.MyDataDiagramSynthesis", | ||
| 82 | "KLighD MyData Diagram", data); | ||
| 83 | // FIXME: Note that the incremental update currently is under re-development | ||
| 84 | // for Eclipse Kepler after large API changes to EMF Compare had been made. | ||
| 85 | // At this time we cannot use the incremental update strategy (commented out). | ||
| 86 | // As soon as it is fixed, uncomment the line below and delete the | ||
| 87 | // SimpleUpdateStrategy line: | ||
| 88 | |||
| 89 | // klighdEffect.setProperty(LightDiagramServices.REQUESTED_UPDATE_STRATEGY, | ||
| 90 | // UpdateStrategy.ID); | ||
| 91 | klighdEffect.setProperty( | ||
| 92 | LightDiagramServices.REQUESTED_UPDATE_STRATEGY, | ||
| 93 | SimpleUpdateStrategy.ID); | ||
| 94 | // Do the diagram synthesis | ||
| 95 | klighdEffect.execute(); | ||
| 96 | } | ||
| 97 | public void selectionChanged(IAction action, ISelection selection) { | ||
| 98 | } | ||
| 99 | public void dispose() { | ||
| 100 | } | ||
| 101 | public void init(IWorkbenchWindow window) { | ||
| 102 | } | ||
| 103 | } | ||
| 104 | {{/code}} | ||
| 105 | |||
| 106 | {{code title="MyDataDiagramSynthesis.xtend"}} | ||
| 107 | class MyDataDiagramSynthesis extends AbstractDiagramSynthesis<MyData> { | ||
| 108 | @Inject | ||
| 109 | extension KNodeExtensions | ||
| 110 | @Inject | ||
| 111 | extension KEdgeExtensions | ||
| 112 | @Inject | ||
| 113 | extension KPortExtensions | ||
| 114 | @Inject | ||
| 115 | extension KLabelExtensions | ||
| 116 | @Inject | ||
| 117 | extension KRenderingExtensions | ||
| 118 | @Inject | ||
| 119 | extension KContainerRenderingExtensions | ||
| 120 | @Inject | ||
| 121 | extension KPolylineExtensions | ||
| 122 | @Inject | ||
| 123 | extension KColorExtensions | ||
| 124 | // Some self-defined colors | ||
| 125 | private static val KColor BLUE1 = RENDERING_FACTORY.createKColor() => | ||
| 126 | [it.red = 248; it.green = 249; it.blue = 253]; | ||
| 127 | private static val KColor BLUE2 = RENDERING_FACTORY.createKColor() => | ||
| 128 | [it.red = 205; it.green = 220; it.blue = 243]; | ||
| 129 | // Additional transformation option to hide or show a shadow | ||
| 130 | private static val TransformationOption SHOW_SHADOW = TransformationOption::createCheckOption("Shadow", true); | ||
| 131 | // Add all transformation options (comma separated) | ||
| 132 | override public getTransformationOptions() { | ||
| 133 | return ImmutableSet::of( | ||
| 134 | SHOW_SHADOW | ||
| 135 | ); | ||
| 136 | } | ||
| 137 | override KNode transform(MyData data) { | ||
| 138 | val root = data.createNode() | ||
| 139 | root.putToLookUpWith(data) => [ | ||
| 140 | // Optional Layout parameters can be set | ||
| 141 | //it.addLayoutParam(LayoutOptions::ALGORITHM, "de.cau.cs.kieler.kiml.ogdf.planarization"); | ||
| 142 | //it.addLayoutParam(LayoutOptions::SPACING, 75f); | ||
| 143 | //it.addLayoutParam(LayoutOptions::DIRECTION, Direction::UP); | ||
| 144 | // A rounded rectangle is created for every MyData instance | ||
| 145 | it.addRoundedRectangle(5, 5) => [ | ||
| 146 | // Set linewith, foreground color, and a fading background color | ||
| 147 | it.lineWidth = 1; | ||
| 148 | it.setForeground("darkGray".color) | ||
| 149 | // We need a fresh copy of each color item, because it is contained by its element | ||
| 150 | it.setBackgroundGradient(BLUE1.copy, BLUE2.copy, 90) | ||
| 151 | // Here we see a how to use a boolean transformation/diagram option | ||
| 152 | if (SHOW_SHADOW.optionBooleanValue) { | ||
| 153 | it.shadow = "black".color; | ||
| 154 | } | ||
| 155 | // Set a text | ||
| 156 | it.addText(" " + data.name + " ") => [ | ||
| 157 | it.setFontSize(9) | ||
| 158 | it.setForeground("black".color) | ||
| 159 | ] | ||
| 160 | // If this is a hierarchical MyData instance, then create horizontal splitter, | ||
| 161 | // a child area, and add its children | ||
| 162 | if (data.subData.length > 0) { | ||
| 163 | it.setGridPlacement(1); | ||
| 164 | it.addHorizontalSeperatorLine(1, 2).setForeground("darkGray".color) | ||
| 165 | it.addChildArea().setGridPlacementData() => [ | ||
| 166 | from(LEFT, 3, 0, TOP, 3, 0).to(RIGHT, 3, 0, BOTTOM, 3, 0) | ||
| 167 | minCellHeight = 5; | ||
| 168 | minCellWidth = 5; | ||
| 169 | ]; | ||
| 170 | for (subData : data.subData) { | ||
| 171 | // To the recursive call to transform for all children | ||
| 172 | val child = subData.transform | ||
| 173 | // It is important to add all children to the root! | ||
| 174 | root.children.add(child) | ||
| 175 | } | ||
| 176 | } | ||
| 177 | ] | ||
| 178 | ] | ||
| 179 | return root; | ||
| 180 | } | ||
| 181 | } | ||
| 182 | {{/code}} | ||
| 183 | |||
| 184 | |||
| 185 | |||
| 186 |