package com.dbxml.db.admin.swing;

/*
 * dbXML - Native XML Database
 * Copyright (C) 1999-2004  The dbXML Group, L.L.C.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: NestedBorder.java,v 1.2 2004/02/12 00:17:55 bradford Exp $
 */

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Insets;
import javax.swing.border.AbstractBorder;

/**
 * NestedBorder
 */

public final class NestedBorder extends AbstractBorder {
   protected Color highlight;
   protected Color shadow;
   protected boolean top;
   protected boolean left;

   public NestedBorder() {
      this(null, null);
   }

   public NestedBorder(Color highlight, Color shadow) {
      this(highlight, shadow, true, true);
   }

   public NestedBorder(boolean top, boolean left) {
      this(null, null, top, left);
   }

   public NestedBorder(Color highlight, Color shadow, boolean top, boolean left) {
      this.highlight = highlight;
      this.shadow = shadow;
      this.top = top;
      this.left = left;
   }

   public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
      g.translate(x, y);
      g.setColor(getShadowColor(c));
      if ( top )
         g.drawLine(0, 1, width - 1, 1);
      if ( left )
         g.drawLine(1, 0, 1, height - 1);

      g.setColor(getHighlightColor(c));
      if ( top )
         g.drawLine(0, 0, width - 1, 0);
      if ( left )
         g.drawLine(0, 0, 0, height - 1);
      g.translate(-x, -y);
   }

   public Insets getBorderInsets(Component c) {
		int topInset;
		if ( top )
			topInset = 2;
		else
			topInset = 0;
		
		int leftInset;
		if ( left )
			leftInset = 2;
		else
			leftInset = 0;
		
      return new Insets(topInset, leftInset, 0, 0);
   }

   public Insets getBorderInsets(Component c, Insets insets) {
		if ( top )
			insets.top = 2;
		else
			insets.top = 0;
		if ( left )
			insets.left = 2;
		else
			insets.left = 0;
      insets.right = 0;
      insets.bottom = 0;
      return insets;
   }

   public boolean isBorderOpaque() {
      return true;
   }

   public Color getHighlightColor(Component c) {
		if ( highlight != null )
			return highlight;
		else
			return c.getBackground().brighter();
   }

   public Color getHighlightColor() {
      return highlight;
   }

   public Color getShadowColor(Component c) {
		if ( shadow != null )
			return shadow;
		else
			return c.getBackground().darker();
   }

   public Color getShadowColor() {
      return shadow;
   }

   public boolean isTop() {
      return top;
   }

   public void setTop(boolean top) {
      this.top = top;
   }

   public boolean isLeft() {
      return left;
   }

   public void setLeft(boolean left) {
      this.left = left;
   }
}
