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: SlopedTabLineBorder.java,v 1.3 2005/02/16 11:28:14 jesper Exp $
023: package net.infonode.tabbedpanel.internal;
024:
025: import net.infonode.gui.colorprovider.ColorProvider;
026: import net.infonode.gui.colorprovider.UIManagerColorProvider;
027: import net.infonode.gui.shaped.border.AbstractPolygonBorder;
028:
029: import java.awt.*;
030:
031: /**
032: * @author johan
033: * @since 1.2.0
034: */
035: public class SlopedTabLineBorder extends AbstractPolygonBorder {
036: private static final long serialVersionUID = 1;
037:
038: private static final int[][][] corners = {
039: { { 0, 0 }, { -1, 0, 0, -1 }, { -2, 0, -1, -1, 0, -1 },
040: { -4, 0, -3, -1, -2, -1, -1, -2, -1, -3, 0, -4 }, },
041: { { 0, 0 }, { 0, 0 }, { 0, 1, 1, 1, 2, 0 },
042: { 0, 4, 1, 3, 1, 2, 2, 1, 3, 1, 4, 0 }, },
043: { { 0, 0 }, { 0, 0 }, { -2, 0, -1, 1, 0, 1 },
044: { -4, 0, -3, 1, -2, 1, -1, 2, -1, 3, 0, 4 } },
045: { { 0, 0 }, { 0, -1, 1, 0 }, { 0, -1, 1, -1, 2, 0 },
046: { 0, -4, 1, -3, 1, -2, 2, -1, 3, -1, 4, 0 }, },
047: { { 0, 0 }, { 0, 0 }, { 0, 2, 1, 1, 1, 0 },
048: { 0, 4, 1, 3, 1, 2, 2, 1, 3, 1, 4, 0 }, },
049: { { 0, 0 }, { 0, 0 }, { -1, 0, -1, 1, 0, 2 },
050: { -4, 0, -3, 1, -2, 1, -1, 2, -1, 3, 0, 4 }, }, };
051:
052: private boolean drawBottomLine;
053: private float leftSlope;
054: private float rightSlope;
055: private boolean bottomLeftRounded;
056: private boolean topLeftRounded;
057: private boolean topRightRounded;
058: private boolean bottomRightRounded;
059: private int leftHeight;
060: private int rightHeight;
061:
062: private static int[] xCoords = new int[100];
063: private static int[] yCoords = new int[100];
064: private static int x;
065: private static int y;
066: private static int index;
067:
068: public SlopedTabLineBorder() {
069: this (0, 1);
070: }
071:
072: public SlopedTabLineBorder(float leftSlope, float rightSlope) {
073: this (leftSlope, rightSlope, 22, 22);
074: }
075:
076: public SlopedTabLineBorder(float leftSlope, float rightSlope,
077: int leftHeight, int rightHeight) {
078: this (leftSlope, rightSlope, leftHeight, rightHeight, false,
079: false, false, false);
080: }
081:
082: public SlopedTabLineBorder(float leftSlope, float rightSlope,
083: boolean bottomLeftRounded, boolean topLeftRounded,
084: boolean topRightRounded, boolean bottomRightRounded) {
085: this (leftSlope, rightSlope, 22, 22, bottomLeftRounded,
086: topLeftRounded, topRightRounded, bottomRightRounded);
087: }
088:
089: public SlopedTabLineBorder(float leftSlope, float rightSlope,
090: int leftHeight, int rightHeight, boolean bottomLeftRounded,
091: boolean topLeftRounded, boolean topRightRounded,
092: boolean bottomRightRounded) {
093: this (UIManagerColorProvider.TABBED_PANE_DARK_SHADOW,
094: UIManagerColorProvider.TABBED_PANE_HIGHLIGHT, false,
095: leftSlope, rightSlope, leftHeight, rightHeight,
096: bottomLeftRounded, topLeftRounded, topRightRounded,
097: bottomRightRounded);
098: }
099:
100: public SlopedTabLineBorder(ColorProvider lineColor,
101: ColorProvider highlightColor, boolean drawBottomLine,
102: float leftSlope, float rightSlope, int leftHeight,
103: int rightHeight, boolean bottomLeftRounded,
104: boolean topLeftRounded, boolean topRightRounded,
105: boolean bottomRightRounded) {
106: super (lineColor, highlightColor);
107: this .drawBottomLine = drawBottomLine;
108: this .leftHeight = leftHeight;
109: this .rightHeight = rightHeight;
110: this .leftSlope = leftSlope;
111: this .rightSlope = rightSlope;
112: this .bottomLeftRounded = bottomLeftRounded;
113: this .topLeftRounded = topLeftRounded;
114: this .topRightRounded = topRightRounded;
115: this .bottomRightRounded = bottomRightRounded;
116: }
117:
118: protected boolean lineIsDrawn(int index, Polygon polygon) {
119: return drawBottomLine || index < polygon.npoints - 1;
120: }
121:
122: protected Insets getShapedBorderInsets(Component c) {
123: return new Insets(1, (isBottomLeftRounded(c) ? 4 : 1)
124: + (topLeftRounded ? (leftSlope <= 0.5f ? 4 : 1) : 0)
125: + (int) (leftSlope * leftHeight), drawBottomLine ? 1
126: : 0, (bottomRightRounded ? 4 : 1)
127: + (topRightRounded ? (rightSlope <= 0.5f ? 4 : 1) : 0)
128: + (int) (rightSlope * rightHeight));
129: }
130:
131: protected boolean isBottomLeftRounded(Component c) {
132: return bottomLeftRounded;
133: }
134:
135: private static int[] getCorner(int type, float slope,
136: boolean rounded) {
137: return corners[type][!rounded ? 0 : type < 4 ? (slope >= 2f ? 1
138: : slope >= 1f ? 2 : 3) : (slope <= 0.5f ? 1
139: : slope <= 1f ? 2 : 3)];
140: }
141:
142: private static void addPoint(int px, int py) {
143: xCoords[index] = px;
144: yCoords[index++] = py;
145: x = px;
146: y = py;
147: }
148:
149: private static void addCorner(int px, int py, int[] c) {
150: for (int i = 0; i < c.length; i++) {
151: addPoint(px + c[i++], py + c[i]);
152: }
153: }
154:
155: private static int getStartY(int[] corner) {
156: return corner[1];
157: }
158:
159: private static int getEndY(int[] corner) {
160: return corner[corner.length - 1];
161: }
162:
163: protected Polygon createPolygon(Component c, int width, int height) {
164: boolean bottomLeftRounded = isBottomLeftRounded(c);
165: int leftX = (int) (leftHeight * leftSlope + (bottomLeftRounded ? 4
166: : 0));
167: int rightX = width
168: - 1
169: - (int) (rightHeight * rightSlope + (bottomRightRounded ? 4
170: : 0));
171: int bottomY = height - (drawBottomLine ? 1 : 0);
172:
173: int[] topLeft = getCorner(1, leftSlope, topLeftRounded);
174: int[] topRight = getCorner(2, rightSlope, topRightRounded);
175: int[] bottomLeft = getCorner(0, leftSlope, bottomLeftRounded);
176: int[] bottomRight = getCorner(3, rightSlope, bottomRightRounded);
177:
178: index = 0;
179: y = bottomY;
180: int dy = height - getStartY(topLeft) + getEndY(bottomLeft);
181:
182: if (dy <= leftHeight) {
183: x = leftX - (int) (dy * leftSlope);
184: addCorner(x, y, bottomLeft);
185: } else {
186: x = leftX - (int) (leftHeight * leftSlope);
187: addCorner(x, y, getCorner(0, 0, bottomLeftRounded));
188: addPoint(x, getStartY(topLeft) + leftHeight);
189: }
190:
191: addCorner(leftX, 0, topLeft);
192: addCorner(rightX, 0, topRight);
193: dy = height - getEndY(topRight) + getStartY(bottomRight);
194:
195: if (dy <= rightHeight) {
196: addCorner((int) (rightX + dy * rightSlope), bottomY,
197: bottomRight);
198: } else {
199: addPoint((int) (rightX + rightSlope * rightHeight),
200: getEndY(topRight) + rightHeight);
201: addCorner(x, bottomY, getCorner(3, 0, bottomRightRounded));
202: }
203:
204: return new Polygon(xCoords, yCoords, index);
205: }
206:
207: }
|