/**
 * Copyright (C) 2004-2011 Jive Software. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jivesoftware.spark.component.borders;

import javax.swing.border.AbstractBorder;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;

/**
 * Renders edge borders for a concave appearance.
 *
 * @author Derek DeMoro
 */
public class PartialLineBorder extends AbstractBorder {
    private final Color color;
    private final int thickness;

    final boolean top;
    final boolean left;
    final boolean bottom;
    final boolean right;

    public PartialLineBorder(Color color, int thickness) {
        top = true;
        left = true;
        bottom = true;
        right = true;

        this.color = color;
        this.thickness = thickness;
    }

    @Override
	public boolean isBorderOpaque() {
        return true;
    }

    @Override
	public Insets getBorderInsets(Component component) {
        return new Insets(2, 2, 2, 2);
    }

    public int getThickness() {
        return thickness;
    }

    @Override
	public void paintBorder(Component component, Graphics g, int x, int y, int width, int height) {
        Graphics2D g2 = (Graphics2D)g;
        g2.setStroke(new BasicStroke(1.0f));
        g2.setColor(color);

        if (top) {
            g2.drawLine(x, y, x + width, y);
        }
        if (left) {
            g2.drawLine(x, y, x, y + height);
        }
        if (bottom) {
            g2.drawLine(x, y + height, x + width, y + height);
        }
        if (right) {
            g2.drawLine(x + width, y, x + width, y + height);
        }
    }
}
