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.swing;
14  
15  import java.awt.event.ActionEvent;
16  import java.awt.event.MouseAdapter;
17  import java.awt.event.MouseEvent;
18  
19  import javax.swing.JPopupMenu;
20  import javax.swing.JTable;
21  
22  import com.eviware.soapui.support.UISupport;
23  import com.eviware.soapui.support.action.swing.ActionList;
24  import com.eviware.soapui.support.action.swing.ActionSupport;
25  
26  /***
27   * Abstract MouseListener for JLists that displays a row-sensitive popup-menu
28   * 
29   * @author ole.matzura
30   */
31  
32  public abstract class AbstractTableMouseListener extends MouseAdapter
33  {
34  	private boolean enablePopup;
35  	private JPopupMenu menu;
36  	
37  	protected abstract ActionList getActionsForRow( JTable table, int row );
38  	
39  	public AbstractTableMouseListener()
40  	{
41  		this( true );
42  	}
43  	
44  	public AbstractTableMouseListener( boolean enablePopup )
45  	{
46  		this.enablePopup = enablePopup;
47  	}
48  	
49  	public void mouseClicked( MouseEvent e )
50  	{
51  		if (e.getClickCount() < 2)
52  			return;
53  		
54  		JTable list = ( JTable ) e.getSource();
55  		
56  		int selectedIndex = list.getSelectedRow();
57  		if (selectedIndex == -1)
58  			return;
59  		
60  		ActionList actions = getActionsForRow( list, selectedIndex );
61  		
62  		if(actions != null)
63  			actions.performDefaultAction(new ActionEvent(this, 0, null));
64  	}
65  
66  	public void mousePressed( MouseEvent e )
67  	{
68  		if (e.isPopupTrigger())
69  			showPopup(e);
70  	}
71  
72  	public void mouseReleased( MouseEvent e )
73  	{
74  		if (e.isPopupTrigger())
75  			showPopup(e);
76  	}
77  
78  	public void showPopup( MouseEvent e )
79  	{
80  		if( !enablePopup )
81  			return;
82  		
83  		JTable list = ( JTable ) e.getSource();
84  		int row = list.rowAtPoint(e.getPoint());
85  		if (row == -1)
86  			return;
87  	
88  		if (list.getSelectedRow() != row)
89  		{
90  			list.setRowSelectionInterval(row, row);
91  		}
92  	
93  		ActionList actions = getActionsForRow( list, row );
94  	
95  		if (actions == null || actions.getActionCount() == 0)
96  			return;
97  	
98  		JPopupMenu popup = menu == null ? ActionSupport.buildPopup(actions) : menu;
99  		UISupport.showPopup(popup, list, e.getPoint());
100 	}
101 	
102 	public void setPopupMenu( JPopupMenu menu )
103 	{
104 		this.menu = menu;
105 	}
106 }