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: AbstractPolygonBorder.java,v 1.18 2005/12/04 13:46:04 jesper Exp $
023: package net.infonode.gui.shaped.border;
024:
025: import net.infonode.gui.GraphicsUtil;
026: import net.infonode.gui.HighlightPainter;
027: import net.infonode.gui.InsetsUtil;
028: import net.infonode.gui.colorprovider.BackgroundPainterColorProvider;
029: import net.infonode.gui.colorprovider.ColorProvider;
030: import net.infonode.gui.colorprovider.FixedColorProvider;
031: import net.infonode.gui.shaped.ShapedUtil;
032: import net.infonode.gui.shaped.panel.ShapedPanel;
033:
034: import java.awt.*;
035:
036: /**
037: * @author johan
038: */
039: abstract public class AbstractPolygonBorder extends
040: AbstractShapedBorder {
041: private static final long serialVersionUID = 1;
042:
043: private static final Insets HIGHLIGHT_INSETS = new Insets(1, 1, 0,
044: 0);
045: private ColorProvider lineColor;
046: private ColorProvider highlightColor = new FixedColorProvider(
047: new Color(255, 255, 255));
048: private ColorProvider middleColor;
049: private ColorProvider shadowColor;
050:
051: protected AbstractPolygonBorder(ColorProvider lineColor) {
052: this (lineColor, FixedColorProvider.WHITE);
053: }
054:
055: protected AbstractPolygonBorder(ColorProvider lineColor,
056: ColorProvider highlightColor) {
057: this (lineColor, highlightColor,
058: BackgroundPainterColorProvider.INSTANCE, null);
059: }
060:
061: protected AbstractPolygonBorder(ColorProvider lineColor,
062: ColorProvider highlightColor, ColorProvider middleColor,
063: ColorProvider shadowColor) {
064: this .lineColor = lineColor;
065: this .highlightColor = highlightColor;
066: this .middleColor = middleColor;
067: this .shadowColor = shadowColor;
068: }
069:
070: public Shape getShape(Component c, int x, int y, int width,
071: int height) {
072: int w = ShapedUtil.getWidth(c, width, height);
073: int h = ShapedUtil.getHeight(c, width, height);
074: Polygon polygon = getPolygon(c, x, y, w, h);
075:
076: //printPoints(polygon);
077:
078: //System.out.println("Polygon: width=" + w + " height=" + h);
079:
080: return polygon;
081: }
082:
083: public boolean isBorderOpaque() {
084: return false;
085: }
086:
087: public void paintBorder(Component c, Graphics g, int x, int y,
088: int width, int height) {
089: Shape clip = g.getClip();
090: g.clipRect(x, y, width, height);
091:
092: try {
093: int w = ShapedUtil.getWidth(c, width, height);
094: height = ShapedUtil.getHeight(c, width, height);
095: width = w;
096:
097: Polygon polygon = getPolygon(c, x, y, width, height);
098: Graphics2D g2 = (Graphics2D) g;
099:
100: if (highlightColor != null) {
101: paintHighlight(c, g2, polygon, width, height);
102: }
103:
104: if (lineColor != null) {
105: g.setColor(lineColor.getColor());
106: paintPolygon(c, g2, polygon, width, height);
107: }
108: } finally {
109: g.setClip(clip);
110: }
111: }
112:
113: public Insets getBorderInsets(Component c) {
114: Insets insets = getShapedBorderInsets(c);
115: insets = ShapedUtil.transformInsets(c, insets);
116: return highlightColor != null ? InsetsUtil.add(
117: getShapedBorderHighlightInsets(c), insets) : insets;
118: }
119:
120: protected Insets getShapedBorderInsets(Component c) {
121: return new Insets(0, 0, 0, 0);
122: }
123:
124: protected Insets getShapedBorderHighlightInsets(Component c) {
125: return HIGHLIGHT_INSETS;
126: }
127:
128: protected Polygon createPolygon(Component c, int width, int height) {
129: return new Polygon();
130: }
131:
132: protected void paintPolygon(Component c, Graphics2D g,
133: Polygon polygon, int width, int height) {
134: int i = 0;
135:
136: while (i < polygon.npoints) {
137: if (lineIsDrawn(i, polygon)) {
138: int ni = (i + 1) % polygon.npoints;
139: GraphicsUtil.drawOptimizedLine(g, polygon.xpoints[i],
140: polygon.ypoints[i], polygon.xpoints[ni],
141: polygon.ypoints[ni]);
142: }
143:
144: i++;
145: }
146: }
147:
148: protected void paintHighlight(Component c, Graphics2D g,
149: Polygon polygon, int width, int height) {
150: Color c1 = highlightColor == null ? null : highlightColor
151: .getColor(c);
152: Color c2 = middleColor.getColor(c);
153: Color c3 = shadowColor == null ? null : shadowColor.getColor(c);
154:
155: boolean clockWise = isPointsClockwise(c);
156:
157: for (int i = 0; i < polygon.npoints; i++) {
158: int ni = (i + 1) % polygon.npoints;
159:
160: if (lineIsDrawn(i, polygon)) {
161: HighlightPainter.drawLine(g, polygon.xpoints[i],
162: polygon.ypoints[i], polygon.xpoints[ni],
163: polygon.ypoints[ni], clockWise, true, c1, c2,
164: c3);
165: }
166: }
167: }
168:
169: protected boolean lineIsDrawn(int index, Polygon polygon) {
170: return true;
171: }
172:
173: /* protected int getWidth(Component c) {
174: Insets i = getOuterInsets(c);
175: Direction d = ShapedUtil.getDirection(c);
176: if (d == Direction.UP || d == Direction.DOWN)
177: return c.getHeight() - i.top - i.bottom;
178:
179: return c.getWidth() - i.left - i.right;
180: }
181:
182: protected int getHeight(Component c) {
183: Insets i = getOuterInsets(c);
184: Direction d = ShapedUtil.getDirection(c);
185: if (d == Direction.UP || d == Direction.DOWN)
186: return c.getWidth() - i.left - i.right;
187:
188: return c.getHeight() - i.top - i.bottom;
189: }
190: */
191: protected boolean isHighlightable(int deltaX, int deltaY) {
192: return deltaX > deltaY;
193: }
194:
195: protected boolean isPointsClockwise(Component c) {
196: if (c instanceof ShapedPanel)
197: return !(((ShapedPanel) c).isHorizontalFlip() ^ ((ShapedPanel) c)
198: .isVerticalFlip());
199:
200: return true;
201: }
202:
203: protected int getHighlightOffsetX(int deltaX, int deltaY) {
204: return deltaY - deltaX > 0 ? (deltaX + deltaY > 0 ? -1 : 0)
205: : (deltaX + deltaY > 0 ? 0 : 1); //-deltaY > deltaX ? 1 : 0;
206: }
207:
208: protected int getHighlightOffsetY(int deltaX, int deltaY) {
209: return deltaY - deltaX > 0 ? (deltaX + deltaY > 0 ? 0 : -1)
210: : (deltaX + deltaY > 0 ? 1 : 0); //-deltaY > deltaX ? 1 : 0;
211: }
212:
213: protected void setPoint(Polygon polygon, int x, int y) {
214: polygon.xpoints[polygon.npoints] = x;
215: polygon.ypoints[polygon.npoints] = y;
216: polygon.npoints++;
217: }
218:
219: private Polygon getPolygon(Component c, int x, int y, int width,
220: int height) {
221: Polygon polygon = createPolygon(c, width, height);
222: flipPolygon(c, polygon, width, height);
223: rotatePolygon(c, polygon, width, height);
224: fixGraphicsOffset(c, polygon, x, y);
225: return polygon;
226: }
227:
228: private void flipPolygon(Component c, Polygon polygon, int width,
229: int height) {
230: if (c instanceof ShapedPanel) {
231: if (((ShapedPanel) c).isHorizontalFlip()) {
232: for (int i = 0; i < polygon.npoints; i++)
233: polygon.xpoints[i] = Math.abs(width
234: - polygon.xpoints[i]) - 1;
235: }
236:
237: if (((ShapedPanel) c).isVerticalFlip()) {
238: for (int i = 0; i < polygon.npoints; i++)
239: polygon.ypoints[i] = Math.abs(height
240: - polygon.ypoints[i]) - 1;
241: }
242: }
243: }
244:
245: private void rotatePolygon(Component c, Polygon polygon, int width,
246: int height) {
247: ShapedUtil.rotate(polygon, ShapedUtil.getDirection(c), width,
248: height);
249: }
250:
251: private void fixGraphicsOffset(Component c, Polygon polygon, int x,
252: int y) {
253: for (int i = 0; i < polygon.npoints; i++) {
254: polygon.xpoints[i] += x;
255: polygon.ypoints[i] += y;
256: }
257: }
258:
259: }
|