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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
/*
* Copyright (c) 2004 - 2011 Eike Stepper (Berlin, Germany) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Martin Fluegge - initial API and implementation
*
*/
package org.eclipse.emf.cdo.dawn.examples.acore.graphiti.features;
import org.eclipse.emf.cdo.dawn.examples.acore.ABasicClass;
import org.eclipse.emf.cdo.dawn.examples.acore.graphiti.util.DawnGraphitiAcoreResourceUtil;
import org.eclipse.graphiti.features.IDirectEditingInfo;
import org.eclipse.graphiti.features.IFeatureProvider;
import org.eclipse.graphiti.features.context.IAddContext;
import org.eclipse.graphiti.features.impl.AbstractAddShapeFeature;
import org.eclipse.graphiti.mm.algorithms.Polyline;
import org.eclipse.graphiti.mm.algorithms.Rectangle;
import org.eclipse.graphiti.mm.algorithms.RoundedRectangle;
import org.eclipse.graphiti.mm.algorithms.Text;
import org.eclipse.graphiti.mm.algorithms.styles.Color;
import org.eclipse.graphiti.mm.algorithms.styles.Orientation;
import org.eclipse.graphiti.mm.pictograms.BoxRelativeAnchor;
import org.eclipse.graphiti.mm.pictograms.ContainerShape;
import org.eclipse.graphiti.mm.pictograms.Diagram;
import org.eclipse.graphiti.mm.pictograms.PictogramElement;
import org.eclipse.graphiti.mm.pictograms.Shape;
import org.eclipse.graphiti.services.Graphiti;
import org.eclipse.graphiti.services.IGaService;
import org.eclipse.graphiti.services.IPeCreateService;
/**
* @author Martin Fluegge
*/
public abstract class AcoreBasicAddElementFeature extends AbstractAddShapeFeature
{
public AcoreBasicAddElementFeature(IFeatureProvider fp)
{
super(fp);
}
public abstract boolean canAdd(IAddContext context);
protected abstract Color getBackgroundColor();
protected abstract Color getForegroundColor();
public PictogramElement add(IAddContext context)
{
ABasicClass addedClass = (ABasicClass)context.getNewObject();
Diagram targetDiagram = (Diagram)context.getTargetContainer();
// CONTAINER SHAPE WITH ROUNDED RECTANGLE
IPeCreateService peCreateService = Graphiti.getPeCreateService();
// define a default size for the shape
int width = 100;
int height = 50;
IGaService gaService = Graphiti.getGaService();
addToResourceIfNeeded(addedClass);
ContainerShape containerShape = createContainerShape(context, addedClass, targetDiagram, peCreateService, width,
height, gaService);
return containerShape;
}
private ContainerShape createContainerShape(IAddContext context, ABasicClass addedClass, Diagram targetDiagram,
IPeCreateService peCreateService, int width, int height, IGaService gaService)
{
ContainerShape containerShape = peCreateService.createContainerShape(targetDiagram, true);
// create and set graphics algorithm
RoundedRectangle roundedRectangle = gaService.createRoundedRectangle(containerShape, 5, 5);
roundedRectangle.setForeground(getForegroundColor());
roundedRectangle.setBackground(getBackgroundColor());
roundedRectangle.setLineWidth(2);
gaService.setLocationAndSize(roundedRectangle, context.getX(), context.getY(), width, height);
// create link and wire it
link(containerShape, addedClass);
// SHAPE WITH LINE
{
// create shape for line
Shape shape = peCreateService.createShape(containerShape, false);
// create and set graphics algorithm
Polyline polyline = gaService.createPolyline(shape, new int[] { 0, 20, width, 20 });
polyline.setForeground(getForegroundColor());
polyline.setLineWidth(2);
}
// SHAPE WITH TEXT
{
// create shape for text
final Shape shape = peCreateService.createShape(containerShape, false);
// create and set text graphics algorithm
final Text text = gaService.createText(shape, addedClass.getName());
// text.setStyle(StyleUtil.getStyleForAClassText(getDiagram()));
text.setHorizontalAlignment(Orientation.ALIGNMENT_CENTER);
text.setVerticalAlignment(Orientation.ALIGNMENT_CENTER);
gaService.setLocationAndSize(text, 0, 0, width, 20);
// create link and wire it
link(shape, addedClass);
activateDirectEditing(containerShape, shape, text);
}
// add a chopbox anchor to the shape
peCreateService.createChopboxAnchor(containerShape);
// create an additional box relative anchor at middle-right
final BoxRelativeAnchor boxAnchor = peCreateService.createBoxRelativeAnchor(containerShape);
boxAnchor.setRelativeWidth(1.0);
boxAnchor.setRelativeHeight(0.5);
// anchor references visible rectangle instead of invisible rectangle
boxAnchor.setReferencedGraphicsAlgorithm(roundedRectangle);
// assign a graphics algorithm for the box relative anchor
Rectangle rectangle = gaService.createRectangle(boxAnchor);
rectangle.setFilled(true);
// anchor is located on the right border of the visible rectangle
// and touches the border of the invisible rectangle
int w = 6;
gaService.setLocationAndSize(rectangle, -2 * w, -w, 2 * w, 2 * w);
rectangle.setForeground(getForegroundColor());
rectangle.setBackground(getBackgroundColor());
// call the layout feature
layoutPictogramElement(containerShape);
return containerShape;
}
private void activateDirectEditing(ContainerShape containerShape, final Shape shape, final Text text)
{
// provide information to support direct-editing directly
// after object creation (must be activated additionally)
final IDirectEditingInfo directEditingInfo = getFeatureProvider().getDirectEditingInfo();
// set container shape for direct editing after object creation
directEditingInfo.setMainPictogramElement(containerShape);
// set shape and graphics algorithm where the editor for
// direct editing shall be opened after object creation
directEditingInfo.setPictogramElement(shape);
directEditingInfo.setGraphicsAlgorithm(text);
}
private void addToResourceIfNeeded(ABasicClass addedClass)
{
// if added Class has no resource we add it to the resource
// of the diagram
// in a real scenario the business model would have its own resource
if (addedClass.eResource() == null)
{
// getDiagram().eResource().getContents().add(addedClass);
DawnGraphitiAcoreResourceUtil.addToModelResource(addedClass, getDiagram().eResource().getResourceSet());
}
}
}
|