1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.cms.gui.dialog;
35
36 import info.magnolia.cms.beans.config.ContentRepository;
37 import info.magnolia.cms.core.Content;
38 import info.magnolia.cms.gui.control.Button;
39 import info.magnolia.cms.gui.control.ButtonSet;
40 import info.magnolia.cms.gui.control.ControlImpl;
41 import info.magnolia.cms.gui.control.Hidden;
42 import info.magnolia.cms.gui.misc.CssConstants;
43 import info.magnolia.cms.security.AccessDeniedException;
44 import info.magnolia.cms.util.ContentUtil;
45 import info.magnolia.cms.util.NodeDataUtil;
46
47 import java.io.IOException;
48 import java.io.Writer;
49 import java.util.ArrayList;
50 import java.util.Collection;
51 import java.util.Iterator;
52 import java.util.List;
53
54 import javax.jcr.PathNotFoundException;
55 import javax.jcr.PropertyType;
56 import javax.jcr.RepositoryException;
57 import javax.servlet.http.HttpServletRequest;
58 import javax.servlet.http.HttpServletResponse;
59
60 import org.apache.commons.lang.StringUtils;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
63
64
65
66
67
68
69 public class DialogButtonSet extends DialogBox {
70
71
72
73
74 private static Logger log = LoggerFactory.getLogger(DialogButtonSet.class);
75
76 private int buttonType = ControlImpl.BUTTONTYPE_RADIO;
77
78 public void setOptions(Content configNode, boolean setDefaultSelected) {
79
80
81 List options = new ArrayList();
82 try {
83 Iterator it = getOptionNodes(configNode).iterator();
84 while (it.hasNext()) {
85 Content n = ((Content) it.next());
86 String valueNodeData = this.getConfigValue("valueNodeData", "value");
87 String labelNodeData = this.getConfigValue("labelNodeData", "label");
88
89 String value = NodeDataUtil.getString(n, valueNodeData);
90 String label = NodeDataUtil.getString(n, labelNodeData);
91
92
93 Button button = new Button(this.getName(), value);
94
95 button.setLabel(label);
96
97 String iconSrc = n.getNodeData("iconSrc").getString();
98 if (StringUtils.isNotEmpty(iconSrc)) {
99 button.setIconSrc(iconSrc);
100 }
101
102 if (setDefaultSelected && n.getNodeData("selected").getBoolean()) {
103 button.setState(ControlImpl.BUTTONSTATE_PUSHED);
104 }
105 options.add(button);
106 }
107 }
108 catch (RepositoryException e) {
109 if (log.isDebugEnabled()) {
110 log.debug("Exception caught: " + e.getMessage(), e);
111 }
112 }
113 this.setOptions(options);
114 }
115
116 protected Collection getOptionNodes(Content configNode) throws PathNotFoundException, RepositoryException, AccessDeniedException {
117 Content optionsNode = null;
118
119 if(configNode.hasContent("options")){
120 optionsNode = configNode.getContent("options");
121 }
122 else{
123 String repository = this.getConfigValue("repository", ContentRepository.WEBSITE);
124 String path = this.getConfigValue("path");
125 if(StringUtils.isNotEmpty(path)){
126 optionsNode = ContentUtil.getContent(repository, path);
127 }
128 }
129
130 if(optionsNode != null){
131 return ContentUtil.getAllChildren(optionsNode);
132 }
133 return new ArrayList();
134 }
135
136 public void setOption(Content configNode) {
137
138 List options = new ArrayList();
139 Button button = new Button(this.getName() + "_dummy", StringUtils.EMPTY);
140 String label = configNode.getNodeData("buttonLabel").getString();
141
142 button.setLabel(label);
143
144 if (configNode.getNodeData("selected").getBoolean()) {
145 button.setState(ControlImpl.BUTTONSTATE_PUSHED);
146 }
147
148 button.setValue("true");
149 button.setOnclick("mgnlDialogShiftCheckboxSwitch('" + this.getName() + "');");
150 options.add(button);
151 this.setOptions(options);
152 }
153
154
155
156
157 public void init(HttpServletRequest request, HttpServletResponse response, Content websiteNode, Content configNode)
158 throws RepositoryException {
159 super.init(request, response, websiteNode, configNode);
160
161
162 if (configNode != null) {
163 String controlType = this.getConfigValue("selectType",this.getConfigValue("controlType"));
164
165 if (log.isDebugEnabled()) {
166 log.debug("Init DialogButtonSet with type=" + controlType);
167 }
168
169
170 if (controlType.equals("radio")) {
171 setButtonType(ControlImpl.BUTTONTYPE_RADIO);
172 setOptions(configNode, true);
173 }
174 else if (controlType.equals("checkbox")) {
175 setButtonType(ControlImpl.BUTTONTYPE_CHECKBOX);
176 setOptions(configNode, false);
177 setConfig("valueType", "multiple");
178 }
179 else if (controlType.equals("checkboxSwitch")) {
180 setButtonType(ControlImpl.BUTTONTYPE_CHECKBOX);
181 setOption(configNode);
182 }
183 }
184 }
185
186 public void drawHtmlPreSubs(Writer out) throws IOException {
187 this.drawHtmlPre(out);
188 }
189
190 public void drawHtmlPostSubs(Writer out) throws IOException {
191 this.drawHtmlPost(out);
192 }
193
194 public void setButtonType(int i) {
195 this.buttonType = i;
196 }
197
198 public int getButtonType() {
199 return this.buttonType;
200 }
201
202
203
204
205 public void drawHtml(Writer out) throws IOException {
206 this.drawHtmlPre(out);
207
208 for (int i = 0; i < this.getOptions().size(); i++) {
209 Button b = (Button) this.getOptions().get(i);
210
211 b.setLabel(this.getMessage(b.getLabel()));
212 if(b.getName().endsWith("_dummy")){
213 b.setName(this.getName() + "_dummy");
214 b.setOnclick("mgnlDialogShiftCheckboxSwitch('" + this.getName() + "');");
215 }else if(this.getName().startsWith(b.getName() + "_")){
216 b.setName(this.getName());
217 }
218 }
219
220 ButtonSet control;
221 if (this.getConfigValue("valueType").equals("multiple")) {
222
223 control = new ButtonSet(this.getName(), this.getValues());
224 control.setValueType(ControlImpl.VALUETYPE_MULTIPLE);
225 }
226 else if (this.getButtonType() == ControlImpl.BUTTONTYPE_CHECKBOX) {
227
228 control = new ButtonSet(this.getName() + "_SWITCH", this.getValue());
229 }
230 else {
231
232 control = new ButtonSet(this.getName(), this.getValue());
233 }
234 control.setButtonType(this.getButtonType());
235
236
237 control.setCssClass(this.getConfigValue("cssClass", CssConstants.CSSCLASS_BUTTONSETBUTTON));
238
239 if (this.getConfigValue("saveInfo").equals("false")) {
240 control.setSaveInfo(false);
241 }
242 final String type = this.getConfigValue("type", PropertyType.TYPENAME_STRING);
243 control.setType(type);
244 String width = this.getConfigValue("width", null);
245 control.setButtonHtmlPre("<tr><td class=\"" + CssConstants.CSSCLASS_BUTTONSETBUTTON + "\">");
246 control.setButtonHtmlInter("</td><td class=\"" + CssConstants.CSSCLASS_BUTTONSETLABEL + "\">");
247 control.setButtonHtmlPost("</td></tr>");
248 int cols = Integer.valueOf(this.getConfigValue("cols", "1")).intValue();
249 if (cols > 1) {
250 width = "100%";
251 control.setHtmlPre(control.getHtmlPre() + "<tr>");
252 control.setHtmlPost("</tr>" + control.getHtmlPost());
253 int item = 1;
254 int itemsPerCol = (int) Math.ceil(this.getOptions().size() / ((double) cols));
255 for (int i = 0; i < this.getOptions().size(); i++) {
256 Button b = (Button) this.getOptions().get(i);
257 if (item == 1) {
258 b.setHtmlPre("<td><table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">"
259 + control.getButtonHtmlPre());
260 }
261 if (item == itemsPerCol) {
262 b.setHtmlPost(control.getButtonHtmlPost() + "</table></td><td class=\""
263 + CssConstants.CSSCLASS_BUTTONSETINTERCOL
264 + "\"></td>");
265 item = 1;
266 }
267 else {
268 item++;
269 }
270 }
271
272 int lastIndex = this.getOptions().size() - 1;
273
274 if (lastIndex > -1) {
275 ((Button) this.getOptions().get(lastIndex)).setHtmlPost(control.getButtonHtmlPost() + "</table>");
276 }
277 }
278 if (width != null) {
279 control.setHtmlPre("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"" + width + "\">");
280 }
281 control.setButtons(this.getOptions());
282 out.write(control.getHtml());
283 if (control.getButtonType() == ControlImpl.BUTTONTYPE_CHECKBOX
284 && control.getValueType() != ControlImpl.VALUETYPE_MULTIPLE) {
285
286 String value = this.getValue();
287 if (StringUtils.isEmpty(value)) {
288 if (this.getConfigValue("selected").equals("true")) {
289 value = "true";
290 }
291 else {
292 value = "false";
293 }
294 }
295 Hidden hiddenValueField = new Hidden(this.getName(), value);
296 hiddenValueField.setType(type);
297
298 out.write(hiddenValueField.getHtml());
299 }
300 this.drawHtmlPost(out);
301 }
302 }