1 5 package com.tonbeller.wcf.popup; 6 7 import java.util.ArrayList ; 8 import java.util.Iterator ; 9 import java.util.List ; 10 11 import org.w3c.dom.Document ; 12 import org.w3c.dom.Element ; 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 label; 21 private String image; 22 private String id = DomUtils.randomId(); 23 24 private List items = new ArrayList (); 25 26 public PopUp() { 27 } 28 29 public void addItem(Item item) { 30 items.add(item); 31 } 32 33 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 list) { 51 for (Iterator 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 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 parent; 82 private int level; 83 84 Renderer(Element 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 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 elem = DomUtils.appendElement(parent, "popup-item"); 100 setItemAttrs(elem, item); 101 elem.setAttribute("href", item.getHref()); 102 } 103 104 private void setItemAttrs(Element 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 render(Document factory) { 114 if (isEmpty(items)) 115 return null; 116 Element 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 render(RequestContext context) throws Exception { 128 Document dom = XmlUtils.createDocument(); 129 Element root = render(dom); 130 dom.appendChild(root); 131 return dom; 132 } 133 134 public String getImage() { 135 return image; 136 } 137 138 public void setImage(String image) { 139 this.image = image; 140 } 141 142 public String getLabel() { 143 return label; 144 } 145 146 public void setLabel(String label) { 147 this.label = label; 148 } 149 150 public String getId() { 151 return id; 152 } 153 154 public void setId(String id) { 155 this.id = id; 156 } 157 } 158 | Popular Tags |