001: package org.drools.eclipse.flow.common.editor.core.command;
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.ArrayList;
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import org.drools.eclipse.flow.common.editor.core.ElementConnection;
024: import org.drools.eclipse.flow.common.editor.core.ElementWrapper;
025: import org.drools.eclipse.flow.common.editor.core.ProcessWrapper;
026: import org.eclipse.gef.commands.Command;
027:
028: /**
029: * A command for deleting an element.
030: *
031: * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
032: */
033: public class DeleteElementCommand extends Command {
034:
035: private ElementWrapper child;
036: private ProcessWrapper parent;
037:
038: private List incomingElementWrappers = new ArrayList();
039: private List outgoingElementWrappers = new ArrayList();
040: private List incomingConnections = new ArrayList();
041: private List outgoingConnections = new ArrayList();
042:
043: private void deleteConnections(ElementWrapper element) {
044: for (Iterator it = element.getIncomingConnections().iterator(); it
045: .hasNext();) {
046: ElementConnection connection = (ElementConnection) it
047: .next();
048: incomingElementWrappers.add(connection.getSource());
049: incomingConnections.add(connection);
050: }
051: for (Iterator it = element.getOutgoingConnections().iterator(); it
052: .hasNext();) {
053: ElementConnection connection = (ElementConnection) it
054: .next();
055: outgoingElementWrappers.add(connection.getTarget());
056: outgoingConnections.add(connection);
057: }
058: for (Iterator it = incomingConnections.iterator(); it.hasNext();) {
059: ElementConnection connection = (ElementConnection) it
060: .next();
061: connection.disconnect();
062: }
063: for (Iterator it = outgoingConnections.iterator(); it.hasNext();) {
064: ElementConnection connection = (ElementConnection) it
065: .next();
066: connection.disconnect();
067: }
068: }
069:
070: public void execute() {
071: deleteConnections(child);
072: parent.removeElement(child);
073: }
074:
075: private void restoreConnections() {
076: int i = 0;
077: for (Iterator it = incomingConnections.iterator(); it.hasNext();) {
078: ElementConnection connection = (ElementConnection) it
079: .next();
080: connection.connect((ElementWrapper) incomingElementWrappers
081: .get(i), child);
082: i++;
083: }
084: i = 0;
085: for (Iterator it = outgoingConnections.iterator(); it.hasNext();) {
086: ElementConnection connection = (ElementConnection) it
087: .next();
088: connection.connect(child,
089: (ElementWrapper) outgoingElementWrappers.get(i));
090: i++;
091: }
092: incomingConnections.clear();
093: incomingElementWrappers.clear();
094: outgoingConnections.clear();
095: outgoingElementWrappers.clear();
096: }
097:
098: public void setChild(ElementWrapper child) {
099: this .child = child;
100: }
101:
102: public void setParent(ProcessWrapper parent) {
103: this .parent = parent;
104: }
105:
106: public void undo() {
107: parent.addElement(child);
108: restoreConnections();
109: }
110:
111: }
|