001: package org.drools.eclipse.flow.common.editor.editpart;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.util.Iterator;
020: import java.util.List;
021:
022: import org.drools.eclipse.flow.common.editor.core.ElementWrapper;
023: import org.drools.eclipse.flow.common.editor.core.ModelEvent;
024: import org.drools.eclipse.flow.common.editor.core.ModelListener;
025: import org.drools.eclipse.flow.common.editor.core.ProcessWrapper;
026: import org.drools.eclipse.flow.common.editor.editpart.figure.ElementFigure;
027: import org.drools.eclipse.flow.common.editor.policy.ProcessLayoutEditPolicy;
028: import org.eclipse.draw2d.Animation;
029: import org.eclipse.draw2d.AutomaticRouter;
030: import org.eclipse.draw2d.BendpointConnectionRouter;
031: import org.eclipse.draw2d.ConnectionLayer;
032: import org.eclipse.draw2d.FanRouter;
033: import org.eclipse.draw2d.Figure;
034: import org.eclipse.draw2d.IFigure;
035: import org.eclipse.draw2d.ManhattanConnectionRouter;
036: import org.eclipse.draw2d.ShortestPathConnectionRouter;
037: import org.eclipse.draw2d.XYLayout;
038: import org.eclipse.draw2d.geometry.Rectangle;
039: import org.eclipse.gef.EditPolicy;
040: import org.eclipse.gef.LayerConstants;
041: import org.eclipse.gef.SnapToGrid;
042: import org.eclipse.gef.SnapToHelper;
043: import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
044: import org.eclipse.gef.editpolicies.RootComponentEditPolicy;
045: import org.eclipse.swt.SWT;
046:
047: /**
048: * Default implementation of a process EditPart.
049: *
050: * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
051: */
052: public class ProcessEditPart extends AbstractGraphicalEditPart
053: implements ModelListener {
054:
055: // private GraphLayoutManager graphLayoutManager;
056:
057: protected IFigure createFigure() {
058: Figure f = new Figure();
059: f.setLayoutManager(new XYLayout());
060: // graphLayoutManager = new GraphLayoutManager(this);
061: // f.setLayoutManager(graphLayoutManager);
062: // graphLayoutManager.layout(f);
063: return f;
064: }
065:
066: protected void createEditPolicies() {
067: installEditPolicy(EditPolicy.NODE_ROLE, null);
068: installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE, null);
069: installEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE, null);
070: installEditPolicy(EditPolicy.LAYOUT_ROLE,
071: new ProcessLayoutEditPolicy());
072: installEditPolicy(EditPolicy.COMPONENT_ROLE,
073: new RootComponentEditPolicy());
074: }
075:
076: protected List getModelChildren() {
077: return getProcessWrapper().getElements();
078: }
079:
080: protected ProcessWrapper getProcessWrapper() {
081: return (ProcessWrapper) getModel();
082: }
083:
084: public void activate() {
085: super .activate();
086: ((ProcessWrapper) getModel()).addListener(this );
087: }
088:
089: public void deactivate() {
090: ((ProcessWrapper) getModel()).removeListener(this );
091: super .deactivate();
092: }
093:
094: public void modelChanged(ModelEvent event) {
095: if (event.getChange() == ProcessWrapper.CHANGE_ELEMENTS) {
096: refreshChildren();
097: } else if (event.getChange() == ProcessWrapper.CHANGE_ROUTER_LAYOUT) {
098: refreshVisuals();
099: }
100: }
101:
102: public Object getAdapter(Class adapter) {
103: if (adapter == SnapToHelper.class) {
104: Boolean val = (Boolean) getViewer().getProperty(
105: SnapToGrid.PROPERTY_GRID_ENABLED);
106: if (val != null && val.booleanValue()) {
107: return new SnapToGrid(this );
108: }
109: }
110: return super .getAdapter(adapter);
111: }
112:
113: protected void refreshVisuals() {
114: Animation.markBegin();
115: ConnectionLayer layer = (ConnectionLayer) getLayer(LayerConstants.CONNECTION_LAYER);
116: if ((getViewer().getControl().getStyle() & SWT.MIRRORED) == 0) {
117: layer.setAntialias(SWT.ON);
118: }
119:
120: if (getProcessWrapper().getRouterLayout().equals(
121: ProcessWrapper.ROUTER_LAYOUT_MANUAL)) {
122: AutomaticRouter router = new FanRouter();
123: router.setNextRouter(new BendpointConnectionRouter());
124: layer.setConnectionRouter(router);
125: } else if (getProcessWrapper().getRouterLayout().equals(
126: ProcessWrapper.ROUTER_LAYOUT_MANHATTAN)) {
127: layer.setConnectionRouter(new ManhattanConnectionRouter());
128: } else {
129: layer.setConnectionRouter(new ShortestPathConnectionRouter(
130: getFigure()));
131: }
132: Animation.run(400);
133: }
134:
135: public boolean setTableModelBounds() {
136: List tableParts = getChildren();
137: for (Iterator iter = tableParts.iterator(); iter.hasNext();) {
138: ElementEditPart elementEditPart = (ElementEditPart) iter
139: .next();
140: ElementFigure elementFigure = (ElementFigure) elementEditPart
141: .getFigure();
142: if (elementFigure == null) {
143: continue;
144: }
145: Rectangle constraint = elementFigure.getBounds().getCopy();
146: ElementWrapper elementWrapper = elementEditPart
147: .getElementWrapper();
148: elementWrapper.setConstraint(constraint);
149: }
150: return true;
151: }
152: }
|