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.List;
020:
021: import org.drools.eclipse.flow.common.editor.core.ElementWrapper;
022: import org.drools.eclipse.flow.common.editor.core.ModelEvent;
023: import org.drools.eclipse.flow.common.editor.core.ModelListener;
024: import org.drools.eclipse.flow.common.editor.editpart.figure.ElementFigure;
025: import org.drools.eclipse.flow.common.editor.policy.ElementDirectEditManager;
026: import org.drools.eclipse.flow.common.editor.policy.ElementDirectEditPolicy;
027: import org.drools.eclipse.flow.common.editor.policy.ElementEditPolicy;
028: import org.drools.eclipse.flow.common.editor.policy.ElementNodeEditPolicy;
029: import org.eclipse.draw2d.ChopboxAnchor;
030: import org.eclipse.draw2d.ConnectionAnchor;
031: import org.eclipse.gef.ConnectionEditPart;
032: import org.eclipse.gef.EditPolicy;
033: import org.eclipse.gef.GraphicalEditPart;
034: import org.eclipse.gef.NodeEditPart;
035: import org.eclipse.gef.Request;
036: import org.eclipse.gef.RequestConstants;
037: import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
038: import org.eclipse.gef.tools.DirectEditManager;
039: import org.eclipse.jface.viewers.TextCellEditor;
040:
041: /**
042: * Default implementation of an element EditPart.
043: *
044: * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
045: */
046: public abstract class ElementEditPart extends AbstractGraphicalEditPart
047: implements NodeEditPart, ModelListener {
048:
049: private DirectEditManager manager;
050:
051: protected void createEditPolicies() {
052: installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE,
053: new ElementNodeEditPolicy());
054: installEditPolicy(EditPolicy.COMPONENT_ROLE,
055: new ElementEditPolicy());
056: installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE,
057: new ElementDirectEditPolicy());
058: }
059:
060: protected ElementWrapper getElementWrapper() {
061: return (ElementWrapper) getModel();
062: }
063:
064: protected List getModelSourceConnections() {
065: return getElementWrapper().getOutgoingConnections();
066: }
067:
068: protected List getModelTargetConnections() {
069: return getElementWrapper().getIncomingConnections();
070: }
071:
072: public ConnectionAnchor getSourceConnectionAnchor(
073: ConnectionEditPart connection) {
074: return new ChopboxAnchor(getFigure());
075: }
076:
077: public ConnectionAnchor getTargetConnectionAnchor(
078: ConnectionEditPart connection) {
079: return new ChopboxAnchor(getFigure());
080: }
081:
082: public ConnectionAnchor getSourceConnectionAnchor(Request request) {
083: return new ChopboxAnchor(getFigure());
084: }
085:
086: public ConnectionAnchor getTargetConnectionAnchor(Request request) {
087: return new ChopboxAnchor(getFigure());
088: }
089:
090: protected void refreshVisuals() {
091: ElementWrapper element = getElementWrapper();
092: ElementFigure figure = (ElementFigure) getFigure();
093: figure.setText(element.getName());
094: if (element.getConstraint().width == -1) {
095: element.getConstraint().width = figure.getBounds().width;
096: }
097: if (element.getConstraint().height == -1) {
098: element.getConstraint().height = figure.getBounds().height;
099: }
100: ((GraphicalEditPart) getParent()).setLayoutConstraint(this ,
101: figure, element.getConstraint());
102: }
103:
104: public void modelChanged(ModelEvent event) {
105: if (event.getChange() == ElementWrapper.CHANGE_INCOMING_CONNECTIONS) {
106: refreshTargetConnections();
107: } else if (event.getChange() == ElementWrapper.CHANGE_OUTGOING_CONNECTIONS) {
108: refreshSourceConnections();
109: } else if (event.getChange() == ElementWrapper.CHANGE_NAME) {
110: refreshVisuals();
111: } else if (event.getChange() == ElementWrapper.CHANGE_CONSTRAINT) {
112: refreshVisuals();
113: }
114: }
115:
116: public void activate() {
117: super .activate();
118: ((ElementWrapper) getModel()).addListener(this );
119: }
120:
121: public void deactivate() {
122: ((ElementWrapper) getModel()).removeListener(this );
123: super .deactivate();
124: }
125:
126: public void performRequest(Request request) {
127: if (request.getType() == RequestConstants.REQ_DIRECT_EDIT) {
128: performDirectEdit();
129: } else {
130: super .performRequest(request);
131: }
132: }
133:
134: private void performDirectEdit() {
135: if (manager == null) {
136: manager = new ElementDirectEditManager(this ,
137: TextCellEditor.class, new ElementCellEditorLocator(
138: ((ElementFigure) getFigure()).getLabel()));
139: }
140: manager.show();
141: }
142:
143: }
|