1 package de.fzi.wim.guibase.graphview.graph;
2
3 import java.util.Collection;
4 import java.util.EventListener;
5 import javax.swing.event.EventListenerList;
6
7 /***
8 * An abstract base class for all graphs.
9 */
10 public abstract class AbstractGraph implements Graph {
11 /*** The list of listeners. */
12 protected EventListenerList m_listeners;
13
14 /***
15 * Creates a new graph.
16 */
17 public AbstractGraph() {
18 m_listeners=new EventListenerList();
19 }
20 /***
21 * Adds a listener to the graph.
22 *
23 * @param listener the listener added to the graph
24 */
25 public void addGraphListener(GraphListener listener) {
26 m_listeners.add(GraphListener.class,listener);
27 }
28 /***
29 * Removes a listener from the graph.
30 *
31 * @param listener the listener removed from the graph
32 */
33 public void removeGraphListener(GraphListener listener) {
34 m_listeners.remove(GraphListener.class,listener);
35 }
36 /***
37 * Notifies the graph that its elements have been updated.
38 */
39 public synchronized void notifyLayoutUpdated() {
40 EventListener[] listeners=m_listeners.getListeners(GraphListener.class);
41 for (int i=0;i<listeners.length;i++)
42 ((GraphListener)listeners[i]).graphLayoutUpdated(this);
43 }
44 /***
45 * Notifies the graph that its nodes have been updated, but that no node has changed position.
46 */
47 public synchronized void notifyUpdated() {
48 EventListener[] listeners=m_listeners.getListeners(GraphListener.class);
49 for (int i=0;i<listeners.length;i++)
50 ((GraphListener)listeners[i]).graphUpdated(this);
51 }
52 /***
53 * Fires the graph contents changed event.
54 */
55 protected void fireGraphContentsChanged() {
56 EventListener[] listeners=m_listeners.getListeners(GraphListener.class);
57 for (int i=0;i<listeners.length;i++)
58 ((GraphListener)listeners[i]).graphContentsChanged(this);
59 }
60 /***
61 * Fires the elements added event.
62 *
63 * @param nodes the collection of nodes
64 * @param edges the collection of edges
65 */
66 protected void fireElementsAdded(Collection nodes,Collection edges) {
67 EventListener[] listeners=m_listeners.getListeners(GraphListener.class);
68 for (int i=0;i<listeners.length;i++)
69 ((GraphListener)listeners[i]).elementsAdded(this,nodes,edges);
70 }
71 /***
72 * Fires the elements removed event.
73 *
74 * @param nodes the collection of nodes
75 * @param edges the collection of edges
76 */
77 protected void fireElementsRemoved(Collection nodes,Collection edges) {
78 EventListener[] listeners=m_listeners.getListeners(GraphListener.class);
79 for (int i=0;i<listeners.length;i++)
80 ((GraphListener)listeners[i]).elementsRemoved(this,nodes,edges);
81 }
82 }