0N/A/*
2362N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage sun.swing;
0N/A
0N/Aimport java.awt.Container;
0N/Aimport java.awt.Insets;
0N/Aimport javax.swing.*;
0N/Aimport javax.swing.LayoutStyle.ComponentPlacement;
0N/Aimport javax.swing.border.Border;
0N/Aimport javax.swing.plaf.UIResource;
0N/A
0N/A/**
0N/A * An implementation of <code>LayoutStyle</code> that returns 6 for related
0N/A * components, otherwise 12. This class also provides helper methods for
0N/A * subclasses.
0N/A *
0N/A */
0N/Apublic class DefaultLayoutStyle extends LayoutStyle {
0N/A private static final DefaultLayoutStyle INSTANCE =
0N/A new DefaultLayoutStyle();
0N/A
0N/A public static LayoutStyle getInstance() {
0N/A return INSTANCE;
0N/A }
0N/A
0N/A @Override
0N/A public int getPreferredGap(JComponent component1, JComponent component2,
0N/A ComponentPlacement type, int position, Container parent) {
0N/A if (component1 == null || component2 == null || type == null) {
0N/A throw new NullPointerException();
0N/A }
4356N/A
4356N/A checkPosition(position);
4356N/A
0N/A if (type == ComponentPlacement.INDENT &&
0N/A (position == SwingConstants.EAST ||
0N/A position == SwingConstants.WEST)) {
0N/A int indent = getIndent(component1, position);
0N/A if (indent > 0) {
0N/A return indent;
0N/A }
0N/A }
0N/A return (type == ComponentPlacement.UNRELATED) ? 12 : 6;
0N/A }
0N/A
0N/A @Override
0N/A public int getContainerGap(JComponent component, int position,
0N/A Container parent) {
0N/A if (component == null) {
0N/A throw new NullPointerException();
0N/A }
0N/A checkPosition(position);
0N/A return 6;
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the classes identify a JLabel and a non-JLabel
0N/A * along the horizontal axis.
0N/A */
0N/A protected boolean isLabelAndNonlabel(JComponent c1, JComponent c2,
0N/A int position) {
0N/A if (position == SwingConstants.EAST ||
0N/A position == SwingConstants.WEST) {
0N/A boolean c1Label = (c1 instanceof JLabel);
0N/A boolean c2Label = (c2 instanceof JLabel);
0N/A return ((c1Label || c2Label) && (c1Label != c2Label));
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * For some look and feels check boxs and radio buttons typically
0N/A * don't paint the border, yet they have padding for a border. Look
0N/A * and feel guidelines generally don't include this space. Use
0N/A * this method to subtract this space from the specified
0N/A * components.
0N/A *
0N/A * @param source First component
0N/A * @param target Second component
0N/A * @param position Position doing layout along.
0N/A * @param offset Ideal offset, not including border/margin
0N/A * @return offset - border/margin around the component.
0N/A */
0N/A protected int getButtonGap(JComponent source, JComponent target,
0N/A int position, int offset) {
0N/A offset -= getButtonGap(source, position);
0N/A if (offset > 0) {
0N/A offset -= getButtonGap(target, flipDirection(position));
0N/A }
0N/A if (offset < 0) {
0N/A return 0;
0N/A }
0N/A return offset;
0N/A }
0N/A
0N/A /**
0N/A * For some look and feels check boxs and radio buttons typically
0N/A * don't paint the border, yet they have padding for a border. Look
0N/A * and feel guidelines generally don't include this space. Use
0N/A * this method to subtract this space from the specified
0N/A * components.
0N/A *
0N/A * @param source Component
0N/A * @param position Position doing layout along.
0N/A * @param offset Ideal offset, not including border/margin
0N/A * @return offset - border/margin around the component.
0N/A */
0N/A protected int getButtonGap(JComponent source, int position, int offset) {
0N/A offset -= getButtonGap(source, position);
0N/A return Math.max(offset, 0);
0N/A }
0N/A
0N/A /**
0N/A * If <code>c</code> is a check box or radio button, and the border is
0N/A * not painted this returns the inset along the specified axis.
0N/A */
0N/A public int getButtonGap(JComponent c, int position) {
0N/A String classID = c.getUIClassID();
0N/A if ((classID == "CheckBoxUI" || classID == "RadioButtonUI") &&
0N/A !((AbstractButton)c).isBorderPainted()) {
0N/A Border border = c.getBorder();
0N/A if (border instanceof UIResource) {
0N/A return getInset(c, position);
0N/A }
0N/A }
0N/A return 0;
0N/A }
0N/A
0N/A private void checkPosition(int position) {
0N/A if (position != SwingConstants.NORTH &&
0N/A position != SwingConstants.SOUTH &&
0N/A position != SwingConstants.WEST &&
0N/A position != SwingConstants.EAST) {
0N/A throw new IllegalArgumentException();
0N/A }
0N/A }
0N/A
0N/A protected int flipDirection(int position) {
0N/A switch(position) {
0N/A case SwingConstants.NORTH:
0N/A return SwingConstants.SOUTH;
0N/A case SwingConstants.SOUTH:
0N/A return SwingConstants.NORTH;
0N/A case SwingConstants.EAST:
0N/A return SwingConstants.WEST;
0N/A case SwingConstants.WEST:
0N/A return SwingConstants.EAST;
0N/A }
0N/A assert false;
0N/A return 0;
0N/A }
0N/A
0N/A /**
0N/A * Returns the amount to indent the specified component if it's
0N/A * a JCheckBox or JRadioButton. If the component is not a JCheckBox or
0N/A * JRadioButton, 0 will be returned.
0N/A */
0N/A protected int getIndent(JComponent c, int position) {
0N/A String classID = c.getUIClassID();
0N/A if (classID == "CheckBoxUI" || classID == "RadioButtonUI") {
0N/A AbstractButton button = (AbstractButton)c;
0N/A Insets insets = c.getInsets();
0N/A Icon icon = getIcon(button);
0N/A int gap = button.getIconTextGap();
0N/A if (isLeftAligned(button, position)) {
0N/A return insets.left + icon.getIconWidth() + gap;
0N/A } else if (isRightAligned(button, position)) {
0N/A return insets.right + icon.getIconWidth() + gap;
0N/A }
0N/A }
0N/A return 0;
0N/A }
0N/A
0N/A private Icon getIcon(AbstractButton button) {
0N/A Icon icon = button.getIcon();
0N/A if (icon != null) {
0N/A return icon;
0N/A }
0N/A String key = null;
0N/A if (button instanceof JCheckBox) {
0N/A key = "CheckBox.icon";
0N/A } else if (button instanceof JRadioButton) {
0N/A key = "RadioButton.icon";
0N/A }
0N/A if (key != null) {
0N/A Object oIcon = UIManager.get(key);
0N/A if (oIcon instanceof Icon) {
0N/A return (Icon)oIcon;
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A private boolean isLeftAligned(AbstractButton button, int position) {
0N/A if (position == SwingConstants.WEST) {
0N/A boolean ltr = button.getComponentOrientation().isLeftToRight();
0N/A int hAlign = button.getHorizontalAlignment();
0N/A return ((ltr && (hAlign == SwingConstants.LEFT ||
0N/A hAlign == SwingConstants.LEADING)) ||
0N/A (!ltr && (hAlign == SwingConstants.TRAILING)));
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A private boolean isRightAligned(AbstractButton button, int position) {
0N/A if (position == SwingConstants.EAST) {
0N/A boolean ltr = button.getComponentOrientation().isLeftToRight();
0N/A int hAlign = button.getHorizontalAlignment();
0N/A return ((ltr && (hAlign == SwingConstants.RIGHT ||
0N/A hAlign == SwingConstants.TRAILING)) ||
0N/A (!ltr && (hAlign == SwingConstants.LEADING)));
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A private int getInset(JComponent c, int position) {
0N/A return getInset(c.getInsets(), position);
0N/A }
0N/A
0N/A private int getInset(Insets insets, int position) {
0N/A if (insets == null) {
0N/A return 0;
0N/A }
0N/A switch(position) {
0N/A case SwingConstants.NORTH:
0N/A return insets.top;
0N/A case SwingConstants.SOUTH:
0N/A return insets.bottom;
0N/A case SwingConstants.EAST:
0N/A return insets.right;
0N/A case SwingConstants.WEST:
0N/A return insets.left;
0N/A }
0N/A assert false;
0N/A return 0;
0N/A }
0N/A}