View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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 && defaultAction != null )
119 		{
120 			performDefaultAction( new ActionEvent( e.getSource(), 0, null ) );
121 			e.consume();
122 		}
123 		else
124 		{
125 			for( int c = 0; c < actions.size(); c++ )
126 			{
127 				Action action = actions.get( c );
128 				KeyStroke acc = ( KeyStroke )action.getValue( Action.ACCELERATOR_KEY );
129 				if( acc == null )
130 					continue;
131 
132 				if( acc.equals( KeyStroke.getKeyStrokeForEvent( e ) ) )
133 				{
134 					action.actionPerformed( new ActionEvent( e.getSource(), 0, null ) );
135 					e.consume();
136 					return;
137 				}
138 			}
139 		}
140 	}
141 
142 	public void addActions( ActionList defaultActions )
143 	{
144 		for( int c = 0; c < defaultActions.getActionCount(); c++ )
145 			addAction( defaultActions.getActionAt( c ) );
146 	}
147 
148 	public void setEnabled( boolean b )
149 	{
150 		for( int c = 0; c < actions.size(); c++ )
151 		{
152 			Action action = actions.get( c );
153 			action.setEnabled( b );
154 		}
155 	}
156 
157 	public void removeAction( int index )
158 	{
159 		actions.remove( index );
160 	}
161 
162 	/***
163 	 * Update all actions that are instances of UpdateableAction.
164 	 */
165 	public void update()
166 	{
167 		for( Action a : actions )
168 		{
169 			if( a instanceof UpdateableAction )
170 			{
171 				( ( UpdateableAction )a ).update();
172 			}
173 		}
174 	}
175 
176 }