/** * */ package ie.dcu.apps.ist.export.imagemap; import java.awt.Polygon; import java.awt.Rectangle; import java.net.*; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; public class MapArea { private String href; private String alt; private AreaShape shape; private int[] coords; private final Map attrs; public MapArea() { href = ""; alt = ""; shape = AreaShape.Polygon; coords = new int[0]; attrs = new HashMap(); } public void addAttr(String key, String value) { attrs.put(key, value); } public String getHref() { return href; } public void setHref(String href) { this.href = href == null ? "" : href; } public String getAlt() { return alt; } public void setAlt(String alt) { this.alt = alt == null ? "" : alt; } public AreaShape getShape() { return shape; } public void setShape(AreaShape shape) { this.shape = shape == null ? AreaShape.Polygon : shape; } public int[] getCoords() { return coords; } public void setCoords(int[] coords) { this.coords = coords == null ? new int[0] : coords; } public void setRect(Rectangle rect) { setShape(AreaShape.Rectangle); int[] coords = { rect.x, rect.y, rect.x + rect.width, rect.y + rect.height }; setCoords(coords); } public void setCircle(int x, int y, int r) { setShape(AreaShape.Circle); int[] coords = { x, y, r }; setCoords(coords); } public void setPolygon(Polygon poly) { setShape(AreaShape.Polygon); int[] coords = new int[2*poly.npoints]; for (int i = 0, j = 0; i < poly.npoints; i++) { coords[j++] = poly.xpoints[i]; coords[j++] = poly.ypoints[i]; } setCoords(coords); } public void export(StringBuffer sb, int indent) { String encodedHREF = href; if (href.length() != 0) { try { URI uri = new URI(href); URL url = uri.toURL(); encodedHREF = url.toString(); } catch (URISyntaxException e) { // Ignore exceptions, the href string will be used instead } catch (MalformedURLException e) { // Ignore exceptions, the href string will be used instead } } HtmlTag tag = new HtmlTag("area"); tag .attr("href", encodedHREF) .attr("alt", alt) .attr("shape", shape.toString()) .attr("coords", getCoordsString()); for (Entry entry : attrs.entrySet()) { tag.attr(entry.getKey(), entry.getValue()); } tag.open(sb, indent, true); } public String getCoordsString() { StringBuffer sb = new StringBuffer(); boolean first = true; for (int coord : coords) { if (!first) { sb.append(','); } sb.append(coord); first = false; } return sb.toString(); } }