1 13 package com.tonbeller.wcf.selection; 14 15 import java.util.ArrayList ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Map ; 19 20 import org.apache.log4j.Logger; 21 import org.w3c.dom.Element ; 22 23 import com.tonbeller.wcf.component.Form; 24 import com.tonbeller.wcf.component.FormListener; 25 import com.tonbeller.wcf.component.RenderListener; 26 import com.tonbeller.wcf.controller.Dispatcher; 27 import com.tonbeller.wcf.controller.DispatcherSupport; 28 import com.tonbeller.wcf.controller.RequestContext; 29 import com.tonbeller.wcf.controller.RequestListener; 30 import com.tonbeller.wcf.convert.BooleanConverter; 31 import com.tonbeller.wcf.convert.CheckBoxConverter; 32 import com.tonbeller.wcf.convert.RadioButtonConverter; 33 import com.tonbeller.wcf.ui.CheckBox; 34 import com.tonbeller.wcf.ui.Item; 35 import com.tonbeller.wcf.ui.RadioButton; 36 import com.tonbeller.wcf.utils.DomUtils; 37 38 43 public class SelectionMgr implements FormListener, RenderListener { 44 45 String groupId = DomUtils.randomId(); 46 BooleanConverter radioConv = new RadioButtonConverter(); 47 BooleanConverter checkConv = new CheckBoxConverter(); 48 List selectionHandlers = new ArrayList (); 49 Dispatcher dispatcher = new DispatcherSupport(); 50 TitleProvider titleProvider = null; 51 boolean readOnly; 52 53 private static Logger logger = Logger.getLogger(SelectionMgr.class); 54 55 SelectionModel selectionModel; 56 57 public SelectionMgr(Dispatcher dispatcher, Form form) { 58 this(dispatcher, form, new DefaultSelectionModel()); 59 } 60 61 public SelectionMgr(Dispatcher dispatcher, Form form, SelectionModel selectionModel) { 62 this.selectionModel = selectionModel; 63 form.addFormListener(this); 64 dispatcher.addRequestListener(null, null, this.dispatcher); 65 } 66 67 70 public void startRendering(RequestContext context) { 71 selectionHandlers.clear(); 72 dispatcher.clear(); 73 } 74 75 78 public void stopRendering() { 79 } 80 81 84 public void renderButton(Element parent, Object obj) { 85 86 if (!selectionModel.isSelectable(obj)) { 87 DomUtils.appendNbsp(parent); 88 return; 89 } 90 91 int selMode = selectionModel.getMode(); 92 93 if (selMode == SelectionModel.SINGLE_SELECTION_HREF || selMode == SelectionModel.MULTIPLE_SELECTION_HREF) { 94 if (readOnly) { 95 if (selectionModel.contains(obj)) 96 parent.setAttribute("style", "selected"); 97 } else { 98 String id = DomUtils.randomId(); 99 parent.setAttribute("hrefId", id); 100 if (selMode == SelectionModel.SINGLE_SELECTION_HREF) 101 dispatcher.addRequestListener(id, null, new SingleSelectHandler(obj)); 102 else 103 dispatcher.addRequestListener(id, null, new MultipleSelectHandler(obj)); 104 if (selectionModel.contains(obj)) 105 parent.setAttribute("style", "selected"); 106 } 107 } 108 109 else if (selMode == SelectionModel.SINGLE_SELECTION_BUTTON || selMode == SelectionModel.MULTIPLE_SELECTION_BUTTON) { 110 if (readOnly) { 111 if (selectionModel.contains(obj)) 112 parent.setAttribute("style", "selected"); 113 } else { 114 String id = DomUtils.randomId(); 115 parent.setAttribute("buttonId", id); 116 if (selMode == SelectionModel.SINGLE_SELECTION_BUTTON) 117 dispatcher.addRequestListener(id, null, new SingleSelectHandler(obj)); 118 else 119 dispatcher.addRequestListener(id, null, new MultipleSelectHandler(obj)); 120 if (selectionModel.contains(obj)) 121 parent.setAttribute("selected", "true"); 122 } 123 } 124 125 else if (selMode == SelectionModel.SINGLE_SELECTION 127 || selMode == SelectionModel.MULTIPLE_SELECTION) { 128 129 Element button; 130 String buttonId = DomUtils.randomId(); 131 132 if (selectionModel.getMode() == SelectionModel.SINGLE_SELECTION) { 133 button = RadioButton.addRadioButton(parent); 134 RadioButton.setGroupId(button, groupId); 135 RadioButton.setId(button, buttonId); 136 RadioButton.setDisabled(button, readOnly); 137 selectionHandlers.add(new SelectionHandler(obj, button, radioConv)); 138 } else { 139 button = CheckBox.addCheckBox(parent); 140 CheckBox.setId(button, buttonId); 141 CheckBox.setDisabled(button, readOnly); 142 selectionHandlers.add(new SelectionHandler(obj, button, checkConv)); 143 } 144 145 Item.setId(button, DomUtils.randomId()); 146 Item.setSelected(button, selectionModel.contains(obj)); 147 if (titleProvider != null) { 148 String title = titleProvider.getLabel(obj); 149 button.setAttribute("title", title); 150 } 151 } 152 } 153 154 158 public SelectionModel getSelectionModel() { 159 return selectionModel; 160 } 161 162 166 public void setSelectionModel(SelectionModel selectionModel) { 167 this.selectionModel = selectionModel; 168 } 169 170 class SelectionHandler { 171 Object obj; 172 Element elem; 173 BooleanConverter conv; 174 175 public SelectionHandler(Object obj, Element elem, BooleanConverter conv) { 176 this.obj = obj; 177 this.elem = elem; 178 this.conv = conv; 179 } 180 181 public void validate(RequestContext context) { 182 Map params = context.getParameters(); 183 switch (conv.isSelected(elem, params)) { 184 case BooleanConverter.TRUE: 185 if (selectionModel.getMode() == SelectionModel.SINGLE_SELECTION) { 186 selectionModel.setSingleSelection(obj); 187 } else { 188 selectionModel.add(obj); 189 } 190 selectionModel.fireSelectionChanged(context); 191 break; 192 case BooleanConverter.FALSE: 193 selectionModel.remove(obj); 194 selectionModel.fireSelectionChanged(context); 195 break; 196 default: break; 198 } 199 } 200 } 201 202 public void revert(RequestContext context) { 203 } 204 205 public boolean validate(RequestContext context) { 206 logger.info("enter"); 207 for (Iterator it = selectionHandlers.iterator(); it.hasNext();) { 208 SelectionHandler sh = (SelectionHandler) it.next(); 209 sh.validate(context); 210 } 211 return true; 212 } 213 214 215 class SingleSelectHandler implements RequestListener { 216 private Object node; 217 218 SingleSelectHandler(Object node) { 219 this.node = node; 220 } 221 222 public void request(RequestContext context) throws Exception { 223 selectionModel.setSingleSelection(node); 224 selectionModel.fireSelectionChanged(context); 225 } 226 } 227 228 229 class MultipleSelectHandler implements RequestListener { 230 private Object node; 231 232 MultipleSelectHandler(Object node) { 233 this.node = node; 234 } 235 236 public void request(RequestContext context) throws Exception { 237 if (selectionModel.contains(node)) 238 selectionModel.remove(node); 239 else 240 selectionModel.add(node); 241 selectionModel.fireSelectionChanged(context); 242 } 243 } 244 245 248 public TitleProvider getTitleProvider() { 249 return titleProvider; 250 } 251 252 255 public void setTitleProvider(TitleProvider provider) { 256 titleProvider = provider; 257 } 258 259 public boolean isReadOnly() { 260 return readOnly; 261 } 262 263 public void setReadOnly(boolean readOnly) { 264 this.readOnly = readOnly; 265 } 266 267 } 268 | Popular Tags |