annotate src/samer/core_/util/swing/MenuBuilder.java @ 8:5e3cbbf173aa tip

Reorganise some more
author samer
date Fri, 05 Apr 2019 22:41:58 +0100
parents bf79fb79ee13
children
rev   line source
samer@0 1 /*
samer@0 2 * MenuBuilder.java
samer@0 3 *
samer@0 4 * Copyright (c) 2000, 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.util.*;
samer@0 14 import samer.core.*;
samer@0 15 import javax.swing.*;
samer@0 16 import java.awt.*;
samer@0 17 import java.awt.event.*;
samer@0 18
samer@0 19 public class MenuBuilder implements Agent.Registry
samer@0 20 {
samer@0 21 JPopupMenu menu;
samer@0 22 AgentAdapter ad;
samer@0 23
samer@0 24 public MenuBuilder(JPopupMenu m, AgentAdapter a) { menu=m; ad=a; }
samer@0 25
samer@0 26 public Agent.Registry add(String l)
samer@0 27 {
samer@0 28 // could get icon here
samer@0 29 JMenuItem mi = new JMenuItem(l);
samer@0 30 mi.addActionListener(ad);
samer@0 31 menu.add(mi);
samer@0 32 return this;
samer@0 33 }
samer@0 34 public Agent.Registry add(String l, boolean state)
samer@0 35 {
samer@0 36 // could get icon here
samer@0 37 JCheckBoxMenuItem mi = new JCheckBoxMenuItem(l, state);
samer@0 38 mi.setName(l);
samer@0 39 mi.addItemListener(ad);
samer@0 40 menu.add(mi);
samer@0 41 return this;
samer@0 42 }
samer@0 43 public void group() { menu.addSeparator(); }
samer@0 44
samer@0 45 public void setTarget(Agent a) { ad=new AgentAdapter(a); group(); }
samer@0 46
samer@0 47 public static PopupHandler addPopup(JPopupMenu popup, Component c) {
samer@0 48 PopupHandler h=new PopupHandler(popup);
samer@0 49 insertML(h,c); return h;
samer@0 50 }
samer@0 51
samer@0 52 private static void insertML(MouseListener l, Component c) {
samer@0 53 // what if this listener isn't there?
samer@0 54 c.removeMouseListener(MouseRetarget.listener);
samer@0 55 c.addMouseListener(l);
samer@0 56 c.addMouseListener(MouseRetarget.listener);
samer@0 57 }
samer@0 58
samer@0 59 /** Add commands for given Agent to given menu, to be show in given component.
samer@0 60 If menu is null, then a new menu is created and associated with the component
samer@0 61 using a popup menu mouse handler. The popup will be shown when the user
samer@0 62 right clicks in the component.
samer@0 63 */
samer@0 64
samer@0 65 public static JPopupMenu showCommands( Agent agent, Component c, JPopupMenu menu)
samer@0 66 {
samer@0 67 if (menu==null) {
samer@0 68 menu=new JPopupMenu(c.getName());
samer@0 69 addPopup(menu, c);
samer@0 70 } else {
samer@0 71 menu.addSeparator();
samer@0 72 }
samer@0 73
samer@0 74 agent.getCommands( new MenuBuilder(menu,new AgentAdapter(agent)));
samer@0 75 return menu;
samer@0 76 }
samer@0 77
samer@0 78 /** Add commands for given Agent to the given DynamicPopupHandler. This maintains
samer@0 79 a list of agents, so when the handler is triggered by the user right-clicking in
samer@0 80 the given component, a popup menu is built dynamically from the commands reported
samer@0 81 by the Agents.
samer@0 82 */
samer@0 83
samer@0 84 public static DynamicPopupHandler showCommands( Agent agent, Component c, DynamicPopupHandler h)
samer@0 85 {
samer@0 86 if (h==null) {
samer@0 87 h=new DynamicPopupHandler(c.getName());
samer@0 88 insertML(h,c);
samer@0 89 }
samer@0 90 h.addAgent(agent);
samer@0 91 return h;
samer@0 92 }
samer@0 93 };
samer@0 94
samer@0 95