001: package org.drools.eclipse.flow.common.editor.core;
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.io.IOException;
020: import java.io.ObjectInputStream;
021: import java.io.Serializable;
022: import java.util.ArrayList;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import org.eclipse.draw2d.geometry.Point;
027:
028: /**
029: * A connection between two model elements.
030: *
031: * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
032: */
033: public class ElementConnection implements Serializable {
034:
035: private static final long serialVersionUID = 400L;
036:
037: public static final int CHANGE_BENDPOINTS = 1;
038:
039: private ElementWrapper source;
040: private ElementWrapper target;
041: private int type;
042: private List bendpoints = new ArrayList();
043: private transient List listeners = new ArrayList();
044:
045: public ElementConnection(int type) {
046: this .type = type;
047: }
048:
049: public ElementConnection(ElementWrapper source,
050: ElementWrapper target, int type) {
051: this (type);
052: if (source == null) {
053: throw new IllegalArgumentException("source is null");
054: }
055: if (target == null) {
056: throw new IllegalArgumentException("target is null");
057: }
058: this .source = source;
059: this .target = target;
060: }
061:
062: public void disconnect() {
063: if (source == null) {
064: throw new IllegalStateException(
065: "Can't disconnect, source is null");
066: }
067: if (target == null) {
068: throw new IllegalStateException(
069: "Can't disconnect, target is null");
070: }
071: source.removeOutgoingConnection(this );
072: target.removeIncomingConnection(this );
073: source = null;
074: target = null;
075: }
076:
077: public void connect(ElementWrapper source, ElementWrapper target) {
078: if (source == null) {
079: throw new IllegalArgumentException("source is null");
080: }
081: if (this .source != null) {
082: throw new IllegalStateException(
083: "The source of a connection cannot be changed");
084: }
085: if (target == null) {
086: throw new IllegalArgumentException("target is null");
087: }
088: if (this .target != null) {
089: throw new IllegalStateException(
090: "The target of a connection cannot be changed");
091: }
092: this .source = source;
093: this .target = target;
094: source.addOutgoingConnection(this );
095: target.addIncomingConnection(this );
096: }
097:
098: public ElementWrapper getSource() {
099: return source;
100: }
101:
102: public ElementWrapper getTarget() {
103: return target;
104: }
105:
106: public int getType() {
107: return type;
108: }
109:
110: public void addBendpoint(int index, Point point) {
111: bendpoints.add(index, point);
112: notifyListeners(CHANGE_BENDPOINTS);
113: }
114:
115: public void removeBendpoint(int index) {
116: bendpoints.remove(index);
117: notifyListeners(CHANGE_BENDPOINTS);
118: }
119:
120: public void replaceBendpoint(int index, Point point) {
121: bendpoints.set(index, point);
122: notifyListeners(CHANGE_BENDPOINTS);
123: }
124:
125: public List getBendpoints() {
126: return bendpoints;
127: }
128:
129: public void addListener(ModelListener listener) {
130: listeners.add(listener);
131: }
132:
133: public void removeListener(ModelListener listener) {
134: listeners.remove(listener);
135: }
136:
137: protected void notifyListeners(int change) {
138: ModelEvent event = new ModelEvent(change);
139: for (Iterator it = listeners.iterator(); it.hasNext();) {
140: ModelListener listener = (ModelListener) it.next();
141: listener.modelChanged(event);
142: }
143: }
144:
145: private void readObject(ObjectInputStream aInputStream)
146: throws ClassNotFoundException, IOException {
147: aInputStream.defaultReadObject();
148: listeners = new ArrayList();
149: }
150: }
|