001: package org.drools.eclipse.flow.common.editor.policy;
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 org.drools.eclipse.flow.common.editor.core.ElementConnection;
020: import org.drools.eclipse.flow.common.editor.core.ElementConnectionFactory;
021: import org.drools.eclipse.flow.common.editor.core.ElementWrapper;
022: import org.drools.eclipse.flow.common.editor.core.ProcessWrapper;
023: import org.drools.eclipse.flow.common.editor.core.command.DeleteConnectionCommand;
024: import org.drools.eclipse.flow.common.editor.core.command.SplitConnectionCommand;
025: import org.drools.eclipse.flow.common.editor.editpart.ElementConnectionEditPart;
026: import org.eclipse.draw2d.PolylineConnection;
027: import org.eclipse.gef.EditPart;
028: import org.eclipse.gef.Request;
029: import org.eclipse.gef.commands.Command;
030: import org.eclipse.gef.requests.CreateRequest;
031: import org.eclipse.gef.requests.GroupRequest;
032:
033: /**
034: * Policy for editing connections.
035: *
036: * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
037: */
038: public class ConnectionEditPolicy extends
039: org.eclipse.gef.editpolicies.ConnectionEditPolicy {
040:
041: private ElementConnectionFactory elementConnectionFactory;
042:
043: public void setDefaultElementConnectionFactory(
044: ElementConnectionFactory factory) {
045: if (factory == null) {
046: throw new IllegalArgumentException(
047: "ElementConnectionFactory is null");
048: }
049: this .elementConnectionFactory = factory;
050: }
051:
052: public ElementConnectionFactory getDefaultElementConnectionFactory() {
053: return elementConnectionFactory;
054: }
055:
056: public Command getCommand(Request request) {
057: if (REQ_CREATE.equals(request.getType()))
058: return getSplitTransitionCommand(request);
059: return super .getCommand(request);
060: }
061:
062: private PolylineConnection getConnectionFigure() {
063: return ((PolylineConnection) ((ElementConnectionEditPart) getHost())
064: .getFigure());
065: }
066:
067: protected Command getDeleteCommand(GroupRequest request) {
068: DeleteConnectionCommand cmd = new DeleteConnectionCommand();
069: ElementConnection connection = (ElementConnection) getHost()
070: .getModel();
071: cmd.setAntecedentTaskConnection(connection);
072: cmd.setSource(connection.getSource());
073: cmd.setTarget(connection.getTarget());
074: return cmd;
075: }
076:
077: protected Command getSplitTransitionCommand(Request request) {
078: if (elementConnectionFactory == null) {
079: throw new IllegalStateException(
080: "DefaultElementConnectionFactory is null");
081: }
082: SplitConnectionCommand cmd = new SplitConnectionCommand();
083: cmd.setElementConnection(((ElementConnection) getHost()
084: .getModel()));
085: cmd.setNewSecondConnection(elementConnectionFactory
086: .createElementConnection());
087: cmd
088: .setParent(((ProcessWrapper) ((ElementConnectionEditPart) getHost())
089: .getSource().getParent().getModel()));
090: cmd.setNewElement(((ElementWrapper) ((CreateRequest) request)
091: .getNewObject()));
092: return cmd;
093: }
094:
095: public EditPart getTargetEditPart(Request request) {
096: if (REQ_CREATE.equals(request.getType()))
097: return getHost();
098: return null;
099: }
100:
101: public void eraseTargetFeedback(Request request) {
102: if (REQ_CREATE.equals(request.getType()))
103: getConnectionFigure().setLineWidth(1);
104: }
105:
106: public void showTargetFeedback(Request request) {
107: if (REQ_CREATE.equals(request.getType()))
108: getConnectionFigure().setLineWidth(2);
109: }
110:
111: }
|