001/*
002 * $Id$
003 *
004 * Copyright 2009 Sun Microsystems, Inc., 4150 Network Circle,
005 * Santa Clara, California 95054, U.S.A. All rights reserved.
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 * 
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015 * Lesser General Public License for more details.
016 * 
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this library; if not, write to the Free Software
019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
020 *
021 */
022package org.jdesktop.swingx.plaf.synth;
023
024/*
025 * @(#)SynthBorder.java 1.15 06/11/30
026 *
027 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
028 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
029 */
030
031import java.awt.Component;
032import java.awt.Graphics;
033import java.awt.Insets;
034
035import javax.swing.JComponent;
036import javax.swing.border.AbstractBorder;
037import javax.swing.plaf.UIResource;
038import javax.swing.plaf.synth.SynthContext;
039import javax.swing.plaf.synth.SynthStyle;
040
041/**
042 * SynthBorder is a border that delegates to a Painter. The Insets
043 * are determined at construction time.<p>
044 * 
045 * Copied from core
046 *
047 * @version 1.15, 11/30/06
048 * @author Scott Violet
049 */
050class SynthBorder extends AbstractBorder implements UIResource {
051    private SynthUI ui;
052    private Insets insets;
053
054    SynthBorder(SynthUI ui, Insets insets) {
055        this.ui = ui;
056        this.insets = insets;
057    }
058
059    SynthBorder(SynthUI ui) {
060        this(ui, null);
061    }
062
063    @Override
064    public void paintBorder(Component c, Graphics g, int x, int y,
065                            int width, int height) {
066        JComponent jc = (JComponent)c;
067        SynthContext context = ui.getContext(jc);
068        SynthStyle style = context.getStyle();
069        if (style == null) {
070            assert false: "SynthBorder is being used outside after the UI " +
071                          "has been uninstalled";
072            return;
073        }
074        ui.paintBorder(context, g, x, y, width, height);
075    }
076
077    /**
078     * This default implementation returns a new <code>Insets</code>
079     * instance where the <code>top</code>, <code>left</code>,
080     * <code>bottom</code>, and 
081     * <code>right</code> fields are set to <code>0</code>.
082     * @param c the component for which this border insets value applies
083     * @return the new <code>Insets</code> object initialized to 0
084     */
085    @Override
086    public Insets getBorderInsets(Component c) { 
087        return getBorderInsets(c, null);
088    }
089
090    /** 
091     * Reinitializes the insets parameter with this Border's current Insets. 
092     * @param c the component for which this border insets value applies
093     * @param insets the object to be reinitialized
094     * @return the <code>insets</code> object
095     */
096    @Override
097    public Insets getBorderInsets(Component c, Insets insets) {
098        if (this.insets != null) {
099            if (insets == null) {
100                insets = new Insets(this.insets.top, this.insets.left,
101                                  this.insets.bottom, this.insets.right);
102            }
103            else {
104                insets.top    = this.insets.top;
105                insets.bottom = this.insets.bottom;
106                insets.left   = this.insets.left;
107                insets.right  = this.insets.right;
108            }
109        }
110        else if (insets == null) {
111            insets = new Insets(0, 0, 0, 0);
112        }
113        else {
114            insets.top = insets.bottom = insets.left = insets.right = 0;
115        }
116        return insets;
117    }
118
119    /**
120     * This default implementation returns false.
121     * @return false
122     */
123    @Override
124    public boolean isBorderOpaque() {
125        return false;
126    }
127
128}