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