KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > list > MappedListBox


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.list;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.Collections JavaDoc;
17 import java.util.Comparator JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20
21 import org.apache.log4j.Logger;
22 import org.w3c.dom.Element JavaDoc;
23
24 import com.tonbeller.wcf.component.FormListener;
25 import com.tonbeller.wcf.controller.RequestContext;
26 import com.tonbeller.wcf.convert.ConvertException;
27 import com.tonbeller.wcf.ui.ListItem;
28 import com.tonbeller.wcf.utils.DomUtils;
29 import com.tonbeller.wcf.utils.SoftException;
30
31 /**
32  * THIS IS CRAP - DONT USE!
33  * <p/>
34  * stateful listbox.
35  *
36  * initialize():
37  * <pre>
38  * Element parent = ...
39  * // erzeuge Listbox Element
40  * Element lb = ListBox1.addListBox1(parent);
41  * ListBox1.setId(lb, DomUtils.randomId());
42     ListBox1.setLabel(lb, p.getLabel());
43
44     // create MappedListBox
45     MappedListBox mlb = new MappedListBox(lb);
46
47     // add Items
48     Object[] objs = ...
49     for (int i = 0; i &lt; objs.length; i++) {
50       Object o = objs[i];
51       MappedListBox.Item item = new MappedListBox.Item(hier.getLabel(), o);
52       mlb.getEntries().add(item);
53       if (some condition)
54         item.setSelected(true);
55     }
56     mlb.sortByLabel();
57
58     // create DOM Elements
59     mlb.revert();
60  * </pre>
61  *
62  * validate():
63  * <pre>
64  * mlb.validate(context);
65     MappedListBox.Item item = mlb.getSingleSelection();
66     if (item != null)
67       ... // work with selection
68  * </pre>
69  *
70  * revert():
71  * <pre>
72  * mlb.revert();
73  * </pre>
74  *
75  * @author av
76  */

77 public class MappedListBox implements FormListener {
78   List JavaDoc entries = new ArrayList JavaDoc();
79   Element JavaDoc listBox;
80   private static Logger logger = Logger.getLogger(MappedListBox.class);
81
82   public MappedListBox(Element JavaDoc listBox) {
83     this.listBox = listBox;
84   }
85
86   public void revert(RequestContext context) {
87     DomUtils.removeChildElements(listBox);
88     for (Iterator JavaDoc iter = entries.iterator(); iter.hasNext();) {
89       Item item = (Item) iter.next();
90       Element JavaDoc elem = ListItem.addListItem(listBox);
91       item.setElement(elem);
92       ListItem.setId(elem, item.getId());
93       ListItem.setLabel(elem, item.getLabel());
94       if (item.isSelected())
95         ListItem.setSelected(elem, true);
96     }
97   }
98
99   public boolean validate(RequestContext context) {
100     try {
101       context.getConverter().validate(context.getParameters(), context.getFileParameters(), listBox, null);
102       for (Iterator JavaDoc it = entries.iterator(); it.hasNext();) {
103         Item item = (Item) it.next();
104         item.setSelected(ListItem.isSelected(item.getElement()));
105       }
106     } catch (ConvertException e) {
107       logger.error("exception caught", e);
108       throw new SoftException(e);
109     }
110     return true;
111   }
112
113   public Item getSingleSelection() {
114     for (Iterator JavaDoc it = entries.iterator(); it.hasNext();) {
115       Item item = (Item) it.next();
116       if (item.isSelected())
117         return item;
118     }
119     return null;
120   }
121
122   public void sortByLabel() {
123     Collections.sort(entries, new Comparator JavaDoc(){
124       public int compare(Object JavaDoc o1, Object JavaDoc o2) {
125         String JavaDoc l1 = ((Item)o1).getLabel();
126         String JavaDoc l2 = ((Item)o2).getLabel();
127         return l1.compareTo(l2);
128       }
129     });
130   }
131
132   /**
133    * listbox entry
134    */

135   public static class Item {
136     String JavaDoc label;
137     String JavaDoc id;
138     Object JavaDoc value;
139     boolean selected;
140     Element JavaDoc element;
141
142     public Item(String JavaDoc label, Object JavaDoc value) {
143       this.label = label;
144       this.value = value;
145       this.id = DomUtils.randomId();
146     }
147
148     /**
149      * Returns the label.
150      * @return String
151      */

152     public String JavaDoc getLabel() {
153       return label;
154     }
155
156     /**
157      * Returns the value.
158      * @return Object
159      */

160     public Object JavaDoc getValue() {
161       return value;
162     }
163
164     /**
165      * Sets the label.
166      * @param label The label to set
167      */

168     public void setLabel(String JavaDoc label) {
169       this.label = label;
170     }
171
172     /**
173      * Sets the value.
174      * @param value The value to set
175      */

176     public void setValue(Object JavaDoc value) {
177       this.value = value;
178     }
179     /**
180      * Returns the id.
181      * @return String
182      */

183     public String JavaDoc getId() {
184       return id;
185     }
186
187     /**
188      * Sets the id.
189      * @param id The id to set
190      */

191     public void setId(String JavaDoc id) {
192       this.id = id;
193     }
194
195     /**
196      * Returns the selected.
197      * @return boolean
198      */

199     public boolean isSelected() {
200       return selected;
201     }
202
203     /**
204      * Sets the selected.
205      * @param selected The selected to set
206      */

207     public void setSelected(boolean selected) {
208       this.selected = selected;
209     }
210
211     /**
212      * Returns the element.
213      * @return Element
214      */

215     public Element JavaDoc getElement() {
216       return element;
217     }
218
219     /**
220      * Sets the element.
221      * @param element The element to set
222      */

223     public void setElement(Element JavaDoc element) {
224       this.element = element;
225     }
226
227   }
228
229   /**
230    * Returns the entries.
231    * @return List
232    */

233   public List JavaDoc getEntries() {
234     return entries;
235   }
236
237   /**
238    * Sets the entries.
239    * @param entries The entries to set
240    */

241   public void setEntries(List JavaDoc entries) {
242     this.entries = entries;
243   }
244
245 }
246
Popular Tags