samer@0: /* samer@0: * MenuBuilder.java samer@0: * samer@0: * Copyright (c) 2000, 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.util.*; samer@0: import samer.core.*; samer@0: import javax.swing.*; samer@0: import java.awt.*; samer@0: import java.awt.event.*; samer@0: samer@0: public class MenuBuilder implements Agent.Registry samer@0: { samer@0: JPopupMenu menu; samer@0: AgentAdapter ad; samer@0: samer@0: public MenuBuilder(JPopupMenu m, AgentAdapter a) { menu=m; ad=a; } samer@0: samer@0: public Agent.Registry add(String l) samer@0: { samer@0: // could get icon here samer@0: JMenuItem mi = new JMenuItem(l); samer@0: mi.addActionListener(ad); samer@0: menu.add(mi); samer@0: return this; samer@0: } samer@0: public Agent.Registry add(String l, boolean state) samer@0: { samer@0: // could get icon here samer@0: JCheckBoxMenuItem mi = new JCheckBoxMenuItem(l, state); samer@0: mi.setName(l); samer@0: mi.addItemListener(ad); samer@0: menu.add(mi); samer@0: return this; samer@0: } samer@0: public void group() { menu.addSeparator(); } samer@0: samer@0: public void setTarget(Agent a) { ad=new AgentAdapter(a); group(); } samer@0: samer@0: public static PopupHandler addPopup(JPopupMenu popup, Component c) { samer@0: PopupHandler h=new PopupHandler(popup); samer@0: insertML(h,c); return h; samer@0: } samer@0: samer@0: private static void insertML(MouseListener l, Component c) { samer@0: // what if this listener isn't there? samer@0: c.removeMouseListener(MouseRetarget.listener); samer@0: c.addMouseListener(l); samer@0: c.addMouseListener(MouseRetarget.listener); samer@0: } samer@0: samer@0: /** Add commands for given Agent to given menu, to be show in given component. samer@0: If menu is null, then a new menu is created and associated with the component samer@0: using a popup menu mouse handler. The popup will be shown when the user samer@0: right clicks in the component. samer@0: */ samer@0: samer@0: public static JPopupMenu showCommands( Agent agent, Component c, JPopupMenu menu) samer@0: { samer@0: if (menu==null) { samer@0: menu=new JPopupMenu(c.getName()); samer@0: addPopup(menu, c); samer@0: } else { samer@0: menu.addSeparator(); samer@0: } samer@0: samer@0: agent.getCommands( new MenuBuilder(menu,new AgentAdapter(agent))); samer@0: return menu; samer@0: } samer@0: samer@0: /** Add commands for given Agent to the given DynamicPopupHandler. This maintains samer@0: a list of agents, so when the handler is triggered by the user right-clicking in samer@0: the given component, a popup menu is built dynamically from the commands reported samer@0: by the Agents. samer@0: */ samer@0: samer@0: public static DynamicPopupHandler showCommands( Agent agent, Component c, DynamicPopupHandler h) samer@0: { samer@0: if (h==null) { samer@0: h=new DynamicPopupHandler(c.getName()); samer@0: insertML(h,c); samer@0: } samer@0: h.addAgent(agent); samer@0: return h; samer@0: } samer@0: }; samer@0: samer@0: