1 package org.catacomb.druid.blocks;
2
3 import org.catacomb.druid.build.Context;
4 import org.catacomb.druid.build.GUIPath;
5 import org.catacomb.druid.build.Realizer;
6 import org.catacomb.druid.gui.edit.DruCheckboxMenuItem;
7 import org.catacomb.druid.gui.edit.DruMenu;
8 import org.catacomb.druid.gui.edit.DruMenuItem;
9 import org.catacomb.interlish.structure.AddableTo;
10 import org.catacomb.report.E;
11
12
13 import java.util.ArrayList;
14
15
16 public class Menu implements AddableTo, Realizer {
17
18 public String label;
19 public String id;
20
21 public String action;
22
23 public ArrayList<Object> items;
24
25
26 public Menu() {
27 }
28
29
30 public Menu(String s) {
31 label = s;
32 }
33
34
35
36 public void add(Object obj) {
37 if (items == null) {
38 items = new ArrayList<Object>();
39 }
40 items.add(obj);
41 }
42
43
44 public void addMenuItem(MenuItem mit) {
45 if (items == null) {
46 items = new ArrayList<Object>();
47 }
48 items.add(mit);
49 }
50
51
52
53 public Object realize(Context ctx, GUIPath gpathin) {
54 GUIPath gpath = gpathin;
55 gpath = gpath.extend(id);
56
57
58
59 DruMenu drum = new DruMenu(label);
60 drum.setID(id);
61
62 drum.setAction(action);
63
64 addItems(drum, ctx, gpath);
65
66 ctx.addComponent(drum, gpath);
67
68 return drum;
69 }
70
71
72 protected void addItems(DruMenu drum, Context ctx, GUIPath gpath) {
73 if (items != null) {
74 for (Object onxt : items) {
75 if (onxt instanceof MenuItem) {
76 MenuItem mcfg = (MenuItem)(onxt);
77 drum.addItem((DruMenuItem)(mcfg.realize(ctx, gpath)));
78
79 } else if (onxt instanceof CheckboxMenuItem) {
80 CheckboxMenuItem mcfg = (CheckboxMenuItem)(onxt);
81 drum.addItem((DruCheckboxMenuItem)(mcfg.realize(ctx, gpath)));
82
83
84 } else if (onxt instanceof Separator) {
85 drum.addSeparator();
86
87 } else if (onxt instanceof SubMenu) {
88 SubMenu smenu = (SubMenu)onxt;
89 drum.addSubMenu((DruMenu)(smenu.realize(ctx, gpath)));
90
91 } else if (onxt instanceof String) {
92 drum.addItem((String)onxt);
93 } else {
94 E.error(" - bad item in menu " + onxt);
95 }
96 }
97 }
98 }
99
100
101 }