/* * BioJava development code * * This code may be freely distributed and modified under the * terms of the GNU Lesser General Public Licence. This should * be distributed with the code. If you do not have a copy, * see: * * http://www.gnu.org/copyleft/lesser.html * * Copyright for this code is held jointly by the individual * authors. These should be listed in @author doc comments. * * For more information on the BioJava project and its aims, * or to join the biojava-l mailing list, visit the home page * at: * * http://www.biojava.org/ * */ package org.biojava.bio.gui.sequence; import java.io.Serializable; import java.net.URL; import java.util.ArrayList; import java.util.Iterator; import java.util.List; /** * ImageMap represents a collection of image map * hotspots. It does not represent the raster image itself. * * @author Keith James * @author Greg Cox * @since 1.3 */ public interface ImageMap { /** * RECT indicates a rectangular image map hotspot. */ public static final String RECT = "rect"; /** * CIRCLE indicates a circular image map hotspot. */ public static final String CIRCLE = "circle"; /** * POLY indicates a polygonal image map hotspot. */ public static final String POLY = "poly"; /** * addHotSpot adds a hotspot to the map. * * @param hotSpot a HotSpot. */ public void addHotSpot(HotSpot hotSpot); /** * hotSpots iterates over the hotspots in the map * * @return an Iterator. */ public Iterator hotSpots(); /** *

HotSpots represent an image map hotspot. For * example (in server-side map format):

* *

rect http://www.biojava.org 0,0 100,20

* *

A user object may be set for each hot spot. This would * typically contain extra data used to construct a representation * of the hotspot in a document or application. For example, in an * image map representing Blast search results the user object * could be a sequence in a database. In an HTML document the user * object could be used to assign values to actions such as * mouseover.

*/ public static final class HotSpot implements Serializable { private String type; private URL url; private Integer [] coordinates; private Object userObject; /** * Creates a new HotSpot with a null user object. * * @param type a String of hotspot. The only * valid arguments are ImageMap.RECT, ImageMap.CIRCLE or * ImageMap.POLY (checked by object reference equalty); * @param url a URL target. * @param coordinates an Integer [] array of * hotspot coordinates, in order. */ public HotSpot(String type, URL url, Integer [] coordinates) { if (! (type == RECT || type == CIRCLE || type == POLY)) throw new IllegalArgumentException("Failed to create HotSpot. Constructor was passed an invalid type '" + type + "'"); if (! (coordinates.length % 2 == 0)) throw new IllegalArgumentException("Failed to create HotSpot. The coordinates array contained an odd number of points"); this.type = type; this.url = url; this.coordinates = coordinates; } /** * Creates a new HotSpot. * * @param type a String of hotspot. The only * valid arguments are ImageMap.RECT, ImageMap.CIRCLE or * ImageMap.POLY (checked by object reference equalty); * @param url a URL target. * @param coordinates an Integer [] array of * hotspot coordinates, in order. * @param userObject an Object */ public HotSpot(String type, URL url, Integer [] coordinates, Object userObject) { this(type, url, coordinates); this.userObject = userObject; } /** * getType returns the type of hotspot. * * @return a String. */ public String getType() { return type; } /** * getURL returns the hotspot URL. * * @return a URL. */ public URL getURL() { return url; } /** * getCoordinates returns the hotspot coordinates. * * @return an Integer [] array. */ public Integer [] getCoordinates() { return coordinates; } /** * getUserObject returns the current user object * (or null). * * @return an Object. */ public Object getUserObject() { return userObject; } /** * setUserObject sets the user object. * * @param userObject an Object. */ public void setUserObject(Object userObject) { this.userObject = userObject; } public String toString() { return "HotSpot to " + url.toString(); } } /** * ClientSide represents a client-side style image * map. */ public static class ClientSide implements ImageMap, Serializable { private String name; private List hotSpots; /** * Creates a new ClientSide image map. * * @param name a String name by which the map * will be known. */ public ClientSide(String name) { this.name = name; hotSpots = new ArrayList(); } public void addHotSpot(HotSpot hotSpot) { hotSpots.add(hotSpot); } public Iterator hotSpots() { return hotSpots.iterator(); } public String toString() { StringBuffer sb = new StringBuffer(); sb.append("\n"); for (Iterator hi = hotSpots.iterator(); hi.hasNext();) { HotSpot hs = (HotSpot) hi.next(); Integer [] coords = hs.getCoordinates(); sb.append("\n"); } sb.append(""); return sb.substring(0); } } /** * ServerSide represents a server-side style image * map. */ public static class ServerSide implements ImageMap, Serializable { private List hotSpots; /** * Creates a new ServerSide image map. */ public ServerSide() { hotSpots = new ArrayList(); } public void addHotSpot(HotSpot hotSpot) { hotSpots.add(hotSpot); } public Iterator hotSpots() { return hotSpots.iterator(); } public String toString() { StringBuffer sb = new StringBuffer(); for (Iterator hi = hotSpots.iterator(); hi.hasNext();) { HotSpot hs = (HotSpot) hi.next(); Integer [] coords = hs.getCoordinates(); sb.append(hs.getType()); sb.append(" "); sb.append(hs.getURL().toString()); sb.append(" "); for (int i = 0; i < (coords.length - 1); i += 2) { sb.append(coords[i]); sb.append(","); sb.append(coords[i + 1]); sb.append(" "); } } return sb.substring(0).trim(); } } }