001: /*
002: * Copyright (C) 2004 NNL Technology AB
003: * Visit www.infonode.net for information about InfoNode(R)
004: * products and how to contact NNL Technology AB.
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
019: * MA 02111-1307, USA.
020: */
021:
022: // $Id: EdgeBorder.java,v 1.14 2005/12/04 13:46:03 jesper Exp $
023: package net.infonode.gui.border;
024:
025: import net.infonode.gui.ComponentUtil;
026: import net.infonode.gui.GraphicsUtil;
027: import net.infonode.gui.colorprovider.ColorProvider;
028: import net.infonode.gui.colorprovider.FixedColorProvider;
029: import net.infonode.util.ColorUtil;
030:
031: import javax.swing.border.Border;
032: import java.awt.*;
033: import java.io.Serializable;
034:
035: /**
036: * @author $Author: jesper $
037: * @version $Revision: 1.14 $
038: */
039: public class EdgeBorder implements Border, Serializable {
040: private static final long serialVersionUID = 1;
041:
042: private ColorProvider topLeftColor;
043: private ColorProvider bottomRightColor;
044: private boolean drawTop;
045: private boolean drawBottom;
046: private boolean drawLeft;
047: private boolean drawRight;
048: private Insets insets;
049:
050: public EdgeBorder() {
051: this (true, true, true, true);
052: }
053:
054: public EdgeBorder(boolean drawTop, boolean drawBottom,
055: boolean drawLeft, boolean drawRight) {
056: this (null, drawTop, drawBottom, drawLeft, drawRight);
057: }
058:
059: public EdgeBorder(Color color, boolean drawTop, boolean drawBottom,
060: boolean drawLeft, boolean drawRight) {
061: ColorProvider c = color == null ? null
062: : new FixedColorProvider(color);
063: init(c, c, drawTop, drawBottom, drawLeft, drawRight);
064: }
065:
066: public EdgeBorder(ColorProvider color) {
067: init(color, color, true, true, true, true);
068: }
069:
070: public EdgeBorder(ColorProvider topLeftColor,
071: ColorProvider bottomRightColor, boolean drawTop,
072: boolean drawBottom, boolean drawLeft, boolean drawRight) {
073: init(topLeftColor, bottomRightColor, drawTop, drawBottom,
074: drawLeft, drawRight);
075: }
076:
077: private void init(ColorProvider topLeftColor,
078: ColorProvider bottomRightColor, boolean drawTop,
079: boolean drawBottom, boolean drawLeft, boolean drawRight) {
080: this .topLeftColor = topLeftColor;
081: this .bottomRightColor = bottomRightColor;
082: this .drawTop = drawTop;
083: this .drawBottom = drawBottom;
084: this .drawLeft = drawLeft;
085: this .drawRight = drawRight;
086: insets = new Insets(drawTop ? 1 : 0, drawLeft ? 1 : 0,
087: drawBottom ? 1 : 0, drawRight ? 1 : 0);
088: }
089:
090: public void paintBorder(Component c, Graphics g, int x, int y,
091: int width, int height) {
092: Color topLeft = getColor(topLeftColor, c);
093: Color bottomRight = getColor(bottomRightColor, c);
094:
095: if (topLeft != null && bottomRight != null) {
096: g.setColor(topLeft);
097:
098: if (drawTop)
099: GraphicsUtil.drawOptimizedLine(g, x, y, x + width - 1,
100: y);
101:
102: if (drawLeft)
103: GraphicsUtil.drawOptimizedLine(g, x, y, x, y + height
104: - 1);
105:
106: g.setColor(bottomRight);
107: if (drawRight)
108: GraphicsUtil.drawOptimizedLine(g, x + width - 1, y, x
109: + width - 1, y + height - 1);
110:
111: if (drawBottom)
112: GraphicsUtil.drawOptimizedLine(g, x, y + height - 1, x
113: + width - 1, y + height - 1);
114: }
115: }
116:
117: public Insets getBorderInsets(Component c) {
118: return insets;
119: }
120:
121: public boolean isBorderOpaque() {
122: return false;
123: }
124:
125: private Color getColor(ColorProvider color, Component c) {
126: Color col = color != null ? color.getColor() : null;
127:
128: if (col == null) {
129: Color background = ComponentUtil.getBackgroundColor(c);
130: return background == null ? null : ColorUtil.mult(
131: background, 0.7f);
132: }
133:
134: return col;
135: }
136: }
|