001: /*
002: * Copyright (c) 2004 JETA Software, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without modification,
005: * are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JETA Software nor the names of its contributors may
015: * be used to endorse or promote products derived from this software without
016: * specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
021: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
022: * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
023: * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
024: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
025: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
026: * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: */
029:
030: package com.jeta.forms.components.border;
031:
032: import java.awt.Color;
033: import java.awt.Component;
034: import java.awt.Graphics;
035: import java.awt.Insets;
036:
037: import javax.swing.border.LineBorder;
038:
039: /**
040: * Extension of a line border that supports painting specific sides of the
041: * border (i.e. top, left, bottom, or right ).
042: *
043: * @author Jeff Tassin
044: */
045: public class JETALineBorder extends LineBorder {
046: private boolean m_top = true;
047: private boolean m_left = true;
048: private boolean m_bottom = true;
049: private boolean m_right = true;
050:
051: public JETALineBorder() {
052: super (Color.black);
053: }
054:
055: /**
056: * ctor
057: */
058: public JETALineBorder(Color c, int thickness, boolean curved,
059: boolean top, boolean left, boolean bottom, boolean right) {
060: super (c, thickness, curved);
061: m_top = top;
062: m_left = left;
063: m_bottom = bottom;
064: m_right = right;
065: }
066:
067: /**
068: * Returns the insets of the border.
069: *
070: * @param c
071: * the component for which this border insets value applies
072: */
073: public Insets getBorderInsets(Component c) {
074: if (getRoundedCorners())
075: return super .getBorderInsets(c);
076:
077: int thickness = getThickness();
078: return new Insets((m_top ? thickness : 0), (m_left ? thickness
079: : 0), (m_bottom ? thickness : 0), (m_right ? thickness
080: : 0));
081: }
082:
083: /**
084: * Reinitialize the insets parameter with this Border's current Insets.
085: *
086: * @param c
087: * the component for which this border insets value applies
088: * @param insets
089: * the object to be reinitialized
090: */
091: public Insets getBorderInsets(Component c, Insets insets) {
092: if (getRoundedCorners())
093: return super .getBorderInsets(c, insets);
094:
095: insets.left = (m_left ? thickness : 0);
096: insets.top = (m_top ? thickness : 0);
097: insets.right = (m_right ? thickness : 0);
098: insets.bottom = (m_bottom ? thickness : 0);
099: return insets;
100: }
101:
102: /**
103: * Renders the border on the graphics context.
104: */
105: public void paintBorder(Component c, Graphics g, int x, int y,
106: int width, int height) {
107: if (getRoundedCorners()
108: || (m_top && m_bottom && m_right && m_left)) {
109: super .paintBorder(c, g, x, y, width, height);
110: } else {
111:
112: Color oldColor = g.getColor();
113: int i;
114:
115: g.setColor(lineColor);
116: for (i = 0; i < thickness; i++) {
117: int top = y + i;
118: int left = x + i;
119: int right = left + width - i - i - 1;
120: int bottom = top + height - i - i - 1;
121:
122: if (m_top) {
123: g.drawLine(x, top, x + width - 1, top);
124: }
125:
126: if (m_left) {
127: g.drawLine(left, y, left, y + height - 1);
128: }
129:
130: if (m_bottom) {
131: g.drawLine(x, bottom, x + width - 1, bottom);
132: }
133:
134: if (m_right) {
135: g.drawLine(right, y, right, y + height - 1);
136: }
137: }
138: g.setColor(oldColor);
139: }
140: }
141: }
|