01: package org.drools.eclipse.flow.common.editor.core.command;
02:
03: /*
04: * Copyright 2005 JBoss Inc
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: import java.util.List;
20:
21: import org.drools.eclipse.flow.common.editor.core.ElementConnection;
22: import org.drools.eclipse.flow.common.editor.core.ElementWrapper;
23: import org.eclipse.gef.commands.Command;
24:
25: /**
26: * A command for reconnecting the source of a connection.
27: *
28: * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
29: */
30: public class ReconnectElementConnectionSourceCommand extends Command {
31:
32: private ElementConnection connection;
33: private ElementWrapper target;
34: private ElementWrapper oldSource;
35: private ElementWrapper newSource;
36:
37: public boolean canExecute() {
38: if (connection.getTarget().equals(newSource))
39: return false;
40:
41: List connections = newSource.getOutgoingConnections(connection
42: .getType());
43: for (int i = 0; i < connections.size(); i++) {
44: ElementConnection connection = ((ElementConnection) (connections
45: .get(i)));
46: if (connection.getTarget().equals(target)
47: && !connection.getSource().equals(oldSource))
48: return false;
49: }
50: return newSource.acceptsOutgoingConnection(connection); //XXX
51: }
52:
53: public void execute() {
54: if (newSource != null) {
55: connection.disconnect();
56: connection.connect(newSource, target);
57: }
58: }
59:
60: public void setSource(ElementWrapper source) {
61: this .newSource = source;
62: }
63:
64: public void setConnection(ElementConnection connection) {
65: this .connection = connection;
66: this .target = connection.getTarget();
67: this .oldSource = connection.getSource();
68: }
69:
70: public void undo() {
71: connection.disconnect();
72: connection.connect(oldSource, target);
73: }
74:
75: public void redo() {
76: connection.disconnect();
77: connection.connect(newSource, target);
78: }
79: }
|