// RulePanel.java
//
// By Wayne Conrad <wconrad@pcsiaz.com>, March 1998.
// Java 1.1
// Use at your own risk!


package wec.awt;


import java.awt.*;
import java.awt.event.*;


// This class is a panel with a border on selected edges.

public class RulePanel extends Panel
  {


  // Edges.  These can be combined.

  public final static int NONE  = 0;
  public final static int NORTH = 1 << 0;
  public final static int SOUTH = 1 << 1;
  public final static int EAST  = 1 << 2;
  public final static int WEST  = 1 << 3;


  // The edges which will have a rule.  This is a combination of one or more
  // of the above constants.

  protected int ruledEdges = NONE;


  // The inner margin, which is the space between the rule and the panel's
  // contents.  On an edge with no rule, this is not used.

  protected int innerMargin = 6;


  // The outer margin, which is the space between the panel's edge and the
  // rule.  On an edge with no rule, this is used as the space between the
  // panel's edge and the panel's contents.

  protected int outerMargin = 3;


  // Default constructor.  No edge will have a rule.

  public RulePanel()
    {
    this (NONE);
    }


  // Constructor taking the edge or edges that will have rules.

  public RulePanel (int ruledEdges)
    {
    this.ruledEdges = ruledEdges;
    }


  // Return the ruled edges.

  public int getRuledEdges()
    {
    return ruledEdges;
    }


  // Set the ruled edges.  This causes the component to be redrawn.

  public void setRuledEdges (int ruledEdges)
    {
    if (this.ruledEdges != ruledEdges)
      {
      this.ruledEdges = ruledEdges;
      repaint();
      }
    }


  // Return the inner margin, which is the space between the rule and the
  // panel's contents.  On an edge with no rule, this is not used.

  public int getInnerMargin()
    {
    return innerMargin;
    }


  // Set the inner margin, which is the space between the rule and the
  // panel's contents.  On an edge with no rule, this is not used.  The
  // component will be redrawn if necessary.

  public void setInnerMargin (int innerMargin)
    {
    if (this.innerMargin != innerMargin)
      {
      this.innerMargin = innerMargin;
      repaint();
      }
    }


  // Return the outer margin, which is the space between the panel's edge and
  // the rule.  On an edge with no rule, this is used as the space between
  // the panel's edge and the panel's contents.

  public int getOuterMargin()
    {
    return outerMargin;
    }


  // Set the outer margin, which is the space between the panel's edge and
  // the rule.  On an edge with no rule, this is used as the space between
  // the panel's edge and the panel's contents. The component will be redrawn
  // if necessary.

  public void setOuterMargin (int outerMargin)
    {
    if (this.outerMargin != outerMargin)
      {
      this.outerMargin = outerMargin;
      repaint();
      }
    }


  // (Override) Return the insets.

  public Insets getInsets()
    {
    int margin = innerMargin + outerMargin;
    int topInset = 0;
    int leftInset = 0;
    int bottomInset = 0;
    int rightInset = 0;
    if ( (ruledEdges & NORTH) != 0)
      topInset = margin;
    if ( (ruledEdges & SOUTH) != 0)
      bottomInset = margin;
    if ( (ruledEdges & EAST) != 0)
      rightInset = margin;
    if ( (ruledEdges & WEST) != 0)
      leftInset = margin;
    return new Insets (topInset, leftInset, bottomInset, rightInset);
    }


  // (Override) Paint the border.

  public void paint (Graphics g)
    {
    Dimension size = getSize();
    int x1 = outerMargin;
    int x2 = size.width - outerMargin;
    int y1 = outerMargin;
    int y2 = size.height - outerMargin;
    if (getBackground().equals (Color.black) )
      g.setColor (Color.white);
    else
      g.setColor (Color.black);
    if ( (ruledEdges & NORTH) != 0)
      g.drawLine (x1, y1, x2, y1);
    if ( (ruledEdges & SOUTH) != 0)
      g.drawLine (x1, y2, x2, y2);
    if ( (ruledEdges & EAST) != 0)
      g.drawLine (x2, y1, x2, y2);
    if ( (ruledEdges & WEST) != 0)
      g.drawLine (x1, y1, x1, y2);
    }


  // Test program.

  public static void main (String[] args)
    {
    Frame frame = new Frame ("RulePanel");
    frame.addWindowListener
      (
      new WindowAdapter()
        {public void windowClosing (WindowEvent e) {System.exit (0);} }
      );
    RulePanel rulePanel = new RulePanel
      (RulePanel.NORTH + RulePanel.WEST + RulePanel.EAST + RulePanel.SOUTH);
    rulePanel.add (new Button ("This button does nothing") );
    frame.add (rulePanel);
    frame.pack();
    frame.setVisible (true);
    }


  }