001/*
002 * Created on 04.11.2010
003 *
004 */
005package org.jdesktop.swingx.plaf;
006
007import java.awt.Component;
008import java.awt.Graphics;
009import java.awt.Insets;
010import java.awt.Rectangle;
011import java.awt.Component.BaselineResizeBehavior;
012
013import javax.swing.border.AbstractBorder;
014import javax.swing.plaf.UIResource;
015
016/**
017 * Wrapper around a delegate with the same behaviour as the delegate except that
018 * it catches null insets (hack around Issue 1297-swingx which is core bug
019 * 6739738)
020 */
021public class SafeBorder extends AbstractBorder implements UIResource {
022
023    private AbstractBorder delegate;
024
025    public SafeBorder(AbstractBorder delegate) {
026        this.delegate = delegate;
027    }
028
029    /**
030     * {@inheritDoc}
031     */
032    @Override
033    public int getBaseline(Component c, int width, int height) {
034        return delegate.getBaseline(c, width, height);
035    }
036
037    /** 
038     * {@inheritDoc}
039     */
040    @Override
041    public BaselineResizeBehavior getBaselineResizeBehavior(Component c) {
042        return delegate.getBaselineResizeBehavior(c);
043    }
044
045    /** 
046     * {@inheritDoc}
047     */
048    @Override
049    public Insets getBorderInsets(Component c, Insets insets) {
050        Insets result = delegate.getBorderInsets(c, safeInsets(insets));
051        return safeInsets(result);
052    }
053
054    /**
055     * @param insets
056     *            the insets to query
057     * @return the insets supplied or an empty insets if the value is {@code null}
058     */
059    private Insets safeInsets(Insets insets) {
060        return insets != null ? insets : new Insets(0, 0, 0, 0);
061    }
062
063    /** 
064     * {@inheritDoc}
065     */
066    @Override
067    public Insets getBorderInsets(Component c) {
068        Insets result = delegate.getBorderInsets(c);
069        return safeInsets(result);
070    }
071
072    /** 
073     * {@inheritDoc}
074     */
075    @Override
076    public Rectangle getInteriorRectangle(Component c, int x, int y, int width,
077            int height) {
078        return delegate.getInteriorRectangle(c, x, y, width, height);
079    }
080
081    /** 
082     * {@inheritDoc}
083     */
084    @Override
085    public boolean isBorderOpaque() {
086        return delegate.isBorderOpaque();
087    }
088
089    /** 
090     * {@inheritDoc}
091     */
092    @Override
093    public void paintBorder(Component c, Graphics g, int x, int y, int width,
094            int height) {
095        delegate.paintBorder(c, g, x, y, width, height);
096    }
097   
098    
099}