1
2
3
4
5
6
7
8
9
10
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 }