package net.xoetrope.optional.svg;
import com.kitfox.svg.SVGElement;
import java.awt.AlphaComposite;
import java.awt.Composite;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.URL;
import javax.swing.AbstractButton;
import javax.swing.DefaultButtonModel;
import net.xoetrope.optional.svg.svgsalamander.XSvgPainter;
/**
* A button rendered via a set of layered SVG images
* @author luano
* <p>Copyright (c) Xoetrope 2001-2006, see license.txt for more details</p>
*/
public class XSvgButton extends AbstractButton implements MouseListener, ComponentListener
{
protected XSvgPainter svgPainter;
private String[] mouseDownSelection;
/** Creates a new instance of XSvgButton */
public XSvgButton()
{
setModel( new DefaultButtonModel());
addMouseListener( this );
addComponentListener( this );
svgPainter = new XSvgPainter( this );
}
/**
* Prepare the button for display
*/
public void setup()
{
svgPainter.setup();
}
/**
* Get the ID of the selected element
*/
public String getSelectedId()
{
return svgPainter.getSelectedId();
}
/**
* Set the element ids for interactive elements
* @param ids the SVG element id to use
*/
public void setElementIds( String[][] ids )
{
svgPainter.setElementIds( ids );
}
/**
* Flag this component as having an animation painter
* @param state true to animate
*/
public void setAnimate( boolean state )
{
svgPainter.setAnimate( state );
}
public void play()
{
svgPainter.play();
}
public double getCurTime()
{
return svgPainter.getCurTime();
}
public void setImage( URL image )
{
if ( svgPainter == null )
svgPainter = new XSvgPainter( this );
svgPainter.setImage( image );
}
/**
* Set the meta data associated with this image
* @param md the url of the metadata
*/
public void setMetadata( URL md )
{
svgPainter.setMetadata( md );
}
public void paintComponent( Graphics g )
{
Graphics2D g2 = (Graphics2D)g.create();
Composite composite = g2.getComposite ();
Composite fade = AlphaComposite.getInstance( AlphaComposite.SRC_OVER, 0F );
g2.setComposite( fade );
Dimension sz = getSize();
svgPainter.paint( (Graphics2D)g, this, sz.width, sz.height );
}
public Dimension getPreferredSize()
{
return new Dimension( 200, 200 );
}
public boolean contains( int x, int y )
{
return ((HitTester)svgPainter).contains( x, y );
}
/**
* Invoked when the mouse button has been clicked (pressed
* and released) on a component.
*/
public void mouseClicked(MouseEvent e)
{
}
/**
* Invoked when a mouse button has been pressed on a component.
*/
public void mousePressed( MouseEvent e )
{
getModel().setPressed( true );
svgPainter.updateState( -1 );
mouseDownSelection = svgPainter.getSelectedId().split( "," );
}
/**
* Invoked when a mouse button has been released on a component.
*/
public void mouseReleased( MouseEvent e )
{
getModel().setPressed( false );
// svgPainter.updateState();
String selection = svgPainter.getSelectedId();
String[] mouseUpSelection = selection.split( "," );
int numIds = mouseUpSelection.length;
for ( int i = 0; i < numIds; i++ ) {
if ( !mouseDownSelection[ i ].equals( mouseUpSelection[ i ] ))
return;
}
if ( numIds > 0 ) {
ActionEvent ae = new ActionEvent( this,
ActionEvent.ACTION_PERFORMED,
selection,
e.getWhen(),
e.getModifiers());
fireActionPerformed( ae );
}
}
/**
* Invoked when the mouse enters a component.
*/
public void mouseEntered( MouseEvent e )
{
}
/**
* Invoked when the mouse exits a component.
*/
public void mouseExited( MouseEvent e )
{
}
/**
* Invoked when the component's size changes.
*/
public void componentResized( ComponentEvent e )
{
svgPainter.componentResized();
}
/**
* Invoked when the component's position changes.
*/
public void componentMoved( ComponentEvent e ) {}
/**
* Invoked when the component has been made visible.
*/
public void componentShown( ComponentEvent e ) {}
/**
* Invoked when the component has been made invisible.
*/
public void componentHidden( ComponentEvent e ) {}
public SVGElement getElement( String id )
{
return svgPainter.getElement( id );
}
public void update()
{
svgPainter.updateState( -1 );
}
}