1 13 package com.tonbeller.wcf.catedit; 14 15 import java.util.ArrayList ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 19 import org.w3c.dom.Document ; 20 import org.w3c.dom.Element ; 21 22 import com.tonbeller.wcf.changeorder.ChangeOrderMgr; 23 import com.tonbeller.wcf.changeorder.ChangeOrderModel; 24 import com.tonbeller.wcf.changeorder.ChangeOrderUtils; 25 import com.tonbeller.wcf.component.Component; 26 import com.tonbeller.wcf.component.NestableComponentSupport; 27 import com.tonbeller.wcf.controller.Dispatcher; 28 import com.tonbeller.wcf.controller.DispatcherSupport; 29 import com.tonbeller.wcf.controller.RequestContext; 30 import com.tonbeller.wcf.controller.RequestListener; 31 import com.tonbeller.wcf.scroller.Scroller; 32 import com.tonbeller.wcf.utils.DomUtils; 33 34 40 public class CategoryEditor extends NestableComponentSupport implements ChangeOrderModel, CategoryModelChangeListener { 41 CategoryModel model; 42 String rootElementName = "cat-edit"; 43 String changeCategoryButtonElementName = "cat-button"; 44 CategoryElementRenderer categoryRenderer = new DefaultCategoryElementRenderer(); 45 ItemElementRenderer itemRenderer = new DefaultItemElementRenderer(); 46 Dispatcher dispatcher = new DispatcherSupport(); 47 48 ChangeOrderMgr changeOrderMgr; 49 50 Document factory; 51 RequestContext context; 52 53 public CategoryEditor(String id, Component parent) { 54 super(id, parent); 55 super.getDispatcher().addRequestListener(null, null, dispatcher); 56 } 57 58 public CategoryEditor(String id, Component parent, CategoryModel model) { 59 super(id, parent); 60 super.getDispatcher().addRequestListener(null, null, dispatcher); 61 setModel(model); 62 } 63 64 public void initialize(RequestContext context) throws Exception { 65 super.initialize(context); 66 changeOrderMgr = new ChangeOrderMgr(super.getDispatcher(), this, this); 68 } 69 70 public Element render(RequestContext context, Document factory) throws Exception { 71 this.factory = factory; 72 this.context = context; 73 74 itemRenderer.startRendering(context); 75 76 dispatcher.clear(); 77 dispatcher.addRequestListener(null, null, itemRenderer); 78 79 Element rootElem = factory.createElement(rootElementName); 80 List categories = model.getCategories(); 81 if (categories != null && categories.size() > 0) 82 renderRootElement(rootElem); 83 84 itemRenderer.stopRendering(); 85 86 return rootElem; 87 } 88 89 void renderRootElement(Element rootElem) { 90 changeOrderMgr.startRendering(context); 91 92 for (Iterator it = model.getCategories().iterator(); it.hasNext();) { 93 Category cat = (Category) it.next(); 94 Element catElem = renderCategory(cat); 95 rootElem.appendChild(catElem); 96 } 97 98 changeOrderMgr.stopRendering(); 99 } 100 101 Element renderCategory(Category cat) { 102 Element catElem = categoryRenderer.render(context, factory, cat); 103 int count = cat.getItems().size(); 104 int index = 0; 105 for (Iterator it = cat.getItems().iterator(); it.hasNext();) { 106 Item item = (Item) it.next(); 107 Element itemElem = renderItem(item, cat); 108 catElem.appendChild(itemElem); 109 changeOrderMgr.renderButton(itemElem, cat, item, index, count); 110 ++index; 111 } 112 return catElem; 113 } 114 115 Element renderItem(Item item, Category cat) { 116 Element itemElem = itemRenderer.render(context, factory, cat, item); 117 int N = cat.getItems().size(); 118 119 if (item.isMovable() && (N > 1 || cat.isEmptyAllowed())) { 121 for (Iterator it = model.getCategories().iterator(); it.hasNext();) { 122 Category other = (Category) it.next(); 123 if (other.equals(cat)) continue; 125 Element buttonElem = renderChangeCategoryButton(item, cat, other); 126 itemElem.appendChild(buttonElem); 127 } 128 } 129 return itemElem; 130 } 131 132 Element renderChangeCategoryButton(Item item, Category source, Category target) { 133 Element buttonElem = factory.createElement(changeCategoryButtonElementName); 134 buttonElem.setAttribute("icon", target.getIcon()); 135 String id = DomUtils.randomId(); 136 buttonElem.setAttribute("id", id); 137 dispatcher.addRequestListener(id, null, new ChangeCategoryButtonHandler(item, source, target)); 138 return buttonElem; 139 } 140 141 class ChangeCategoryButtonHandler implements RequestListener { 142 Item item; 143 Category source; 144 Category target; 145 ChangeCategoryButtonHandler(Item item, Category source, Category target) { 146 this.item = item; 147 this.source = source; 148 this.target = target; 149 } 150 public void request(RequestContext context) throws Exception { 151 Scroller.enableScroller(context); 152 validate(context); 153 source.removeItem(item); 154 target.addItem(item); 155 } 156 } 157 158 162 public CategoryModel getModel() { 163 return model; 164 } 165 166 170 public void setModel(CategoryModel newModel) { 171 if (model != null) 172 model.removeCategoryModelChangeListener(this); 173 model = newModel; 174 if (model != null) 175 model.addCategoryModelChangeListener(this); 176 } 177 178 182 public void categoryModelChanged(CategoryModelChangeEvent event) { 183 itemRenderer.categoryModelChanged(event); 184 dispatcher.clear(); 185 } 186 187 191 public CategoryElementRenderer getCategoryRenderer() { 192 return categoryRenderer; 193 } 194 195 199 public String getChangeCategoryButtonElementName() { 200 return changeCategoryButtonElementName; 201 } 202 203 207 public ItemElementRenderer getItemRenderer() { 208 return itemRenderer; 209 } 210 211 215 public String getRootElementName() { 216 return rootElementName; 217 } 218 219 223 public void setCategoryRenderer(CategoryElementRenderer categoryRenderer) { 224 this.categoryRenderer = categoryRenderer; 225 } 226 227 231 public void setChangeCategoryButtonElementName(String changeCategoryButtonElementName) { 232 this.changeCategoryButtonElementName = changeCategoryButtonElementName; 233 } 234 235 239 public void setItemRenderer(ItemElementRenderer itemRenderer) { 240 this.itemRenderer = itemRenderer; 241 } 242 243 247 public void setRootElementName(String rootElementName) { 248 this.rootElementName = rootElementName; 249 } 250 251 254 public boolean mayMove(Object scope, Object item) { 255 Category cat = (Category) scope; 256 if (!cat.isOrderSignificant()) 257 return false; 258 return cat.getItems().size() > 0 || cat.isEmptyAllowed(); 259 } 260 261 264 public void move(Object scope, Object item, int oldIndex, int newIndex) { 265 Category cat = (Category) scope; 266 if (cat == null) 268 return; 269 List items = new ArrayList (); 271 items.addAll(cat.getItems()); 272 ChangeOrderUtils.move(items, oldIndex, newIndex); 273 cat.changeOrder(items); 275 } 276 277 } 278 | Popular Tags |