KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > popup > PopUp


1 /*
2  * Copyright (c) 1971-2003 TONBELLER AG, Bensheim.
3  * All rights reserved.
4  */

5 package com.tonbeller.wcf.popup;
6
7 import java.util.ArrayList JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.List JavaDoc;
10
11 import org.w3c.dom.Document JavaDoc;
12 import org.w3c.dom.Element JavaDoc;
13
14 import com.tonbeller.wcf.component.Renderable;
15 import com.tonbeller.wcf.controller.RequestContext;
16 import com.tonbeller.wcf.utils.DomUtils;
17 import com.tonbeller.wcf.utils.XmlUtils;
18
19 public class PopUp implements Renderable {
20   private String JavaDoc label;
21   private String JavaDoc image;
22   private String JavaDoc id = DomUtils.randomId();
23
24   private List JavaDoc items = new ArrayList JavaDoc();
25
26   public PopUp() {
27   }
28
29   public void addItem(Item item) {
30     items.add(item);
31   }
32
33   /**
34    * simplified visitor
35    */

36   private abstract class Switch {
37     abstract void handleGroupItem(GroupItem g);
38
39     abstract void handleMenuItem(MenuItem m);
40
41     void handleItem(Item it) {
42       if (it == null)
43         return;
44       if (it instanceof GroupItem)
45         handleGroupItem((GroupItem) it);
46       else
47         handleMenuItem((MenuItem) it);
48     }
49
50     void handleList(List JavaDoc list) {
51       for (Iterator JavaDoc it = list.iterator(); it.hasNext();) {
52         handleItem((Item) it.next());
53       }
54     }
55
56   }
57
58   private class EmptySwitch extends Switch {
59     boolean empty = true;
60
61     void handleGroupItem(GroupItem g) {
62       handleList(g.getChildren());
63     }
64
65     void handleMenuItem(MenuItem m) {
66       empty = false;
67     }
68
69     public boolean isEmpty() {
70       return empty;
71     }
72   }
73
74   private boolean isEmpty(List JavaDoc list) {
75     EmptySwitch es = new EmptySwitch();
76     es.handleList(list);
77     return es.isEmpty();
78   }
79
80   private class Renderer extends Switch {
81     private Element JavaDoc parent;
82     private int level;
83
84     Renderer(Element JavaDoc parent, int level) {
85       this.parent = parent;
86       this.level = level;
87     }
88
89     void handleGroupItem(GroupItem item) {
90       if (isEmpty(item.getChildren()))
91         return;
92       Element JavaDoc elem = DomUtils.appendElement(parent, "popup-group");
93       setItemAttrs(elem, item);
94       Renderer r = new Renderer(elem, level + 1);
95       r.handleList(item.getChildren());
96     }
97
98     void handleMenuItem(MenuItem item) {
99       Element JavaDoc elem = DomUtils.appendElement(parent, "popup-item");
100       setItemAttrs(elem, item);
101       elem.setAttribute("href", item.getHref());
102     }
103
104     private void setItemAttrs(Element JavaDoc elem, Item item) {
105       elem.setAttribute("level", "" + level);
106       if (item.getLabel() != null)
107         elem.setAttribute("label", item.getLabel());
108       if (item.getImage() != null)
109         elem.setAttribute("image", item.getImage());
110     }
111   }
112
113   public Element JavaDoc render(Document JavaDoc factory) {
114     if (isEmpty(items))
115       return null;
116     Element JavaDoc popup = factory.createElement("popup-menu");
117     popup.setAttribute("id", id);
118     if (label != null)
119       popup.setAttribute("label", label);
120     if (image != null)
121       popup.setAttribute("image", image);
122     Renderer r = new Renderer(popup, 0);
123     r.handleList(items);
124     return popup;
125   }
126
127   public Document JavaDoc render(RequestContext context) throws Exception JavaDoc {
128     Document JavaDoc dom = XmlUtils.createDocument();
129     Element JavaDoc root = render(dom);
130     dom.appendChild(root);
131     return dom;
132   }
133
134   public String JavaDoc getImage() {
135     return image;
136   }
137
138   public void setImage(String JavaDoc image) {
139     this.image = image;
140   }
141
142   public String JavaDoc getLabel() {
143     return label;
144   }
145
146   public void setLabel(String JavaDoc label) {
147     this.label = label;
148   }
149
150   public String JavaDoc getId() {
151     return id;
152   }
153
154   public void setId(String JavaDoc id) {
155     this.id = id;
156   }
157 }
158
Popular Tags