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  import com.eviware.soapui.actions.UpdateableAction;
24  
25  /***
26   * Default ActionList implementation
27   * 
28   * @author Ole.Matzura
29   */
30  
31  public class DefaultActionList implements ActionList
32  {
33  	private List<Action> actions = new ArrayList<Action>();
34  	private Action defaultAction;
35  	private final String label;
36  	
37  	public DefaultActionList()
38  	{
39  		this( null );
40  	}
41  	
42  	public DefaultActionList( String label )
43  	{
44  		this.label = label;
45  	}
46  	
47  	public String getLabel()
48  	{
49  		return label;
50  	}
51  
52  	public int getActionCount()
53  	{
54  		return actions.size();
55  	}
56  
57  	public Action getActionAt(int index)
58  	{
59  		return actions.get( index );
60  	}
61  
62  	public Action getDefaultAction()
63  	{
64  		return defaultAction;
65  	}
66  	
67  	public void setDefaultAction( Action defaultAction )
68  	{
69  		this.defaultAction = defaultAction;
70  	}
71  	
72  	public void addAction( Action action )
73  	{
74  		actions.add( action );
75  	}
76  	
77  	public void addAction( Action action, boolean isDefault )
78  	{
79  		actions.add( action );
80  		if( isDefault )
81  			setDefaultAction( action );
82  	}
83  	
84  	public void addSeparator()
85  	{
86  		actions.add( ActionSupport.SEPARATOR_ACTION );
87  	}
88  	
89  	public void insertAction( Action action, int index )
90  	{
91  		actions.add( index, action );
92  	}
93  
94  	public void insertSeparator( int index )
95  	{
96  		actions.add( index, ActionSupport.SEPARATOR_ACTION );
97  	}
98  
99  	public boolean hasDefaultAction()
100 	{
101 		return defaultAction != null;
102 	}
103 
104 	public void performDefaultAction(ActionEvent event)
105 	{
106 		if( defaultAction != null )
107 			defaultAction.actionPerformed( event );
108 	}
109 
110 	public void clear()
111 	{
112 		actions.clear();
113 		defaultAction = null;
114 	}
115 
116 	public void dispatchKeyEvent(KeyEvent e)
117 	{
118 		if( e.getKeyChar() == KeyEvent.VK_ENTER )
119 		{
120 			performDefaultAction( new ActionEvent( e.getSource(), 0, null ));
121 		}
122 		else
123 		{
124 			for( int c = 0; c < actions.size(); c++ )
125 			{
126 				Action action = actions.get( c );
127 				KeyStroke acc = (KeyStroke) action.getValue( Action.ACCELERATOR_KEY );
128 				if( acc == null )
129 					continue;
130 				
131 				if( acc.equals( KeyStroke.getKeyStrokeForEvent( e )))
132 				{
133 					action.actionPerformed( new ActionEvent( e.getSource(), 0, null ) );
134 					e.consume();
135 					return;
136 				}
137 			}
138 		}
139 	}
140 
141 	public void addActions( ActionList defaultActions )
142 	{
143 		for( int c = 0; c < defaultActions.getActionCount(); c++ )
144 			addAction( defaultActions.getActionAt( c ));
145 	}
146 
147 	public void setEnabled( boolean b )
148 	{
149 		for( int c = 0; c < actions.size(); c++ )
150 		{
151 			Action action = actions.get( c );
152 			action.setEnabled( b );
153 		}
154 	}
155 
156 	public void removeAction( int index )
157 	{
158 		actions.remove( index );
159 	}
160 	
161 	/***
162 	 * Update all actions that are instances of UpdateableAction.
163 	 */
164    public void update()
165    {
166       for(Action a : actions)
167       {
168          if(a instanceof UpdateableAction)
169          {
170             ((UpdateableAction)a).update();
171          }
172       }
173    }
174    
175 }