1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package pl.edu.agh.cast.model.visual.backward;
19
20 import org.eclipse.draw2d.geometry.Point;
21 import org.eclipse.osgi.util.NLS;
22
23 import pl.edu.agh.cast.backward.figure.icons.NodeIconProvider;
24 import pl.edu.agh.cast.backward.resources.xml.AttributeManagerConverter;
25 import pl.edu.agh.cast.common.HexColor;
26 import pl.edu.agh.cast.model.attributes.AttributeManager;
27 import pl.edu.agh.cast.model.attributes.LegendAttributeManager;
28 import pl.edu.agh.cast.model.attributes.ValueType;
29 import pl.edu.agh.cast.util.Messages;
30
31 import com.thoughtworks.xstream.annotations.XStreamAlias;
32 import com.thoughtworks.xstream.annotations.XStreamConverter;
33
34
35
36
37
38
39 @XStreamAlias("legend")
40 public class Legend extends ModelElement implements IMoveable {
41
42 private static final long serialVersionUID = 5735945176078321158L;
43
44
45
46
47 public static final String LOCATION = "Legend.locationChangedEvent";
48
49 private static final int POSITION_TOP = 10;
50
51 private static final int POSITION_RIGHT = 500;
52
53
54
55
56 @XStreamAlias("location")
57 protected Point location = new Point(2, 2);
58
59
60
61
62 @XStreamAlias("legendAttributes")
63 @XStreamConverter(AttributeManagerConverter.class)
64 private LegendAttributeManager attributeManager;
65
66
67
68
69
70
71
72 public Legend(String diagramName) {
73 super();
74 attributeManager = new LegendAttributeManager();
75 location = new Point(POSITION_RIGHT, POSITION_TOP);
76
77 bindToAttributeManager();
78 setDiagramName(diagramName);
79 }
80
81 public String getDiagramName() {
82 return (String)getAttributeValue(LegendAttributeManager.PERMANENT_LABEL).getValue();
83 }
84
85 public Point getLocation() {
86 return location;
87 }
88
89
90
91
92
93
94
95
96 public String getImageLabel(String imageId) {
97 createOrUpdateProperty(imageId, imageId);
98 return (String)getAttributeValue(imageId).getValue();
99 }
100
101 private void createOrUpdateProperty(String propertyName, String propertyValue) {
102 if (!attributeManager.isRegisteredId(propertyName)) {
103 attributeManager.registerAttribute(propertyName, ValueType.String);
104 setAttributeValue(propertyName, NodeIconProvider.getInstance().getFriendlyName(propertyName));
105 }
106 }
107
108
109
110
111
112
113
114
115 public String getLineColorLabel(HexColor color) {
116 String colorLabelName = NLS.bind(Messages.ColorLine_Label, color.toString());
117 createOrUpdateProperty(colorLabelName, colorLabelName);
118 return (String)getAttributeValue(colorLabelName).getValue();
119 }
120
121
122
123
124
125
126 public void setLocation(Point newLocation) {
127 Point oldLocation = location;
128 location = newLocation;
129 firePropertyChange(Legend.LOCATION, oldLocation, newLocation);
130 }
131
132 public String getLabel() {
133 return getDiagramName();
134 }
135
136
137
138
139
140
141
142 public void setDiagramName(String diagramName) {
143 setAttributeValue(LegendAttributeManager.PERMANENT_LABEL, diagramName);
144 }
145
146
147
148
149
150
151 @Override
152 public AttributeManager getAttributeManager() {
153 return attributeManager;
154 }
155
156 }