samer@0: /* samer@0: * DynamicPopupHandler.java samer@0: * samer@0: * Copyright (c) 2003, Samer Abdallah, King's College London. samer@0: * All rights reserved. samer@0: * samer@0: * This software is provided AS iS and WITHOUT ANY WARRANTY; samer@0: * without even the implied warranty of MERCHANTABILITY or samer@0: * FITNESS FOR A PARTICULAR PURPOSE. samer@0: */ samer@0: samer@0: package samer.core.util.swing; samer@0: import samer.core.*; samer@0: import samer.core.util.*; samer@0: import java.awt.event.*; samer@0: import javax.swing.*; samer@0: samer@0: /** This is a mouse click handler that builds a popup menu on samer@0: the fly from a list of Agents (actually, just one, possibly samer@0: compound agent.) The Agent can be changed so that samer@0: new menu items can be added. samer@0: */ samer@0: samer@0: public class DynamicPopupHandler extends MouseAdapter samer@0: { samer@0: private String name; samer@0: private boolean backstop; samer@0: private AgentAdapter adapter=null; samer@0: samer@0: public DynamicPopupHandler(String nm) { name=nm; } samer@0: samer@0: public void addAgent(Agent agent) { samer@0: adapter=new AgentAdapter( adapter==null ? agent samer@0: : new CompoundAgent(adapter.getAgent(),agent)); samer@0: } samer@0: samer@0: public void setBackstop(boolean f) { backstop=f; } samer@0: public void mousePressed( MouseEvent e) { maybeShowMenu(e); } samer@0: public void mouseReleased( MouseEvent e) { maybeShowMenu(e); } samer@0: samer@0: void maybeShowMenu(MouseEvent e) samer@0: { samer@0: if (!e.isPopupTrigger()) return; samer@0: if (!backstop) { samer@0: // if not backstop, must NOT have Alt or Ctl down samer@0: // modified for Macs: need modifiers to get right mouse click! samer@0: if (e.isShiftDown()) return; samer@0: } samer@0: JPopupMenu popup=new JPopupMenu(name); samer@0: adapter.getAgent().getCommands(new MenuBuilder(popup,adapter)); samer@0: popup.show( e.getComponent(), e.getX(), e.getY()); samer@0: e.consume(); samer@0: } samer@0: }