KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > controls > ZoomToFitControl


1 package prefuse.controls;
2
3 import java.awt.event.MouseEvent JavaDoc;
4 import java.awt.geom.Rectangle2D JavaDoc;
5
6 import prefuse.Display;
7 import prefuse.Visualization;
8 import prefuse.util.GraphicsLib;
9 import prefuse.util.display.DisplayLib;
10 import prefuse.util.ui.UILib;
11 import prefuse.visual.VisualItem;
12
13
14 /**
15  * Zooms a display such that all items within a given group will fit within
16  * the display bounds. By default, this achieved by clicking the right
17  * mouse button once, with no dragging.
18  *
19  * @author <a HREF="http://jheer.org">jeffrey heer</a>
20  */

21 public class ZoomToFitControl extends ControlAdapter {
22
23     private long m_duration = 2000;
24     private int m_margin = 50;
25     private int m_button = RIGHT_MOUSE_BUTTON;
26     private boolean m_zoomOverItem = true;
27     private String JavaDoc m_group = Visualization.ALL_ITEMS;
28     
29     /**
30      * Create a new ZoomToFitControl.
31      */

32     public ZoomToFitControl() {
33     }
34     
35     /**
36      * Create a new ZoomToFitControl.
37      * @param group the data group that should fit the Display
38      */

39     public ZoomToFitControl(String JavaDoc group) {
40         this.m_group = group;
41     }
42     
43     /**
44      * Create a new ZoomToFitControl.
45      * @param button the mouse button used to initiate the zoom-to-fit. One of
46      * {@link Control#LEFT_MOUSE_BUTTON}, {@link Control#MIDDLE_MOUSE_BUTTON},
47      * or {@link Control#RIGHT_MOUSE_BUTTON}.
48      */

49     public ZoomToFitControl(int button) {
50         this.m_button = button;
51     }
52
53     /**
54      * Create a new ZoomToFitControl.
55      * @param group the data group that should fit the Display
56      * @param button the mouse button used to initiate the zoom-to-fit. One of
57      * {@link Control#LEFT_MOUSE_BUTTON}, {@link Control#MIDDLE_MOUSE_BUTTON},
58      * or {@link Control#RIGHT_MOUSE_BUTTON}.
59      */

60     public ZoomToFitControl(String JavaDoc group, int button) {
61         this.m_group = group;
62         this.m_button = button;
63     }
64     
65     /**
66      * Create a new ZoomToFitControl.
67      * @param group the data group that should fit the Display
68      * @param margin the margin, in pixels, desired between the group
69      * and the edge of the display
70      * @param duration the duration of the animated zoom
71      * @param button the mouse button used to initiate the zoom-to-fit. One of
72      * {@link Control#LEFT_MOUSE_BUTTON}, {@link Control#MIDDLE_MOUSE_BUTTON},
73      * or {@link Control#RIGHT_MOUSE_BUTTON}.
74      */

75     public ZoomToFitControl(String JavaDoc group, int margin,
76                             long duration, int button)
77     {
78         this.m_group = group;
79         this.m_margin = margin;
80         this.m_duration = duration;
81         this.m_button = button;
82     }
83     
84     /**
85      * @see prefuse.controls.Control#itemClicked(prefuse.visual.VisualItem, java.awt.event.MouseEvent)
86      */

87     public void itemClicked(VisualItem item, MouseEvent JavaDoc e) {
88         if ( m_zoomOverItem )
89             mouseClicked(e);
90     }
91     
92     /**
93      * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
94      */

95     public void mouseClicked(MouseEvent JavaDoc e) {
96         Display display = (Display)e.getComponent();
97         if ( !display.isTranformInProgress() &&
98               UILib.isButtonPressed(e, m_button) )
99         {
100             Visualization vis = display.getVisualization();
101             Rectangle2D JavaDoc bounds = vis.getBounds(m_group);
102             GraphicsLib.expand(bounds, m_margin + (int)(1/display.getScale()));
103             DisplayLib.fitViewToBounds(display, bounds, m_duration);
104         }
105     }
106     
107     /**
108      * Indicates if the zoom control will work while the mouse is
109      * over a VisualItem.
110      * @return true if the control still operates over a VisualItem
111      */

112     public boolean isZoomOverItem() {
113         return m_zoomOverItem;
114     }
115
116     /**
117      * Determines if the zoom control will work while the mouse is
118      * over a VisualItem
119      * @param zoomOverItem true to indicate the control operates
120      * over VisualItems, false otherwise
121      */

122     public void setZoomOverItem(boolean zoomOverItem) {
123         this.m_zoomOverItem = zoomOverItem;
124     }
125     
126     /**
127      * Get the display margin to include within the "zoomed-to-fit" bounds.
128      * @return Display margin currently in use
129      */

130     public int getMargin() {
131         return m_margin;
132     }
133     
134     /**
135      * Set the display margin to include within the "zoomed-to-fit" bounds.
136      * @param margin Display margin to use
137      */

138     public void setMargin(int margin) {
139         this.m_margin = margin;
140     }
141     
142 } // end of class ZoomToFitControl
143
Popular Tags