View Javadoc

    1   /*
    2    *  soapUI, copyright (C) 2004-2007 eviware.com
    3    *
    4    *  soapUI is free software; you can redistribute it and/or modify it under the
    5    *  terms of version 2.1 of the GNU Lesser General Public License as published by
    6    *  the Free Software Foundation.
    7    *
    8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
    9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    10   *  See the GNU Lesser General Public License for more details at gnu.org.
    11   */
    12
    13  package com.eviware.soapui.support.action.swing;
    14
    15  import java.awt.event.ActionEvent;
    16  import java.awt.event.KeyEvent;
    17  import java.util.ArrayList;
    18  import java.util.List;
    19
    20  import javax.swing.Action;
    21  import javax.swing.KeyStroke;
    22
    23  /***
    24   * Default ActionList implementation
    25   *
    26   * @author Ole.Matzura
    27   */
    28
    29  public class DefaultActionList implements ActionList
    30  {
    31  	private List<Action> actions = new ArrayList<Action>();
    32  	private Action defaultAction;
    33  	private final String label;
    34
    35  	public DefaultActionList()
    36  	{
    37  		this( null );
    38  	}
    39
    40  	public DefaultActionList( String label )
    41  	{
    42  		this.label = label;
    43  	}
    44
    45  	public String getLabel()
    46  	{
    47  		return label;
    48  	}
    49
    50  	public int getActionCount()
    51  	{
    52  		return actions.size();
    53  	}
    54
    55  	public Action getActionAt(int index)
    56  	{
    57  		return actions.get( index );
    58  	}
    59
    60  	public Action getDefaultAction()
    61  	{
    62  		return defaultAction;
    63  	}
    64
    65  	public void setDefaultAction( Action defaultAction )
    66  	{
    67  		this.defaultAction = defaultAction;
    68  	}
    69
    70  	public void addAction( Action action )
    71  	{
    72  		actions.add( action );
    73  	}
    74
    75  	public void addSeparator()
    76  	{
    77  		actions.add( ActionSupport.SEPARATOR_ACTION );
    78  	}
    79
    80  	public void insertAction( Action action, int index )
    81  	{
    82  		actions.add( index, action );
    83  	}
    84
    85  	public void insertSeparator( int index )
    86  	{
    87  		actions.add( index, ActionSupport.SEPARATOR_ACTION );
    88  	}
    89
    90  	public boolean hasDefaultAction()
    91  	{
    92  		return defaultAction != null;
    93  	}
    94
    95  	public void performDefaultAction(ActionEvent event)
    96  	{
    97  		if( defaultAction != null )
    98  			defaultAction.actionPerformed( event );
    99  	}
    100
    101 	public void clear()
    102 	{
    103 		actions.clear();
    104 		defaultAction = null;
    105 	}
    106
    107 	public void dispatchKeyEvent(KeyEvent e)
    108 	{
    109 		if( e.getKeyChar() == KeyEvent.VK_ENTER )
    110 		{
    111 			performDefaultAction( new ActionEvent( e.getSource(), 0, null ));
    112 		}
    113 		else
    114 		{
    115 			for( int c = 0; c < actions.size(); c++ )
    116 			{
    117 				Action action = actions.get( c );
    118 				KeyStroke acc = (KeyStroke) action.getValue( Action.ACCELERATOR_KEY );
    119 				if( acc == null )
    120 					continue;
    121
    122 				if( acc.equals( KeyStroke.getKeyStrokeForEvent( e )))
    123 				{
    124 					action.actionPerformed( new ActionEvent( e.getSource(), 0, null ) );
    125 					e.consume();
    126 					return;
    127 				}
    128 			}
    129 		}
    130 	}
    131
    132 	public void addActions( ActionList defaultActions )
    133 	{
    134 		for( int c = 0; c < defaultActions.getActionCount(); c++ )
    135 			addAction( defaultActions.getActionAt( c ));
    136 	}
    137
    138 	public void setEnabled( boolean b )
    139 	{
    140 		for( int c = 0; c < actions.size(); c++ )
    141 		{
    142 			Action action = actions.get( c );
    143 			action.setEnabled( b );
    144 		}
    145 	}
    146
    147 	public void removeAction( int index )
    148 	{
    149 		actions.remove( index );
    150 	}
    151 }