KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > controls > NeighborHighlightControl


1 package prefuse.controls;
2
3 import java.awt.event.MouseEvent JavaDoc;
4 import java.util.Iterator JavaDoc;
5
6 import prefuse.visual.EdgeItem;
7 import prefuse.visual.NodeItem;
8 import prefuse.visual.VisualItem;
9
10
11 /**
12  * <p>
13  * A ControlListener that sets the highlighted status (using the
14  * {@link prefuse.visual.VisualItem#setHighlighted(boolean)
15  * VisualItem.setHighlighted} method) for nodes neighboring the node
16  * currently under the mouse pointer. The highlight flag might then be used
17  * by a color function to change node appearance as desired.
18  * </p>
19  *
20  * @author <a HREF="http://jheer.org">jeffrey heer</a>
21  */

22 public class NeighborHighlightControl extends ControlAdapter {
23
24     private String JavaDoc activity = null;
25     private boolean highlightWithInvisibleEdge = false;
26     
27     /**
28      * Creates a new highlight control.
29      */

30     public NeighborHighlightControl() {
31         this(null);
32     }
33     
34     /**
35      * Creates a new highlight control that runs the given activity
36      * whenever the neighbor highlight changes.
37      * @param activity the update Activity to run
38      */

39     public NeighborHighlightControl(String JavaDoc activity) {
40         this.activity = activity;
41     }
42     
43     /**
44      * @see prefuse.controls.Control#itemEntered(prefuse.visual.VisualItem, java.awt.event.MouseEvent)
45      */

46     public void itemEntered(VisualItem item, MouseEvent JavaDoc e) {
47         if ( item instanceof NodeItem )
48             setNeighborHighlight((NodeItem)item, true);
49     }
50     
51     /**
52      * @see prefuse.controls.Control#itemExited(prefuse.visual.VisualItem, java.awt.event.MouseEvent)
53      */

54     public void itemExited(VisualItem item, MouseEvent JavaDoc e) {
55         if ( item instanceof NodeItem )
56             setNeighborHighlight((NodeItem)item, false);
57     }
58     
59     /**
60      * Set the highlighted state of the neighbors of a node.
61      * @param n the node under consideration
62      * @param state the highlighting state to apply to neighbors
63      */

64     protected void setNeighborHighlight(NodeItem n, boolean state) {
65         Iterator JavaDoc iter = n.edges();
66         while ( iter.hasNext() ) {
67             EdgeItem eitem = (EdgeItem)iter.next();
68             NodeItem nitem = eitem.getAdjacentItem(n);
69             if (eitem.isVisible() || highlightWithInvisibleEdge) {
70                 eitem.setHighlighted(state);
71                 nitem.setHighlighted(state);
72             }
73         }
74         if ( activity != null )
75             n.getVisualization().run(activity);
76     }
77     
78     /**
79      * Indicates if neighbor nodes with edges currently not visible still
80      * get highlighted.
81      * @return true if neighbors with invisible edges still get highlighted,
82      * false otherwise.
83      */

84     public boolean isHighlightWithInvisibleEdge() {
85         return highlightWithInvisibleEdge;
86     }
87    
88     /**
89      * Determines if neighbor nodes with edges currently not visible still
90      * get highlighted.
91      * @param highlightWithInvisibleEdge assign true if neighbors with invisible
92      * edges should still get highlighted, false otherwise.
93      */

94     public void setHighlightWithInvisibleEdge(boolean highlightWithInvisibleEdge) {
95         this.highlightWithInvisibleEdge = highlightWithInvisibleEdge;
96     }
97     
98 } // end of class NeighborHighlightControl
99
Popular Tags