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<RegionContainment>(numAttributes);
125 }
126 for (int counter = 0; counter < numAttributes; counter++) {
127 if (counter >= numAreas) {
128 areas.addElement(createRegionContainment
129 (areaAttributes.elementAt(counter)));
130 }
131 RegionContainment rc = areas.elementAt(counter);
132 if (rc != null && rc.contains(x, y, width, height)) {
159 else if (shapeString.equals("circle")) {
160 rc = new CircleRegionContainment(attributes);
161 }
162 else if (shapeString.equals("poly")) {
163 rc = new PolygonRegionContainment(attributes);
164 }
165 else if (shapeString.equals("default")) {
166 rc = DefaultRegionContainment.sharedInstance();
167 }
168 } catch (RuntimeException re) {
169 // Something wrong with attributes.
170 rc = null;
171 }
172 return rc;
173 }
174 return null;
175 }
176
177 /**
178 * Creates and returns an array of integers from the String
179 * <code>stringCoords</code>. If one of the values represents a
180 * % the returned value with be negative. If a parse error results
181 * from trying to parse one of the numbers null is returned.
182 */
183 protected static int[] extractCoords(Object stringCoords) {
184 if (stringCoords == null || !(stringCoords instanceof String)) {
185 return null;
186 }
187
188 StringTokenizer st = new StringTokenizer((String)stringCoords,
189 ", \t\n\r");
190 int[] retValue = null;
191 int numCoords = 0;
192
193 while(st.hasMoreElements()) {
194 String token = st.nextToken();
195 int scale;
196
197 if (token.endsWith("%")) {
198 scale = -1;
199 token = token.substring(0, token.length() - 1);
217 } catch (NumberFormatException nfe) {
218 return null;
219 }
220 }
221 if (numCoords > 0 && numCoords != retValue.length) {
222 int[] temp = new int[numCoords];
223
224 System.arraycopy(retValue, 0, temp, 0, numCoords);
225 retValue = temp;
226 }
227 return retValue;
228 }
229
230
231 /**
232 * Defines the interface used for to check if a point is inside a
233 * region.
234 */
235 interface RegionContainment {
236 /**
237 * Returns true if the location <code>x</code>, <code>y</code>
238 * falls inside the region defined in the receiver.
239 * <code>width</code>, <code>height</code> is the size of
240 * the enclosing region.
241 */
242 public boolean contains(int x, int y, int width, int height);
243 }
244
245
246 /**
247 * Used to test for containment in a rectangular region.
248 */
249 static class RectangleRegionContainment implements RegionContainment {
250 /** Will be non-null if one of the values is a percent, and any value
251 * that is non null indicates it is a percent
252 * (order is x, y, width, height). */
253 float[] percents;
254 /** Last value of width passed in. */
255 int lastWidth;
256 /** Last value of height passed in. */
257 int lastHeight;
258 /** Top left. */
259 int x0;
|
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 y}. {@code width}, {@code height}
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<RegionContainment>(numAttributes);
125 }
126 for (int counter = 0; counter < numAttributes; counter++) {
127 if (counter >= numAreas) {
128 areas.addElement(createRegionContainment
129 (areaAttributes.elementAt(counter)));
130 }
131 RegionContainment rc = areas.elementAt(counter);
132 if (rc != null && rc.contains(x, y, width, height)) {
159 else if (shapeString.equals("circle")) {
160 rc = new CircleRegionContainment(attributes);
161 }
162 else if (shapeString.equals("poly")) {
163 rc = new PolygonRegionContainment(attributes);
164 }
165 else if (shapeString.equals("default")) {
166 rc = DefaultRegionContainment.sharedInstance();
167 }
168 } catch (RuntimeException re) {
169 // Something wrong with attributes.
170 rc = null;
171 }
172 return rc;
173 }
174 return null;
175 }
176
177 /**
178 * Creates and returns an array of integers from the String
179 * {@code stringCoords}. If one of the values represents a
180 * % the returned value with be negative. If a parse error results
181 * from trying to parse one of the numbers null is returned.
182 */
183 protected static int[] extractCoords(Object stringCoords) {
184 if (stringCoords == null || !(stringCoords instanceof String)) {
185 return null;
186 }
187
188 StringTokenizer st = new StringTokenizer((String)stringCoords,
189 ", \t\n\r");
190 int[] retValue = null;
191 int numCoords = 0;
192
193 while(st.hasMoreElements()) {
194 String token = st.nextToken();
195 int scale;
196
197 if (token.endsWith("%")) {
198 scale = -1;
199 token = token.substring(0, token.length() - 1);
217 } catch (NumberFormatException nfe) {
218 return null;
219 }
220 }
221 if (numCoords > 0 && numCoords != retValue.length) {
222 int[] temp = new int[numCoords];
223
224 System.arraycopy(retValue, 0, temp, 0, numCoords);
225 retValue = temp;
226 }
227 return retValue;
228 }
229
230
231 /**
232 * Defines the interface used for to check if a point is inside a
233 * region.
234 */
235 interface RegionContainment {
236 /**
237 * Returns true if the location {@code x}, {@code y}
238 * falls inside the region defined in the receiver.
239 * {@code width}, {@code height} is the size of
240 * the enclosing region.
241 */
242 public boolean contains(int x, int y, int width, int height);
243 }
244
245
246 /**
247 * Used to test for containment in a rectangular region.
248 */
249 static class RectangleRegionContainment implements RegionContainment {
250 /** Will be non-null if one of the values is a percent, and any value
251 * that is non null indicates it is a percent
252 * (order is x, y, width, height). */
253 float[] percents;
254 /** Last value of width passed in. */
255 int lastWidth;
256 /** Last value of height passed in. */
257 int lastHeight;
258 /** Top left. */
259 int x0;
|