-
Notifications
You must be signed in to change notification settings - Fork 4
/
DiagramWithCustomId.java
137 lines (104 loc) · 3.52 KB
/
DiagramWithCustomId.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package com.xored.q7.quality.mockups.draw2d;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.draw2d.FreeformLayer;
import org.eclipse.draw2d.FreeformLayout;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.RectangleFigure;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartFactory;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.eclipse.gef.editparts.FreeformGraphicalRootEditPart;
import org.eclipse.gef.ui.parts.ScrollingGraphicalViewer;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import com.xored.q7.quality.mockups.issues.BaseMockupPart;
public class DiagramWithCustomId extends BaseMockupPart {
@Override
public Control construct(Composite parent) {
Composite content = new Composite(parent, SWT.NONE);
GridDataFactory.fillDefaults().grab(true, true).applyTo(content);
GridLayoutFactory.fillDefaults().applyTo(content);
ScrollingGraphicalViewer viewer = new ScrollingGraphicalViewer();
viewer.createControl(content);
viewer.setRootEditPart(new FreeformGraphicalRootEditPart());
viewer.setEditPartFactory(new CustomEditPartFactory());
ContainerModel container = new ContainerModel("container-01");
ComponentModel component1 = new ComponentModel("component-01", 0, 0);
ComponentModel component2 = new ComponentModel("component-02", 200, 0);
container.components.add(component1);
container.components.add(component2);
viewer.setContents(container);
return content;
}
public class ContainerEditPart extends AbstractGraphicalEditPart {
@Override
protected IFigure createFigure() {
FreeformLayer layer = new FreeformLayer();
layer.setLayoutManager(new FreeformLayout());
layer.setBorder(new LineBorder(1));
return layer;
}
@Override
protected void createEditPolicies() {
}
@Override
protected List<ComponentModel> getModelChildren() {
ContainerModel container = (ContainerModel) getModel();
return container.components;
}
}
public class ComponentEditPart extends AbstractGraphicalEditPart {
@Override
protected IFigure createFigure() {
IFigure rectangle = new RectangleFigure();
rectangle.setBackgroundColor(new Color(null, 230, 230, 255));
return rectangle;
}
@Override
protected void createEditPolicies() {
}
@Override
protected void refreshVisuals() {
ComponentModel component = (ComponentModel) getModel();
Rectangle bounds = new Rectangle(component.x, component.y, 100, 100);
getFigure().setBounds(bounds);
}
}
public class ContainerModel {
public String id;
public List<ComponentModel> components = new ArrayList<ComponentModel>();
public ContainerModel(String id) {
this.id = id;
}
}
public class ComponentModel {
public String id;
public int x;
public int y;
public ComponentModel(String id, int x, int y) {
this.id = id;
this.x = x;
this.y = y;
}
}
public class CustomEditPartFactory implements EditPartFactory {
@Override
public EditPart createEditPart(EditPart context, Object model) {
EditPart editPart = null;
if (model instanceof ContainerModel) {
editPart = new ContainerEditPart();
} else if (model instanceof ComponentModel) {
editPart = new ComponentEditPart();
}
editPart.setModel(model);
return editPart;
}
}
}