001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: /*
042: * EqualPolygon.java
043: *
044: * Created on February 10, 2004, 1:17 PM
045: */
046:
047: package org.netbeans.swing.tabcontrol.plaf;
048:
049: import java.awt.*;
050: import java.util.Arrays;
051: import java.util.Comparator;
052: import java.util.HashSet;
053:
054: /**
055: * A Polygon which implements a proper equals/hashcode contract. In order to
056: * optimize drag and drop repainting, it is necessary that the Shape objects
057: * returned by getTabIndication() be able to be compared properly.
058: * <p/>
059: * To ease migration of older code, this class also implements a couple methods
060: * of GeneralPath, which was used before. These methods just delegate to
061: * addPoint(), so the full functionality of GeneralPath is not replicated
062: * (specifically, a polygon must be contiguous and closed).
063: * <p/>
064: *
065: * @author Tim Boudreau
066: */
067: public final class EqualPolygon extends Polygon {
068:
069: /**
070: * Creates a new instance of EqualGeneralPath
071: */
072: public EqualPolygon() {
073: }
074:
075: /**
076: * Copy constructor will copy the xpoints/ypoints arrays so the caller can
077: * later modify them without changing the polygon constructor here.
078: */
079: public EqualPolygon(int[] x, int[] y, int n) {
080: //Must clone the arrays, or transforms on the source of the polygon
081: //will also transform this one
082: xpoints = new int[n];
083: ypoints = new int[n];
084: System.arraycopy(x, 0, xpoints, 0, xpoints.length);
085: System.arraycopy(y, 0, ypoints, 0, ypoints.length);
086: npoints = n;
087: }
088:
089: /**
090: * Copy constructor - takes either another EqualPolygon or a Polygon. Copies
091: * the points arrays of the original polygon, so the passed polygon may be
092: * modified without affecting the instance constructed here.
093: *
094: * @param p
095: */
096: public EqualPolygon(Polygon p) {
097: super (p.xpoints, p.ypoints, p.npoints);
098: }
099:
100: /** Convenience constructor which takes a Rectangle */
101: public EqualPolygon(Rectangle r) {
102: super (new int[] { r.x, r.x + r.width, r.x + r.width, r.x },
103: new int[] { r.y, r.y, r.y + r.height, r.y + r.height },
104: 4);
105: }
106:
107: /**
108: * Non copy constructor based on fixed arrays. Takes the point count
109: * parameter from<code>x.length</code>.
110: */
111: public EqualPolygon(int[] x, int[] y) {
112: super (x, y, x.length);
113: }
114:
115: /**
116: * Delegates to <code>Polygon.addPoint()</code>.
117: *
118: * @param x x coordinate
119: * @param y y coordinate
120: */
121: public void moveTo(int x, int y) {
122: addPoint(x, y);
123: }
124:
125: /**
126: * Delegates to <code>Polygon.addPoint()</code>.
127: *
128: * @param x x coordinate
129: * @param y y coordinate
130: */
131: public void lineTo(int x, int y) {
132: addPoint(x, y);
133: }
134:
135: /**
136: * Creates a new EqualPolygon using the copy constructor - the resulting
137: * polygon may be modified without affecting the original.
138: *
139: * @return A new instance of EqualPolygon with the same point values
140: */
141: public Object clone() {
142: return new EqualPolygon(xpoints, ypoints, xpoints.length);
143: }
144:
145: /**
146: * Overridden to produce a meaningful result.
147: *
148: * @return A string representation of the EqualPolygon
149: */
150: public String toString() {
151: StringBuffer sb = new StringBuffer("EqualPolygon: "); //NOI18N
152: for (int i = 0; i < npoints; i++) {
153: sb.append(' '); //NOI18N
154: sb.append(xpoints[i]);
155: sb.append(','); //NOI18N
156: sb.append(ypoints[i]);
157: }
158: return sb.toString();
159: }
160:
161: /**
162: * Computes a hashCode based on the points arrays.
163: *
164: * @return The hash code
165: */
166: public int hashCode() {
167: return arrayHashCode(xpoints) ^ arrayHashCode(ypoints);
168: }
169:
170: private int arrayHashCode(int[] o) {
171: int result = 0;
172: for (int i = 0; i < npoints; i++) {
173: result += o[i] ^ i;
174: }
175: return result;
176: }
177:
178: /**
179: * Returns true if the argument is a Polygon (does not need to be
180: * EqualPolygon) and its point arrays and number of points matches.
181: *
182: * @param o Another polygon
183: * @return whether or not they are equal
184: */
185: public boolean equals(Object o) {
186: if (o == this ) {
187: return true;
188: }
189: if (o instanceof Polygon) {
190: Polygon p = (Polygon) o;
191: int[] ox = p.xpoints;
192: int[] oy = p.ypoints;
193: boolean result = Arrays.equals(xpoints, ox)
194: && Arrays.equals(ypoints, oy);
195: result &= p.npoints == npoints;
196: return result;
197: } else {
198: return false;
199: }
200: }
201:
202: private Point[] sortPoints(Point[] p) {
203: //Prune duplicates
204: HashSet<Point> set = new HashSet<Point>(Arrays.asList(p));
205: p = new Point[set.size()];
206: p = set.toArray(p);
207: //Then sort
208: Arrays.sort(p, comparator);
209: return p;
210: }
211:
212: private static final Comparator<Point> comparator = new PointsComparator();
213:
214: private static class PointsComparator implements Comparator<Point> {
215: public int compare(Point a, Point b) {
216: int result = (a.y * (a.x - b.x)) - (b.y * (b.x - a.x));
217: return result;
218: }
219: }
220:
221: }
|