001: package org.openwfe.gpe.parts;
002:
003: import java.beans.PropertyChangeEvent;
004: import java.util.List;
005: import java.util.Map;
006:
007: import org.eclipse.draw2d.IFigure;
008: import org.eclipse.draw2d.Label;
009: import org.eclipse.draw2d.geometry.Insets;
010: import org.eclipse.draw2d.geometry.Point;
011: import org.eclipse.draw2d.graph.CompoundDirectedGraph;
012: import org.eclipse.draw2d.graph.Subgraph;
013:
014: import org.eclipse.gef.EditPolicy;
015: import org.eclipse.gef.GraphicalEditPart;
016: import org.eclipse.gef.Request;
017: import org.eclipse.gef.RequestConstants;
018: import org.eclipse.gef.requests.DirectEditRequest;
019:
020: import org.openwfe.gpe.figures.SubgraphFigure;
021: import org.openwfe.gpe.model.AbstractFlow;
022: import org.openwfe.gpe.model.CompositeOrOneChild;
023: import org.openwfe.gpe.policies.ActivityContainerEditPolicy;
024: import org.openwfe.gpe.policies.ActivityContainerHighlightEditPolicy;
025: import org.openwfe.gpe.policies.ActivityEditPolicy;
026: import org.openwfe.gpe.policies.StructuredActivityLayoutEditPolicy;
027:
028: public abstract class CompositeOrOneChildPart extends FlowElementPart {
029:
030: static final Insets PADDING = new Insets(8, 6, 8, 6);
031: static final Insets INNER_PADDING = new Insets(0);
032:
033: protected void applyChildrenResults(CompoundDirectedGraph graph,
034: Map map) {
035: for (int i = 0; i < getChildren().size(); i++) {
036: FlowElementPart part = (FlowElementPart) getChildren().get(
037: i);
038: part.applyGraphResults(graph, map);
039: }
040: }
041:
042: protected void applyGraphResults(CompoundDirectedGraph graph,
043: Map map) {
044: applyOwnResults(graph, map);
045: applyChildrenResults(graph, map);
046: }
047:
048: protected void applyOwnResults(CompoundDirectedGraph graph, Map map) {
049: super .applyGraphResults(graph, map);
050: }
051:
052: /**
053: * @see org.openwfe.gpe.parts.FlowElementPart#createEditPolicies()
054: */
055: protected void createEditPolicies() {
056:
057: installEditPolicy(EditPolicy.COMPONENT_ROLE,
058: new ActivityEditPolicy());
059: installEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE,
060: new ActivityContainerHighlightEditPolicy());
061: installEditPolicy(EditPolicy.CONTAINER_ROLE,
062: new ActivityContainerEditPolicy());
063: installEditPolicy(EditPolicy.LAYOUT_ROLE,
064: new StructuredActivityLayoutEditPolicy());
065:
066: }
067:
068: public void contributeNodesToGraph(CompoundDirectedGraph graph,
069: Subgraph s, Map map) {
070: IFigure fig = getFigure();
071: GraphAnimation.recordInitialState(getContentPane());
072: Subgraph me = new Subgraph(this , s);
073: me.outgoingOffset = 5;
074: me.incomingOffset = 5;
075:
076: if (fig instanceof SubgraphFigure) {
077: me.width = fig.getPreferredSize(me.width, me.height).width;
078: int tagHeight = ((SubgraphFigure) fig).getHeader()
079: .getPreferredSize().height;
080: me.insets.top = tagHeight;
081: me.insets.left = 0;
082: me.insets.bottom = tagHeight;
083: }
084: me.innerPadding = INNER_PADDING;
085: me.setPadding(PADDING);
086: map.put(this , me);
087: graph.nodes.add(me);
088: for (int i = 0; i < getChildren().size(); i++) {
089: FlowElementPart activity = (FlowElementPart) getChildren()
090: .get(i);
091: activity.contributeNodesToGraph(graph, me, map);
092: }
093:
094: }
095:
096: private boolean directEditHitTest(Point requestLoc) {
097: IFigure header = ((SubgraphFigure) getFigure()).getHeader();
098: header.translateToRelative(requestLoc);
099: if (header.containsPoint(requestLoc))
100: return true;
101: return false;
102: }
103:
104: /**
105: * @see org.eclipse.gef.EditPart#performRequest(org.eclipse.gef.Request)
106: */
107: public void performRequest(Request request) {
108: if (request.getType() == RequestConstants.REQ_DIRECT_EDIT) {
109: if (request instanceof DirectEditRequest
110: && !directEditHitTest(((DirectEditRequest) request)
111: .getLocation().getCopy()))
112: return;
113: performDirectEdit();
114: }
115: }
116:
117: public IFigure getContentPane() {
118: if (getFigure() instanceof SubgraphFigure)
119: return ((SubgraphFigure) getFigure()).getContents();
120: return getFigure();
121: }
122:
123: protected List getModelChildren() {
124: return getStructuredActivity().getChildren();
125: }
126:
127: CompositeOrOneChild getStructuredActivity() {
128: return (CompositeOrOneChild) getModel();
129: }
130:
131: /**
132: * @see org.eclipse.gef.editparts.AbstractEditPart#refreshVisuals()
133: */
134: protected void refreshVisuals() {
135: ((Label) ((SubgraphFigure) getFigure()).getHeader())
136: .setText(getActivity().getName());
137: ((Label) ((SubgraphFigure) getFigure()).getFooter())
138: .setText("/" + getActivity().getName()); //$NON-NLS-1$
139: }
140:
141: public void propertyChange(PropertyChangeEvent evt) {
142: String prop = evt.getPropertyName();
143: if (AbstractFlow.CHILDREN.equals(prop))
144: refreshChildren();
145: this .refreshVisuals();
146: // Causes Graph to re-layout
147: ((GraphicalEditPart) (getViewer().getContents())).getFigure()
148: .revalidate();
149: }
150:
151: }
|