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: TabAreaLineBorder.java,v 1.19 2005/12/04 13:46:05 jesper Exp $
023: package net.infonode.tabbedpanel.border;
024:
025: import net.infonode.gui.GraphicsUtil;
026: import net.infonode.gui.colorprovider.*;
027: import net.infonode.tabbedpanel.*;
028: import net.infonode.util.Direction;
029:
030: import javax.swing.*;
031: import javax.swing.border.Border;
032: import java.awt.*;
033: import java.io.Serializable;
034:
035: /**
036: * TabAreaLineBorder draws a 1 pixel wide border on all sides except the side towards
037: * the content area of a tabbed panel.
038: *
039: * @author $Author: jesper $
040: * @author $Author: jesper $
041: * @version $Revision: 1.19 $
042: * @see Tab
043: * @see TabbedPanel
044: * @see TabbedPanelProperties
045: * @see TabAreaProperties
046: * @see TabAreaComponentsProperties
047: * @since ITP 1.1.0
048: */
049: public class TabAreaLineBorder implements Border, Serializable {
050: private static final long serialVersionUID = 1;
051:
052: private ColorProvider color;
053: private boolean drawTop;
054: private boolean drawLeft;
055: private boolean drawRight;
056: private boolean flipLeftRight;
057:
058: /**
059: * Constructs a TabAreaLineBorder with color based on the look and feel
060: */
061: public TabAreaLineBorder() {
062: this (null);
063: }
064:
065: /**
066: * Constructs a TabAreaLineBorder with the give color
067: *
068: * @param color color for the border
069: */
070: public TabAreaLineBorder(Color color) {
071: this (color, true, true, true, false);
072: }
073:
074: /**
075: * Constructor.
076: *
077: * @param drawTop draw the top line
078: * @param drawLeft draw the left line
079: * @param drawRight draw the right line
080: * @param flipLeftRight if true the left line is rotated so that it is always to the left or at the top and
081: * vice versa for the right line, if false the left and right lines are rotated the same way as
082: * the other lines
083: */
084: public TabAreaLineBorder(boolean drawTop, boolean drawLeft,
085: boolean drawRight, boolean flipLeftRight) {
086: this ((Color) null, drawTop, drawLeft, drawRight, flipLeftRight);
087: }
088:
089: /**
090: * Constructor.
091: *
092: * @param color the line color
093: * @param drawTop draw the top line
094: * @param drawLeft draw the left line
095: * @param drawRight draw the right line
096: * @param flipLeftRight if true the left line is rotated so that it is always to the left or at the top and
097: * vice versa for the right line, if false the left and right lines are rotated the same way as
098: * the other lines
099: */
100: public TabAreaLineBorder(Color color, boolean drawTop,
101: boolean drawLeft, boolean drawRight, boolean flipLeftRight) {
102: this (ColorProviderUtil.getColorProvider(color,
103: new ColorProviderList(
104: UIManagerColorProvider.TABBED_PANE_DARK_SHADOW,
105: UIManagerColorProvider.CONTROL_DARK_SHADOW,
106: FixedColorProvider.BLACK)), drawTop, drawLeft,
107: drawRight, flipLeftRight);
108: }
109:
110: /**
111: * Constructor.
112: *
113: * @param colorProvider the line color provider
114: * @param drawTop draw the top line
115: * @param drawLeft draw the left line
116: * @param drawRight draw the right line
117: * @param flipLeftRight if true the left line is rotated so that it is always to the left or at the top and
118: * vice versa for the right line, if false the left and right lines are rotated the same way as
119: * the other lines
120: */
121: public TabAreaLineBorder(ColorProvider colorProvider,
122: boolean drawTop, boolean drawLeft, boolean drawRight,
123: boolean flipLeftRight) {
124: this .color = colorProvider;
125: this .drawTop = drawTop;
126: this .drawLeft = drawLeft;
127: this .drawRight = drawRight;
128: this .flipLeftRight = flipLeftRight;
129: }
130:
131: public boolean isBorderOpaque() {
132: return true;
133: }
134:
135: public void paintBorder(Component c, Graphics g, int x, int y,
136: int width, int height) {
137: Insets insets = getBorderInsets(c);
138: g.setColor(color.getColor(c));
139:
140: if (insets.top == 1)
141: GraphicsUtil.drawOptimizedLine(g, x, y, x + width - 1, y);
142:
143: if (insets.bottom == 1)
144: GraphicsUtil.drawOptimizedLine(g, x, y + height - 1, x
145: + width - 1, y + height - 1);
146:
147: if (insets.left == 1)
148: GraphicsUtil.drawOptimizedLine(g, x, y, x, y + height - 1);
149:
150: if (insets.right == 1)
151: GraphicsUtil.drawOptimizedLine(g, x + width - 1, y, x
152: + width - 1, y + height - 1);
153: }
154:
155: private boolean drawTop(Direction orientation) {
156: return orientation == Direction.UP ? drawTop
157: : orientation == Direction.LEFT ? (flipLeftRight ? drawLeft
158: : drawRight)
159: : orientation == Direction.RIGHT ? drawLeft
160: : false;
161: }
162:
163: private boolean drawLeft(Direction orientation) {
164: return orientation == Direction.UP ? drawLeft
165: : orientation == Direction.LEFT ? drawTop
166: : orientation == Direction.DOWN ? (flipLeftRight ? drawLeft
167: : drawRight)
168: : false;
169: }
170:
171: private boolean drawRight(Direction orientation) {
172: return orientation == Direction.UP ? drawRight
173: : orientation == Direction.LEFT ? false
174: : orientation == Direction.DOWN ? (flipLeftRight ? drawRight
175: : drawLeft)
176: : drawTop;
177: }
178:
179: private boolean drawBottom(Direction orientation) {
180: return orientation == Direction.UP ? false
181: : orientation == Direction.LEFT ? (flipLeftRight ? drawRight
182: : drawLeft)
183: : orientation == Direction.RIGHT ? drawRight
184: : drawTop;
185: }
186:
187: public Insets getBorderInsets(Component c) {
188: if (c instanceof JComponent
189: && ((JComponent) c).getComponentCount() == 0)
190: return new Insets(0, 0, 0, 0);
191:
192: Direction orientation = getTabAreaDirection(c);
193:
194: return orientation != null ? new Insets(
195: drawTop(orientation) ? 1 : 0, drawLeft(orientation) ? 1
196: : 0, drawBottom(orientation) ? 1 : 0,
197: drawRight(orientation) ? 1 : 0)
198: : new Insets(0, 0, 0, 0);
199: }
200:
201: private static Direction getTabAreaDirection(Component c) {
202: TabbedPanel tp = TabbedUtils.getParentTabbedPanel(c);
203: return tp != null ? tp.getProperties().getTabAreaOrientation()
204: : null;
205:
206: }
207:
208: }
|