15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package sun.swing;
26
27 import java.awt.Container;
28 import java.awt.Insets;
29 import javax.swing.*;
30 import javax.swing.LayoutStyle.ComponentPlacement;
31 import javax.swing.border.Border;
32 import javax.swing.plaf.UIResource;
33
34 /**
35 * An implementation of <code>LayoutStyle</code> that returns 6 for related
36 * components, otherwise 12. This class also provides helper methods for
37 * subclasses.
38 *
39 */
40 public class DefaultLayoutStyle extends LayoutStyle {
41 private static final DefaultLayoutStyle INSTANCE =
42 new DefaultLayoutStyle();
43
44 public static LayoutStyle getInstance() {
45 return INSTANCE;
46 }
47
48 @Override
49 public int getPreferredGap(JComponent component1, JComponent component2,
50 ComponentPlacement type, int position, Container parent) {
51 if (component1 == null || component2 == null || type == null) {
52 throw new NullPointerException();
53 }
54
55 checkPosition(position);
116 }
117
118 /**
119 * For some look and feels check boxs and radio buttons typically
120 * don't paint the border, yet they have padding for a border. Look
121 * and feel guidelines generally don't include this space. Use
122 * this method to subtract this space from the specified
123 * components.
124 *
125 * @param source Component
126 * @param position Position doing layout along.
127 * @param offset Ideal offset, not including border/margin
128 * @return offset - border/margin around the component.
129 */
130 protected int getButtonGap(JComponent source, int position, int offset) {
131 offset -= getButtonGap(source, position);
132 return Math.max(offset, 0);
133 }
134
135 /**
136 * If <code>c</code> is a check box or radio button, and the border is
137 * not painted this returns the inset along the specified axis.
138 */
139 public int getButtonGap(JComponent c, int position) {
140 String classID = c.getUIClassID();
141 if ((classID == "CheckBoxUI" || classID == "RadioButtonUI") &&
142 !((AbstractButton)c).isBorderPainted()) {
143 Border border = c.getBorder();
144 if (border instanceof UIResource) {
145 return getInset(c, position);
146 }
147 }
148 return 0;
149 }
150
151 private void checkPosition(int position) {
152 if (position != SwingConstants.NORTH &&
153 position != SwingConstants.SOUTH &&
154 position != SwingConstants.WEST &&
155 position != SwingConstants.EAST) {
156 throw new IllegalArgumentException();
|
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package sun.swing;
26
27 import java.awt.Container;
28 import java.awt.Insets;
29 import javax.swing.*;
30 import javax.swing.LayoutStyle.ComponentPlacement;
31 import javax.swing.border.Border;
32 import javax.swing.plaf.UIResource;
33
34 /**
35 * An implementation of {@code LayoutStyle} that returns 6 for related
36 * components, otherwise 12. This class also provides helper methods for
37 * subclasses.
38 *
39 */
40 public class DefaultLayoutStyle extends LayoutStyle {
41 private static final DefaultLayoutStyle INSTANCE =
42 new DefaultLayoutStyle();
43
44 public static LayoutStyle getInstance() {
45 return INSTANCE;
46 }
47
48 @Override
49 public int getPreferredGap(JComponent component1, JComponent component2,
50 ComponentPlacement type, int position, Container parent) {
51 if (component1 == null || component2 == null || type == null) {
52 throw new NullPointerException();
53 }
54
55 checkPosition(position);
116 }
117
118 /**
119 * For some look and feels check boxs and radio buttons typically
120 * don't paint the border, yet they have padding for a border. Look
121 * and feel guidelines generally don't include this space. Use
122 * this method to subtract this space from the specified
123 * components.
124 *
125 * @param source Component
126 * @param position Position doing layout along.
127 * @param offset Ideal offset, not including border/margin
128 * @return offset - border/margin around the component.
129 */
130 protected int getButtonGap(JComponent source, int position, int offset) {
131 offset -= getButtonGap(source, position);
132 return Math.max(offset, 0);
133 }
134
135 /**
136 * If {@code c} is a check box or radio button, and the border is
137 * not painted this returns the inset along the specified axis.
138 */
139 public int getButtonGap(JComponent c, int position) {
140 String classID = c.getUIClassID();
141 if ((classID == "CheckBoxUI" || classID == "RadioButtonUI") &&
142 !((AbstractButton)c).isBorderPainted()) {
143 Border border = c.getBorder();
144 if (border instanceof UIResource) {
145 return getInset(c, position);
146 }
147 }
148 return 0;
149 }
150
151 private void checkPosition(int position) {
152 if (position != SwingConstants.NORTH &&
153 position != SwingConstants.SOUTH &&
154 position != SwingConstants.WEST &&
155 position != SwingConstants.EAST) {
156 throw new IllegalArgumentException();
|