KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > LayoutStyle


1 /*
2  * @(#)LayoutStyle.java 1.2 05/11/17
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing;
8
9 import java.awt.Container JavaDoc;
10 import javax.swing.plaf.ComponentUI JavaDoc;
11 import sun.awt.AppContext;
12
13 /**
14  * <code>LayoutStyle</code> provides information about how to position
15  * components. This class is primarily useful for visual tools and
16  * layout managers. Most developers will not need to use this class.
17  * <p>
18  * You typically don't set or create a
19  * <code>LayoutStyle</code>. Instead use the static method
20  * <code>getInstance</code> to obtain the current instance.
21  *
22  * @version 1.2, 11/17/05
23  * @since 1.6
24  */

25 public abstract class LayoutStyle {
26     /**
27      * Sets the shared instance of <code>LayoutStyle</code>. Specifying
28      * <code>null</code> results in using the <code>LayoutStyle</code> from
29      * the current <code>LookAndFeel</code>.
30      *
31      * @param style the <code>LayoutStyle</code>, or <code>null</code>
32      * @see #getInstance
33      */

34     public static void setInstance(LayoutStyle JavaDoc style) {
35         synchronized(LayoutStyle JavaDoc.class) {
36             if (style == null) {
37                 AppContext.getAppContext().remove(LayoutStyle JavaDoc.class);
38             }
39             else {
40                 AppContext.getAppContext().put(LayoutStyle JavaDoc.class, style);
41             }
42         }
43     }
44
45     /**
46      * Returns the shared instance of <code>LayoutStyle</code>. If an instance
47      * has not been specified in <code>setInstance</code>, this will return
48      * the <code>LayoutStyle</code> from the current <code>LookAndFeel</code>.
49      *
50      * @see LookAndFeel#getLayoutStyle
51      * @return the shared instance of <code>LayoutStyle</code>
52      */

53     public static LayoutStyle JavaDoc getInstance() {
54         LayoutStyle JavaDoc style;
55         synchronized(LayoutStyle JavaDoc.class) {
56             style = (LayoutStyle JavaDoc)AppContext.getAppContext().
57                     get(LayoutStyle JavaDoc.class);
58         }
59         if (style == null) {
60             return UIManager.getLookAndFeel().getLayoutStyle();
61         }
62         return style;
63     }
64
65
66     /**
67      * <code>ComponentPlacement</code> is an enumeration of the
68      * possible ways two components can be placed relative to each
69      * other. <code>ComponentPlacement</code> is used by the
70      * <code>LayoutStyle</code> method <code>getPreferredGap</code>. Refer to
71      * <code>LayoutStyle</code> for more information.
72      *
73      * @see LayoutStyle#getPreferredGap(JComponent,JComponent,
74      * ComponentPlacement,int,Container)
75      * @since 1.6
76      */

77     public enum ComponentPlacement {
78         /**
79          * Enumeration value indicating the two components are
80          * visually related and will be placed in the same parent.
81          * For example, a <code>JLabel</code> providing a label for a
82          * <code>JTextField</code> is typically visually associated
83          * with the <code>JTextField</code>; the constant <code>RELATED</code>
84          * is used for this.
85          */

86         RELATED,
87
88         /**
89          * Enumeration value indicating the two components are
90          * visually unrelated and will be placed in the same parent.
91          * For example, groupings of components are usually visually
92          * separated; the constant <code>UNRELATED</code> is used for this.
93          */

94         UNRELATED,
95
96         /**
97          * Enumeration value indicating the distance to indent a component
98          * is being requested. For example, often times the children of
99          * a label will be horizontally indented from the label. To determine
100          * the preferred distance for such a gap use the
101          * <code>INDENT</code> type.
102          * <p>
103          * This value is typically only useful with a direction of
104          * <code>EAST</code> or <code>WEST</code>.
105          */

106         INDENT;
107     }
108
109
110     /**
111      * Creates a new <code>LayoutStyle</code>. You generally don't
112      * create a <code>LayoutStyle</code>. Instead use the method
113      * <code>getInstance</code> to obtain the current
114      * <code>LayoutStyle</code>.
115      */

116     public LayoutStyle() {
117     }
118
119     /**
120      * Returns the amount of space to use between two components.
121      * The return value indicates the distance to place
122      * <code>component2</code> relative to <code>component1</code>.
123      * For example, the following returns the amount of space to place
124      * between <code>component2</code> and <code>component1</code>
125      * when <code>component2</code> is placed vertically above
126      * <code>component1</code>:
127      * <pre>
128      * int gap = getPreferredGap(component1, component2,
129      * ComponentPlacement.RELATED,
130      * SwingConstants.NORTH, parent);
131      * </pre>
132      * The <code>type</code> parameter indicates the relation between
133      * the two components. If the two components will be contained in
134      * the same parent and are showing similar logically related
135      * items, use <code>RELATED</code>. If the two components will be
136      * contained in the same parent but show logically unrelated items
137      * use <code>UNRELATED</code>. Some look and feels may not
138      * distinguish between the <code>RELATED</code> and
139      * <code>UNRELATED</code> types.
140      * <p>
141      * The return value is not intended to take into account the
142      * current size and position of <code>component2</code> or
143      * <code>component1</code>. The return value may take into
144      * consideration various properties of the components. For
145      * example, the space may vary based on font size, or the preferred
146      * size of the component.
147      *
148      * @param component1 the <code>JComponent</code>
149      * <code>component2</code> is being placed relative to
150      * @param component2 the <code>JComponent</code> being placed
151      * @param position the position <code>component2</code> is being placed
152      * relative to <code>component1</code>; one of
153      * <code>SwingConstants.NORTH</code>,
154      * <code>SwingConstants.SOUTH</code>,
155      * <code>SwingConstants.EAST</code> or
156      * <code>SwingConstants.WEST</code>
157      * @param type how the two components are being placed
158      * @param parent the parent of <code>component2</code>; this may differ
159      * from the actual parent and it may be <code>null</code>
160      * @return the amount of space to place between the two components
161      * @throws NullPointerException if <code>component1</code>,
162      * <code>component2</code> or <code>type</code> is
163      * <code>null</code>
164      * @throws IllegalArgumentException if <code>position</code> is not
165      * one of <code>SwingConstants.NORTH</code>,
166      * <code>SwingConstants.SOUTH</code>,
167      * <code>SwingConstants.EAST</code> or
168      * <code>SwingConstants.WEST</code>
169      * @see LookAndFeel#getLayoutStyle
170      * @since 1.6
171      */

172     public abstract int getPreferredGap(JComponent JavaDoc component1,
173                                         JComponent JavaDoc component2,
174                                         ComponentPlacement type, int position,
175                                         Container JavaDoc parent);
176
177     /**
178      * Returns the amount of space to place between the component and specified
179      * edge of its parent.
180      *
181      * @param component the <code>JComponent</code> being positioned
182      * @param position the position <code>component</code> is being placed
183      * relative to its parent; one of
184      * <code>SwingConstants.NORTH</code>,
185      * <code>SwingConstants.SOUTH</code>,
186      * <code>SwingConstants.EAST</code> or
187      * <code>SwingConstants.WEST</code>
188      * @param parent the parent of <code>component</code>; this may differ
189      * from the actual parent and may be <code>null</code>
190      * @return the amount of space to place between the component and specified
191      * edge
192      * @throws IllegalArgumentException if <code>position</code> is not
193      * one of <code>SwingConstants.NORTH</code>,
194      * <code>SwingConstants.SOUTH</code>,
195      * <code>SwingConstants.EAST</code> or
196      * <code>SwingConstants.WEST</code>
197      */

198     public abstract int getContainerGap(JComponent JavaDoc component, int position,
199                                         Container JavaDoc parent);
200 }
201
Popular Tags