1 package de.fzi.wim.guibase.graphview.selection;
2
3 import java.util.Collection;
4 import java.util.Collections;
5 import java.util.Set;
6 import java.util.HashSet;
7 import java.util.Iterator;
8 import java.util.EventListener;
9 import javax.swing.event.EventListenerList;
10
11 import de.fzi.wim.guibase.graphview.graph.*;
12
13 /***
14 * A default implementation of the node selection model.
15 */
16 public class DefaultNodeSelectionModel implements NodeSelectionModel {
17 /*** The list of registered listeners. */
18 protected EventListenerList m_eventListenerList;
19 /*** The set of selected nodes. */
20 protected Set m_selectedNodes;
21 /*** The graph being managed by this model. */
22 protected Graph m_graph;
23
24 /***
25 * Creates an instance of this class.
26 *
27 * @param graph the graph
28 */
29 public DefaultNodeSelectionModel(Graph graph) {
30 m_graph=graph;
31 m_eventListenerList=new EventListenerList();
32 m_selectedNodes=new HashSet();
33 m_graph.addGraphListener(new GraphHandler());
34 }
35 /***
36 * Adds a node to the selection.
37 *
38 * @param node the node to add to the selection
39 */
40 public void addNode(Node node) {
41 Set addedNode=new HashSet();
42 addedNode.add(node);
43 addNodes(addedNode);
44 }
45 /***
46 * Adds a collection of nodes to the selection.
47 *
48 * @param nodes the collection of nodes to add to the selection
49 */
50 public void addNodes(Collection nodes) {
51 if (m_selectedNodes.addAll(nodes))
52 fireNodesAddedToSelection(nodes);
53 }
54 /***
55 * Removes a node from the selection.
56 *
57 * @param node the node to remove from the selection
58 */
59 public void removeNode(Node node) {
60 Set removedNode=new HashSet();
61 removedNode.add(node);
62 addNodes(removedNode);
63 }
64 /***
65 * Removes a collection of nodes from the selection.
66 *
67 * @param nodes the collection of nodes to remove from the selection
68 */
69 public void removeNodes(Collection nodes) {
70 if (m_selectedNodes.removeAll(nodes))
71 fireNodesRemovedFromSelection(nodes);
72 }
73 /***
74 * Clears the selection.
75 */
76 public void clear() {
77 if (!m_selectedNodes.isEmpty()) {
78 m_selectedNodes.clear();
79 fireSelectionCleared();
80 }
81 }
82 /***
83 * Returns the selection.
84 *
85 * @return the selection
86 */
87 public Collection getSelectedNodes() {
88 return Collections.unmodifiableCollection(m_selectedNodes);
89 }
90 /***
91 * Checks whteher the node has been selected.
92 *
93 * @param node the node that is checked
94 * @return <code>true</code> if the node has been selected
95 */
96 public boolean isNodeSelected(Node node) {
97 return m_selectedNodes.contains(node);
98 }
99 /***
100 * Adds a listener to the selection.
101 *
102 * @param listener the listener to add to the selection
103 */
104 public void addNodeSelectionListener(NodeSelectionListener listener) {
105 m_eventListenerList.add(NodeSelectionListener.class,listener);
106 }
107 /***
108 * Removes a listener from the selection.
109 *
110 * @param listener the listener to add to the selection
111 */
112 public void removeNodeSelectionListener(NodeSelectionListener listener) {
113 m_eventListenerList.remove(NodeSelectionListener.class,listener);
114 }
115 /***
116 * Fires the event that the nodes have been added to the selection.
117 *
118 * @param nodes the collection of nodes added to the selection
119 */
120 protected void fireNodesAddedToSelection(Collection nodes) {
121 EventListener[] listeners=m_eventListenerList.getListeners(NodeSelectionListener.class);
122 for (int i=0;i<listeners.length;i++)
123 ((NodeSelectionListener)listeners[i]).nodesAddedToSelection(this,nodes);
124 }
125 /***
126 * Fires the event that the nodes have been removed from the selection.
127 *
128 * @param nodes the collection of nodes removed from the selection
129 */
130 protected void fireNodesRemovedFromSelection(Collection nodes) {
131 EventListener[] listeners=m_eventListenerList.getListeners(NodeSelectionListener.class);
132 for (int i=0;i<listeners.length;i++)
133 ((NodeSelectionListener)listeners[i]).nodesRemovedFromSelection(this,nodes);
134 }
135 /***
136 * Fires the event that the selection has been cleared.
137 */
138 protected void fireSelectionCleared() {
139 EventListener[] listeners=m_eventListenerList.getListeners(NodeSelectionListener.class);
140 for (int i=0;i<listeners.length;i++)
141 ((NodeSelectionListener)listeners[i]).selectionCleared(this);
142 }
143
144 /***
145 * Handles graph events.
146 */
147 protected class GraphHandler implements GraphListener {
148 public void graphLayoutUpdated(Graph graph) {
149 }
150 public void graphUpdated(Graph graph) {
151 }
152 public void graphContentsChanged(Graph graph) {
153 clear();
154 }
155 public void elementsAdded(Graph graph,Collection nodes,Collection edges) {
156 }
157 public void elementsRemoved(Graph graph,Collection nodes,Collection edges) {
158 if (nodes!=null) {
159 Set toRemove=null;
160 Iterator iterator=nodes.iterator();
161 while (iterator.hasNext()) {
162 Node node=(Node)iterator.next();
163 if (m_selectedNodes.contains(node)) {
164 if (toRemove==null)
165 toRemove=new HashSet();
166 toRemove.add(node);
167 }
168 }
169 if (toRemove!=null)
170 removeNodes(toRemove);
171 }
172 }
173 }
174 }