001: package org.drools.eclipse.editors.rete.part;
002:
003: import java.beans.PropertyChangeEvent;
004: import java.beans.PropertyChangeListener;
005: import java.util.List;
006:
007: import org.drools.eclipse.editors.rete.figure.VertexFigure;
008: import org.drools.eclipse.editors.rete.model.GraphicalVertex;
009: import org.drools.eclipse.editors.rete.model.ModelElement;
010: import org.drools.eclipse.editors.rete.model.VertexPropertySource;
011: import org.drools.reteoo.BaseVertex;
012: import org.eclipse.draw2d.ConnectionAnchor;
013: import org.eclipse.draw2d.EllipseAnchor;
014: import org.eclipse.draw2d.IFigure;
015: import org.eclipse.draw2d.geometry.Rectangle;
016: import org.eclipse.gef.ConnectionEditPart;
017: import org.eclipse.gef.GraphicalEditPart;
018: import org.eclipse.gef.Request;
019: import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
020: import org.eclipse.ui.views.properties.IPropertySource;
021:
022: /**
023: * EditPart used for Vertices
024: */
025: class VertexEditPart extends AbstractGraphicalEditPart implements
026: PropertyChangeListener, org.eclipse.gef.NodeEditPart {
027:
028: private IPropertySource propertySource;
029:
030: private ConnectionAnchor anchor;
031:
032: /* (non-Javadoc)
033: * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#activate()
034: */
035: public void activate() {
036: if (!isActive()) {
037: super .activate();
038: ((ModelElement) getModel()).addPropertyChangeListener(this );
039: propertySource = new VertexPropertySource(getCastedModel());
040: }
041: }
042:
043: /* (non-Javadoc)
044: * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
045: */
046: protected void createEditPolicies() {
047: }
048:
049: /*(non-Javadoc)
050: * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
051: */
052: protected IFigure createFigure() {
053: return new VertexFigure(getCastedModel().getFillColor(),
054: getCastedModel().getDrawColor());
055: }
056:
057: /* (non-Javadoc)
058: * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#deactivate()
059: */
060: public void deactivate() {
061: if (isActive()) {
062: super .deactivate();
063: ((ModelElement) getModel())
064: .removePropertyChangeListener(this );
065: propertySource = null;
066: }
067: }
068:
069: private BaseVertex getCastedModel() {
070: return (BaseVertex) getModel();
071: }
072:
073: private ConnectionAnchor getConnectionAnchor() {
074: if (anchor == null) {
075: if (getModel() instanceof BaseVertex)
076: anchor = new EllipseAnchor(getFigure());
077: else
078: // if Nodes gets extended the conditions above must be updated
079: throw new IllegalArgumentException("unexpected model");
080: }
081: return anchor;
082: }
083:
084: /*
085: * (non-Javadoc)
086: * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#getModelSourceConnections()
087: */
088: protected List getModelSourceConnections() {
089: return getCastedModel().getSourceConnections();
090: }
091:
092: /*
093: * (non-Javadoc)
094: * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#getModelTargetConnections()
095: */
096: protected List getModelTargetConnections() {
097: return getCastedModel().getTargetConnections();
098: }
099:
100: /*
101: * (non-Javadoc)
102: * @see org.eclipse.gef.NodeEditPart#getSourceConnectionAnchor(org.eclipse.gef.ConnectionEditPart)
103: */
104: public ConnectionAnchor getSourceConnectionAnchor(
105: ConnectionEditPart connection) {
106: return getConnectionAnchor();
107: }
108:
109: /*
110: * (non-Javadoc)
111: * @see org.eclipse.gef.NodeEditPart#getSourceConnectionAnchor(org.eclipse.gef.Request)
112: */
113: public ConnectionAnchor getSourceConnectionAnchor(Request request) {
114: return getConnectionAnchor();
115: }
116:
117: /*
118: * (non-Javadoc)
119: * @see org.eclipse.gef.NodeEditPart#getTargetConnectionAnchor(org.eclipse.gef.ConnectionEditPart)
120: */
121: public ConnectionAnchor getTargetConnectionAnchor(
122: ConnectionEditPart connection) {
123: return getConnectionAnchor();
124: }
125:
126: /*
127: * (non-Javadoc)
128: * @see org.eclipse.gef.NodeEditPart#getTargetConnectionAnchor(org.eclipse.gef.Request)
129: */
130: public ConnectionAnchor getTargetConnectionAnchor(Request request) {
131: return getConnectionAnchor();
132: }
133:
134: /* (non-Javadoc)
135: * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
136: */
137: public void propertyChange(PropertyChangeEvent evt) {
138: String prop = evt.getPropertyName();
139: if (GraphicalVertex.SIZE_PROP.equals(prop)
140: || GraphicalVertex.LOCATION_PROP.equals(prop)) {
141: refreshVisuals();
142: } else if (GraphicalVertex.SOURCE_CONNECTIONS_PROP.equals(prop)) {
143: refreshSourceConnections();
144: } else if (GraphicalVertex.TARGET_CONNECTIONS_PROP.equals(prop)) {
145: refreshTargetConnections();
146: }
147: }
148:
149: /* (non-Javadoc)
150: * @see org.eclipse.gef.editparts.AbstractEditPart#refreshVisuals()
151: */
152: protected void refreshVisuals() {
153: Rectangle bounds = new Rectangle(
154: getCastedModel().getLocation(), getCastedModel()
155: .getSize());
156: ((GraphicalEditPart) getParent()).setLayoutConstraint(this ,
157: getFigure(), bounds);
158: }
159:
160: /* (non-Javadoc)
161: * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#getAdapter(java.lang.Class)
162: */
163: public Object getAdapter(Class key) {
164: if (key == IPropertySource.class) {
165: return propertySource;
166: }
167: return super.getAdapter(key);
168: }
169:
170: }
|