001: /*
002: * Copyright 2006 JBoss Inc
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.drools.eclipse.editors.rete.part;
017:
018: import java.beans.PropertyChangeEvent;
019: import java.beans.PropertyChangeListener;
020: import java.util.List;
021:
022: import org.drools.eclipse.editors.rete.commands.NodeSetConstraintCommand;
023: import org.drools.eclipse.editors.rete.model.ModelElement;
024: import org.drools.eclipse.editors.rete.model.ReteGraph;
025: import org.drools.reteoo.BaseVertex;
026: import org.eclipse.draw2d.Figure;
027: import org.eclipse.draw2d.FreeformLayer;
028: import org.eclipse.draw2d.FreeformLayout;
029: import org.eclipse.draw2d.IFigure;
030: import org.eclipse.draw2d.MarginBorder;
031: import org.eclipse.draw2d.geometry.Rectangle;
032: import org.eclipse.gef.EditPart;
033: import org.eclipse.gef.EditPolicy;
034: import org.eclipse.gef.NodeEditPart;
035: import org.eclipse.gef.commands.Command;
036: import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
037: import org.eclipse.gef.editpolicies.RootComponentEditPolicy;
038: import org.eclipse.gef.editpolicies.XYLayoutEditPolicy;
039: import org.eclipse.gef.requests.ChangeBoundsRequest;
040: import org.eclipse.gef.requests.CreateRequest;
041:
042: /**
043: * EditPart for ReteGraph
044: *
045: */
046: public class DiagramEditPart extends AbstractGraphicalEditPart
047: implements PropertyChangeListener {
048:
049: /* (non-Javadoc)
050: * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#activate()
051: */
052: public void activate() {
053: if (!isActive()) {
054: super .activate();
055: ((ModelElement) getModel()).addPropertyChangeListener(this );
056: }
057: }
058:
059: /* (non-Javadoc)
060: * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
061: */
062: protected void createEditPolicies() {
063: installEditPolicy(EditPolicy.COMPONENT_ROLE,
064: new RootComponentEditPolicy());
065: installEditPolicy(EditPolicy.LAYOUT_ROLE,
066: new NodesXYLayoutEditPolicy());
067: }
068:
069: /* (non-Javadoc)
070: * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
071: */
072: protected IFigure createFigure() {
073: Figure f = new FreeformLayer();
074: f.setBorder(new MarginBorder(3));
075: f.setLayoutManager(new FreeformLayout());
076: return f;
077: }
078:
079: /* (non-Javadoc)
080: * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#deactivate()
081: */
082: public void deactivate() {
083: if (isActive()) {
084: super .deactivate();
085: ((ModelElement) getModel())
086: .removePropertyChangeListener(this );
087: }
088: }
089:
090: private ReteGraph getCastedModel() {
091: return (ReteGraph) getModel();
092: }
093:
094: /* (non-Javadoc)
095: * @see org.eclipse.gef.editparts.AbstractEditPart#getModelChildren()
096: */
097: protected List getModelChildren() {
098: return getCastedModel().getChildren(); // return a list of nodes
099: }
100:
101: /* (non-Javadoc)
102: * @see java.beans.PropertyChangeListener#propertyChange(PropertyChangeEvent)
103: */
104: public void propertyChange(PropertyChangeEvent evt) {
105: String prop = evt.getPropertyName();
106: if (ReteGraph.PROP_CHILD_ADDED.equals(prop)
107: || ReteGraph.PROP_CHILD_REMOVED.equals(prop)) {
108: refreshChildren();
109: }
110: }
111:
112: private static class NodesXYLayoutEditPolicy extends
113: XYLayoutEditPolicy {
114:
115: /* (non-Javadoc)
116: * @see ConstrainedLayoutEditPolicy#createChangeConstraintCommand(ChangeBoundsRequest, EditPart, Object)
117: */
118: protected Command createChangeConstraintCommand(
119: ChangeBoundsRequest request, EditPart child,
120: Object constraint) {
121: if (child instanceof NodeEditPart
122: && constraint instanceof Rectangle) {
123: // return a command that can move and/or resize a BaseVertex
124: return new NodeSetConstraintCommand((BaseVertex) child
125: .getModel(), request, (Rectangle) constraint);
126: }
127: return super .createChangeConstraintCommand(request, child,
128: constraint);
129: }
130:
131: /* (non-Javadoc)
132: * @see ConstrainedLayoutEditPolicy#createChangeConstraintCommand(EditPart, Object)
133: */
134: protected Command createChangeConstraintCommand(EditPart child,
135: Object constraint) {
136: return null;
137: }
138:
139: /* (non-Javadoc)
140: * @see LayoutEditPolicy#getCreateCommand(CreateRequest)
141: */
142: protected Command getCreateCommand(CreateRequest request) {
143: return null;
144: }
145:
146: }
147:
148: /* (non-Javadoc)
149: * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#getAdapter(java.lang.Class)
150: */
151: public Object getAdapter(Class key) {
152: return super.getAdapter(key);
153: }
154:
155: }
|