/*
 This file is part of the software framework JStatCom, 
 a library for building applications for data based analysis.

 Copyright (C) 2000-2005  Alexander Benkwitz, Markus Kraetzig

 This library is free software; you can redistribute it and/or modify
 it under the terms of the GNU Lesser General Public License as
 published by the Free Software Foundation; either version 2.1 of the
 License, or (at your option) any later version.

 This library 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
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 USA
 */

package com.jstatcom.component;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Insets;

import javax.swing.border.AbstractBorder;

/**
 * This border puts angular brackets [ ] around a component.
 * It can be used to visualize a matrix or a range object.
 * The appearance of the brackets can be configured via the
 * constructor, but reasonable defaults are provided
 * by static factory methods as well.
 *
 * @author A. Benkwitz, Markus Kraetzig
 */

public final class MatrixBorder extends AbstractBorder {
	private java.awt.Insets insets;
	private int outerTopSpace;
	private int outerLeftSpace;
	private int innerTopSpace;
	private int innerLeftSpace;
	private int thickness;
	private int bracketWidth;

/**
 * Creates the default MatrixBorder instance.
 */
public MatrixBorder() {
    super();
    thickness = 1;
    bracketWidth = 8; 

    outerTopSpace = 1;
    innerTopSpace = 3;

    outerLeftSpace = 1;
    innerLeftSpace = 3;

    int top = thickness + outerTopSpace + innerTopSpace;
    int left = thickness + outerLeftSpace + innerLeftSpace;
    insets = new Insets(top, left, top, left);
}


/**
 * Creates a MatrixBorder instance according to spacing
 * and size parameters. Instead of this constructor, the static factory
 * methods can be called for some reasonable variations of the default
 * MatrixBorder.
 *
 * @param thick      border thickness, default is 1
 * @param width      width of the brackets, default is 8
 * @param outerTop   space between outer top and bracket, default is 1
 * @param innerTop   space between bracket and top of component, default is 3
 * @param outerLeft  space between outer left and bracket, default is 1
 * @param innerLeft  space between bracket and left of component, default is 3 
 */
public MatrixBorder(
    int thick,
    int width,
    int outerTop,
    int innerTop,
    int outerLeft,
    int innerLeft) {

    super();

    thickness = thick;
    bracketWidth = width;

    outerTopSpace = outerTop;
    innerTopSpace = innerTop;

    outerLeftSpace = outerLeft;
    innerLeftSpace = innerLeft;

    int top = thickness + outerTopSpace + innerTopSpace;
    int left = thickness + outerLeftSpace + innerLeftSpace;
    insets = new Insets(top, left, top, left);
}


/** 
 * Overrides super method.
 *
 * @param  c the component for which this border insets value applies
 * @return the insets
 */
public Insets getBorderInsets(Component c) {
	return insets;
}


/** 
 * Reinitialize the insets parameter with this border's current insets.
 * Overrides super method.
 *
 * @param  c      the component for which this border insets value applies
 * @param  insets the object to be reinitialized
 * @return the insets
 */
public Insets getBorderInsets(Component c, Insets insets) {
	insets.left = this.insets.left;
	insets.top = this.insets.top;
	insets.right = this.insets.right;
	insets.bottom = this.insets.bottom;
	return insets;
}


/**
 * Gets a MatrixBorder that is smaller than the
 * default, for example to be used with default sized text fields.
 * 
 * @return a new border instance 
 */
public static MatrixBorder getInstanceSmall() {
    return new MatrixBorder(1, 5, 1, 1, 1, 1);
}


/**
 * Gets a MatrixBorder that is thicker than the
 * default.
 * 
 * @return a new border instance 
 */
public static MatrixBorder getInstanceThick() {
    return new MatrixBorder(2, 5, 2, 1, 3, 1);
}


/** 
 * This default implementation returns false. 
 * Overrides super method.
 *
 * @return false
 */
public boolean isBorderOpaque() {
	return false;
}


/** 
 * Called by the repaint manager to paint this border.
 */
public void paintBorder(
    Component c,
    Graphics g,
    int x,
    int y,
    int width,
    int height) {

    Color oldColor = g.getColor();

    if (c.isEnabled()) {
        Color newColor = c.getForeground();
        g.setColor(newColor);
    } else {
        Color newColor = c.getBackground().darker();
        g.setColor(newColor);
    }

    // paint left bracket
    g.fillRect(
        outerLeftSpace,
        outerTopSpace,
        thickness,
        height - 2 * outerTopSpace);
    g.fillRect(outerLeftSpace, outerTopSpace, bracketWidth, thickness);
    g.fillRect(
        outerLeftSpace,
        height - outerTopSpace - thickness,
        bracketWidth,
        thickness);

    // paint right bracket
    g.fillRect(
        width - outerLeftSpace - thickness,
        outerTopSpace,
        thickness,
        height - 2 * outerTopSpace);
    g.fillRect(
        width - outerLeftSpace - bracketWidth,
        outerTopSpace,
        bracketWidth,
        thickness);
    g.fillRect(
        width - outerLeftSpace - bracketWidth,
        height - outerTopSpace - thickness,
        bracketWidth,
        thickness);

    g.setColor(oldColor);
}
}
 

Ads help maintain this website.