FileDocCategorySizeDatePackage
Map.javaAPI DocJava SE 5 API13612Fri Aug 26 14:58:18 BST 2005javax.swing.text.html

Map

public class Map extends Object
Map is used to represent a map element that is part of an HTML document. Once a Map has been created, and any number of areas have been added, you can test if a point falls inside the map via the contains method.
author
Scott Violet
version
1.8 12/19/03

Fields Summary
private String
name
Name of the Map.
private Vector
areaAttributes
An array of AttributeSets.
private Vector
areas
An array of RegionContainments, will slowly grow to match the length of areaAttributes as needed.
Constructors Summary
public Map()

    
public Map(String name)

	this.name = name;
    
Methods Summary
public voidaddArea(javax.swing.text.AttributeSet as)
Defines a region of the Map, based on the passed in AttributeSet.

	if (as == null) {
	    return;
	}
	if (areaAttributes == null) {
	    areaAttributes = new Vector(2);
	}
	areaAttributes.addElement(as.copyAttributes());
    
protected javax.swing.text.html.Map$RegionContainmentcreateRegionContainment(javax.swing.text.AttributeSet attributes)
Creates and returns an instance of RegionContainment that can be used to test if a particular point lies inside a region.

	Object     shape = attributes.getAttribute(HTML.Attribute.SHAPE);

	if (shape == null) {
	    shape = "rect";
	}
	if (shape instanceof String) {
	    String                shapeString = ((String)shape).toLowerCase();
	    RegionContainment     rc = null;

	    try {
		if (shapeString.equals("rect")) {
		    rc = new RectangleRegionContainment(attributes);
		}
		else if (shapeString.equals("circle")) {
		    rc = new CircleRegionContainment(attributes);
		}
		else if (shapeString.equals("poly")) {
		    rc = new PolygonRegionContainment(attributes);
		}
		else if (shapeString.equals("default")) {
		    rc = DefaultRegionContainment.sharedInstance();
		}
	    } catch (RuntimeException re) {
		// Something wrong with attributes.
		rc = null;
	    }
	    return rc;
	}
	return null;
    
protected static int[]extractCoords(java.lang.Object stringCoords)
Creates and returns an array of integers from the String stringCoords. If one of the values represents a % the returned value with be negative. If a parse error results from trying to parse one of the numbers null is returned.

	if (stringCoords == null || !(stringCoords instanceof String)) {
	    return null;
	}

	StringTokenizer    st = new StringTokenizer((String)stringCoords,
						    ", \t\n\r");
	int[]              retValue = null;
	int                numCoords = 0;

	while(st.hasMoreElements()) {
	    String         token = st.nextToken();
	    int            scale;

	    if (token.endsWith("%")) {
		scale = -1;
		token = token.substring(0, token.length() - 1);
	    }
	    else {
		scale = 1;
	    }
	    try {
		int       intValue = Integer.parseInt(token);

		if (retValue == null) {
		    retValue = new int[4];
		}
		else if(numCoords == retValue.length) {
		    int[]    temp = new int[retValue.length * 2];

		    System.arraycopy(retValue, 0, temp, 0, retValue.length);
		    retValue = temp;
		}
		retValue[numCoords++] = intValue * scale;
	    } catch (NumberFormatException nfe) {
		return null;
	    }
	}
	if (numCoords > 0 && numCoords != retValue.length) {
	    int[]    temp = new int[numCoords];

	    System.arraycopy(retValue, 0, temp, 0, numCoords);
	    retValue = temp;
	}
	return retValue;
    
public javax.swing.text.AttributeSetgetArea(int x, int y, int width, int height)
Returns the AttributeSet that contains the passed in location, x, y. width, height gives the size of the region the map is defined over. If a matching area is found, the AttribueSet for it is returned.

	int      numAttributes = (areaAttributes != null) ?
	                         areaAttributes.size() : 0;

	if (numAttributes > 0) {
	    int      numAreas = (areas != null) ? areas.size() : 0;

	    if (areas == null) {
		areas = new Vector(numAttributes);
	    }
	    for (int counter = 0; counter < numAttributes; counter++) {
		if (counter >= numAreas) {
		    areas.addElement(createRegionContainment
			    ((AttributeSet)areaAttributes.elementAt(counter)));
		}
		RegionContainment       rc = (RegionContainment)areas.
                                             elementAt(counter);
		if (rc != null && rc.contains(x, y, width, height)) {
		    return (AttributeSet)areaAttributes.elementAt(counter);
		}
	    }
	}
	return null;
    
public javax.swing.text.AttributeSet[]getAreas()
Returns the AttributeSets representing the differet areas of the Map.

	int numAttributes = (areaAttributes != null) ? areaAttributes.size() :
	                    0;
	if (numAttributes != 0) {
	    AttributeSet[]    retValue = new AttributeSet[numAttributes];

	    areaAttributes.copyInto(retValue);
	    return retValue;
	}
	return null;
    
public java.lang.StringgetName()
Returns the name of the Map.

	return name;
    
public voidremoveArea(javax.swing.text.AttributeSet as)
Removes the previously created area.

	if (as != null && areaAttributes != null) {
	    int numAreas = (areas != null) ? areas.size() : 0;
	    for (int counter = areaAttributes.size() - 1; counter >= 0;
		 counter--) {
		if (((AttributeSet)areaAttributes.elementAt(counter)).
		    isEqual(as)){
		    areaAttributes.removeElementAt(counter);
		    if (counter < numAreas) {
			areas.removeElementAt(counter);
		    }
		}
	    }
	}