~ubuntu-branches/ubuntu/saucy/libtggraphlayout-java/saucy

« back to all changes in this revision

Viewing changes to com/touchgraph/graphlayout/graphelements/Locality.java

  • Committer: Bazaar Package Importer
  • Author(s): Morten Sørensen
  • Date: 2009-12-06 17:57:32 UTC
  • Revision ID: james.westby@ubuntu.com-20091206175732-auvd39j1u30e0x50
Tags: upstream-122
Import upstream version 122

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * TouchGraph LLC. Apache-Style Software License
 
3
 *
 
4
 *
 
5
 * Copyright (c) 2001-2002 Alexander Shapiro. All rights reserved.
 
6
 *
 
7
 * Redistribution and use in source and binary forms, with or without
 
8
 * modification, are permitted provided that the following conditions
 
9
 * are met:
 
10
 *
 
11
 * 1. Redistributions of source code must retain the above copyright
 
12
 *    notice, this list of conditions and the following disclaimer. 
 
13
 *
 
14
 * 2. Redistributions in binary form must reproduce the above copyright
 
15
 *    notice, this list of conditions and the following disclaimer in
 
16
 *    the documentation and/or other materials provided with the
 
17
 *    distribution.
 
18
 *
 
19
 * 3. The end-user documentation included with the redistribution,
 
20
 *    if any, must include the following acknowledgment:  
 
21
 *       "This product includes software developed by 
 
22
 *        TouchGraph LLC (http://www.touchgraph.com/)."
 
23
 *    Alternately, this acknowledgment may appear in the software itself,
 
24
 *    if and wherever such third-party acknowledgments normally appear.
 
25
 *
 
26
 * 4. The names "TouchGraph" or "TouchGraph LLC" must not be used to endorse 
 
27
 *    or promote products derived from this software without prior written 
 
28
 *    permission.  For written permission, please contact 
 
29
 *    alex@touchgraph.com
 
30
 *
 
31
 * 5. Products derived from this software may not be called "TouchGraph",
 
32
 *    nor may "TouchGraph" appear in their name, without prior written
 
33
 *    permission of alex@touchgraph.com.
 
34
 *
 
35
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 
36
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 
37
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 
38
 * DISCLAIMED.  IN NO EVENT SHALL TOUCHGRAPH OR ITS CONTRIBUTORS BE 
 
39
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 
40
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 
41
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
 
42
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 
43
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 
44
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
 
45
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
46
 * ====================================================================
 
47
 *
 
48
 */
 
49
 
 
50
package com.touchgraph.graphlayout.graphelements;
 
51
 
 
52
import  com.touchgraph.graphlayout.Node;
 
53
import  com.touchgraph.graphlayout.Edge;
 
54
import  com.touchgraph.graphlayout.TGException;
 
55
 
 
56
import  java.util.Vector;
 
57
 
 
58
/**  Locality:  A way of representing a subset of a larger set of nodes.
 
59
  *  Allows for both manipulation of the subset, and manipulation of the
 
60
  *  larger set.  For instance, one can call removeNode to delete it from
 
61
  *  the subset, or deleteNode to remove it from the larger set.
 
62
  *
 
63
  *  Locality is used in conjunction with LocalityUtils, which handle
 
64
  *  locality shift animations.
 
65
  *
 
66
  *  More synchronization will almost definitely be required.
 
67
  *
 
68
  * @author   Alexander Shapiro
 
69
  * @version  1.22-jre1.1  $Id: Locality.java,v 1.1 2002/09/19 15:58:32 ldornbusch Exp $
 
70
  */
 
71
public class Locality extends GraphEltSet {
 
72
 
 
73
    protected GraphEltSet completeEltSet;
 
74
 
 
75
  // ............
 
76
 
 
77
  /** Constructor with GraphEltSet <tt>ges</tt>.
 
78
    */
 
79
    public Locality(GraphEltSet ges) {
 
80
        super();
 
81
        completeEltSet = ges;
 
82
    }
 
83
 
 
84
    public GraphEltSet getCompleteEltSet() {
 
85
        return completeEltSet;
 
86
    }
 
87
 
 
88
    public synchronized void addNode( Node n ) throws TGException {
 
89
        if (!contains(n)) {
 
90
            super.addNode(n);
 
91
            //If a new Node is created, and then added to Locality, then add the new edge
 
92
            //to completeEltSet as well.
 
93
            if (!completeEltSet.contains(n)) completeEltSet.addNode(n);
 
94
        }
 
95
    }
 
96
 
 
97
    public void addEdge( Edge e ) {
 
98
        if(!contains(e)) {
 
99
            edges.addElement(e);
 
100
            //If a new Edge is created, and then added to Locality, then add the new edge
 
101
            //to completeEltSet as well.
 
102
            if (!completeEltSet.contains(e)) completeEltSet.addEdge(e);
 
103
        }
 
104
    }
 
105
 
 
106
    public synchronized void addNodeWithEdges( Node n ) throws TGException {
 
107
        addNode(n);
 
108
        for (int i = 0 ; i < n.edgeCount(); i++) {
 
109
            Edge e=n.edgeAt(i);
 
110
            if(contains(e.getOtherEndpt(n))) addEdge(e);
 
111
        }
 
112
 
 
113
    }
 
114
    
 
115
    public synchronized void addAll() throws TGException {
 
116
        synchronized (completeEltSet) {
 
117
            for (int i = 0 ; i<completeEltSet.nodeCount(); i++) {
 
118
                addNode(completeEltSet.nodeAt(i));            
 
119
            }
 
120
            for (int i = 0 ; i<completeEltSet.edgeCount(); i++) {
 
121
                addEdge(completeEltSet.edgeAt(i));            
 
122
            }
 
123
        }
 
124
    }
 
125
 
 
126
    public Edge findEdge( Node from, Node to ) {
 
127
        Edge foundEdge=super.findEdge(from,to);
 
128
        if (foundEdge!=null && edges.contains(foundEdge)) return foundEdge;
 
129
        else return null;
 
130
    }
 
131
 
 
132
    public boolean deleteEdge( Edge e ) {
 
133
        if (e == null) return false;
 
134
        else {
 
135
            removeEdge(e);
 
136
            return completeEltSet.deleteEdge(e);
 
137
        }
 
138
    }
 
139
 
 
140
    public synchronized void deleteEdges( Vector edgesToDelete ) {
 
141
        removeEdges(edgesToDelete);
 
142
        completeEltSet.deleteEdges(edgesToDelete);
 
143
    }
 
144
 
 
145
    public boolean removeEdge( Edge e ) {
 
146
        if (e == null) return false;
 
147
            else {
 
148
                if(edges.removeElement(e)) {
 
149
                    return true;
 
150
                }
 
151
            return false;
 
152
        }
 
153
    }
 
154
 
 
155
    public synchronized void removeEdges( Vector edgesToRemove ) {
 
156
        for (int i=0;i<edgesToRemove.size();i++) {
 
157
            removeEdge((Edge) edgesToRemove.elementAt(i));
 
158
        }
 
159
    }
 
160
 
 
161
    public boolean deleteNode( Node node ) {
 
162
        if ( node == null ) return false;
 
163
        else {
 
164
            removeNode(node);
 
165
            return completeEltSet.deleteNode(node);
 
166
        }
 
167
    }
 
168
 
 
169
    public synchronized void deleteNodes( Vector nodesToDelete ) {
 
170
        removeNodes(nodesToDelete);
 
171
        completeEltSet.deleteNodes(nodesToDelete);
 
172
    }
 
173
 
 
174
    public boolean removeNode( Node node ) {
 
175
          if (node == null) return false;
 
176
          if (!nodes.removeElement(node)) return false;
 
177
                  
 
178
                  String id = node.getID();
 
179
          if ( id != null ) nodeIDRegistry.remove(id); // remove from registry
 
180
        
 
181
          for (int i = 0 ; i < node.edgeCount(); i++) {
 
182
             removeEdge(node.edgeAt(i));
 
183
         }
 
184
 
 
185
         return true;
 
186
    }
 
187
 
 
188
    public synchronized void removeNodes( Vector nodesToRemove ) {
 
189
        for (int i=0;i<nodesToRemove.size();i++) {
 
190
            removeNode((Node) nodesToRemove.elementAt(i));
 
191
        }
 
192
    }
 
193
 
 
194
    public synchronized void removeAll() {
 
195
        super.clearAll();        
 
196
    }
 
197
 
 
198
    public synchronized void clearAll() {
 
199
        removeAll();
 
200
        completeEltSet.clearAll();
 
201
    }
 
202
 
 
203
} // end com.touchgraph.graphlayout.graphelements.Locality