KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > admin > common > XPopupListener


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.admin.common;
5
6 import java.awt.event.MouseAdapter JavaDoc;
7 import java.awt.event.MouseEvent JavaDoc;
8
9 import javax.swing.JComponent JavaDoc;
10 import javax.swing.JPopupMenu JavaDoc;
11
12 /**
13  * A utility helper for components that want to have a context menu.
14  */

15
16 public class XPopupListener extends MouseAdapter JavaDoc {
17   protected JComponent JavaDoc m_target;
18   protected JPopupMenu JavaDoc m_popupMenu;
19
20   public XPopupListener() {
21     super();
22   }
23   
24   public XPopupListener(JComponent JavaDoc target) {
25     this();
26     m_target = target;
27   }
28   
29   public void setTarget(JComponent JavaDoc target) {
30     if(m_target != null) {
31       if(m_popupMenu != null) {
32         m_target.removeMouseListener(this);
33         m_target.remove(m_popupMenu);
34       }
35     }
36     
37     if((m_target = target) != null) {
38       if(m_popupMenu != null) {
39         m_target.add(m_popupMenu);
40         m_target.addMouseListener(this);
41       }
42     }
43   }
44   
45   public void mousePressed(MouseEvent JavaDoc e) {
46     testPopup(e);
47   }
48
49   public void mouseReleased(MouseEvent JavaDoc e) {
50     testPopup(e);
51   }
52
53   public void testPopup(MouseEvent JavaDoc e) {
54     if(e.isPopupTrigger()) {
55       m_popupMenu.show(m_target, e.getX(), e.getY());
56     }
57   }
58
59   public void setPopupMenu(JPopupMenu JavaDoc popupMenu) {
60     if(m_popupMenu != null) {
61       m_target.removeMouseListener(this);
62       m_target.remove(m_popupMenu);
63     }
64
65     if((m_popupMenu = popupMenu) != null) {
66       m_target.add(popupMenu);
67       m_target.addMouseListener(this);
68     }
69   }
70
71   public JPopupMenu JavaDoc getPopupMenu() {
72     return m_popupMenu;
73   }
74 }
75
Popular Tags