View Javadoc

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.*;
53  
54  
55  /***  VisibleLocality:  Extends Locality to spefically handle the 
56    *  Nodes + Edges that are visible on screen.  The visible attribute
57    *  of the nodes + edges is set to true when they appear on screen, and
58    *  false when they are removed from screen.
59    *
60    *  Locality is used in conjunction with LocalityUtils, which handle
61    *  locality shift animations.
62    *
63    *
64    * @author   Alexander Shapiro
65    * @version  1.21  $Id: VisibleLocality.java,v 1.1.1.1 2004/02/06 08:44:07 keesj Exp $
66    */
67  public class VisibleLocality extends Locality {
68  
69      public VisibleLocality(GraphEltSet ges) {
70          super(ges);
71      }
72  
73      public void addNode( Node node ) throws TGException {
74          super.addNode(node);
75          node.setVisible(true);        
76      }
77  
78      public void addEdge( Edge edge ) {
79          if(!contains(edge)) {
80              super.addEdge(edge);            
81              edge.from.visibleEdgeCnt++;
82              edge.to.visibleEdgeCnt++;
83          }
84      }
85  
86      public boolean removeEdge( Edge edge ) {
87          boolean removed = super.removeEdge(edge);
88          if (removed) {
89              edge.setVisible(false);
90              edge.from.visibleEdgeCnt--;
91              edge.to.visibleEdgeCnt--;
92          }
93          return removed;
94      }
95  
96      public boolean removeNode( Node node ) {
97          boolean removed = super.removeNode(node);
98          if (removed) {
99              node.setVisible(false);            
100         }        
101         return removed;
102     }
103 
104     public synchronized void removeAll() {
105         for (int i = 0 ; i < nodeCount(); i++) {
106             nodeAt(i).setVisible(false);
107         }
108         for (int i = 0 ; i < edgeCount(); i++) {
109             edgeAt(i).setVisible(false);
110         }
111         super.removeAll();        
112     }
113     
114     public void updateLocalityFromVisibility() throws TGException {
115          //for (int i = 0 ; i < completeEltSet.nodeCount(); i++) {
116          //   Node n = nodeAt(i);
117         TGForEachNode fen = new TGForEachNode() {
118             public void forEachNode( Node node ) {
119                 try {
120                     if (node.isVisible() && !contains(node)) 
121                         addNode(node);
122                     else if (!node.isVisible() && contains(node))
123                         removeNode(node);
124                 }
125                 catch (TGException ex) { ex.printStackTrace(); }
126             }         
127         };
128         completeEltSet.forAllNodes(fen); 
129         
130         //for (int i = 0 ; i < edgeCount(); i++) {
131         //    Edge e = edgeAt(i);              
132         TGForEachEdge fee = new TGForEachEdge() { 
133             public void forEachEdge( Edge edge ) {                           
134                 if (edge.isVisible() && !contains(edge)) 
135                     addEdge(edge);
136                 else if (!edge.isVisible() && contains(edge))
137                     removeEdge(edge);                
138             }        
139         };
140         completeEltSet.forAllEdges(fee);    
141     }
142 } // end com.touchgraph.graphlayout.graphelements.VisibleLocality