01: package gnu.jemacs.swing;
02:
03: import java.awt.event.*;
04: import javax.swing.*;
05: import gnu.lists.*;
06: import gnu.mapping.Procedure;
07: import gnu.jemacs.buffer.Command;
08: import gnu.jemacs.buffer.EMenu;
09: import gnu.jemacs.lang.*;
10: import java.util.*;
11:
12: /**
13: * This manages a menu (for menubars or popup menus).
14: * @author Simon Josefsson <jas@pdc.kth.se> (orginal contribution)
15: */
16:
17: public class SwingMenu extends JMenu implements EMenu {
18: public SwingMenu() {
19: super ();
20: }
21:
22: public SwingMenu(LList menu) {
23: super ();
24: setMenu(menu);
25: }
26:
27: public void setMenu(LList menu) {
28: java.util.Enumeration e = menu.elements();
29: for (int i = 0; e.hasMoreElements(); i++) {
30: Object item = e.nextElement();
31: if (item == null) {
32: this .add(Box.createHorizontalGlue());
33: } else if (item instanceof FString) {
34: if (i == 0)
35: this .setText(item.toString());
36: else
37: // FIXME handle different type of separators
38: this .addSeparator();
39: } else if (item instanceof FVector) {
40: FVector menuEntry = (FVector) item;
41:
42: if (menuEntry.get(0) instanceof FString) {
43: FString txt = (FString) menuEntry.get(0);
44: Object proc = menuEntry.get(1);
45:
46: // FIXME handle all possible keywords
47:
48: JMenuItem menuItem = new MenuItem(txt.toString(),
49: proc);
50: this .add(menuItem);
51: }
52: } else if (item instanceof JComponent)
53: this .add((JComponent) item);
54: else if (item instanceof LList) {
55: // FIXME don't create new objects, keep it within this
56: SwingMenu tmp = new SwingMenu((LList) item);
57: this .add(tmp);
58: }
59: }
60: }
61:
62: }
63:
64: class MenuItem extends JMenuItem implements
65: java.awt.event.ActionListener {
66: Object command;
67:
68: public MenuItem(String text, Object command) {
69: super (text);
70: this .command = command;
71: this .addActionListener(this );
72: }
73:
74: public void actionPerformed(java.awt.event.ActionEvent event) {
75: JMenuItem source = (JMenuItem) event.getSource();
76: Command.perform(command);
77: }
78: }
|