-
Notifications
You must be signed in to change notification settings - Fork 45
/
StylesFactory.java
195 lines (182 loc) · 10.6 KB
/
StylesFactory.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*******************************************************************************
* Copyright (c) 2021, 2023 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.components.view.emf.diagram;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import org.eclipse.sirius.components.core.api.IObjectService;
import org.eclipse.sirius.components.diagrams.ArrowStyle;
import org.eclipse.sirius.components.diagrams.EdgeStyle;
import org.eclipse.sirius.components.diagrams.INodeStyle;
import org.eclipse.sirius.components.diagrams.IconLabelNodeStyle;
import org.eclipse.sirius.components.diagrams.LineStyle;
import org.eclipse.sirius.components.diagrams.NodeType;
import org.eclipse.sirius.components.diagrams.RectangularNodeStyle;
import org.eclipse.sirius.components.diagrams.description.LabelStyleDescription;
import org.eclipse.sirius.components.representations.VariableManager;
import org.eclipse.sirius.components.view.FixedColor;
import org.eclipse.sirius.components.view.diagram.IconLabelNodeStyleDescription;
import org.eclipse.sirius.components.view.diagram.NodeStyleDescription;
import org.eclipse.sirius.components.view.diagram.RectangularNodeStyleDescription;
import org.eclipse.sirius.components.view.emf.ViewConverter;
/**
* Factory to create the basic style descriptions used by {@link ViewConverter}.
*
* @author pcdavid
* @author lfasani
*/
public final class StylesFactory {
private static final String DEFAULT_COLOR = "black";
private final List<INodeStyleProvider> iNodeStyleProviders;
private final IObjectService objectService;
public StylesFactory(List<INodeStyleProvider> iNodeStyleProviders, IObjectService objectService) {
this.iNodeStyleProviders = Objects.requireNonNull(iNodeStyleProviders);
this.objectService = Objects.requireNonNull(objectService);
}
public LabelStyleDescription createLabelStyleDescription(NodeStyleDescription nodeStyle) {
// @formatter:off
return LabelStyleDescription.newLabelStyleDescription()
.colorProvider(variableManager -> Optional.ofNullable(nodeStyle.getLabelColor())
.filter(FixedColor.class::isInstance)
.map(FixedColor.class::cast)
.map(FixedColor::getValue)
.orElse(DEFAULT_COLOR))
.fontSizeProvider(variableManager -> nodeStyle.getFontSize())
.boldProvider(variableManager -> nodeStyle.isBold())
.italicProvider(variableManager -> nodeStyle.isItalic())
.underlineProvider(variableManager -> nodeStyle.isUnderline())
.strikeThroughProvider(variableManager -> nodeStyle.isStrikeThrough())
.iconURLProvider(variableManager -> {
String iconURL = "";
if (nodeStyle.isShowIcon()) {
iconURL = variableManager.get(VariableManager.SELF, Object.class).map(this.objectService::getImagePath).orElse("");
}
return iconURL;
})
.build();
// @formatter:on
}
public LabelStyleDescription createEdgeLabelStyleDescription(org.eclipse.sirius.components.view.diagram.EdgeStyle edgeStyle) {
// @formatter:off
return LabelStyleDescription.newLabelStyleDescription()
.colorProvider(variableManager -> Optional.ofNullable(edgeStyle.getColor())
.filter(FixedColor.class::isInstance)
.map(FixedColor.class::cast)
.map(FixedColor::getValue)
.orElse(DEFAULT_COLOR))
.fontSizeProvider(variableManager -> edgeStyle.getFontSize())
.boldProvider(variableManager -> edgeStyle.isBold())
.italicProvider(variableManager -> edgeStyle.isItalic())
.underlineProvider(variableManager -> edgeStyle.isUnderline())
.strikeThroughProvider(variableManager -> edgeStyle.isStrikeThrough())
.iconURLProvider(variableManager -> {
String iconURL = "";
if (edgeStyle.isShowIcon()) {
iconURL = variableManager.get(VariableManager.SELF, Object.class).map(this.objectService::getImagePath).orElse("");
}
return iconURL;
})
.build();
// @formatter:on
}
public EdgeStyle createEdgeStyle(org.eclipse.sirius.components.view.diagram.EdgeStyle edgeStyle) {
// @formatter:off
return EdgeStyle.newEdgeStyle()
.color(Optional.ofNullable(edgeStyle.getColor())
.filter(FixedColor.class::isInstance)
.map(FixedColor.class::cast)
.map(FixedColor::getValue)
.orElse(DEFAULT_COLOR))
.lineStyle(LineStyle.valueOf(edgeStyle.getLineStyle().getLiteral()))
.size(edgeStyle.getEdgeWidth())
.sourceArrow(ArrowStyle.valueOf(edgeStyle.getSourceArrowStyle().getLiteral()))
.targetArrow(ArrowStyle.valueOf(edgeStyle.getTargetArrowStyle().getLiteral()))
.build();
// @formatter:on
}
public String getNodeType(NodeStyleDescription nodeStyleDescription) {
Optional<String> type = Optional.empty();
if (nodeStyleDescription instanceof RectangularNodeStyleDescription) {
type = Optional.of(NodeType.NODE_RECTANGLE);
} else if (nodeStyleDescription instanceof IconLabelNodeStyleDescription) {
type = Optional.of(NodeType.NODE_ICON_LABEL);
} else {
for (INodeStyleProvider iNodeStyleProvider : this.iNodeStyleProviders) {
Optional<String> nodeType = iNodeStyleProvider.getNodeType(nodeStyleDescription);
if (nodeType.isPresent()) {
type = Optional.of(nodeType.get());
break;
}
}
}
return type.orElse(NodeType.NODE_RECTANGLE);
}
public INodeStyle createNodeStyle(NodeStyleDescription nodeStyle, Optional<String> optionalEditingContextId) {
INodeStyle result = null;
switch (this.getNodeType(nodeStyle)) {
case NodeType.NODE_ICON_LABEL:
result = IconLabelNodeStyle.newIconLabelNodeStyle().backgroundColor("transparent").build();
break;
case NodeType.NODE_RECTANGLE:
// @formatter:off
result = RectangularNodeStyle.newRectangularNodeStyle()
.withHeader(((RectangularNodeStyleDescription) nodeStyle).isWithHeader())
.color(Optional.ofNullable(nodeStyle.getColor())
.filter(FixedColor.class::isInstance)
.map(FixedColor.class::cast)
.map(FixedColor::getValue)
.orElse(DEFAULT_COLOR))
.borderColor(Optional.ofNullable(nodeStyle.getBorderColor())
.filter(FixedColor.class::isInstance)
.map(FixedColor.class::cast)
.map(FixedColor::getValue)
.orElse(""))
.borderSize(nodeStyle.getBorderSize())
.borderStyle(LineStyle.valueOf(nodeStyle.getBorderLineStyle().getLiteral()))
.borderRadius(nodeStyle.getBorderRadius())
.build();
break;
// @formatter:on
default:
for (INodeStyleProvider iNodeStyleProvider : this.iNodeStyleProviders) {
Optional<INodeStyle> nodeStyleOpt = iNodeStyleProvider.createNodeStyle(nodeStyle, optionalEditingContextId);
if (nodeStyleOpt.isPresent()) {
result = nodeStyleOpt.get();
break;
}
}
}
if (result == null) {
// @formatter:off
result = RectangularNodeStyle.newRectangularNodeStyle()
.withHeader(true)
.color(Optional.ofNullable(nodeStyle.getColor())
.filter(FixedColor.class::isInstance)
.map(FixedColor.class::cast)
.map(FixedColor::getValue)
.orElse(DEFAULT_COLOR))
.borderColor(Optional.ofNullable(nodeStyle.getBorderColor())
.filter(FixedColor.class::isInstance)
.map(FixedColor.class::cast)
.map(FixedColor::getValue)
.orElse(""))
.borderSize(nodeStyle.getBorderSize())
.borderStyle(LineStyle.valueOf(nodeStyle.getBorderLineStyle().getLiteral()))
.borderRadius(nodeStyle.getBorderRadius())
.build();
// @formatter:on
}
return result;
}
// CHECKSTYLE:ON
}