1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package pl.edu.agh.cast.backward.figure;
19
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 import org.eclipse.draw2d.ColorConstants;
27 import org.eclipse.draw2d.Figure;
28 import org.eclipse.draw2d.FrameBorder;
29 import org.eclipse.draw2d.Label;
30 import org.eclipse.draw2d.MarginBorder;
31 import org.eclipse.draw2d.RectangleFigure;
32 import org.eclipse.draw2d.ToolbarLayout;
33 import org.eclipse.swt.graphics.Color;
34
35 import pl.edu.agh.cast.common.HexColor;
36 import pl.edu.agh.cast.editpart.LegendEditPart;
37 import pl.edu.agh.cast.model.visual.backward.ConnectionGroup;
38 import pl.edu.agh.cast.model.visual.backward.Diagram;
39 import pl.edu.agh.cast.model.visual.backward.Legend;
40 import pl.edu.agh.cast.model.visual.backward.Node;
41 import pl.edu.agh.cast.util.Images;
42
43
44
45
46
47
48 public class LegendFigure extends Figure {
49
50
51
52
53 protected LegendEditPart controller;
54
55
56
57
58 protected Legend legend;
59
60 private FrameBorder legendBorder;
61
62
63
64
65 private Map<String, Figure> imageFigures;
66
67
68
69
70 private Map<HexColor, Figure> colorFigures;
71
72
73
74
75
76
77
78
79
80 public LegendFigure(Legend legend, LegendEditPart schemaLegendEditPart) {
81 super();
82 this.legend = legend;
83 this.controller = schemaLegendEditPart;
84
85 init();
86 }
87
88
89
90
91 public void init() {
92 imageFigures = new HashMap<String, Figure>();
93 colorFigures = new HashMap<HexColor, Figure>();
94 this.removeAll();
95 ToolbarLayout layout = new ToolbarLayout();
96 layout.setStretchMinorAxis(false);
97 layout.setMinorAlignment(ToolbarLayout.ALIGN_TOPLEFT);
98 setLayoutManager(layout);
99
100
101 Diagram model = (Diagram)(controller.getParent().getModel());
102 List<String> imageIds = new ArrayList<String>();
103 List<HexColor> colorIds = new ArrayList<HexColor>();
104 for (Node node : model.getNodes()) {
105 if (!imageIds.contains(node.getImageId())) {
106 imageIds.add(node.getImageId());
107 }
108 for (ConnectionGroup connectionGroup : node.getConnectionGroups()) {
109 HexColor lineColor = (HexColor)connectionGroup.getAttributeValue(ConnectionGroup.LINE_COLOR).getValue();
110 if (!colorIds.contains(lineColor)) {
111 colorIds.add(lineColor);
112 }
113 }
114 }
115
116 setIconsLegend(imageIds);
117 setColorsLegend(colorIds);
118
119 legendBorder = new FrameBorder(legend.getDiagramName());
120 this.setBackgroundColor(ColorConstants.listBackground);
121 this.setOpaque(true);
122 this.setBorder(legendBorder);
123 }
124
125 private void setColorsLegend(List<HexColor> colorIds) {
126 for (HexColor colorId : colorIds) {
127 addColoredLineFigure(colorId);
128 }
129
130 }
131
132 private void addColoredLineFigure(HexColor colorId) {
133 int lineWidth = 40;
134 int lineThickness = 3;
135 int marginSize = 5;
136
137 Figure labeledColoredLine = new Figure();
138 ToolbarLayout imageLayout = new ToolbarLayout(true);
139 imageLayout.setStretchMinorAxis(false);
140 imageLayout.setMinorAlignment(ToolbarLayout.ALIGN_CENTER);
141 labeledColoredLine.setLayoutManager(imageLayout);
142 RectangleFigure colorLineFigure = new RectangleFigure();
143 colorLineFigure.setSize(lineWidth, lineThickness);
144
145 colorLineFigure.setForegroundColor(new Color(null, colorId.getRed(), colorId.getGreen(), colorId.getBlue()));
146 colorLineFigure.setBackgroundColor(new Color(null, colorId.getRed(), colorId.getGreen(), colorId.getBlue()));
147
148 RectangleFigure marginFigure = new RectangleFigure();
149 marginFigure.setSize(marginSize, lineThickness);
150 marginFigure.setForegroundColor(ColorConstants.listBackground);
151 marginFigure.setBackgroundColor(ColorConstants.listBackground);
152
153 Label imageLabel = null;
154 imageLabel = new Label(legend.getLineColorLabel(colorId));
155 imageLabel.setOpaque(true);
156
157 labeledColoredLine.add(colorLineFigure);
158 labeledColoredLine.add(marginFigure);
159
160 labeledColoredLine.add(imageLabel);
161 labeledColoredLine.setOpaque(true);
162 MarginBorder colorLineBorder = new MarginBorder(marginSize);
163 labeledColoredLine.setBorder(colorLineBorder);
164
165 Figure oldColor = colorFigures.get(colorId);
166 if (oldColor != null) {
167 this.remove(oldColor);
168 colorFigures.remove(oldColor);
169 }
170 add(labeledColoredLine);
171 colorFigures.put(colorId, labeledColoredLine);
172 }
173
174 private void setIconsLegend(List<String> imageIds) {
175
176 if (imageIds.contains(null)) {
177 imageIds.remove(null);
178 if (!imageIds.contains(Images.ENTITY_BIG)) {
179 imageIds.add(Images.ENTITY_BIG);
180 }
181 }
182
183 Collections.sort(imageIds);
184
185 for (String imageId : imageIds) {
186 addImageFigure(imageId);
187 }
188
189 }
190
191 private void addImageFigure(String imageId) {
192 Figure labeledImageFigure = new Figure();
193 ToolbarLayout imageLayout = new ToolbarLayout(true);
194 imageLayout.setStretchMinorAxis(false);
195 imageLayout.setMinorAlignment(ToolbarLayout.ALIGN_CENTER);
196 labeledImageFigure.setLayoutManager(imageLayout);
197 ImageFigure icon = new ImageFigure(imageId);
198 Label imageLabel = null;
199 if (imageId != null) {
200 imageLabel = new Label(legend.getImageLabel(imageId));
201 }
202 if (imageLabel != null) {
203 imageLabel.setOpaque(true);
204 icon.setOpaque(true);
205 labeledImageFigure.add(icon);
206 labeledImageFigure.add(imageLabel);
207 labeledImageFigure.setOpaque(true);
208
209 Figure oldImage = imageFigures.get(imageId);
210 if (oldImage != null) {
211 this.remove(oldImage);
212 imageFigures.remove(oldImage);
213 }
214 add(labeledImageFigure);
215 imageFigures.put(imageId, labeledImageFigure);
216 }
217 }
218
219 public LegendEditPart getController() {
220 return controller;
221 }
222 }