1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 package com.touchgraph.graphlayout.graphelements;
51
52 import java.util.Vector;
53
54 import com.touchgraph.graphlayout.*;
55
56 /*** Locality: A way of representing a subset of a larger set of nodes.
57 * Allows for both manipulation of the subset, and manipulation of the
58 * larger set. For instance, one can call removeNode to delete it from
59 * the subset, or deleteNode to remove it from the larger set.
60 *
61 * Locality is used in conjunction with LocalityUtils, which handle
62 * locality shift animations.
63 *
64 * More synchronization will almost definitely be required.
65 *
66 * @author Alexander Shapiro
67 * @version 1.21 $Id: Locality.java,v 1.1.1.1 2004/02/06 08:44:07 keesj Exp $
68 */
69 public class Locality extends GraphEltSet {
70
71 protected GraphEltSet completeEltSet;
72
73
74
75 /*** Constructor with GraphEltSet <tt>ges</tt>.
76 */
77 public Locality(GraphEltSet ges) {
78 super();
79 completeEltSet = ges;
80 }
81
82 public GraphEltSet getCompleteEltSet() {
83 return completeEltSet;
84 }
85
86 public void addNode( Node n ) throws TGException {
87 if (!contains(n)) {
88 super.addNode(n);
89
90
91 if (!completeEltSet.contains(n)) completeEltSet.addNode(n);
92 }
93 }
94
95 public void addEdge( Edge e ) {
96 if(!contains(e)) {
97 edges.addElement(e);
98
99
100 if (!completeEltSet.contains(e)) completeEltSet.addEdge(e);
101 }
102 }
103
104 public void addNodeWithEdges( Node n ) throws TGException {
105 addNode(n);
106 for (int i = 0 ; i < n.edgeCount(); i++) {
107 Edge e=n.edgeAt(i);
108 if(contains(e.getOtherEndpt(n))) addEdge(e);
109 }
110
111 }
112
113 public synchronized void addAll() throws TGException {
114 synchronized (completeEltSet) {
115 for (int i = 0 ; i<completeEltSet.nodeCount(); i++) {
116 addNode(completeEltSet.nodeAt(i));
117 }
118 for (int i = 0 ; i<completeEltSet.edgeCount(); i++) {
119 addEdge(completeEltSet.edgeAt(i));
120 }
121 }
122 }
123
124 public Edge findEdge( Node from, Node to ) {
125 Edge foundEdge=super.findEdge(from,to);
126 if (foundEdge!=null && edges.contains(foundEdge)) return foundEdge;
127 else return null;
128 }
129
130 public boolean deleteEdge( Edge e ) {
131 if (e == null) return false;
132 else {
133 removeEdge(e);
134 return completeEltSet.deleteEdge(e);
135 }
136 }
137
138 public synchronized void deleteEdges( Vector edgesToDelete ) {
139 removeEdges(edgesToDelete);
140 completeEltSet.deleteEdges(edgesToDelete);
141 }
142
143 public boolean removeEdge( Edge e ) {
144 if (e == null) return false;
145 else {
146 if(edges.removeElement(e)) {
147 return true;
148 }
149 return false;
150 }
151 }
152
153 public synchronized void removeEdges( Vector edgesToRemove ) {
154 for (int i=0;i<edgesToRemove.size();i++) {
155 removeEdge((Edge) edgesToRemove.elementAt(i));
156 }
157 }
158
159 public boolean deleteNode( Node node ) {
160 if ( node == null ) return false;
161 else {
162 removeNode(node);
163 return completeEltSet.deleteNode(node);
164 }
165 }
166
167 public synchronized void deleteNodes( Vector nodesToDelete ) {
168 removeNodes(nodesToDelete);
169 completeEltSet.deleteNodes(nodesToDelete);
170 }
171
172 public boolean removeNode( Node node ) {
173 if (node == null) return false;
174 if (!nodes.removeElement(node)) return false;
175
176 String id = node.getID();
177 if ( id != null ) nodeIDRegistry.remove(id);
178
179 for (int i = 0 ; i < node.edgeCount(); i++) {
180 removeEdge(node.edgeAt(i));
181 }
182
183 return true;
184 }
185
186 public synchronized void removeNodes( Vector nodesToRemove ) {
187 for (int i=0;i<nodesToRemove.size();i++) {
188 removeNode((Node) nodesToRemove.elementAt(i));
189 }
190 }
191
192 public synchronized void removeAll() {
193 super.clearAll();
194 }
195
196 public synchronized void clearAll() {
197 removeAll();
198 completeEltSet.clearAll();
199 }
200
201 }