KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > action > layout > Layout


1 package prefuse.action.layout;
2
3 import java.awt.Insets JavaDoc;
4 import java.awt.geom.Point2D JavaDoc;
5 import java.awt.geom.Rectangle2D JavaDoc;
6
7 import prefuse.Display;
8 import prefuse.action.GroupAction;
9 import prefuse.util.PrefuseLib;
10 import prefuse.visual.VisualItem;
11
12 /**
13  * Abstract base class providing convenience methods for layout algorithms.
14  *
15  * @author <a HREF="http://jheer.org">jeffrey heer</a>
16  */

17 public abstract class Layout extends GroupAction {
18
19     /** The explicitly set layout bounds. May be null. */
20     protected Rectangle2D JavaDoc m_bounds = null;
21     /** The explicitly set anchor point at which the layout can
22      * be centered or rooted. May be null. */

23     protected Point2D JavaDoc m_anchor = null;
24     
25     protected boolean m_margin = false;
26     protected Insets JavaDoc m_insets = new Insets JavaDoc(0,0,0,0);
27     protected double[] m_bpts = new double[4];
28     protected Rectangle2D JavaDoc m_tmpb = new Rectangle2D.Double JavaDoc();
29     protected Point2D JavaDoc m_tmpa = new Point2D.Double JavaDoc();
30     
31     // ------------------------------------------------------------------------
32

33     /**
34      * Create a new Layout.
35      */

36     public Layout() {
37         super();
38     }
39     
40     /**
41      * Create a new Layout.
42      * @param group the data group to layout.
43      */

44     public Layout(String JavaDoc group) {
45         super(group);
46     }
47
48     public Layout(String JavaDoc group, long duration) {
49         super(group, duration);
50     }
51     
52     // ------------------------------------------------------------------------
53

54     /**
55      * Set the margins the layout should observe within its layout bounds.
56      * @param top the top margin, in pixels
57      * @param left the left margin, in pixels
58      * @param bottom the bottom margin, in pixels
59      * @param right the right margin, in pixels
60      */

61     public void setMargin(int top, int left, int bottom, int right) {
62         m_insets.top = top;
63         m_insets.left = left;
64         m_insets.bottom = bottom;
65         m_insets.right = right;
66         m_margin = true;
67     }
68     
69     /**
70      * Returns the bounds in which the layout should be computed. If the
71      * bounds have been explicitly set, that value is used. Otherwise,
72      * an attempt is made to compute the bounds based upon the display
73      * region of the first display found in this action's associated
74      * Visualization.
75      * @return the layout bounds within which to constain the layout.
76      */

77     public Rectangle2D JavaDoc getLayoutBounds() {
78         if ( m_bounds != null )
79             return m_bounds;
80         
81         if ( m_vis != null && m_vis.getDisplayCount() > 0 )
82         {
83             Display d = m_vis.getDisplay(0);
84             Insets JavaDoc i = m_margin ? m_insets : d.getInsets(m_insets);
85             m_bpts[0] = i.left;
86             m_bpts[1] = i.top;
87             m_bpts[2] = d.getWidth()-i.right;
88             m_bpts[3] = d.getHeight()-i.bottom;
89             d.getInverseTransform().transform(m_bpts,0,m_bpts,0,2);
90             m_tmpb.setRect(m_bpts[0],m_bpts[1],
91                           m_bpts[2]-m_bpts[0],
92                           m_bpts[3]-m_bpts[1]);
93             return m_tmpb;
94         } else {
95             return null;
96         }
97     }
98     
99     /**
100      * Explicitly set the layout bounds. A reference to the input rectangle
101      * instance is maintained, not a copy, and so any subsequent changes to
102      * the rectangle object will also change the layout bounds.
103      * @param b a rectangle specifying the layout bounds. A reference to this
104      * same instance is kept.
105      */

106     public void setLayoutBounds(Rectangle2D JavaDoc b) {
107         m_bounds = b;
108     }
109      
110     /**
111      * Return the layout anchor at which to center or root the layout. How this
112      * point is used (if it is used at all) is dependent on the particular
113      * Layout implementation. If no anchor point has been explicitly set, the
114      * center coordinate for the first display found in this action's
115      * associated Visualization is used, if available.
116      * @return the layout anchor point.
117      */

118     public Point2D JavaDoc getLayoutAnchor() {
119         if ( m_anchor != null )
120             return m_anchor;
121         
122         m_tmpa.setLocation(0,0);
123         if ( m_vis != null ) {
124             Display d = m_vis.getDisplay(0);
125             m_tmpa.setLocation(d.getWidth()/2.0,d.getHeight()/2.0);
126             d.getInverseTransform().transform(m_tmpa, m_tmpa);
127         }
128         return m_tmpa;
129     }
130     
131     /**
132      * Explicitly set the layout anchor point. The provided object will be
133      * used directly (rather than copying its values), so subsequent
134      * changes to that point object will change the layout anchor.
135      * @param a the layout anchor point to use
136      */

137     public void setLayoutAnchor(Point2D JavaDoc a) {
138         m_anchor = a;
139     }
140     
141     /**
142      * Convenience method for setting an x-coordinate. The start value of the
143      * x-coordinate will be set to the current value, and the current and end
144      * values will be set to the provided x-coordinate. If the current value
145      * is not a number (NaN), the x-coordinate of the provided referrer
146      * item (if non null) will be used to set the start coordinate.
147      * @param item the item to set
148      * @param referrer the referrer item to use for the start location if
149      * the current valu eis not a number (NaN)
150      * @param x the x-coordinate value to set. This will be set for both
151      * the current and end values.
152      * @see prefuse.util.PrefuseLib#setX(VisualItem, VisualItem, double)
153      */

154     public void setX(VisualItem item, VisualItem referrer, double x) {
155         PrefuseLib.setX(item, referrer, x);
156     }
157     
158     /**
159      * Convenience method for setting an y-coordinate. The start value of the
160      * y-coordinate will be set to the current value, and the current and end
161      * values will be set to the provided y-coordinate. If the current value
162      * is not a number (NaN), the y-coordinate of the provided referrer
163      * item (if non null) will be used to set the start coordinate.
164      * @param item the item to set
165      * @param referrer the referrer item to use for the start location if
166      * the current valu eis not a number (NaN)
167      * @param y the y-coordinate value to set. This will be set for both
168      * the current and end values.
169      * @see prefuse.util.PrefuseLib#setY(VisualItem, VisualItem, double)
170      */

171     public void setY(VisualItem item, VisualItem referrer, double y) {
172         PrefuseLib.setY(item, referrer, y);
173     }
174
175 } // end of abstract class Layout
176
Popular Tags