Source code: com/xpn/xwiki/objects/classes/ListClass.java
1 /**
2 * ===================================================================
3 *
4 * Copyright (c) 2003 Ludovic Dubost, All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details, published at
15 * http://www.gnu.org/copyleft/gpl.html or in gpl.txt in the
16 * root folder of this distribution.
17
18 * Created by
19 * User: Ludovic Dubost
20 * Date: 1 févr. 2004
21 * Time: 21:46:26
22 */
23 package com.xpn.xwiki.objects.classes;
24
25 import com.xpn.xwiki.XWikiContext;
26 import com.xpn.xwiki.objects.*;
27 import com.xpn.xwiki.objects.meta.PropertyMetaClass;
28 import org.apache.commons.lang.StringUtils;
29 import org.apache.ecs.xhtml.input;
30 import org.apache.ecs.xhtml.option;
31 import org.apache.ecs.xhtml.select;
32 import org.dom4j.Element;
33
34 import java.util.ArrayList;
35 import java.util.Iterator;
36 import java.util.List;
37
38 public abstract class ListClass extends PropertyClass {
39
40 public ListClass(String name, String prettyname, PropertyMetaClass wclass) {
41 super(name, prettyname, wclass);
42 setRelationalStorage(false);
43 setDisplayType(<font color=red>"select"font>);
44 setMultiSelect(false);
45 setSize(1);
46 }
47
48
49 public ListClass(PropertyMetaClass wclass) {
50 this(<font color=red>"list"font>, <font color=red>"List"font>, wclass);
51 }
52
53 public ListClass() {
54 this(null);
55 }
56
57 public String getDisplayType() {
58 return getStringValue(<font color=red>"displayType"font>);
59 }
60
61 public void setDisplayType(String type) {
62 setStringValue(<font color=red>"displayType"font>, type);
63 }
64
65 public int getSize() {
66 return getIntValue(<font color=red>"size"font>);
67 }
68
69 public void setSize(int size) {
70 setIntValue(<font color=red>"size"font>, size);
71 }
72
73 public boolean isMultiSelect() {
74 return (getIntValue(<font color=red>"multiSelect"font>)==1);
75 }
76
77 public void setMultiSelect(boolean multiSelect) {
78 setIntValue(<font color=red>"multiSelect"font>, multiSelect ? 1 : 0);
79 }
80
81 public boolean isRelationalStorage() {
82 return (getIntValue(<font color=red>"relationalStorage"font>)==1);
83 }
84
85 public void setRelationalStorage(boolean storage) {
86 setIntValue(<font color=red>"relationalStorage"font>, storage ? 1 : 0);
87 }
88
89 public static List getListFromString(String value) {
90 List list = new ArrayList();
91 if (value==null)
92 return list;
93
94 String val = StringUtils.replace(value, <font color=red>"\\|"font>, <font color=red>"%PIPE%"font>);
95 String[] result = StringUtils.split(value,<font color=red>"|"font>);
96 for (int i=0;i<result.length;i++)
97 list.add(StringUtils.replace(result[i],<font color=red>"%PIPE%"font>, <font color=red>"|"font>));
98 return list;
99 }
100
101 public BaseProperty newProperty() {
102 BaseProperty lprop;
103
104 if (isRelationalStorage()&&isMultiSelect())
105 lprop = new DBStringListProperty();
106 else if (isMultiSelect())
107 lprop = new StringListProperty();
108 else
109 lprop = new StringProperty();
110 return lprop;
111 }
112
113 public BaseProperty fromString(String value) {
114 BaseProperty prop = newProperty();
115 if (isMultiSelect())
116 ((ListProperty)prop).setList(getListFromString(value));
117 else
118 prop.setValue(value);
119 return prop;
120 }
121
122 public BaseProperty fromStringArray(String[] strings) {
123 if ((!isMultiSelect())||(strings.length==1))
124 return fromString(strings[0]);
125 else {
126 List list = new ArrayList();
127 for (int i=0;i<strings.length;i++)
128 list.add(strings[i]);
129 BaseProperty prop = newProperty();
130 ((ListProperty)prop).setList(list);
131 return prop;
132 }
133 }
134
135
136 public BaseProperty newPropertyfromXML(Element ppcel) {
137 if ((!isRelationalStorage())&&(!isMultiSelect()))
138 return super.newPropertyfromXML(ppcel);
139
140 List elist = ppcel.elements(<font color=red>"value"font>);
141 BaseProperty lprop = (BaseProperty)newProperty();
142
143 List llist;
144
145 if ((isRelationalStorage())&&(isMultiSelect()))
146 llist = ((DBStringListProperty)lprop).getList();
147 else
148 llist = ((ListProperty)lprop).getList();
149
150 for (int i=0;i<elist.size();i++) {
151 Element el = (Element) elist.get(i);
152 llist.add(el.getText());
153 }
154 return lprop;
155 }
156
157
158 public void displayHidden(StringBuffer buffer, String name, String prefix, BaseCollection object, XWikiContext context) {
159 input input = new input();
160 BaseProperty prop = (BaseProperty) object.safeget(name);
161 if (prop!=null) input.setValue(prop.toFormString());
162
163 input.setType(<font color=red>"hidden"font>);
164 input.setName(prefix + name);
165 buffer.append(input.toString());
166 }
167
168 public void displayView(StringBuffer buffer, String name, String prefix, BaseCollection object, XWikiContext context) {
169 List selectlist;
170 BaseProperty prop = (BaseProperty)object.safeget(name);
171 if ((prop instanceof ListProperty)||(prop instanceof DBStringListProperty)) {
172 selectlist = (List) prop.getValue();
173 buffer.append(StringUtils.join(selectlist.toArray(), <font color=red>" "font>));
174 } else {
175 buffer.append(prop.getValue().toString());
176 }
177 }
178
179 public void displayEdit(StringBuffer buffer, String name, String prefix, BaseCollection object, XWikiContext context) {
180 if (getDisplayType()==<font color=red>"input"font>) {
181 input input = new input();
182 BaseProperty prop = (BaseProperty) object.safeget(name);
183 if (prop!=null) input.setValue(prop.toFormString());
184 input.setType(<font color=red>"text"font>);
185 input.setSize(60);
186 input.setName(prefix + name);
187 buffer.append(input.toString());
188 } else {
189 select select = new select(prefix + name, 1);
190 select.setMultiple(isMultiSelect());
191 select.setSize(getSize());
192
193 List list = getList(context);
194 List selectlist;
195
196 BaseProperty prop = (BaseProperty)object.safeget(name);
197 if (prop==null) {
198 selectlist = new ArrayList();
199 } else if ((prop instanceof ListProperty)||(prop instanceof DBStringListProperty)) {
200 selectlist = (List) prop.getValue();
201 } else {
202 selectlist = new ArrayList();
203 selectlist.add(prop.getValue());
204 }
205
206 // Add options from Set
207 for (Iterator it=list.iterator();it.hasNext();) {
208 String value = it.next().toString();
209 option option = new option(value, value);
210 option.addElement(value);
211 if (selectlist.contains(value))
212 option.setSelected(true);
213 select.addElement(option);
214 }
215
216 buffer.append(select.toString());
217 }
218 }
219
220 public abstract List getList(XWikiContext context);
221
222
223 }