View Javadoc

1   package de.fzi.wim.guibase.graphview.graph;
2   
3   import  java.util.Collection;
4   import  java.util.Iterator;
5   import  java.util.List;
6   import  java.util.LinkedList;
7   
8   /***
9    * A default implementation of a mutable graph.
10   */
11  public class DefaultGraph extends AbstractGraph {
12      /*** Nodes of the graph. */
13      protected List m_nodes;
14      /*** Edges of the graph. */
15      protected List m_edges;
16  
17      /***
18       * Creates a new graph.
19       */
20      public DefaultGraph() {
21          m_nodes=new LinkedList();
22          m_edges=new LinkedList();
23      }
24      /***
25       * Returns a collection of the nodes in the graph.
26       *
27       * @return                      a collection of the nodes in the graph
28       */
29      public List getNodes() {
30          return m_nodes;
31      }
32      /***
33       * Returns a collection of the edges in the graph.
34       *
35       * @return                      the collection of the edges in the graph
36       */
37      public List getEdges() {
38          return m_edges;
39      }
40      /***
41       * Adds elements to the graph.
42       *
43       * @param nodes                 the nodes added to the graph (may be <code>null</code>)
44       * @param edges                 the edges added to the graph (may be <code>null</code>)
45       */
46      public synchronized void addElements(Collection nodes,Collection edges) {
47          if (nodes!=null) {
48              Iterator iterator=nodes.iterator();
49              while (iterator.hasNext()) {
50                  DefaultNode node=(DefaultNode)iterator.next();
51                  if (!m_nodes.contains(node))
52                      m_nodes.add(node);
53              }
54          }
55          if (edges!=null) {
56              Iterator iterator=edges.iterator();
57              while (iterator.hasNext()) {
58                  DefaultEdge edge=(DefaultEdge)iterator.next();
59                  if (!m_edges.contains(edge)) {
60                      m_edges.add(edge);
61                      ((DefaultNode)edge.getFrom()).notifyEdgeAdded(edge);
62                      ((DefaultNode)edge.getTo()).notifyEdgeAdded(edge);
63                  } 
64              }
65          }
66          fireElementsAdded(nodes,edges);
67      }
68      /***
69       * Deletes elements from the graph.
70       *
71       * @param nodes                 the nodes to delete (may be <code>null</code>)
72       * @param edges                 the edges to delete (may be <code>null</code>)
73       */
74      public synchronized void deleteElements(Collection nodes,Collection edges) {
75          if (nodes!=null) {
76              Iterator iterator=nodes.iterator();
77              while (iterator.hasNext()) {
78                  DefaultNode node=(DefaultNode)iterator.next();
79                  m_nodes.remove(node);
80              }
81          }
82          if (edges!=null) {
83              Iterator iterator=edges.iterator();
84              while (iterator.hasNext()) {
85                  DefaultEdge edge=(DefaultEdge)iterator.next();
86                  if (m_edges.remove(edge)) {
87                      ((DefaultNode)edge.getFrom()).notifyEdgeRemoved(edge);
88                      ((DefaultNode)edge.getTo()).notifyEdgeRemoved(edge);
89                  }
90              }
91          }
92          fireElementsRemoved(nodes,edges);
93      }
94      /***
95       * Clears the graph.
96       */
97      public synchronized void clear() {
98          m_nodes.clear();
99          m_edges.clear();
100         fireGraphContentsChanged();
101     }
102 }