KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > selection > SelectionMgr


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.selection;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.apache.log4j.Logger;
21 import org.w3c.dom.Element JavaDoc;
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 /**
39  * Selection Manager
40  *
41  * @author av
42  */

43 public class SelectionMgr implements FormListener, RenderListener {
44
45   String JavaDoc groupId = DomUtils.randomId();
46   BooleanConverter radioConv = new RadioButtonConverter();
47   BooleanConverter checkConv = new CheckBoxConverter();
48   List JavaDoc selectionHandlers = new ArrayList JavaDoc();
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   /**
68    * must be called once before rendering
69    */

70   public void startRendering(RequestContext context) {
71     selectionHandlers.clear();
72     dispatcher.clear();
73   }
74
75   /**
76    * must be called once after rendering
77    */

78   public void stopRendering() {
79   }
80
81   /**
82    * if selection is enabled adds a checkbox or radiobutton element to the parent.
83    */

84   public void renderButton(Element JavaDoc parent, Object JavaDoc 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 JavaDoc 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 JavaDoc 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     // create button element
126
else if (selMode == SelectionModel.SINGLE_SELECTION
127         || selMode == SelectionModel.MULTIPLE_SELECTION) {
128
129       Element JavaDoc button;
130       String JavaDoc 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 JavaDoc title = titleProvider.getLabel(obj);
149         button.setAttribute("title", title);
150       }
151     }
152   }
153
154   /**
155    * Returns the model.
156    * @return SelectionModel
157    */

158   public SelectionModel getSelectionModel() {
159     return selectionModel;
160   }
161
162   /**
163    * Sets the model.
164    * @param model The model to set
165    */

166   public void setSelectionModel(SelectionModel selectionModel) {
167     this.selectionModel = selectionModel;
168   }
169
170   class SelectionHandler {
171     Object JavaDoc obj;
172     Element JavaDoc elem;
173     BooleanConverter conv;
174
175     public SelectionHandler(Object JavaDoc obj, Element JavaDoc 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 JavaDoc 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: // UNKNOWN, i.e. not rendered
197
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 JavaDoc it = selectionHandlers.iterator(); it.hasNext();) {
208       SelectionHandler sh = (SelectionHandler) it.next();
209       sh.validate(context);
210     }
211     return true;
212   }
213
214   /** single selection via href hyperlink */
215   class SingleSelectHandler implements RequestListener {
216     private Object JavaDoc node;
217
218     SingleSelectHandler(Object JavaDoc node) {
219       this.node = node;
220     }
221
222     public void request(RequestContext context) throws Exception JavaDoc {
223       selectionModel.setSingleSelection(node);
224       selectionModel.fireSelectionChanged(context);
225     }
226   }
227
228   /** multiple selection via image buttons */
229   class MultipleSelectHandler implements RequestListener {
230     private Object JavaDoc node;
231
232     MultipleSelectHandler(Object JavaDoc node) {
233       this.node = node;
234     }
235
236     public void request(RequestContext context) throws Exception JavaDoc {
237       if (selectionModel.contains(node))
238         selectionModel.remove(node);
239       else
240         selectionModel.add(node);
241       selectionModel.fireSelectionChanged(context);
242     }
243   }
244
245   /**
246    * if set creates title attribute
247    */

248   public TitleProvider getTitleProvider() {
249     return titleProvider;
250   }
251
252   /**
253    * if set creates title attribute
254    */

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