1 package org.catacomb.druid.gui.edit;
2
3 import org.catacomb.druid.event.LabelActor;
4 import org.catacomb.druid.swing.DMenu;
5 import org.catacomb.druid.swing.DMenuItem;
6 import org.catacomb.interlish.structure.*;
7 import org.catacomb.report.E;
8 import org.catacomb.util.StringTree;
9 import org.catacomb.util.StringTreeLeaf;
10 import org.catacomb.util.StringUtil;
11
12
13 import java.awt.Color;
14 import java.util.ArrayList;
15
16
17
18 public class DruMenu implements PopulableMenu, ActionSource,
19 LabelActor, Syncable {
20
21 static final long serialVersionUID = 1001;
22
23 String name;
24
25 String id;
26
27 DruMenu parentMenu;
28
29 protected String methodName;
30
31 ActionRelay actionRelay;
32
33 ArrayList<Syncable> items;
34
35 DMenu dMenu;
36
37
38 public DruMenu(String s) {
39 dMenu = new DMenu(s);
40 name = s;
41
42 items = new ArrayList<Syncable>();
43 dMenu.applyRollover();
44 }
45
46 public DMenu getGUIPeer() {
47 return dMenu;
48 }
49
50
51 void setParentMenu(DruMenu dm) {
52 parentMenu = dm;
53 }
54
55
56 public String toString() {
57 return "DruMenu " + name;
58 }
59
60
61 public String getID() {
62 return id;
63 }
64
65
66 public void addSubMenu(DruMenu dsm) {
67 items.add(dsm);
68 dMenu.add(dsm.getGUIPeer());
69 dsm.setParentMenu(this);
70 }
71
72
73
74 public void addItem(DruMenuItem dmi) {
75 items.add(dmi);
76 dMenu.add(dmi.getGUIPeer());
77
78 }
79
80 public void addItem(DruCheckboxMenuItem dmi) {
81 items.add(dmi);
82 dMenu.add(dmi.getGUIPeer());
83 }
84
85 public void addItem(String s) {
86 DMenuItem dmi = new DMenuItem(s);
87 dMenu.add(dmi);
88 dmi.setLabelActor(this);
89
90 }
91
92 public void addItem(String lab, String val) {
93 DMenuItem dmi = new DMenuItem(lab, val);
94 dMenu.add(dmi);
95 dmi.setLabelActor(this);
96
97 }
98
99 public void setBg(Color background) {
100 dMenu.setBackground(background);
101 }
102
103
104 public void setFg(Color foreground) {
105 dMenu.setForeground(foreground);
106 }
107
108
109 public void setAction(String s) {
110 methodName = s;
111 }
112
113
114 public boolean hasRelay() {
115 return (actionRelay != null);
116 }
117
118
119 public void setActionRelay(ActionRelay ac) {
120 actionRelay = ac;
121 }
122
123
124 public void labelAction(String s, boolean b) {
125
126
127 if (actionRelay != null) {
128 actionRelay.actionS(methodName, s);
129
130 } else if (parentMenu != null) {
131 parentMenu.labelAction(s, b);
132
133 } else {
134 E.warning("dropped menu action " + s);
135 }
136 }
137
138
139 public void setOptions(String[] sa) {
140 setItems(sa);
141 }
142
143 public void setItems(String[] sa) {
144 dMenu.removeAll();
145 if (sa == null || sa.length == 0) {
146 dMenu.setEnabled(false);
147 } else {
148 dMenu.setEnabled(true);
149 for (int i = 0; i < sa.length; i++) {
150 addItem(sa[i]);
151 }
152 }
153 }
154
155
156 public void setItemsTree(String[] sa) {
157 dMenu.removeAll();
158
159 StringTree stree = StringUtil.flatTreeify(sa, "root");
160 for (Object obj : stree.getChildren()) {
161 if (obj instanceof String) {
162 addItem((String)obj, (String)obj);
163
164 } else if (obj instanceof StringTree) {
165 addSubMenu(makeMenu((StringTree)obj));
166
167 } else if (obj instanceof StringTreeLeaf) {
168 String stln = ((StringTreeLeaf)obj).getLabel();
169 addItem(stln, stln);
170
171 } else {
172 E.error("unkown item " + obj);
173 }
174 }
175 }
176
177
178 private DruMenu makeMenu(StringTree stree) {
179 DruMenu dm = new DruMenu(stree.getLabel());
180 for (Object obj : stree.getChildren()) {
181 if (obj instanceof String) {
182 dm.addItem((String)obj, stree.getPath() + obj);
183
184 } else if (obj instanceof StringTreeLeaf) {
185 StringTreeLeaf stl = (StringTreeLeaf)obj;
186 dm.addItem(stl.getLabel(), stree.getPath() + stl.getLabel());
187
188 } else if (obj instanceof StringTree) {
189 dm.addSubMenu(makeMenu((StringTree)obj));
190
191 } else {
192 E.error("unkown item " + obj);
193 }
194 }
195 return dm;
196 }
197
198
199 public void addSeparator() {
200 dMenu.addSeparator();
201 }
202
203
204
205
206
207 public void setID(String s) {
208 id = s;
209 }
210
211
212
213 public void preShowSync() {
214 sync();
215 }
216
217
218 public void sync() {
219 for (Syncable sbl : items) {
220 sbl.sync();
221 }
222 }
223
224 public void showPopup(DruMenuButton button, int i, int j) {
225 dMenu.showPopup(button.getGUIPeer(), i, j);
226 }
227
228
229 }