KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > contrib > dbroggisch > repopulation > GroupFormElement


1 /*
2  * Copyright (C) 2003 Diez B. Roggisch [deets@web.de]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: GroupFormElement.java,v 1.2 2004/02/01 05:16:27 christianc Exp $
19  */

20 package org.enhydra.barracuda.contrib.dbroggisch.repopulation;
21
22 import java.util.List JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import org.enhydra.barracuda.core.forms.DefaultFormElement;
25 import org.enhydra.barracuda.core.comp.ListModel;
26 import org.enhydra.barracuda.core.comp.DefaultListModel;
27 import org.apache.log4j.Logger;
28 import org.enhydra.barracuda.core.comp.ListSelectionModel;
29 import org.enhydra.barracuda.core.forms.FormType;
30 import org.enhydra.barracuda.core.forms.FormValidator;
31 import org.enhydra.barracuda.core.comp.ItemMap;
32 import org.enhydra.barracuda.core.comp.ViewContext;
33 import org.enhydra.barracuda.core.comp.BToggleButton;
34 import org.enhydra.barracuda.core.comp.BInput;
35 import org.enhydra.barracuda.core.comp.ListModel;
36 import org.enhydra.barracuda.core.comp.ListSelectionModel;
37 import org.enhydra.barracuda.core.comp.AbstractTemplateModel;
38 import org.enhydra.barracuda.core.comp.IterativeModel;
39
40 public class GroupFormElement extends SelectFormElement implements ChildModelElement
41 {
42     public String JavaDoc KEY = "KEY";
43     public String JavaDoc VALUE = "VALUE";
44
45     public IterativeModel getChildModel(String JavaDoc parentName, String JavaDoc prefix) {
46         return new ChildModel(parentName, getName(), prefix);
47     }
48
49     public GroupFormElement(ListModel lm,
50                  java.lang.String JavaDoc ikey,
51                  FormType itype,
52                  java.lang.Object JavaDoc idefaultVal,
53                  FormValidator ivalidator,
54                  boolean iallowMultiples) {
55         super(lm, ikey, itype, idefaultVal, ivalidator, iallowMultiples);
56     }
57
58
59     public class ChildModel extends AbstractTemplateModel implements IterativeModel {
60
61         String JavaDoc _name, _parent_name, _child_name, _prefix;
62         ListSelectionModel _lsm;
63         ListModel _lm;
64         int _count;
65         Object JavaDoc _key, _value;
66         boolean _allows_multiple;
67
68         public ChildModel(String JavaDoc parentName, String JavaDoc childName, String JavaDoc prefix) {
69             _parent_name = parentName;
70             _child_name = childName;
71             _name = _parent_name + "_" + _child_name;
72             if(prefix != null) {
73                 _prefix = prefix;
74             } else {
75                 _prefix = "";
76             }
77             _allows_multiple = allowMultiples();
78         }
79
80         public String JavaDoc getName() {
81             return _name;
82         }
83
84
85         public Object JavaDoc getItem(String JavaDoc key) {
86             if(key.equals(KEY)) {
87                 return _key;
88             } else if(key.equals(VALUE)) {
89                 return _value;
90             } else {
91                 return super.getItem(key);
92             }
93         }
94
95         public void preIterate() {
96             _lsm = getListSelectionModel();
97             _lm = getListModel();
98             _count = -1;
99         }
100
101         public void postIterate() {
102         }
103
104         public boolean hasNext() {
105             return _lm.getSize() > 0 && _count < _lm.getSize() - 1;
106         }
107
108         public void loadNext() {
109             _count++;
110             Object JavaDoc item = _lm.getItemAt(_count);
111             BToggleButton btog = new BToggleButton();
112             btog.setName(_child_name);
113             btog.setSelected(_lsm.isSelectedIndex(_count));
114             if(item instanceof ItemMap) {
115                 ItemMap im = (ItemMap)item;
116                 btog.setValue("" + im.getKey());
117                 _value = im.getValue();
118             } else {
119                 btog.setValue("" + item);
120                 _value = item;
121             }
122             if(_allows_multiple) {
123                 btog.setType(BInput.CHECKBOX);
124             } else {
125                 btog.setType(BInput.RADIO);
126             }
127             _key = btog;
128         }
129     }
130
131 }
132
Popular Tags