View Javadoc

1   /*
2    * This file is a part of CAST project.
3    * (c) Copyright 2007, AGH University of Science & Technology
4    * https://caribou.iisg.agh.edu.pl/trac/cast
5    *
6    * Licensed under the Eclipse Public License, Version 1.0 (the "License").
7    * You may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * http://www.eclipse.org/legal/epl-v10.html
10   */
11  /*
12   * File: Legend.java
13   * Created: 2007-00-00
14   * Author: cast
15   * $Id: Legend.java 3054 2009-07-27 10:23:07Z kpietak $
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   * Diagram legend.
36   *
37   * @author AGH CAST Team
38   */
39  @XStreamAlias("legend")
40  public class Legend extends ModelElement implements IMoveable {
41  
42  	private static final long serialVersionUID = 5735945176078321158L;
43  
44  	/**
45  	 * Name of the <em>Location changed</em> event.
46  	 */
47  	public static final String LOCATION = "Legend.locationChangedEvent"; //$NON-NLS-1$
48  
49  	private static final int POSITION_TOP = 10;
50  
51  	private static final int POSITION_RIGHT = 500;
52  
53  	/**
54  	 * Location of the legend.
55  	 */
56  	@XStreamAlias("location")
57  	protected Point location = new Point(2, 2);
58  
59  	/**
60  	 * Legend's {@link AttributeManager}.
61  	 */
62  	@XStreamAlias("legendAttributes")
63  	@XStreamConverter(AttributeManagerConverter.class)
64  	private LegendAttributeManager attributeManager;
65  
66  	/**
67  	 * Creates new Legend for given diagram.
68  	 *
69  	 * @param diagramName
70  	 *            the name of the diagram.
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  	 * Returns label for the given image.
91  	 *
92  	 * @param imageId
93  	 *            id of the image
94  	 * @return label for the image
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 	 * Returns label for the given line color.
110 	 *
111 	 * @param color
112 	 *            line color
113 	 * @return label for the line color
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 	 * {@inheritDoc}
123 	 *
124 	 * @see pl.edu.agh.cast.model.visual.backward.IMoveable#setLocation(org.eclipse.draw2d.geometry.Point)
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 	 * Sets the name of the {@link IDiagram} this legend is for.
138 	 *
139 	 * @param diagramName
140 	 *            name of the diagram
141 	 */
142 	public void setDiagramName(String diagramName) {
143 		setAttributeValue(LegendAttributeManager.PERMANENT_LABEL, diagramName);
144 	}
145 
146 	/**
147 	 * {@inheritDoc}
148 	 *
149 	 * @see pl.edu.agh.cast.model.visual.backward.ModelElement#getAttributeManager()
150 	 */
151 	@Override
152 	public AttributeManager getAttributeManager() {
153 		return attributeManager;
154 	}
155 
156 }