01: /*
02: * Copyright 2008 Gerd Ziegler (www.gerdziegler.de)
03: * Licensed under the Apache License, Version 2.0 (the "License");
04: * you may not use this file except in compliance with the License.
05: * You may obtain a copy of the License at
06: * http://www.apache.org/licenses/LICENSE-2.0
07: * Unless required by applicable law or agreed to in writing,
08: * software distributed under the License is distributed on an
09: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
10: * either express or implied. See the License for the specific
11: * language governing permissions and limitations under the License.
12: * 13.02.2008
13: * @author www.gerdziegler.de
14: */
15: package org.ztemplates.yui.menu;
16:
17: import java.util.List;
18: import java.util.Random;
19:
20: import org.ztemplates.web.ZTemplates;
21: import org.ztemplates.yui.YScript;
22:
23: public class YMenuFactory {
24: public static YMenuBar createRandomMenuBar() {
25: Random rnd = new Random();
26: YMenuBar menu = new YMenuBar(ZTemplates.getRenderService()
27: .createJavaScriptId());
28:
29: addRandomMenuItems(menu.getMenu().getMenuItems(), 4, rnd);
30: return menu;
31: }
32:
33: public static YContextMenu createRandomContextMenu(
34: YScript eventSource) {
35: Random rnd = new Random();
36: YContextMenu menu = new YContextMenu(ZTemplates
37: .getRenderService().createJavaScriptId(), eventSource);
38:
39: addRandomMenuItems(menu.getMenu().getMenuItems(), 4, rnd);
40: return menu;
41: }
42:
43: public static YMenuBar createSimpleMenuBar() throws Exception {
44: YMenuBar menu = new YMenuBar();
45:
46: YMenuItem crt = new YMenuItem(menu);
47: crt.setContent("submenu");
48: menu.getMenu().getMenuItems().add(crt);
49:
50: YMenuItem mi = new YMenuItem("item1");
51: crt.getMenuItems().add(mi);
52:
53: YMenuItem mi2 = new YMenuItem("item2");
54: crt.getMenuItems().add(mi2);
55:
56: return menu;
57: }
58:
59: private static void addRandomMenuItems(List<YMenuItem> items,
60: int depth, Random rnd) {
61: if (depth == 0)
62: return;
63: for (int i = 0; i < 4; i++) {
64: YMenuItem crt = new YMenuItem("menu");
65: items.add(crt);
66: if (rnd.nextInt() % 4 != 0) {
67: addRandomMenuItems(crt.getMenuItems(), depth - 1, rnd);
68: } else {
69: crt.setHref("#");
70: }
71: }
72: }
73:
74: }
|