import java.awt.*;
import java.util.*;
import java.awt.geom.*;
import java.io.*;
import java.util.*;
import java.util.List;
/**
* Make a diagram editor that allows for the drawing of hosts, routers,
* and network connections.
*
* @author Eric Lemar
*/
public class NetworkDiagram extends Graph
{
public NetworkDiagram() {
super();
}
public Node[] getNodePrototypes()
{
Node[] nodeTypes = {
new RouterNode(),
new HostNode()
};
return nodeTypes;
}
public Edge[] getEdgePrototypes()
{
Edge[] edgeTypes = {
new NetworkEdge()
};
return edgeTypes;
}
/**
* Connect two nodes together.
*
* Returns false if there isn't a node at one of the two points or
* if the edge is of type NetworkEdge and violates one of the constraints
* of how network nodes can be connected to each other. These constraints
* are defined in the NetworkEdge and NetworkNode classes.
*
* @param e The new edge
* @param p1 The location to look for the first node
* @param p2 The location to look for the second node
* @return true
if added, false
otherwise.
*/
public boolean connect(Edge e, Point2D p1, Point2D p2)
{
Node n1 = findNode(p1);
Node n2 = findNode(p2);
if (n1 != null && n2 != null) {
if (e instanceof NetworkEdge) {
NetworkEdge me=(NetworkEdge)e;
if (me.approvedConnect(n1,n2)) {
super.connect(e,p1,p2);
return true;
}
return false;
} else {
super.connect(e,p1,p2);
return true;
}
}
return false;
}
/**
* Remove an edge.
*
* This method also signals the edge if a NetworkEdge to tell inform it that
* it's being removed.
*
* @param e The edge to remove
*/
public void removeEdge(Edge e){
if (e instanceof NetworkEdge) {
NetworkEdge ne=(NetworkEdge)e;
ne.signalRemove();
}
super.removeEdge(e);
}
/**
* Remove an node.
*
* This method also signals all of the connected edges to tell them
* they are being removed
*
* @param e The edge to remove
*/
public void removeNode(Node n) {
List edges = getEdges();
Iterator it = edges.iterator();
while (it.hasNext()) {
Edge e = (Edge)it.next();
if (e.getStart() == n || e.getEnd() == n) {
if (e instanceof NetworkEdge) {
NetworkEdge ne=(NetworkEdge)e;
ne.signalRemove();
}
}
}
super.removeNode(n);
}
}