1 package gboat2.base.view.components;
2
3 import java.lang.reflect.Array;
4 import java.util.Collection;
5 import java.util.Map;
6
7 import javax.servlet.http.HttpServletRequest;
8 import javax.servlet.http.HttpServletResponse;
9
10 import org.apache.commons.lang3.RandomStringUtils;
11 import org.apache.commons.lang3.StringUtils;
12 import org.apache.struts2.components.ClosingUIBean;
13 import org.apache.struts2.util.MakeIterator;
14 import org.apache.struts2.views.annotations.StrutsTag;
15 import org.apache.struts2.views.annotations.StrutsTagAttribute;
16
17 import com.opensymphony.xwork2.util.ValueStack;
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79 @StrutsTag(
80 name = "accordion",
81 tldTagClass = "gboat2.base.view.jsp.AccordionTag",
82 description = "创建一个可以折叠的面板(Accordion),详见 http://api.jqueryui.com/accordion/")
83 public class Accordion extends ClosingUIBean {
84 public static final String TEMPLATE = "accordion";
85 public static final String TEMPLATE_CLOSE = "accordion-close";
86
87 public static final String JQUERYACTION = "accordion";
88
89 protected boolean throwExceptionOnNullValueAttribute = false;
90
91 protected String active;
92 protected String animate;
93 protected String collapsible;
94 protected String event;
95 protected String header;
96 protected String heightStyle;
97 protected Object list;
98 protected String listKey;
99 protected String listValue;
100 protected String href;
101 protected String paramKeys;
102 protected String paramValues;
103
104 protected String onActivateTopics;
105 protected String onBeforeActivateTopics;
106 protected String onCreateTopics;
107
108 public Accordion(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
109 super(stack, request, response);
110 }
111
112 @Override
113 public String getDefaultOpenTemplate() {
114 return TEMPLATE;
115 }
116
117 @Override
118 protected String getDefaultTemplate() {
119 return TEMPLATE_CLOSE;
120 }
121
122 @SuppressWarnings("rawtypes")
123 @Override
124 public void evaluateExtraParams() {
125 super.evaluateExtraParams();
126
127 addParameter("jqueryaction", JQUERYACTION);
128
129 if (active != null)
130 addParameter("active", findValue(active, Integer.class));
131
132 if (animate != null)
133 addParameter("animate", findString(animate));
134
135 if (collapsible != null)
136 addParameter("collapsible", findValue(this.collapsible, Boolean.class));
137
138 if (event != null)
139 addParameter("event", findString(event));
140
141 if (header != null)
142 addParameter("header", findString(header));
143
144 if (heightStyle != null)
145 addParameter("heightStyle", findString(heightStyle));
146
147 Object value = null;
148 if (list == null) {
149 list = parameters.get("list");
150 }
151 if (list instanceof String) {
152 value = findValue((String) list);
153 } else if (list instanceof Collection) {
154 value = list;
155 } else if (MakeIterator.isIterable(list)) {
156 value = MakeIterator.convert(list);
157 }
158 if (value == null) {
159 if (throwExceptionOnNullValueAttribute) {
160
161 value = findValue((list == null) ? (String) list : list.toString(), "list",
162 "The requested list key '" + list + "' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name}");
163 } else {
164 value = findValue((list == null) ? (String) list : list.toString());
165 }
166 }
167
168 if (value instanceof Collection) {
169 addParameter("list", value);
170 } else {
171 addParameter("list", MakeIterator.convert(value));
172 }
173
174 if (value instanceof Collection) {
175 addParameter("listSize", Integer.valueOf(((Collection) value).size()));
176 } else if (value instanceof Map) {
177 addParameter("listSize", Integer.valueOf(((Map) value).size()));
178 } else if (value != null && value.getClass().isArray()) {
179 addParameter("listSize", Integer.valueOf(Array.getLength(value)));
180 }
181
182 if (listKey != null) {
183 listKey = stripExpressionIfAltSyntax(listKey);
184 addParameter("listKey", listKey);
185 } else if (value instanceof Map) {
186 addParameter("listKey", "key");
187 }
188
189 if (listValue != null) {
190 listValue = stripExpressionIfAltSyntax(listValue);
191 addParameter("listValue", listValue);
192 } else if (value instanceof Map) {
193 addParameter("listValue", "value");
194 }
195
196 if (href != null)
197 addParameter("href", findString(href));
198
199 if (paramKeys != null)
200 addParameter("paramKeys", findString(paramKeys));
201
202 if (paramValues != null)
203 addParameter("paramValues", findString(paramValues));
204
205 if (onActivateTopics != null)
206 addParameter("onActivateTopics", findString(onActivateTopics));
207
208 if (onBeforeActivateTopics != null)
209 addParameter("onBeforeActivateTopics", findString(onBeforeActivateTopics));
210
211 if (onCreateTopics != null)
212 addParameter("onCreateTopics", findString(onCreateTopics));
213
214 if (StringUtils.isBlank(id)) {
215 this.id = "accordion_" + RandomStringUtils.randomNumeric(3);
216 addParameter("id", this.id);
217 }
218 }
219
220 @StrutsTagAttribute(description = "当前打开哪一个面板,0 表示第一个", type = "Integer", defaultValue = "0")
221 public void setActive(String active) {
222 this.active = active;
223 }
224
225 @StrutsTagAttribute(description = "切换面板时要使用的 easing 动画形式 名称,可选值请参见 http://api.jqueryui.com/easings/")
226 public void setAnimate(String animate) {
227 this.animate = animate;
228 }
229
230 @StrutsTagAttribute(description = "是否允许折叠展开的面板", type = "Boolean", defaultValue = "false")
231 public void setCollapsible(String collapsible) {
232 this.collapsible = collapsible;
233 }
234
235 @Override
236 @StrutsTagAttribute(description = "如果设置为 true,则禁用该 Accordion(折叠面板)", type = "Boolean", defaultValue = "false")
237 public void setDisabled(String disabled) {
238 super.setDisabled(disabled);
239 }
240
241 @StrutsTagAttribute(description = "Accordion(折叠面板) 头部会作出反应的事件,用以激活相关的面板。可以指定多个事件,用空格隔开,如 mouseover", defaultValue = "click")
242 public void setEvent(String event) {
243 this.event = event;
244 }
245
246 public String getHeader() {
247 return header;
248 }
249
250 @StrutsTagAttribute(description = "折叠面板的头部标签名(Tag Name)", defaultValue = "h3")
251 public void setHeader(String header) {
252 this.header = header;
253 }
254
255 @StrutsTagAttribute(description = "控制 Accordion(折叠面板) 和每个面板的高度,可选值:'auto' - 所有的面板将会被设置为最高的面板的高度; 'fill' - 基于 accordion 的父元素的高度,扩展到可用的高度; 'content' - 每个面板的高度取决于它的内容", defaultValue = "auto")
256 public void setHeightStyle(String heightStyle) {
257 this.heightStyle = heightStyle;
258 }
259
260 @StrutsTagAttribute(description = "从一个已有列表(集合)加载可折叠面板,该列表可以是 Array/Map/Collection 等 Iterator 对象")
261 public void setList(Object list) {
262 this.list = list;
263 }
264
265 @StrutsTagAttribute(description = "从一个列表中加载可折叠面板时的标题内容")
266 public void setListKey(String listKey) {
267 this.listKey = listKey;
268 }
269
270 @StrutsTagAttribute(description = "从一个列表中加载可折叠面板时的面板内容")
271 public void setListValue(String listValue) {
272 this.listValue = listValue;
273 }
274
275 @StrutsTagAttribute(description = "如果需要通过 Ajax 从远程服务器加载面板内容,则通过此属性设置 ajax 请求的 URL")
276 public void setHref(String href) {
277 this.href = href;
278 }
279
280 @StrutsTagAttribute(description = "跟在 href 后面的参数名称,多值之间使用逗号分隔,如: queryParam1,queryParam2")
281 public void setParamKeys(String paramKeys) {
282 this.paramKeys = paramKeys;
283 }
284
285 @StrutsTagAttribute(description = "跟在 href 后面的参数值,多值之间使用逗号分隔,与 paramKeys 一一对应,如: queryValue1,queryValue2")
286 public void setParamValues(String paramValues) {
287 this.paramValues = paramValues;
288 }
289
290 @StrutsTagAttribute(description = "面板被激活后(在动画完成之后)发布的消息主题,多值之间使用逗号分隔")
291 public void setOnActivateTopics(String onActivateTopics) {
292 this.onActivateTopics = onActivateTopics;
293 }
294
295 @StrutsTagAttribute(description = "面板被激活前发布的消息主题,多值之间使用逗号分隔")
296 public void setOnBeforeActivateTopics(String onBeforeActivateTopics) {
297 this.onBeforeActivateTopics = onBeforeActivateTopics;
298 }
299
300 @StrutsTagAttribute(description = "当创建 accordion 时发布的消息主题,多值之间使用逗号分隔")
301 public void setOnCreateTopics(String onCreateTopics) {
302 this.onCreateTopics = onCreateTopics;
303 }
304 }