001 /*
002 * This file is part of the Echo Point Project. This project is a
003 * collection of Components that have extended the Echo Web Application
004 * Framework Version 3.
005 *
006 * Version: MPL 1.1
007 *
008 * The contents of this file are subject to the Mozilla Public License Version
009 * 1.1 (the "License"); you may not use this file except in compliance with
010 * the License. You may obtain a copy of the License at
011 * http://www.mozilla.org/MPL/
012 *
013 * Software distributed under the License is distributed on an "AS IS" basis,
014 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
015 * for the specific language governing rights and limitations under the
016 * License.
017 */
018 package echopoint.model;
019
020 import com.thoughtworks.xstream.annotations.XStreamAlias;
021
022 import java.util.ArrayList;
023 import java.util.Collection;
024 import java.util.Collections;
025
026 /**
027 * A model object that represents a clickable polygon section in an {@link
028 * echopoint.ImageMap}.
029 *
030 * <p><b>Note:</b> Development of this component was sponsored by
031 * <a href='http://tcnbroadcasting.com/index.jsp' target='_top'>TCN
032 * Broadcasting</a>. We are grateful for their support and sponsorship.</p>
033 *
034 * @author Rakesh 2008-10-19
035 * @version $Id: PolygonSection.java 86 2008-11-09 14:44:29Z sptrakesh $
036 */
037 @XStreamAlias( "polygonSection" )
038 public class PolygonSection extends MapSection
039 {
040 private static final long serialVersionUID = 1l;
041
042 /**
043 * The collection of points that represent the corners of the polygon.
044 */
045 private Collection<Point> vertices = new ArrayList<Point>();
046
047 /** Default constructor. */
048 public PolygonSection() {}
049
050 /**
051 * Create a new instance using the specified values.
052 *
053 * @param values The {@link #actionCommand} and {@link
054 * #altText} values. Note that unless {@link #actionCommand} is
055 * specified the section will not be saved in the image map.
056 */
057 public PolygonSection( final String... values )
058 {
059 if ( values.length > 0 ) setActionCommand( values[0] );
060 if ( values.length > 1 ) setAltText( values[1] );
061 }
062
063 /**
064 * Create a new instance using the specified values.
065 *
066 * @param vertices The array of vertices that represent the polygon. Note
067 * that each vertex is represented by a pair of integer values.
068 * @param values Optionally the {@link #actionCommand} and {@link
069 * #altText} values. Note that unless {@link #actionCommand} is
070 * specified the section will not be saved in the image map.
071 */
072 public PolygonSection( final int[] vertices, final String... values )
073 {
074 this( values );
075
076 for ( int i = 0; i < vertices.length; ++i )
077 {
078 if ( ( i % 2 ) == 0 )
079 {
080 if ( ( i + 1 ) >= vertices.length ) break;
081 this.vertices.add( new Point( vertices[i], vertices[i + 1] ) );
082 }
083 }
084 }
085
086 public PolygonSection( final Collection<Point> vertices, final String... values )
087 {
088 this( values );
089 setVertices( vertices );
090 }
091
092 /**
093 * {@inheritDoc}
094 */
095 @Override
096 public boolean equals( final Object o )
097 {
098 if ( this == o ) return true;
099 if ( o == null || getClass() != o.getClass() ) return false;
100 if ( !super.equals( o ) ) return false;
101
102 PolygonSection that = (PolygonSection) o;
103
104 return super.equals( o ) &&
105 !( vertices != null ? !vertices.equals( that.vertices ) : that.vertices != null );
106 }
107
108 /**
109 * {@inheritDoc}
110 */
111 @Override
112 public int hashCode()
113 {
114 int result = super.hashCode();
115 result = 31 * result + ( vertices != null ? vertices.hashCode() : 0 );
116 return result;
117 }
118
119 /**
120 * Accessor for property 'vertices'.
121 *
122 * @return Value for property 'vertices'.
123 */
124 public Collection<Point> getVertices()
125 {
126 return Collections.unmodifiableCollection( vertices );
127 }
128
129 /**
130 * Mutator for property 'vertices'.
131 *
132 * @param vertices Value to set for property 'vertices'.
133 */
134 public void setVertices( final Collection<Point> vertices )
135 {
136 this.vertices.clear();
137 this.vertices.addAll( vertices );
138 }
139 }