samer@0
|
1 /*
|
samer@0
|
2 * DynamicPopupHandler.java
|
samer@0
|
3 *
|
samer@0
|
4 * Copyright (c) 2003, Samer Abdallah, King's College London.
|
samer@0
|
5 * All rights reserved.
|
samer@0
|
6 *
|
samer@0
|
7 * This software is provided AS iS and WITHOUT ANY WARRANTY;
|
samer@0
|
8 * without even the implied warranty of MERCHANTABILITY or
|
samer@0
|
9 * FITNESS FOR A PARTICULAR PURPOSE.
|
samer@0
|
10 */
|
samer@0
|
11
|
samer@0
|
12 package samer.core.util.swing;
|
samer@0
|
13 import samer.core.*;
|
samer@0
|
14 import samer.core.util.*;
|
samer@0
|
15 import java.awt.event.*;
|
samer@0
|
16 import javax.swing.*;
|
samer@0
|
17
|
samer@0
|
18 /** This is a mouse click handler that builds a popup menu on
|
samer@0
|
19 the fly from a list of Agents (actually, just one, possibly
|
samer@0
|
20 compound agent.) The Agent can be changed so that
|
samer@0
|
21 new menu items can be added.
|
samer@0
|
22 */
|
samer@0
|
23
|
samer@0
|
24 public class DynamicPopupHandler extends MouseAdapter
|
samer@0
|
25 {
|
samer@0
|
26 private String name;
|
samer@0
|
27 private boolean backstop;
|
samer@0
|
28 private AgentAdapter adapter=null;
|
samer@0
|
29
|
samer@0
|
30 public DynamicPopupHandler(String nm) { name=nm; }
|
samer@0
|
31
|
samer@0
|
32 public void addAgent(Agent agent) {
|
samer@0
|
33 adapter=new AgentAdapter( adapter==null ? agent
|
samer@0
|
34 : new CompoundAgent(adapter.getAgent(),agent));
|
samer@0
|
35 }
|
samer@0
|
36
|
samer@0
|
37 public void setBackstop(boolean f) { backstop=f; }
|
samer@0
|
38 public void mousePressed( MouseEvent e) { maybeShowMenu(e); }
|
samer@0
|
39 public void mouseReleased( MouseEvent e) { maybeShowMenu(e); }
|
samer@0
|
40
|
samer@0
|
41 void maybeShowMenu(MouseEvent e)
|
samer@0
|
42 {
|
samer@0
|
43 if (!e.isPopupTrigger()) return;
|
samer@0
|
44 if (!backstop) {
|
samer@0
|
45 // if not backstop, must NOT have Alt or Ctl down
|
samer@0
|
46 // modified for Macs: need modifiers to get right mouse click!
|
samer@0
|
47 if (e.isShiftDown()) return;
|
samer@0
|
48 }
|
samer@0
|
49 JPopupMenu popup=new JPopupMenu(name);
|
samer@0
|
50 adapter.getAgent().getCommands(new MenuBuilder(popup,adapter));
|
samer@0
|
51 popup.show( e.getComponent(), e.getX(), e.getY());
|
samer@0
|
52 e.consume();
|
samer@0
|
53 }
|
samer@0
|
54 }
|