comparison src/samer/core_/util/swing/DynamicPopupHandler.java @ 0:bf79fb79ee13

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