View Javadoc

1   /*
2    *  soapUI, copyright (C) 2006 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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.impl.wsdl.panels.mock;
14  
15  import java.awt.event.ActionEvent;
16  import java.awt.event.MouseAdapter;
17  import java.awt.event.MouseEvent;
18  
19  import javax.swing.JList;
20  import javax.swing.JPopupMenu;
21  
22  import com.eviware.soapui.support.UISupport;
23  import com.eviware.soapui.support.action.ActionList;
24  import com.eviware.soapui.support.action.ActionSupport;
25  
26  public abstract class ListMouseListener extends MouseAdapter
27  {
28  	protected abstract ActionList getActionsForRow( JList list, int row );
29  
30  	public void mouseClicked( MouseEvent e )
31  	{
32  		if (e.getClickCount() < 2)
33  			return;
34  		
35  		JList list = ( JList ) e.getSource();
36  		
37  		int selectedIndex = list.getSelectedIndex();
38  		if (selectedIndex == -1)
39  			return;
40  		
41  		ActionList actions = getActionsForRow( list, selectedIndex );
42  		
43  		if(actions != null)
44  			actions.performDefaultAction(new ActionEvent(this, 0, null));
45  	}
46  
47  	public void mousePressed( MouseEvent e )
48  	{
49  		if (e.isPopupTrigger())
50  			showPopup(e);
51  	}
52  
53  	public void mouseReleased( MouseEvent e )
54  	{
55  		if (e.isPopupTrigger())
56  			showPopup(e);
57  	}
58  
59  	public void showPopup( MouseEvent e )
60  	{
61  		JList list = ( JList ) e.getSource();
62  		int row = list.locationToIndex(e.getPoint());
63  		if (row == -1)
64  			return;
65  	
66  		if (list.getSelectedIndex() != row)
67  		{
68  			list.setSelectedIndex(row);
69  		}
70  	
71  		ActionList actions = getActionsForRow( list, row );
72  	
73  		if (actions == null || actions.getActionCount() == 0)
74  			return;
75  	
76  		JPopupMenu popup = ActionSupport.buildPopup(actions);
77  		UISupport.showPopup(popup, list, e.getPoint());
78  	}
79  
80  }