All Downloads are FREE. Search and download functionalities are using the official Maven repository.

bibliothek.gui.dock.action.actions.GroupedSelectableDockAction Maven / Gradle / Ivy

The newest version!
/*
 * Bibliothek - DockingFrames
 * Library built on Java/Swing, allows the user to "drag and drop"
 * panels containing any Swing-Component the developer likes to add.
 * 
 * Copyright (C) 2007 Benjamin Sigg
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Benjamin Sigg
 * [email protected]
 * CH - Switzerland
 */

package bibliothek.gui.dock.action.actions;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.swing.Icon;

import bibliothek.gui.Dockable;
import bibliothek.gui.dock.action.ActionType;
import bibliothek.gui.dock.action.SelectableDockAction;
import bibliothek.gui.dock.action.view.ActionViewConverter;
import bibliothek.gui.dock.action.view.ViewTarget;
import bibliothek.gui.dock.event.SelectableDockActionListener;

/**
 * An action that can change between selected and not selected. 
 * @author Benjamin Sigg
 *
 * @param  the type of key to distinguish between groups of Dockables
 */
public abstract class GroupedSelectableDockAction extends GroupedDropDownItemAction implements SelectableDockAction{
	/**
	 * An action intended to use as type {@link ActionType#CHECK}
	 * @author Benjamin Sigg
	 * @param  the type of key used to distinguish between groups
	 */
	public static abstract class Check extends GroupedSelectableDockAction{
		/**
		 * Creates a new action.
		 * @param generator the generator to create new keys for unknown 
		 * Dockables
		 */
		public Check( GroupKeyGenerator generator ){
			super( generator, ActionType.CHECK );
		}
		
		@Override
		protected SimpleSelectableAction createGroup( SelectableDockActionListener listener ){
			SimpleSelectableAction action = new SimpleSelectableAction.Check( false );
			action.addSelectableListener( listener );
			return action;
		}
	};

	/**
	 * An action intended to use as type {@link ActionType#RADIO}
	 * @author Benjamin Sigg
	 * @param  the type of key used to distinguish between groups
	 */
	public static abstract class Radio extends GroupedSelectableDockAction{
		/**
		 * Creates a new action.
		 * @param generator the generator to create new keys for unknown 
		 * Dockables
		 */
		public Radio( GroupKeyGenerator generator ){
			super( generator, ActionType.RADIO );
		}
		
		@Override
        protected SimpleSelectableAction createGroup( SelectableDockActionListener listener ){
			SimpleSelectableAction action = new SimpleSelectableAction.Radio( false );
			action.addSelectableListener( listener );
			return action;
		}
	};
	
	/** Listeners added to this action */
	private List listeners = new ArrayList();
	
	/** the type of this action */
	private ActionType type;
	
	/** A listener added to all children of this action */
	private SelectableDockActionListener listener = new SelectableDockActionListener(){
		public void selectedChanged( SelectableDockAction action, Set dockables ){
			fireSelectedChanged( dockables );
		}
	};

	/**
	 * Creates a new action.
	 * @param generator a generator to create keys for Dockables which are not
	 * yet in a group.
	 * @param type the type of this action
	 */
	public GroupedSelectableDockAction( GroupKeyGenerator generator, ActionType type ){
		super( generator );
		
		if( type == null )
			throw new IllegalArgumentException( "Type must not be null" );
		
		this.type = type;
	}
	
	@Override
	public void setGroup( K key, Dockable dockable ){
		super.setGroup( key, dockable );
		fireSelectedChanged( dockable );
	}

	public  V createView( ViewTarget target, ActionViewConverter converter, Dockable dockable ){
		return converter.createView( type, this, target, dockable );
	}
	
	public void addSelectableListener( SelectableDockActionListener listener ){
		listeners.add( listener );
	}
	public void removeSelectableListener( SelectableDockActionListener listener ){
		listeners.remove( listener );
	}
	
	@Override
	protected SimpleSelectableAction createGroup( K key ){
		return createGroup( listener );
	}
	
	/**
	 * Creates a new group and adds a listener to the group.
	 * @param listener the listener to add
	 * @return the new group
	 */
	protected abstract SimpleSelectableAction createGroup( SelectableDockActionListener listener );
	
	/**
	 * Fires a change-event on all known listeners.
	 * @param dockable the Dockable whose state has changed
	 */
	protected void fireSelectedChanged( Dockable dockable ){
		Set set = new HashSet();
		set.add( dockable );
		fireSelectedChanged( set );
	}
	
	/**
	 * Fires a change-event on all known listeners.
	 * @param dockables the Dockables whose state has been changed
	 */
	protected void fireSelectedChanged( Set dockables ){
		for( SelectableDockActionListener listener : listeners.toArray( new SelectableDockActionListener[ listeners.size() ] ))
			listener.selectedChanged( this, dockables );
	}
	
	public boolean isSelected( Dockable dockable ){
		return getGroup( dockable ).isSelected( dockable );
	}
	
	public void setSelected( Dockable dockable, boolean selected ){
		getGroup( dockable ).setSelected( dockable, selected );
	}
	
	/**
     * Sets the selected-state of the group key.
     * If the group does not exist, it will be created.
     * @param key The name of the group
     * @param selected The new state of the group
     */
    public void setSelected( K key, boolean selected ){
        ensureGroup( key ).setSelected( selected );
    }
    
    /**
     * Gets the selected-state property of the group key.
     * @param key The name of the group
     * @return The state
     * @throws IllegalArgumentException If the group does not exist
     * @see #setSelected(Object, boolean)
     */
    public boolean isSelected( Object key ){
        SimpleSelectableAction action = getGroup( key );
        if( action == null )
            throw new IllegalArgumentException( "There is no such group" );
        return action.isSelected();
    }
    
    /**
     * Sets the icon that will be shown when the group
     * named key is selected. If the group does not
     * exist, it will be created.
     * @param key The name of the group
     * @param icon The selected-icon, may be null
     */
    public void setSelectedIcon( K key, Icon icon ){
        ensureGroup( key ).setSelectedIcon( icon );
    }
    
    /**
     * Gets the icon that is shown when the group named key
     * is in the selected-state.
     * @param key The name of the group
     * @return The selected-icon, may be null
     * @throws IllegalArgumentException if the group does not exist
     * @see #setDisabledIcon(Object, Icon)
     */
    public Icon getSelectedIcon( Object key ){
    	SimpleSelectableAction action = getGroup( key );
        if( action == null )
            throw new IllegalArgumentException( "There is no such group" );
        return action.getSelectedIcon();
    }    
    
    /**
     * Sets the icon that will be shown when the group
     * key is disabled and selected. If the group
     * does not exist, it will be created.
     * @param key The name of the group
     * @param icon The icon to display, when the group is 
     * selected and disabled, may be null
     */
    public void setDisabledSelectedIcon( K key, Icon icon ){
        ensureGroup( key ).setDisabledSelectedIcon( icon );
    }
    
    /**
     * Gets the icon that is shown when the group key
     * is selected and disabled.
     * @param key The name of the group
     * @return The disabled-selected-icon, may be null
     * @throws IllegalArgumentException if the group does not exist
     * @see #setDisabledSelectedIcon(Object, Icon)
     */
    public Icon getDisabledSelectedIcon( Object key ){
    	SimpleSelectableAction action = getGroup( key );
        if( action == null )
            throw new IllegalArgumentException( "There is no such group" );
        return action.getDisabledSelectedIcon();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy