All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ch.randelshofer.quaqua.VisualMargin Maven / Gradle / Ivy

Go to download

A Mavenisation of the Quaqua Mac OSX Swing Look and Feel (Java library) Quaqua Look and Feel (C) 2003-2010, Werner Randelshofer. Mavenisation by Matt Gumbley, DevZendo.org - for problems with Mavenisation, see Matt; for issues with Quaqua, see the Quaqua home page. For full license details, see http://randelshofer.ch/quaqua/license.html

The newest version!
/*
 * @(#)VisualMargin.java  
 *
 * Copyright (c) 2004-2010 Werner Randelshofer, Immensee, Switzerland.
 * All rights reserved.
 *
 * You may not use, copy or modify this file, except in compliance with the
 * license agreement you entered into with Werner Randelshofer.
 * For details see accompanying license terms.
 */

package ch.randelshofer.quaqua;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.plaf.*;

/**
 * The VisualMargin is used to visually align components using bounds
 * based on other criterias than the clip bounds of the component.
 *
 * For example: The clip bounds of a JButton includes its cast shadow and its
 * focus ring. When we align the JButton with a JLabel, we want to align the
 * baseline of the Text of the JButton with the text in the JLabel.
 *
 * The visual margin may be quite large. We allow to programmatically set a
 * smaller margin using the client property "Quaqua.Component.margin".
 *
 * @author  Werner Randelshofer
 * @version $Id: VisualMargin.java 361 2010-11-21 11:19:20Z wrandelshofer $
 */
public class VisualMargin extends AbstractBorder implements UIResource {
    /**
     * Defines the margin from the clip bounds of the
     * component to its visually perceived borderline.
     */
    private Insets layoutMargin;
    
    /**
     * The UIManager Property to be used for the default margin.
     */
    private String uiManagerPropertyName = "Component.visualMargin";
    /**
     * The Client Property to be used for the default margin.
     */
    private String propertyName = "Quaqua.Component.visualMargin";
    
    private boolean isTopFixed, isLeftFixed, isBottomFixed, isRightFixed;
    
    /**
     * Creates a new VisualMargin.
     */
    public VisualMargin() {
        layoutMargin = new Insets(0, 0, 0, 0);
    }
    
    /**
     * Creates a new VisualMargin.
     *
     * @param top Defines the margin from the clip bounds of the
     * component to its visual bounds.
     * @param left Defines the margin from the clip bounds of the
     * component to its visual bounds.
     * @param bottom Defines the margin from the clip bounds of the
     * component to its visual bounds.
     * @param right Defines the margin from the clip bounds of the
     * component to its visual bounds.
     */
    public VisualMargin(int top, int left, int bottom, int right) {
        layoutMargin = new Insets(top, left, bottom, right);
    }
    public VisualMargin(int top, int left, int bottom, int right, boolean ftop, boolean fleft, boolean fbottom, boolean fright) {
        layoutMargin = new Insets(top, left, bottom, right);
        isTopFixed = ftop;
        isLeftFixed = fleft;
        isBottomFixed = fbottom;
        isRightFixed = fright;
    }
    public VisualMargin(boolean ftop, boolean fleft, boolean fbottom, boolean fright) {
        layoutMargin = new Insets(0, 0, 0, 0);
        isTopFixed = ftop;
        isLeftFixed = fleft;
        isBottomFixed = fbottom;
        isRightFixed = fright;
    }
    /**
     * Creates a new VisualMargin.
     *
     * @param layoutMargin Defines the margin from the clip bounds of the
     * component to its visual bounds. The margin has usually negative values!
     */
    public VisualMargin(Insets layoutMargin) {
        this.layoutMargin = layoutMargin;
    }
    /**
     * The UIManager Property to be used for the default margin.
     */
    public void setPropertyName(String propertyName) {
        //  this.propertyName = propertyName;
    }
    
    /*
     * Specifies SwingConstants.TOP, LEFT, BOTTOM, RIGHT to be fixed.
     * Set to false to unfix.
     */
    public void setFixed(boolean top, boolean left, boolean bottom, boolean right) {
        isTopFixed = top;
        isLeftFixed = left;
        isBottomFixed = bottom;
        isRightFixed = right;
    }
    
    public Insets getVisualMargin(Component c) {
        return getVisualMargin(c, new Insets(0, 0, 0, 0));
        /*
        Insets insets = new Insets(
         layoutMargin.top,
        layoutMargin.left,
        layoutMargin.bottom,
         layoutMargin.right
         );
        
        if (c instanceof JComponent) {
            Insets componentMargin = (Insets) ((JComponent) c).getClientProperty(propertyName);
            if (componentMargin == null && propertyName != null) {
                componentMargin = UIManager.getInsets(uiManagerPropertyName);
            }
            if (componentMargin != null) {
                if (! isTopFixed) insets.top = componentMargin.top;
                if (! isLeftFixed) insets.left = componentMargin.left;
                if (! isBottomFixed) insets.bottom = componentMargin.bottom;
                if (! isRightFixed) insets.right = componentMargin.right;
            }
        }
        return insets;
         */
    }
    
    public Insets getBorderInsets(Component c) {
        return getBorderInsets(c, new Insets(0, 0, 0, 0));
    }
    
    /**
     * Reinitializes the insets parameter with this Border's current Insets.
     * @param c the component for which this border insets value applies
     * @param insets the object to be reinitialized
     * @return the insets object
     */
    public Insets getBorderInsets(Component c, Insets insets) {
        return getVisualMargin(c, insets);
    }
    /**
     * Reinitializes the insets parameter with this Border's current Insets.
     * @param c the component for which this border insets value applies
     * @param insets the object to be reinitialized
     * @return the insets object
     */
    protected Insets getVisualMargin(Component c, Insets insets) {
        insets.top = -layoutMargin.top;
        insets.left = -layoutMargin.left;
        insets.bottom = -layoutMargin.bottom;
        insets.right = -layoutMargin.right;
        
        if (c instanceof JComponent) {
            Insets componentMargin = (Insets) ((JComponent) c).getClientProperty(propertyName);
            if (componentMargin == null && propertyName != null) {
                componentMargin = UIManager.getInsets(uiManagerPropertyName);
            }
            if (componentMargin != null) {
                if (! isTopFixed) insets.top += componentMargin.top;
                if (! isLeftFixed) insets.left += componentMargin.left;
                if (! isBottomFixed) insets.bottom += componentMargin.bottom;
                if (! isRightFixed) insets.right += componentMargin.right;
            }
        }
        return insets;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy