/*
* Created on 30.05.2005
*
*/
package bnGUI.venn.diagram;
import java.awt.Color;
import java.util.BitSet;
import java.util.Iterator;
import java.util.LinkedList;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import bnGUI.venn.geometry.FPoint;
/**
* Abstract base class for graphical Venn objects.
* See e.g. {@link venn.diagram.VennPolygonObject}.
* Normally they will be generated by the {@link venn.diagram.VennObjectFactory}.
*
* @author muellera
*
*/
public abstract class AbstractVennObject
implements IVennObject
{
private LinkedList listeners;
private final BitSet elements;
private final int card;
private FPoint center;
private double scale;
private Object properties;
private Color fillColor,
borderColor;
private boolean locked;
/**
*
*/
public AbstractVennObject( BitSet elements )
{
this.properties = null;
this.elements = elements;
this.card = elements.cardinality();
this.fillColor = Color.BLUE;
this.borderColor = Color.BLACK;
center = new FPoint(0.5,0.5);
scale = 1.0;
listeners = new LinkedList();
this.locked = false;
}
public void setLock( boolean locked )
{
this.locked = locked;
}
public boolean getLock()
{
return locked;
}
/* (non-Javadoc)
* @see venn.IVennObject#getPosition()
*/
public FPoint getOffset()
{
return center;
}
/* (non-Javadoc)
* @see venn.IVennObject#setPosition(venn.geometry.FPoint)
*/
public void setOffset(FPoint p)
{
if( p == null )
throw new IllegalArgumentException("p must not be null");
if( locked )
return;
if( ! center.equals(p) )
{
center = p;
invalidate();
}
}
public double getScale()
{
return scale;
}
public void setScale( double scale )
{
if( locked )
return;
if( scale != this.scale )
{
this.scale = scale;
invalidate();
}
}
/* (non-Javadoc)
* @see venn.IVennObject#cardinality()
*/
public int cardinality()
{
//return getElements().cardinality();
return card;
}
/* (non-Javadoc)
* @see venn.IVennObject#getElements()
*/
public BitSet getElements()
{
return elements;
}
public synchronized void addChangeListener( ChangeListener obj )
{
if( obj != null )
listeners.add( obj );
}
public synchronized void removeChangeListener( ChangeListener obj )
{
if( obj != null )
listeners.remove(obj);
}
public void fireChangeEvent()
{
synchronized( listeners )
{
Iterator iter = listeners.iterator();
ChangeEvent event = new ChangeEvent(this);
while( iter.hasNext() )
{
((ChangeListener)iter.next()).stateChanged( event );
}
}
}
public void setProperties( Object properties )
{
this.properties = properties;
}
public Object getProperties()
{
return this.properties;
}
public void setFillColor( Color color )
{
fillColor = color;
}
public Color getFillColor()
{
return fillColor;
}
public void setBorderColor( Color color )
{
borderColor = color;
}
public Color getBorderColor()
{
return borderColor;
}
public String toString()
{
StringBuffer buf = new StringBuffer();
buf.append("ELEMENTS ");
buf.append( elements.toString() );
buf.append("\n");
buf.append("CARD ");
buf.append(cardinality());
buf.append("\n");
buf.append("AREA ");
buf.append(area());
buf.append("\n");
buf.append("CENTER ");
buf.append( center.toString() );
buf.append("\n");
buf.append("SCALE ");
buf.append( scale + "\n");
return buf.toString();
}
/**
* Assigns all positions and scales of the source object to this object.
*/
public void assignState( IVennObject source )
{
if( source == null )
throw new IllegalArgumentException("source must not be null!");
setLock(false);
setOffset(source.getOffset());
setScale(source.getScale());
setLock(source.getLock());
}
}