root/draw2d/UmlExample.d

Revision 142:7dca96709d29, 6.8 kB (checked in by Frank Benoit <benoit@tionex.de>, 4 years ago)

Fix: remove trigger of Display creation in static ctors

Line 
1 /++
2  + Original sources from http://www.eclipse.org/articles/Article-GEF-Draw2d/GEF-Draw2d.html
3  + Ported to the D programming language
4  +     Frank Benoit <benoit@tionex.de>
5  +/
6
7 module umlexample.UmlExample;
8
9 import dwt.dwthelper.utils;
10 import dwt.dwthelper.ByteArrayInputStream;
11
12 import dwtx.draw2d.Figure;
13 import dwtx.draw2d.IFigure;
14 import dwtx.draw2d.geometry.PointList;
15 import dwtx.draw2d.geometry.Rectangle;
16 import dwtx.draw2d.geometry.Insets;
17 import dwt.DWT;
18 import dwt.graphics.Font;
19 import dwt.graphics.Image;
20 import dwt.graphics.ImageData;
21 import dwt.widgets.Display;
22 import dwt.widgets.Shell;
23 import dwtx.draw2d.AbstractBorder;
24 import dwtx.draw2d.Graphics;
25 import dwt.graphics.Color;
26 import dwtx.draw2d.Label;
27 import dwtx.draw2d.ToolbarLayout;
28 import dwtx.draw2d.LineBorder;
29 import dwtx.draw2d.ConnectionEndpointLocator;
30 import dwtx.draw2d.ChopboxAnchor;
31 import dwtx.draw2d.PolygonDecoration;
32 import dwtx.draw2d.PolylineConnection;
33 import dwtx.draw2d.ColorConstants;
34 import dwtx.draw2d.LightweightSystem;
35 import dwtx.draw2d.XYLayout;
36
37 version(JIVE) import jive.stacktrace;
38
39 public class CompartmentFigure : Figure {
40
41     public this() {
42         ToolbarLayout layout = new ToolbarLayout();
43         layout.setMinorAlignment(ToolbarLayout.ALIGN_TOPLEFT);
44         layout.setStretchMinorAxis(false);
45         layout.setSpacing(2);
46         setLayoutManager(layout);
47         setBorder(new CompartmentFigureBorder());
48     }
49
50     public class CompartmentFigureBorder : AbstractBorder {
51         public Insets getInsets(IFigure figure) {
52             return new Insets(1,0,0,0);
53         }
54         public void paint(IFigure figure, Graphics graphics, Insets insets) {
55             graphics.drawLine(getPaintRectangle(figure, insets).getTopLeft(),
56                     tempRect.getTopRight());
57         }
58     }
59 }
60
61 public class UMLClassFigure : Figure {
62     private static Color classColor_;
63     public static Color classColor(){
64         if( classColor_ is null ){
65             classColor_ = new Color(null,255,255,206);
66         }
67         return classColor_;
68     }
69
70     private CompartmentFigure attributeFigure;
71     private CompartmentFigure methodFigure;
72
73     public this(Label name) {
74
75         attributeFigure = new CompartmentFigure();
76         methodFigure = new CompartmentFigure();
77
78         ToolbarLayout layout = new ToolbarLayout();
79         setLayoutManager(layout);
80         setBorder(new LineBorder(ColorConstants.black,1));
81         setBackgroundColor(classColor);
82         setOpaque(true);
83
84         add(name);
85         add(attributeFigure);
86         add(methodFigure);
87     }
88     public CompartmentFigure getAttributesCompartment() {
89         return attributeFigure;
90     }
91     public CompartmentFigure getMethodsCompartment() {
92         return methodFigure;
93     }
94 }
95
96 static void[] imgDataClass = import("draw2d.umlexample.class.gif");
97 static void[] imgDataMethod = import("draw2d.umlexample.method.gif");
98 static void[] imgDataField = import("draw2d.umlexample.field.gif");
99
100 Image getImage( Display disp, void[] data ){
101     ImageData imageData = new ImageData( new ByteArrayInputStream( cast(byte[]) data ));
102     return new Image( disp, imageData );
103 }
104
105 /**
106  * A test class to display a UMLFigure
107  */
108 public static void main(){
109     Display d = new Display();
110     final Shell shell = new Shell(d);
111     shell.setSize(400, 400);
112     shell.setText("UMLClassFigure Test");
113     LightweightSystem lws = new LightweightSystem(shell);
114     Figure contents = new Figure();
115     XYLayout contentsLayout = new XYLayout();
116     contents.setLayoutManager(contentsLayout);
117
118     Font classFont = new Font(null, "Arial", 12, DWT.BOLD);
119     Label classLabel1 = new Label("Table", getImage(d, imgDataClass));
120     classLabel1.setFont(classFont);
121
122     Label classLabel2 = new Label("Column", getImage(d, imgDataClass));
123     classLabel2.setFont(classFont);
124
125     final UMLClassFigure classFigure = new UMLClassFigure(classLabel1);
126     final UMLClassFigure classFigure2 = new UMLClassFigure(classLabel2);
127
128     Label attribute1 = new Label("columns: Column[]", getImage(d, imgDataField));
129     Label attribute2 = new Label("rows: Row[]", getImage(d, imgDataField));
130     Label attribute3 = new Label("columnID: int", getImage(d, imgDataField));
131     Label attribute4 = new Label("items: List", getImage(d, imgDataField));
132
133     classFigure.getAttributesCompartment().add(attribute1);
134     classFigure.getAttributesCompartment().add(attribute2);
135     classFigure2.getAttributesCompartment().add(attribute3);
136     classFigure2.getAttributesCompartment().add(attribute4);
137
138     Label method1 = new Label("getColumns(): Column[]", getImage(d, imgDataMethod));
139     Label method2 = new Label("getRows(): Row[]", getImage(d, imgDataMethod));
140     Label method3 = new Label("getColumnID(): int", getImage(d, imgDataMethod));
141     Label method4 = new Label("getItems(): List", getImage(d, imgDataMethod));
142
143     classFigure.getMethodsCompartment().add(method1);
144     classFigure.getMethodsCompartment().add(method2);
145     classFigure2.getMethodsCompartment().add(method3);
146     classFigure2.getMethodsCompartment().add(method4);
147
148     contentsLayout.setConstraint(classFigure, new Rectangle(10,10,-1,-1));
149     contentsLayout.setConstraint(classFigure2, new Rectangle(200, 200, -1, -1));
150
151     /* Creating the connection */
152     PolylineConnection c = new PolylineConnection();
153     ChopboxAnchor sourceAnchor = new ChopboxAnchor(classFigure);
154     ChopboxAnchor targetAnchor = new ChopboxAnchor(classFigure2);
155     c.setSourceAnchor(sourceAnchor);
156     c.setTargetAnchor(targetAnchor);
157
158     /* Creating the decoration */
159     PolygonDecoration decoration = new PolygonDecoration();
160     PointList decorationPointList = new PointList();
161     decorationPointList.addPoint(0,0);
162     decorationPointList.addPoint(-2,2);
163     decorationPointList.addPoint(-4,0);
164     decorationPointList.addPoint(-2,-2);
165     decoration.setTemplate(decorationPointList);
166     c.setSourceDecoration(decoration);
167
168     /* Adding labels to the connection */
169     ConnectionEndpointLocator targetEndpointLocator =
170         new ConnectionEndpointLocator(c, true);
171     targetEndpointLocator.setVDistance(15);
172     Label targetMultiplicityLabel = new Label("1..*");
173     c.add(targetMultiplicityLabel, targetEndpointLocator);
174
175     ConnectionEndpointLocator sourceEndpointLocator =
176         new ConnectionEndpointLocator(c, false);
177     sourceEndpointLocator.setVDistance(15);
178     Label sourceMultiplicityLabel = new Label("1");
179     c.add(sourceMultiplicityLabel, sourceEndpointLocator);
180
181     ConnectionEndpointLocator relationshipLocator =
182         new ConnectionEndpointLocator(c,true);
183     relationshipLocator.setUDistance(10);
184     relationshipLocator.setVDistance(-20);
185     Label relationshipLabel = new Label("contains");
186     c.add(relationshipLabel,relationshipLocator);
187
188     contents.add(classFigure);
189     contents.add(classFigure2);
190     contents.add(c);
191
192     lws.setContents(contents);
193     shell.open();
194     while (!shell.isDisposed())
195         while (!d.readAndDispatch())
196             d.sleep();
197 }
Note: See TracBrowser for help on using the browser.