2
|
1 |
/*
|
|
2 |
* Copyright 1998-2006 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
package javax.swing.text.html;
|
|
26 |
|
|
27 |
import java.awt.Polygon;
|
|
28 |
import java.io.Serializable;
|
|
29 |
import java.util.StringTokenizer;
|
|
30 |
import java.util.Vector;
|
|
31 |
import javax.swing.text.AttributeSet;
|
|
32 |
|
|
33 |
/**
|
|
34 |
* Map is used to represent a map element that is part of an HTML document.
|
|
35 |
* Once a Map has been created, and any number of areas have been added,
|
|
36 |
* you can test if a point falls inside the map via the contains method.
|
|
37 |
*
|
|
38 |
* @author Scott Violet
|
|
39 |
*/
|
|
40 |
class Map implements Serializable {
|
|
41 |
/** Name of the Map. */
|
|
42 |
private String name;
|
|
43 |
/** An array of AttributeSets. */
|
|
44 |
private Vector areaAttributes;
|
|
45 |
/** An array of RegionContainments, will slowly grow to match the
|
|
46 |
* length of areaAttributes as needed. */
|
|
47 |
private Vector areas;
|
|
48 |
|
|
49 |
public Map() {
|
|
50 |
}
|
|
51 |
|
|
52 |
public Map(String name) {
|
|
53 |
this.name = name;
|
|
54 |
}
|
|
55 |
|
|
56 |
/**
|
|
57 |
* Returns the name of the Map.
|
|
58 |
*/
|
|
59 |
public String getName() {
|
|
60 |
return name;
|
|
61 |
}
|
|
62 |
|
|
63 |
/**
|
|
64 |
* Defines a region of the Map, based on the passed in AttributeSet.
|
|
65 |
*/
|
|
66 |
public void addArea(AttributeSet as) {
|
|
67 |
if (as == null) {
|
|
68 |
return;
|
|
69 |
}
|
|
70 |
if (areaAttributes == null) {
|
|
71 |
areaAttributes = new Vector(2);
|
|
72 |
}
|
|
73 |
areaAttributes.addElement(as.copyAttributes());
|
|
74 |
}
|
|
75 |
|
|
76 |
/**
|
|
77 |
* Removes the previously created area.
|
|
78 |
*/
|
|
79 |
public void removeArea(AttributeSet as) {
|
|
80 |
if (as != null && areaAttributes != null) {
|
|
81 |
int numAreas = (areas != null) ? areas.size() : 0;
|
|
82 |
for (int counter = areaAttributes.size() - 1; counter >= 0;
|
|
83 |
counter--) {
|
|
84 |
if (((AttributeSet)areaAttributes.elementAt(counter)).
|
|
85 |
isEqual(as)){
|
|
86 |
areaAttributes.removeElementAt(counter);
|
|
87 |
if (counter < numAreas) {
|
|
88 |
areas.removeElementAt(counter);
|
|
89 |
}
|
|
90 |
}
|
|
91 |
}
|
|
92 |
}
|
|
93 |
}
|
|
94 |
|
|
95 |
/**
|
|
96 |
* Returns the AttributeSets representing the differet areas of the Map.
|
|
97 |
*/
|
|
98 |
public AttributeSet[] getAreas() {
|
|
99 |
int numAttributes = (areaAttributes != null) ? areaAttributes.size() :
|
|
100 |
0;
|
|
101 |
if (numAttributes != 0) {
|
|
102 |
AttributeSet[] retValue = new AttributeSet[numAttributes];
|
|
103 |
|
|
104 |
areaAttributes.copyInto(retValue);
|
|
105 |
return retValue;
|
|
106 |
}
|
|
107 |
return null;
|
|
108 |
}
|
|
109 |
|
|
110 |
/**
|
|
111 |
* Returns the AttributeSet that contains the passed in location,
|
|
112 |
* <code>x</code>, <code>y</code>. <code>width</code>, <code>height</code>
|
|
113 |
* gives the size of the region the map is defined over. If a matching
|
|
114 |
* area is found, the AttribueSet for it is returned.
|
|
115 |
*/
|
|
116 |
public AttributeSet getArea(int x, int y, int width, int height) {
|
|
117 |
int numAttributes = (areaAttributes != null) ?
|
|
118 |
areaAttributes.size() : 0;
|
|
119 |
|
|
120 |
if (numAttributes > 0) {
|
|
121 |
int numAreas = (areas != null) ? areas.size() : 0;
|
|
122 |
|
|
123 |
if (areas == null) {
|
|
124 |
areas = new Vector(numAttributes);
|
|
125 |
}
|
|
126 |
for (int counter = 0; counter < numAttributes; counter++) {
|
|
127 |
if (counter >= numAreas) {
|
|
128 |
areas.addElement(createRegionContainment
|
|
129 |
((AttributeSet)areaAttributes.elementAt(counter)));
|
|
130 |
}
|
|
131 |
RegionContainment rc = (RegionContainment)areas.
|
|
132 |
elementAt(counter);
|
|
133 |
if (rc != null && rc.contains(x, y, width, height)) {
|
|
134 |
return (AttributeSet)areaAttributes.elementAt(counter);
|
|
135 |
}
|
|
136 |
}
|
|
137 |
}
|
|
138 |
return null;
|
|
139 |
}
|
|
140 |
|
|
141 |
/**
|
|
142 |
* Creates and returns an instance of RegionContainment that can be
|
|
143 |
* used to test if a particular point lies inside a region.
|
|
144 |
*/
|
|
145 |
protected RegionContainment createRegionContainment
|
|
146 |
(AttributeSet attributes) {
|
|
147 |
Object shape = attributes.getAttribute(HTML.Attribute.SHAPE);
|
|
148 |
|
|
149 |
if (shape == null) {
|
|
150 |
shape = "rect";
|
|
151 |
}
|
|
152 |
if (shape instanceof String) {
|
|
153 |
String shapeString = ((String)shape).toLowerCase();
|
|
154 |
RegionContainment rc = null;
|
|
155 |
|
|
156 |
try {
|
|
157 |
if (shapeString.equals("rect")) {
|
|
158 |
rc = new RectangleRegionContainment(attributes);
|
|
159 |
}
|
|
160 |
else if (shapeString.equals("circle")) {
|
|
161 |
rc = new CircleRegionContainment(attributes);
|
|
162 |
}
|
|
163 |
else if (shapeString.equals("poly")) {
|
|
164 |
rc = new PolygonRegionContainment(attributes);
|
|
165 |
}
|
|
166 |
else if (shapeString.equals("default")) {
|
|
167 |
rc = DefaultRegionContainment.sharedInstance();
|
|
168 |
}
|
|
169 |
} catch (RuntimeException re) {
|
|
170 |
// Something wrong with attributes.
|
|
171 |
rc = null;
|
|
172 |
}
|
|
173 |
return rc;
|
|
174 |
}
|
|
175 |
return null;
|
|
176 |
}
|
|
177 |
|
|
178 |
/**
|
|
179 |
* Creates and returns an array of integers from the String
|
|
180 |
* <code>stringCoords</code>. If one of the values represents a
|
|
181 |
* % the returned value with be negative. If a parse error results
|
|
182 |
* from trying to parse one of the numbers null is returned.
|
|
183 |
*/
|
|
184 |
static protected int[] extractCoords(Object stringCoords) {
|
|
185 |
if (stringCoords == null || !(stringCoords instanceof String)) {
|
|
186 |
return null;
|
|
187 |
}
|
|
188 |
|
|
189 |
StringTokenizer st = new StringTokenizer((String)stringCoords,
|
|
190 |
", \t\n\r");
|
|
191 |
int[] retValue = null;
|
|
192 |
int numCoords = 0;
|
|
193 |
|
|
194 |
while(st.hasMoreElements()) {
|
|
195 |
String token = st.nextToken();
|
|
196 |
int scale;
|
|
197 |
|
|
198 |
if (token.endsWith("%")) {
|
|
199 |
scale = -1;
|
|
200 |
token = token.substring(0, token.length() - 1);
|
|
201 |
}
|
|
202 |
else {
|
|
203 |
scale = 1;
|
|
204 |
}
|
|
205 |
try {
|
|
206 |
int intValue = Integer.parseInt(token);
|
|
207 |
|
|
208 |
if (retValue == null) {
|
|
209 |
retValue = new int[4];
|
|
210 |
}
|
|
211 |
else if(numCoords == retValue.length) {
|
|
212 |
int[] temp = new int[retValue.length * 2];
|
|
213 |
|
|
214 |
System.arraycopy(retValue, 0, temp, 0, retValue.length);
|
|
215 |
retValue = temp;
|
|
216 |
}
|
|
217 |
retValue[numCoords++] = intValue * scale;
|
|
218 |
} catch (NumberFormatException nfe) {
|
|
219 |
return null;
|
|
220 |
}
|
|
221 |
}
|
|
222 |
if (numCoords > 0 && numCoords != retValue.length) {
|
|
223 |
int[] temp = new int[numCoords];
|
|
224 |
|
|
225 |
System.arraycopy(retValue, 0, temp, 0, numCoords);
|
|
226 |
retValue = temp;
|
|
227 |
}
|
|
228 |
return retValue;
|
|
229 |
}
|
|
230 |
|
|
231 |
|
|
232 |
/**
|
|
233 |
* Defines the interface used for to check if a point is inside a
|
|
234 |
* region.
|
|
235 |
*/
|
|
236 |
interface RegionContainment {
|
|
237 |
/**
|
|
238 |
* Returns true if the location <code>x</code>, <code>y</code>
|
|
239 |
* falls inside the region defined in the receiver.
|
|
240 |
* <code>width</code>, <code>height</code> is the size of
|
|
241 |
* the enclosing region.
|
|
242 |
*/
|
|
243 |
public boolean contains(int x, int y, int width, int height);
|
|
244 |
}
|
|
245 |
|
|
246 |
|
|
247 |
/**
|
|
248 |
* Used to test for containment in a rectangular region.
|
|
249 |
*/
|
|
250 |
static class RectangleRegionContainment implements RegionContainment {
|
|
251 |
/** Will be non-null if one of the values is a percent, and any value
|
|
252 |
* that is non null indicates it is a percent
|
|
253 |
* (order is x, y, width, height). */
|
|
254 |
float[] percents;
|
|
255 |
/** Last value of width passed in. */
|
|
256 |
int lastWidth;
|
|
257 |
/** Last value of height passed in. */
|
|
258 |
int lastHeight;
|
|
259 |
/** Top left. */
|
|
260 |
int x0;
|
|
261 |
int y0;
|
|
262 |
/** Bottom right. */
|
|
263 |
int x1;
|
|
264 |
int y1;
|
|
265 |
|
|
266 |
public RectangleRegionContainment(AttributeSet as) {
|
|
267 |
int[] coords = Map.extractCoords(as.getAttribute(HTML.
|
|
268 |
Attribute.COORDS));
|
|
269 |
|
|
270 |
percents = null;
|
|
271 |
if (coords == null || coords.length != 4) {
|
|
272 |
throw new RuntimeException("Unable to parse rectangular area");
|
|
273 |
}
|
|
274 |
else {
|
|
275 |
x0 = coords[0];
|
|
276 |
y0 = coords[1];
|
|
277 |
x1 = coords[2];
|
|
278 |
y1 = coords[3];
|
|
279 |
if (x0 < 0 || y0 < 0 || x1 < 0 || y1 < 0) {
|
|
280 |
percents = new float[4];
|
|
281 |
lastWidth = lastHeight = -1;
|
|
282 |
for (int counter = 0; counter < 4; counter++) {
|
|
283 |
if (coords[counter] < 0) {
|
|
284 |
percents[counter] = Math.abs
|
|
285 |
(coords[counter]) / 100.0f;
|
|
286 |
}
|
|
287 |
else {
|
|
288 |
percents[counter] = -1.0f;
|
|
289 |
}
|
|
290 |
}
|
|
291 |
}
|
|
292 |
}
|
|
293 |
}
|
|
294 |
|
|
295 |
public boolean contains(int x, int y, int width, int height) {
|
|
296 |
if (percents == null) {
|
|
297 |
return contains(x, y);
|
|
298 |
}
|
|
299 |
if (lastWidth != width || lastHeight != height) {
|
|
300 |
lastWidth = width;
|
|
301 |
lastHeight = height;
|
|
302 |
if (percents[0] != -1.0f) {
|
|
303 |
x0 = (int)(percents[0] * width);
|
|
304 |
}
|
|
305 |
if (percents[1] != -1.0f) {
|
|
306 |
y0 = (int)(percents[1] * height);
|
|
307 |
}
|
|
308 |
if (percents[2] != -1.0f) {
|
|
309 |
x1 = (int)(percents[2] * width);
|
|
310 |
}
|
|
311 |
if (percents[3] != -1.0f) {
|
|
312 |
y1 = (int)(percents[3] * height);
|
|
313 |
}
|
|
314 |
}
|
|
315 |
return contains(x, y);
|
|
316 |
}
|
|
317 |
|
|
318 |
public boolean contains(int x, int y) {
|
|
319 |
return ((x >= x0 && x <= x1) &&
|
|
320 |
(y >= y0 && y <= y1));
|
|
321 |
}
|
|
322 |
}
|
|
323 |
|
|
324 |
|
|
325 |
/**
|
|
326 |
* Used to test for containment in a polygon region.
|
|
327 |
*/
|
|
328 |
static class PolygonRegionContainment extends Polygon implements
|
|
329 |
RegionContainment {
|
|
330 |
/** If any value is a percent there will be an entry here for the
|
|
331 |
* percent value. Use percentIndex to find out the index for it. */
|
|
332 |
float[] percentValues;
|
|
333 |
int[] percentIndexs;
|
|
334 |
/** Last value of width passed in. */
|
|
335 |
int lastWidth;
|
|
336 |
/** Last value of height passed in. */
|
|
337 |
int lastHeight;
|
|
338 |
|
|
339 |
public PolygonRegionContainment(AttributeSet as) {
|
|
340 |
int[] coords = Map.extractCoords(as.getAttribute(HTML.Attribute.
|
|
341 |
COORDS));
|
|
342 |
|
|
343 |
if (coords == null || coords.length == 0 ||
|
|
344 |
coords.length % 2 != 0) {
|
|
345 |
throw new RuntimeException("Unable to parse polygon area");
|
|
346 |
}
|
|
347 |
else {
|
|
348 |
int numPercents = 0;
|
|
349 |
|
|
350 |
lastWidth = lastHeight = -1;
|
|
351 |
for (int counter = coords.length - 1; counter >= 0;
|
|
352 |
counter--) {
|
|
353 |
if (coords[counter] < 0) {
|
|
354 |
numPercents++;
|
|
355 |
}
|
|
356 |
}
|
|
357 |
|
|
358 |
if (numPercents > 0) {
|
|
359 |
percentIndexs = new int[numPercents];
|
|
360 |
percentValues = new float[numPercents];
|
|
361 |
for (int counter = coords.length - 1, pCounter = 0;
|
|
362 |
counter >= 0; counter--) {
|
|
363 |
if (coords[counter] < 0) {
|
|
364 |
percentValues[pCounter] = coords[counter] /
|
|
365 |
-100.0f;
|
|
366 |
percentIndexs[pCounter] = counter;
|
|
367 |
pCounter++;
|
|
368 |
}
|
|
369 |
}
|
|
370 |
}
|
|
371 |
else {
|
|
372 |
percentIndexs = null;
|
|
373 |
percentValues = null;
|
|
374 |
}
|
|
375 |
npoints = coords.length / 2;
|
|
376 |
xpoints = new int[npoints];
|
|
377 |
ypoints = new int[npoints];
|
|
378 |
|
|
379 |
for (int counter = 0; counter < npoints; counter++) {
|
|
380 |
xpoints[counter] = coords[counter + counter];
|
|
381 |
ypoints[counter] = coords[counter + counter + 1];
|
|
382 |
}
|
|
383 |
}
|
|
384 |
}
|
|
385 |
|
|
386 |
public boolean contains(int x, int y, int width, int height) {
|
|
387 |
if (percentValues == null || (lastWidth == width &&
|
|
388 |
lastHeight == height)) {
|
|
389 |
return contains(x, y);
|
|
390 |
}
|
|
391 |
// Force the bounding box to be recalced.
|
|
392 |
bounds = null;
|
|
393 |
lastWidth = width;
|
|
394 |
lastHeight = height;
|
|
395 |
float fWidth = (float)width;
|
|
396 |
float fHeight = (float)height;
|
|
397 |
for (int counter = percentValues.length - 1; counter >= 0;
|
|
398 |
counter--) {
|
|
399 |
if (percentIndexs[counter] % 2 == 0) {
|
|
400 |
// x
|
|
401 |
xpoints[percentIndexs[counter] / 2] =
|
|
402 |
(int)(percentValues[counter] * fWidth);
|
|
403 |
}
|
|
404 |
else {
|
|
405 |
// y
|
|
406 |
ypoints[percentIndexs[counter] / 2] =
|
|
407 |
(int)(percentValues[counter] * fHeight);
|
|
408 |
}
|
|
409 |
}
|
|
410 |
return contains(x, y);
|
|
411 |
}
|
|
412 |
}
|
|
413 |
|
|
414 |
|
|
415 |
/**
|
|
416 |
* Used to test for containment in a circular region.
|
|
417 |
*/
|
|
418 |
static class CircleRegionContainment implements RegionContainment {
|
|
419 |
/** X origin of the circle. */
|
|
420 |
int x;
|
|
421 |
/** Y origin of the circle. */
|
|
422 |
int y;
|
|
423 |
/** Radius of the circle. */
|
|
424 |
int radiusSquared;
|
|
425 |
/** Non-null indicates one of the values represents a percent. */
|
|
426 |
float[] percentValues;
|
|
427 |
/** Last value of width passed in. */
|
|
428 |
int lastWidth;
|
|
429 |
/** Last value of height passed in. */
|
|
430 |
int lastHeight;
|
|
431 |
|
|
432 |
public CircleRegionContainment(AttributeSet as) {
|
|
433 |
int[] coords = Map.extractCoords(as.getAttribute(HTML.Attribute.
|
|
434 |
COORDS));
|
|
435 |
|
|
436 |
if (coords == null || coords.length != 3) {
|
|
437 |
throw new RuntimeException("Unable to parse circular area");
|
|
438 |
}
|
|
439 |
x = coords[0];
|
|
440 |
y = coords[1];
|
|
441 |
radiusSquared = coords[2] * coords[2];
|
|
442 |
if (coords[0] < 0 || coords[1] < 0 || coords[2] < 0) {
|
|
443 |
lastWidth = lastHeight = -1;
|
|
444 |
percentValues = new float[3];
|
|
445 |
for (int counter = 0; counter < 3; counter++) {
|
|
446 |
if (coords[counter] < 0) {
|
|
447 |
percentValues[counter] = coords[counter] /
|
|
448 |
-100.0f;
|
|
449 |
}
|
|
450 |
else {
|
|
451 |
percentValues[counter] = -1.0f;
|
|
452 |
}
|
|
453 |
}
|
|
454 |
}
|
|
455 |
else {
|
|
456 |
percentValues = null;
|
|
457 |
}
|
|
458 |
}
|
|
459 |
|
|
460 |
public boolean contains(int x, int y, int width, int height) {
|
|
461 |
if (percentValues != null && (lastWidth != width ||
|
|
462 |
lastHeight != height)) {
|
|
463 |
int newRad = Math.min(width, height) / 2;
|
|
464 |
|
|
465 |
lastWidth = width;
|
|
466 |
lastHeight = height;
|
|
467 |
if (percentValues[0] != -1.0f) {
|
|
468 |
this.x = (int)(percentValues[0] * width);
|
|
469 |
}
|
|
470 |
if (percentValues[1] != -1.0f) {
|
|
471 |
this.y = (int)(percentValues[1] * height);
|
|
472 |
}
|
|
473 |
if (percentValues[2] != -1.0f) {
|
|
474 |
radiusSquared = (int)(percentValues[2] *
|
|
475 |
Math.min(width, height));
|
|
476 |
radiusSquared *= radiusSquared;
|
|
477 |
}
|
|
478 |
}
|
|
479 |
return (((x - this.x) * (x - this.x) +
|
|
480 |
(y - this.y) * (y - this.y)) <= radiusSquared);
|
|
481 |
}
|
|
482 |
}
|
|
483 |
|
|
484 |
|
|
485 |
/**
|
|
486 |
* An implementation that will return true if the x, y location is
|
|
487 |
* inside a rectangle defined by origin 0, 0, and width equal to
|
|
488 |
* width passed in, and height equal to height passed in.
|
|
489 |
*/
|
|
490 |
static class DefaultRegionContainment implements RegionContainment {
|
|
491 |
/** A global shared instance. */
|
|
492 |
static DefaultRegionContainment si = null;
|
|
493 |
|
|
494 |
public static DefaultRegionContainment sharedInstance() {
|
|
495 |
if (si == null) {
|
|
496 |
si = new DefaultRegionContainment();
|
|
497 |
}
|
|
498 |
return si;
|
|
499 |
}
|
|
500 |
|
|
501 |
public boolean contains(int x, int y, int width, int height) {
|
|
502 |
return (x <= width && x >= 0 && y >= 0 && y <= width);
|
|
503 |
}
|
|
504 |
}
|
|
505 |
}
|