001: package org.drools.eclipse.editors.rete.model;
002:
003: import java.util.ArrayList;
004: import java.util.List;
005:
006: import org.drools.spi.Constraint;
007: import org.eclipse.draw2d.ColorConstants;
008: import org.eclipse.draw2d.geometry.Dimension;
009: import org.eclipse.draw2d.geometry.Point;
010: import org.eclipse.swt.graphics.Color;
011: import org.eclipse.ui.views.properties.IPropertyDescriptor;
012: import org.eclipse.ui.views.properties.TextPropertyDescriptor;
013:
014: /**
015: * Visual vertex representation for ReteGraph.
016: *
017: * Base impl for all rete vertices.
018: *
019: * Can be connected with another BaseVertex with a Connector.
020: *
021: * @author Ahti Kitsik
022: *
023: */
024: abstract public class GraphicalVertex extends ModelElement {
025:
026: private static final String VERTEX_NAME = "BaseVertex ";
027:
028: private static final String WIDTH = "Width";
029:
030: private static final String HEIGHT = "Height";
031:
032: private static IPropertyDescriptor[] descriptors;
033:
034: /** ID for the Height property value (used for by the corresponding property descriptor). */
035: private static final String HEIGHT_PROP = VERTEX_NAME + "."
036: + HEIGHT;
037:
038: /** ID for the Width property value (used for by the corresponding property descriptor). */
039: private static final String WIDTH_PROP = VERTEX_NAME + "." + WIDTH;
040:
041: /** Property ID to use when the location of this shape is modified. */
042: public static final String LOCATION_PROP = VERTEX_NAME
043: + ".Location";
044:
045: /** Property ID to use then the size of this shape is modified. */
046: public static final String SIZE_PROP = VERTEX_NAME + ".Size";
047:
048: /** Property ID to use when the list of outgoing connections is modified. */
049: public static final String SOURCE_CONNECTIONS_PROP = VERTEX_NAME
050: + ".SourceConn";
051:
052: /** Property ID to use when the list of incoming connections is modified. */
053: public static final String TARGET_CONNECTIONS_PROP = VERTEX_NAME
054: + ".TargetConn";
055:
056: /** ID for the X property value (used for by the corresponding property descriptor). */
057: private static final String XPOS_PROP = VERTEX_NAME + ".xPos";
058:
059: /** ID for the Y property value (used for by the corresponding property descriptor). */
060: private static final String YPOS_PROP = VERTEX_NAME + ".yPos";
061:
062: /*
063: * Initializes the property descriptors array.
064: * @see #getPropertyDescriptors()
065: * @see #getPropertyValue(Object)
066: * @see #setPropertyValue(Object, Object)
067: */
068: static {
069: descriptors = new IPropertyDescriptor[] {
070: new TextPropertyDescriptor(XPOS_PROP, "X"), // id and description pair
071: new TextPropertyDescriptor(YPOS_PROP, "Y"),
072: new TextPropertyDescriptor(WIDTH_PROP, WIDTH),
073: new TextPropertyDescriptor(HEIGHT_PROP, HEIGHT), };
074: } // static
075:
076: /** Location of this vertex. */
077: private Point location = new Point(0, 0);
078: /** Size of this vertex. */
079: private final static Dimension size = new Dimension(16, 16);
080: /** List of outgoing Connections. */
081: private List sourceConnections = new ArrayList();
082: /** List of incoming Connections. */
083: private List targetConnections = new ArrayList();
084:
085: /**
086: * HTML formatted representation of this node
087: *
088: * @return #getHtml
089: */
090: abstract public String getHtml();
091:
092: /**
093: * Color used for filling vertex figure
094: *
095: * @return color
096: */
097: abstract public Color getFillColor();
098:
099: /**
100: * Add an incoming or outgoing connection to this vertex.
101: * @param conn a non-null connection instance
102: * @throws IllegalArgumentException if the connection is null or has not distinct endpoints
103: */
104: public void addConnection(Connection conn) {
105: if (conn == null || conn.getSource() == conn.getTarget()) {
106: throw new IllegalArgumentException();
107: }
108: if (conn.getSource() == this ) {
109: sourceConnections.add(conn);
110: firePropertyChange(SOURCE_CONNECTIONS_PROP, null, conn);
111: } else if (conn.getTarget() == this ) {
112: targetConnections.add(conn);
113: firePropertyChange(TARGET_CONNECTIONS_PROP, null, conn);
114: }
115: }
116:
117: /**
118: * Return the Location of this vertex.
119: *
120: * @return a non-null copy of location instance
121: */
122: public Point getLocation() {
123: return location.getCopy();
124: }
125:
126: /**
127: * Returns an array of IPropertyDescriptors for this vertex.
128: *
129: */
130: public IPropertyDescriptor[] getPropertyDescriptors() {
131: return descriptors;
132: }
133:
134: /**
135: * Return the property value for the given propertyId, or null.
136: */
137: public Object getPropertyValue(Object propertyId) {
138: if (XPOS_PROP.equals(propertyId)) {
139: return Integer.toString(location.x);
140: }
141: if (YPOS_PROP.equals(propertyId)) {
142: return Integer.toString(location.y);
143: }
144: if (HEIGHT_PROP.equals(propertyId)) {
145: return Integer.toString(size.height);
146: }
147: if (WIDTH_PROP.equals(propertyId)) {
148: return Integer.toString(size.width);
149: }
150: return null;
151: }
152:
153: /**
154: * Return the Size of this vertex.
155: * @return a non-null copy of Dimension instance
156: */
157: public Dimension getSize() {
158: return size.getCopy();
159: }
160:
161: /**
162: * Return a List of outgoing Connections.
163: */
164: public List getSourceConnections() {
165: return new ArrayList(sourceConnections);
166: }
167:
168: /**
169: * Return a List of incoming Connections.
170: */
171: public List getTargetConnections() {
172: return new ArrayList(targetConnections);
173: }
174:
175: /**
176: * Remove an incoming or outgoing connection from this vertex.
177: *
178: * @param conn a non-null connection instance
179: * @throws IllegalArgumentException if the parameter is null
180: */
181: public void removeConnection(Connection conn) {
182: if (conn == null) {
183: throw new IllegalArgumentException();
184: }
185: if (conn.getSource() == this ) {
186: sourceConnections.remove(conn);
187: firePropertyChange(SOURCE_CONNECTIONS_PROP, null, conn);
188: } else if (conn.getTarget() == this ) {
189: targetConnections.remove(conn);
190: firePropertyChange(TARGET_CONNECTIONS_PROP, null, conn);
191: }
192: }
193:
194: /**
195: * Set the Location of this vertex.
196: * @param newLocation a non-null Point instance
197: * @throws IllegalArgumentException if the parameter is null
198: */
199: public void setLocation(Point newLocation) {
200: if (newLocation == null) {
201: throw new IllegalArgumentException();
202: }
203: location.setLocation(newLocation);
204: firePropertyChange(LOCATION_PROP, null, location);
205: }
206:
207: /**
208: * Set the property value for the given property id.
209: */
210: public void setPropertyValue(Object propertyId, Object value) {
211: if (XPOS_PROP.equals(propertyId)) {
212: int x = Integer.parseInt((String) value);
213: setLocation(new Point(x, location.y));
214: } else if (YPOS_PROP.equals(propertyId)) {
215: int y = Integer.parseInt((String) value);
216: setLocation(new Point(location.x, y));
217: } else if (HEIGHT_PROP.equals(propertyId)) {
218: int height = Integer.parseInt((String) value);
219: setSize(new Dimension(size.width, height));
220: } else if (WIDTH_PROP.equals(propertyId)) {
221: int width = Integer.parseInt((String) value);
222: setSize(new Dimension(width, size.height));
223: }
224: }
225:
226: /**
227: * Set the Size of this vertex.
228: * Will not update the size if newSize is null.
229: * @param newSize a non-null Dimension instance or null
230: */
231: public void setSize(Dimension newSize) {
232: if (newSize != null) {
233: size.setSize(newSize);
234: firePropertyChange(SIZE_PROP, null, size);
235: }
236: }
237:
238: /* (non-Javadoc)
239: * @see java.lang.Object#toString()
240: */
241: public String toString() {
242: return VERTEX_NAME + hashCode();
243: }
244:
245: /**
246: * Color used for borders
247: *
248: * @return draw color
249: */
250: public Color getDrawColor() {
251: return ColorConstants.black;
252: }
253:
254: /**
255: * Constructs constraints string
256: *
257: * @param constraints array of constraints
258: * @return html-formatted constraints representation
259: */
260: public static String dumpConstraints(final Constraint[] constraints) {
261: if (constraints == null) {
262: return null;
263: }
264: final StringBuffer buffer = new StringBuffer();
265: for (int i = 0, length = constraints.length; i < length; i++) {
266: buffer.append(constraints[i].toString() + "<br>");
267: }
268: return buffer.toString();
269: }
270:
271: }
|