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 ListMouseListener extends MouseAdapter
33 {
34 protected abstract ActionList getActionsForRow( JList list, int row );
35
36 public void mouseClicked( MouseEvent e )
37 {
38 if (e.getClickCount() < 2)
39 return;
40
41 JList list = ( JList ) e.getSource();
42
43 int selectedIndex = list.getSelectedIndex();
44 if (selectedIndex == -1)
45 return;
46
47 ActionList actions = getActionsForRow( list, selectedIndex );
48
49 if(actions != null)
50 actions.performDefaultAction(new ActionEvent(this, 0, null));
51 }
52
53 public void mousePressed( MouseEvent e )
54 {
55 if (e.isPopupTrigger())
56 showPopup(e);
57 }
58
59 public void mouseReleased( MouseEvent e )
60 {
61 if (e.isPopupTrigger())
62 showPopup(e);
63 }
64
65 public void showPopup( MouseEvent e )
66 {
67 JList list = ( JList ) e.getSource();
68 int row = list.locationToIndex(e.getPoint());
69 if (row == -1)
70 return;
71
72 if (list.getSelectedIndex() != row)
73 {
74 list.setSelectedIndex(row);
75 }
76
77 ActionList actions = getActionsForRow( list, row );
78
79 if (actions == null || actions.getActionCount() == 0)
80 return;
81
82 JPopupMenu popup = ActionSupport.buildPopup(actions);
83 UISupport.showPopup(popup, list, e.getPoint());
84 }
85
86 }