| 1 13 package com.tonbeller.wcf.changeorder; 14 15 import org.w3c.dom.Element ; 16 17 import com.tonbeller.tbutils.res.Resources; 18 import com.tonbeller.wcf.component.Form; 19 import com.tonbeller.wcf.component.RenderListener; 20 import com.tonbeller.wcf.controller.Dispatcher; 21 import com.tonbeller.wcf.controller.DispatcherSupport; 22 import com.tonbeller.wcf.controller.RequestContext; 23 import com.tonbeller.wcf.controller.RequestListener; 24 import com.tonbeller.wcf.scroller.Scroller; 25 import com.tonbeller.wcf.utils.DomUtils; 26 27 32 public class ChangeOrderMgr implements RenderListener { 33 Dispatcher dispatcher = new DispatcherSupport(); 34 ChangeOrderModel model; 35 36 37 static final String BUTTON_ELEMENT = "move-button"; 38 39 static final String STYLE_ATTR = "style"; 40 41 static final String STYLE_FWD = "fwd"; 42 43 static final String STYLE_BWD = "bwd"; 44 45 static final String STYLE_CUT = "cut"; 46 47 static final String STYLE_UNCUT = "uncut"; 48 49 static final String STYLE_PASTE_BEFORE = "paste-before"; 50 51 static final String STYLE_PASTE_AFTER = "paste-after"; 52 53 static final String TITLE_ATTR = "title"; 54 String titleFwd = ""; 55 String titleBwd = ""; 56 String titleCut = ""; 57 String titleUncut = ""; 58 String titlePasteBefore = ""; 59 String titlePasteAfter = ""; 60 61 Form form; 62 63 public ChangeOrderMgr(Dispatcher parentDispatcher, Form form) { 64 this.form = form; 65 this.model = new DefaultChangeOrderModel(); 66 parentDispatcher.addRequestListener(null, null, dispatcher); 67 } 68 69 75 public ChangeOrderMgr(Dispatcher parentDispatcher, Form form, ChangeOrderModel model) { 76 this.form = form; 77 this.model = model; 78 parentDispatcher.addRequestListener(null, null, dispatcher); 79 } 80 81 94 interface ButtonRenderer extends RenderListener { 95 void renderButton(Element elem, Object scope, Object node, int nodeIndex, int nodeCount); 96 } 97 98 106 class ForwardBackwardButtonRenderer implements ButtonRenderer { 107 108 public void renderButton(Element elem, Object scope, Object node, int nodeIndex, int nodeCount) { 109 if (nodeIndex == 0) { 110 if (nodeCount > 2) 113 appendEmptyButton(elem); 114 appendMoveButton(elem, scope, node, STYLE_FWD, titleFwd, nodeIndex, nodeIndex + 1); 115 } else if (nodeIndex == nodeCount - 1) { 116 appendMoveButton(elem, scope, node, STYLE_BWD, titleBwd, nodeIndex, nodeIndex - 1); 118 if (nodeCount > 2) 120 appendEmptyButton(elem); 121 } else { 122 appendMoveButton(elem, scope, node, STYLE_BWD, titleBwd, nodeIndex, nodeIndex - 1); 124 appendMoveButton(elem, scope, node, STYLE_FWD, titleFwd, nodeIndex, nodeIndex + 1); 125 } 126 } 127 128 public void startRendering(RequestContext context) { 129 } 130 131 public void stopRendering() { 132 } 133 134 void appendEmptyButton(Element elem) { 135 Element buttonElem = elem.getOwnerDocument().createElement(BUTTON_ELEMENT); 136 elem.appendChild(buttonElem); 137 } 138 139 void appendMoveButton(Element elem, Object scope, Object item, String style, String title, int oldIndex, 140 int newIndex) { 141 Element buttonElem = elem.getOwnerDocument().createElement(BUTTON_ELEMENT); 142 elem.appendChild(buttonElem); 143 buttonElem.setAttribute(STYLE_ATTR, style); 144 buttonElem.setAttribute(TITLE_ATTR, title); 145 String id = DomUtils.randomId(); 146 buttonElem.setAttribute("id", id); 147 dispatcher.addRequestListener(id, null, new ChangeOrderButtonHandler(scope, item, oldIndex, 148 newIndex)); 149 } 150 }; 151 152 159 class CutButtonRenderer implements ButtonRenderer { 160 public void renderButton(Element elem, Object scope, Object item, int nodeIndex, int nodeCount) { 161 Element buttonElem = elem.getOwnerDocument().createElement(BUTTON_ELEMENT); 162 elem.appendChild(buttonElem); 163 buttonElem.setAttribute(STYLE_ATTR, STYLE_CUT); 164 buttonElem.setAttribute(TITLE_ATTR, titleCut); 165 String id = DomUtils.randomId(); 166 buttonElem.setAttribute("id", id); 167 dispatcher.addRequestListener(id, null, new CutButtonHandler(scope, item, nodeIndex)); 168 } 169 170 public void startRendering(RequestContext context) { 171 } 172 173 public void stopRendering() { 174 } 175 }; 176 177 184 class CutButtonHandler implements RequestListener { 185 Object scope; 186 Object item; 187 int nodeIndex; 188 189 CutButtonHandler(Object scope, Object item, int nodeIndex) { 190 this.scope = scope; 191 this.item = item; 192 this.nodeIndex = nodeIndex; 193 } 194 195 public void request(RequestContext context) throws Exception { 196 Scroller.enableScroller(context); 197 form.validate(context); 198 if (model != null) 199 buttonRenderer = new PasteButtonRenderer(scope, item, nodeIndex); 200 } 201 } 202 203 211 class PasteButtonRenderer implements ButtonRenderer { 212 Object scope; 213 Object item; 214 int nodeIndex; 215 216 PasteButtonRenderer(Object scope, Object item, int nodeIndex) { 217 this.scope = scope; 218 this.item = item; 219 this.nodeIndex = nodeIndex; 220 } 221 222 boolean equals(Object o1, Object o2) { 223 if (o1 == null) 224 return o2 == null; 225 return o1.equals(o2); 226 } 227 228 public void renderButton(Element elem, Object scope, Object item, int nodeIndex, int nodeCount) { 229 if (!equals(this.scope, scope)) 231 return; 232 Element buttonElem = elem.getOwnerDocument().createElement(BUTTON_ELEMENT); 234 elem.appendChild(buttonElem); 235 String id = DomUtils.randomId(); 236 buttonElem.setAttribute("id", id); 237 if (this.nodeIndex == nodeIndex) { 238 buttonElem.setAttribute(STYLE_ATTR, STYLE_UNCUT); 240 buttonElem.setAttribute(TITLE_ATTR, titleUncut); 241 dispatcher.addRequestListener(id, null, new UnCutButtonHandler()); 242 } else if (this.nodeIndex < nodeIndex) { 243 buttonElem.setAttribute(STYLE_ATTR, STYLE_PASTE_AFTER); 245 buttonElem.setAttribute(TITLE_ATTR, titlePasteAfter); 246 dispatcher.addRequestListener(id, null, new ChangeOrderButtonHandler(scope, item, 247 this.nodeIndex, nodeIndex)); 248 } else { 249 buttonElem.setAttribute(STYLE_ATTR, STYLE_PASTE_BEFORE); 251 buttonElem.setAttribute(TITLE_ATTR, titlePasteBefore); 252 dispatcher.addRequestListener(id, null, new ChangeOrderButtonHandler(scope, item, 253 this.nodeIndex, nodeIndex)); 254 } 255 } 256 257 public void startRendering(RequestContext context) { 258 } 259 260 public void stopRendering() { 261 buttonRenderer = new CutButtonRenderer(); 262 } 263 }; 264 265 271 class UnCutButtonHandler implements RequestListener { 272 public void request(RequestContext context) throws Exception { 273 Scroller.enableScroller(context); 274 form.validate(context); 275 buttonRenderer = new CutButtonRenderer(); 276 } 277 } 278 279 285 class ChangeOrderButtonHandler implements RequestListener { 286 Object scope; 287 Object item; 288 int oldIndex; 289 int newIndex; 290 291 ChangeOrderButtonHandler(Object scope, Object item, int oldIndex, int newIndex) { 292 this.scope = scope; 293 this.item = item; 294 this.oldIndex = oldIndex; 295 this.newIndex = newIndex; 296 } 297 298 public void request(RequestContext context) throws Exception { 299 Scroller.enableScroller(context); 300 form.validate(context); 301 if (model != null) 302 model.move(scope, item, oldIndex, newIndex); 303 } 304 } 305 306 309 ButtonRenderer buttonRenderer = new ForwardBackwardButtonRenderer(); 310 311 public void renderButton(Element elem, Object scope, Object node, int nodeIndex, int nodeCount) { 312 if (model == null || !model.mayMove(scope, node)) 313 return; 314 315 if (nodeCount < 2) 317 return; 318 319 buttonRenderer.renderButton(elem, scope, node, nodeIndex, nodeCount); 320 } 321 322 323 326 public void startRendering(RequestContext context) { 327 Resources res = context.getResources(ChangeOrderMgr.class); 328 titleFwd = res.getString("ChangeOrderMgr.titleFwd"); 329 titleBwd = res.getString("ChangeOrderMgr.titleBwd"); 330 titleCut = res.getString("ChangeOrderMgr.titleCut"); 331 titleUncut = res.getString("ChangeOrderMgr.titleUncut"); 332 titlePasteBefore = res.getString("ChangeOrderMgr.titlePasteBefore"); 333 titlePasteAfter = res.getString("ChangeOrderMgr.titlePasteAfter"); 334 335 dispatcher.clear(); 336 buttonRenderer.startRendering(context); 337 } 338 339 342 public void stopRendering() { 343 buttonRenderer.stopRendering(); 344 } 345 346 public ChangeOrderModel getModel() { 347 return model; 348 } 349 350 public void setModel(ChangeOrderModel model) { 351 this.model = model; 352 } 353 354 public void setCutPasteMode(boolean b) { 355 if (b) 356 buttonRenderer = new CutButtonRenderer(); 357 else 358 buttonRenderer = new ForwardBackwardButtonRenderer(); 359 } 360 361 } | Popular Tags |