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.JList;
20 import javax.swing.JPopupMenu;
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 AbstractListMouseListener extends MouseAdapter
33 {
34 private boolean enablePopup;
35 private JPopupMenu menu;
36
37 protected abstract ActionList getActionsForRow( JList list, int row );
38
39 public AbstractListMouseListener()
40 {
41 this( true );
42 }
43
44 public AbstractListMouseListener( 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 JList list = ( JList ) e.getSource();
55
56 int selectedIndex = list.getSelectedIndex();
57
58 ActionList actions = selectedIndex == -1 ? getDefaultActions() : getActionsForRow( list, selectedIndex );
59
60 if(actions != null)
61 actions.performDefaultAction(new ActionEvent(this, 0, null));
62 }
63
64 protected ActionList getDefaultActions()
65 {
66 return null;
67 }
68
69 public void mousePressed( MouseEvent e )
70 {
71 if (e.isPopupTrigger())
72 showPopup(e);
73 }
74
75 public void mouseReleased( MouseEvent e )
76 {
77 if (e.isPopupTrigger())
78 showPopup(e);
79 }
80
81 public void showPopup( MouseEvent e )
82 {
83 if( !enablePopup )
84 return;
85
86 ActionList actions = null;
87 JList list = ( JList ) e.getSource();
88 int row = list.locationToIndex(e.getPoint());
89 if (row == -1 || !list.getCellBounds( row, row ).contains( e.getPoint() ))
90 {
91 if( list.getSelectedIndex() != -1 )
92 list.clearSelection();
93
94 actions = getDefaultActions();
95 }
96 else
97 {
98 if (list.getSelectedIndex() != row)
99 {
100 list.setSelectedIndex(row);
101 }
102
103 actions = getActionsForRow( list, row );
104 }
105
106 if (actions == null || actions.getActionCount() == 0)
107 return;
108
109 JPopupMenu popup = menu == null ? ActionSupport.buildPopup(actions) : menu;
110 UISupport.showPopup(popup, list, e.getPoint());
111 }
112
113 public void setPopupMenu( JPopupMenu menu )
114 {
115 this.menu = menu;
116 }
117 }