/*
* Created on 05.07.2004
*/
package beziermanager.gui;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Vector;
import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.SwingUtilities;
import beziermanager.Resources;
import beziermanager.event.DropDownButtonListener;
/**
* @author Benjamin Sigg
* @version 1.0
*/
public class DropDownButton extends JComponent {
private JButton button;
private JButton dropButton;
private JPopupMenu menu;
private int dropWidth = 5;
private Vector listeners = new Vector();
private Listener listener = new Listener();
private Capsel selection = null;
private Vector values = new Vector();
private ButtonGroup group = new ButtonGroup();
private static final Insets NULL_INSETS = new Insets( 0, 0, 0, 0 );
public DropDownButton() {
button = new JButton(){
public void paint( Graphics g ){
super.paint( g );
if( dropButton != null ){
g = g.create( dropButton.getX(), dropButton.getY(), dropButton.getWidth(), dropButton.getHeight() );
dropButton.paint( g );
}
}
};
button.addActionListener( listener );
dropButton = new JButton( Resources.getIcon( "menu button" ) ){
public void paint( Graphics g ){
Icon icon = getIcon();
if( icon != null ){
int x = (getWidth() - icon.getIconWidth()) / 2;
int y = (getHeight() - icon.getIconHeight()) / 2;
icon.paintIcon( this, g, x, y );
}
}
};
setLayout( null );
dropButton.setBorder( null );
dropButton.setOpaque( false );
add( dropButton );
add( button );
menu = new JPopupMenu();
dropButton.addActionListener( new ActionListener(){
public void actionPerformed( ActionEvent e ) {
menu.show( dropButton, - dropButton.getX(), dropButton.getHeight() );
}
});
setIcon( null );
}
public void setIcon( Icon defaultIcon ) {
button.setIcon( new ButtonIcon( defaultIcon ));
}
private void setSelection( Capsel capsel ){
selection = capsel;
if( capsel == null ){
setIcon( Resources.EMPTY_ICON );
button.setToolTipText( "" );
}
else{
setIcon( capsel.icon );
button.setToolTipText( capsel.text );
capsel.menuItem.setSelected( true );
}
}
public Object getSelection(){
if( selection == null )
return null;
else
return selection.value;
}
public void setSelection( Object value ){
if( value == null ){
if( selection != null )
selection.menuItem.setSelected( false );
setSelection( null );
}
else{
Capsel capsel = find( value );
if( capsel == null ){
if( selection != null )
selection.menuItem.setSelected( false );
setSelection( null );
}
else{
button.removeActionListener( listener );
setSelection( capsel );
button.addActionListener( listener );
}
}
}
public Insets getInsets() {
Insets insets = button.getInsets();
return insets == null ? NULL_INSETS : insets;
}
/**
* Gibt die Anzahl Werte zurück, welche von diesem DropDownButton
* angezeigt werden.
* @return Die Anzahl Werte
*/
public int getItemCount(){
return values.size();
}
public Object getItem( int index ){
return ((Capsel)values.get( index )).value;
}
public synchronized void insertItem( Icon icon, String text, Object value, int index ){
Capsel capsel = new Capsel( icon, text, value );
values.insertElementAt( capsel, index );
JRadioButtonMenuItem item = new JRadioButtonMenuItem( text, icon, false );
capsel.menuItem = item;
menu.insert( item, index );
group.add( item );
if( selection == null )
setSelection( capsel );
item.addItemListener( listener );
}
public void setIcon( Object value, Icon icon ){
Capsel capsel = find( value );
capsel.icon = icon;
capsel.menuItem.setIcon( icon );
if( selection == capsel )
setIcon( icon );
}
public void setIcon( int index, Icon icon ){
Capsel capsel = (Capsel)values.get( index );
capsel.icon = icon;
capsel.menuItem.setIcon( icon );
if( selection == capsel )
setIcon( icon );
}
public void setText( Object value, String text ){
Capsel capsel = find( value );
capsel.text = text;
capsel.menuItem.setText( text );
if( selection == capsel )
button.setToolTipText( text );
}
public void setText( int index, String text ){
Capsel capsel = (Capsel)values.get( index );
capsel.text = text;
capsel.menuItem.setText( text );
if( selection == capsel )
button.setToolTipText( text );
}
public void removeItem( Object value ){
Capsel capsel = find( value );
if( capsel != null ){
if( capsel == selection )
setSelection( null );
menu.remove( capsel.menuItem );
capsel.menuItem.removeItemListener( listener );
group.remove( capsel.menuItem );
values.remove( capsel );
}
}
public void removeItem( int index ){
Capsel capsel = (Capsel)values.remove( index );
if( capsel == selection )
setSelection( null );
menu.remove( capsel.menuItem );
capsel.menuItem.removeItemListener( listener );
group.remove( capsel.menuItem );
values.remove( capsel );
}
private Capsel find( Object value ){
for( int i = 0, n = values.size(); i<n; i++ ){
Capsel c = (Capsel)values.get( i );
if( c.value == value )
return c;
}
for( int i = 0, n = values.size(); i<n; i++ ){
Capsel c = (Capsel)values.get( i );
if( c.value.equals( value ) )
return c;
}
return null;
}
private Capsel find( JRadioButtonMenuItem item ){
for( int i = 0, n = values.size(); i<n; i++ ){
Capsel c = (Capsel)values.get( i );
if( c.menuItem == item )
return c;
}
return null;
}
public void addDropDownButtonListener( DropDownButtonListener l ){
listeners.add( l );
}
public void removeDropDownButtonListener( DropDownButtonListener l ){
listeners.remove( l );
}
protected void fireAction( ActionEvent invoker, Object selection ){
for( int i = 0, n = listeners.size(); i<n; i++ )
((DropDownButtonListener)listeners.get( i )).actionPerformed( invoker, selection );
}
public void updateUI() {
super.updateUI();
if( dropButton != null )
dropButton.updateUI();
if( menu!= null )
SwingUtilities.updateComponentTreeUI( menu );
}
public Dimension getMinimumSize() {
return button.getMinimumSize();
}
public Dimension getPreferredSize() {
return button.getPreferredSize();
}
public void paint( Graphics g ){
if( button != null )
button.paint( g );
}
public void doLayout() {
if( button != null ){
button.setBounds( 0, 0, getWidth(), getHeight() );
}
if( dropButton != null ){
Insets insets = getInsets();
dropButton.setBounds( getWidth()-dropWidth-insets.right , insets.top,
dropWidth+insets.right, getHeight()-insets.top-insets.bottom );
}
}
private class Listener implements ActionListener, ItemListener{
public void actionPerformed( ActionEvent e ) {
if( selection != null )
fireAction( e, selection.value );
}
public void itemStateChanged( ItemEvent e ) {
JRadioButtonMenuItem item = (JRadioButtonMenuItem)e.getSource();
if( item.isSelected() ){
Capsel capsel = find( item );
setIcon( capsel.icon );
selection = capsel;
button.doClick( 0 );
/*ActionEvent event = new ActionEvent( DropDownButton.this, id,
getActionCommand(), System.currentTimeMillis(), 0 );*/
}
}
}
private class Capsel{
public Icon icon;
public String text;
public Object value;
public JRadioButtonMenuItem menuItem;
public Capsel( Icon icon, String text, Object value ){
this.icon = icon;
this.text = text;
this.value = value;
}
public Capsel(){
}
}
private class ButtonIcon implements Icon{
private Icon original;
public ButtonIcon( Icon original ){
this.original = original;
}
public int getIconHeight() {
return original == null ? 16 : original.getIconHeight();
}
public int getIconWidth() {
return original == null ? 16 : original.getIconWidth() + (getInsets().right + dropWidth) / 2;
}
public void paintIcon( Component c, Graphics g, int x, int y ) {
if( original != null )
original.paintIcon(c, g, x, y);
}
}
}