KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > controls > FocusControl


1 package prefuse.controls;
2
3 import java.awt.Cursor JavaDoc;
4 import java.awt.event.MouseEvent JavaDoc;
5 import java.util.logging.Logger JavaDoc;
6
7 import prefuse.Display;
8 import prefuse.Visualization;
9 import prefuse.data.expression.Predicate;
10 import prefuse.data.tuple.TupleSet;
11 import prefuse.util.StringLib;
12 import prefuse.util.ui.UILib;
13 import prefuse.visual.VisualItem;
14
15
16 /**
17  * <p>Updates the contents of a TupleSet of focus items in response to mouse
18  * actions. For example, clicking a node or double-clicking a node could
19  * update its focus status. This Control supports monitoring a specified
20  * number of clicks to executing a focus change. By default a click pattern
21  * will cause a VisualItem to become the sole member of the focus group.
22  * Hold down the control key while clicking to add an item to a group
23  * without removing the current members.</p>
24  *
25  * <p>Updating a focus group does not necessarily cause
26  * the display to change. For this functionality, either register an action
27  * with this control, or register a TupleSetListener with the focus group.
28  * </p>
29  *
30  * @author <a HREF="http://jheer.org">jeffrey heer</a>
31  */

32 public class FocusControl extends ControlAdapter {
33
34     private String JavaDoc group = Visualization.FOCUS_ITEMS;
35     protected String JavaDoc activity;
36     protected VisualItem curFocus;
37     protected int ccount;
38     protected int button = Control.LEFT_MOUSE_BUTTON;
39     protected Predicate filter = null;
40     
41     /**
42      * Creates a new FocusControl that changes the focus to another item
43      * when that item is clicked once.
44      */

45     public FocusControl() {
46         this(1);
47     }
48     
49     /**
50      * Creates a new FocusControl that changes the focus to another item
51      * when that item is clicked once.
52      * @param focusGroup the name of the focus group to use
53      */

54     public FocusControl(String JavaDoc focusGroup) {
55         this(1);
56         group = focusGroup;
57     }
58     
59     /**
60      * Creates a new FocusControl that changes the focus when an item is
61      * clicked the specified number of times. A click value of zero indicates
62      * that the focus should be changed in response to mouse-over events.
63      * @param clicks the number of clicks needed to switch the focus.
64      */

65     public FocusControl(int clicks) {
66         ccount = clicks;
67     }
68     
69     /**
70      * Creates a new FocusControl that changes the focus when an item is
71      * clicked the specified number of times. A click value of zero indicates
72      * that the focus should be changed in response to mouse-over events.
73      * @param focusGroup the name of the focus group to use
74      * @param clicks the number of clicks needed to switch the focus.
75      */

76     public FocusControl(String JavaDoc focusGroup, int clicks) {
77         ccount = clicks;
78         group = focusGroup;
79     }
80     
81     /**
82      * Creates a new FocusControl that changes the focus when an item is
83      * clicked the specified number of times. A click value of zero indicates
84      * that the focus should be changed in response to mouse-over events.
85      * @param clicks the number of clicks needed to switch the focus.
86      * @param act an action run to upon focus change
87      */

88     public FocusControl(int clicks, String JavaDoc act) {
89         ccount = clicks;
90         activity = act;
91     }
92     
93     /**
94      * Creates a new FocusControl that changes the focus when an item is
95      * clicked the specified number of times. A click value of zero indicates
96      * that the focus should be changed in response to mouse-over events.
97      * @param focusGroup the name of the focus group to use
98      * @param clicks the number of clicks needed to switch the focus.
99      * @param act an action run to upon focus change
100      */

101     public FocusControl(String JavaDoc focusGroup, int clicks, String JavaDoc act) {
102         ccount = clicks;
103         activity = act;
104         this.group = focusGroup;
105     }
106     
107     // ------------------------------------------------------------------------
108

109     /**
110      * Set a filter for processing items by this focus control. Only items for
111      * which the predicate returns true (or doesn't throw an exception) will
112      * be considered by this control. A null value indicates that no filtering
113      * should be applied. That is, all items will be considered.
114      * @param p the filtering predicate to apply
115      */

116     public void setFilter(Predicate p) {
117         this.filter = p;
118     }
119     
120     /**
121      * Get the filter for processing items by this focus control. Only items
122      * for which the predicate returns true (or doesn't throw an exception)
123      * are considered by this control. A null value indicates that no
124      * filtering is applied.
125      * @return the filtering predicate
126      */

127     public Predicate getFilter() {
128         return filter;
129     }
130     
131     /**
132      * Perform a filtering check on the input item.
133      * @param item the item to check against the filter
134      * @return true if the item should be considered, false otherwise
135      */

136     protected boolean filterCheck(VisualItem item) {
137         if ( filter == null )
138             return true;
139         
140         try {
141             return filter.getBoolean(item);
142         } catch ( Exception JavaDoc e ) {
143             Logger.getLogger(getClass().getName()).warning(
144                 e.getMessage() + "\n" + StringLib.getStackTrace(e));
145             return false;
146         }
147     }
148     
149     // ------------------------------------------------------------------------
150

151     /**
152      * @see prefuse.controls.Control#itemEntered(prefuse.visual.VisualItem, java.awt.event.MouseEvent)
153      */

154     public void itemEntered(VisualItem item, MouseEvent JavaDoc e) {
155         if ( !filterCheck(item) ) return;
156         Display d = (Display)e.getSource();
157         d.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
158         if ( ccount == 0 ) {
159             Visualization vis = item.getVisualization();
160             TupleSet ts = vis.getFocusGroup(group);
161             ts.setTuple(item);
162             curFocus = item;
163             runActivity(vis);
164         }
165     }
166     
167     /**
168      * @see prefuse.controls.Control#itemExited(prefuse.visual.VisualItem, java.awt.event.MouseEvent)
169      */

170     public void itemExited(VisualItem item, MouseEvent JavaDoc e) {
171         if ( !filterCheck(item) ) return;
172         Display d = (Display)e.getSource();
173         d.setCursor(Cursor.getDefaultCursor());
174         if ( ccount == 0 ) {
175             curFocus = null;
176             Visualization vis = item.getVisualization();
177             TupleSet ts = vis.getFocusGroup(group);
178             ts.removeTuple(item);
179             runActivity(vis);
180         }
181     }
182     
183     /**
184      * @see prefuse.controls.Control#itemClicked(prefuse.visual.VisualItem, java.awt.event.MouseEvent)
185      */

186     public void itemClicked(VisualItem item, MouseEvent JavaDoc e) {
187         if ( !filterCheck(item) ) return;
188         if ( UILib.isButtonPressed(e, button) &&
189              e.getClickCount() == ccount )
190         {
191             if ( item != curFocus ) {
192                 Visualization vis = item.getVisualization();
193                 TupleSet ts = vis.getFocusGroup(group);
194                     
195                 boolean ctrl = e.isControlDown();
196                 if ( !ctrl ) {
197                     curFocus = item;
198                     ts.setTuple(item);
199                 } else if ( ts.containsTuple(item) ) {
200                     ts.removeTuple(item);
201                 } else {
202                     ts.addTuple(item);
203                 }
204                 runActivity(vis);
205                 
206             } else if ( e.isControlDown() ) {
207                 Visualization vis = item.getVisualization();
208                 TupleSet ts = vis.getFocusGroup(group);
209                 ts.removeTuple(item);
210                 curFocus = null;
211                 runActivity(vis);
212             }
213         }
214     }
215     
216     private void runActivity(Visualization vis) {
217         if ( activity != null ) {
218             vis.run(activity);
219         }
220     }
221     
222 } // end of class FocusControl
223
Popular Tags