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.ControlImpl;
39 import info.magnolia.cms.gui.control.Select;
40 import info.magnolia.cms.gui.control.SelectOption;
41 import info.magnolia.cms.gui.misc.CssConstants;
42 import info.magnolia.cms.security.AccessDeniedException;
43 import info.magnolia.cms.util.ContentUtil;
44 import info.magnolia.cms.util.NodeDataUtil;
45
46 import java.io.IOException;
47 import java.io.Writer;
48 import java.util.ArrayList;
49 import java.util.Collection;
50 import java.util.Collections;
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 DialogSelect extends DialogBox {
70
71
72
73
74 private static Logger log = LoggerFactory.getLogger(DialogSelect.class);
75
76 public void setOptions(Content configNode) {
77 List options = new ArrayList();
78 try {
79 Iterator it = this.getOptionNodes(configNode).iterator();
80 while (it.hasNext()) {
81 Content n = (Content) it.next();
82 String valueNodeData = this.getConfigValue("valueNodeData", "value");
83 String labelNodeData = this.getConfigValue("labelNodeData", "label");
84
85 String value = n.getName();
86 if (n.hasNodeData(valueNodeData)) {
87 value = NodeDataUtil.getString(n, valueNodeData);
88 }
89 String label = NodeDataUtil.getString(n, labelNodeData, value);
90
91 SelectOption option = new SelectOption(label, value);
92 if (n.getNodeData("selected").getBoolean()) {
93 option.setSelected(true);
94 }
95 options.add(option);
96 }
97 }
98 catch (RepositoryException e) {
99 if (log.isDebugEnabled()) {
100 log.debug("Exception caught: " + e.getMessage(), e);
101 }
102 }
103 this.setOptions(options);
104 }
105
106 protected Collection getOptionNodes(Content configNode) throws PathNotFoundException, RepositoryException, AccessDeniedException {
107 Content optionsNode = null;
108
109 if(configNode.hasContent("options")){
110 optionsNode = configNode.getContent("options");
111 }
112 else{
113 String repository = this.getConfigValue("repository", ContentRepository.WEBSITE);
114 String path = this.getConfigValue("path");
115 if(StringUtils.isNotEmpty(path)){
116 optionsNode = ContentUtil.getContent(repository, path);
117 }
118 }
119
120 if(optionsNode != null){
121 return ContentUtil.getAllChildren(optionsNode);
122 }
123 return new ArrayList();
124 }
125
126
127
128
129 public void init(HttpServletRequest request, HttpServletResponse response, Content websiteNode, Content configNode)
130 throws RepositoryException {
131 super.init(request, response, websiteNode, configNode);
132 if (configNode != null) {
133 setOptions(configNode);
134 }
135 }
136
137
138
139
140 public void drawHtml(Writer out) throws IOException {
141 Select control;
142 if (this.getConfigValue("valueType").equals("multiple")) {
143 List values = this.getValues();
144 if ((values == null || values.isEmpty()) && getValue() != null) {
145 values = Collections.singletonList(getValue());
146 }
147 control = new Select(this.getName(), values);
148 control.setValueType(ControlImpl.VALUETYPE_MULTIPLE);
149 }
150 else {
151 control = new Select(this.getName(), this.getValue());
152 }
153 control.setType(this.getConfigValue("type", PropertyType.TYPENAME_STRING));
154 if (this.getConfigValue("saveInfo").equals("false")) {
155 control.setSaveInfo(false);
156 }
157 control.setCssClass(CssConstants.CSSCLASS_SELECT);
158 control.setMultiple(this.getConfigValue("multiple", "false"));
159 control.setCssStyles("width", this.getConfigValue("width", "100%"));
160
161
162 for (Iterator iter = this.getOptions().iterator(); iter.hasNext();) {
163 SelectOption option = (SelectOption) iter.next();
164 option.setLabel(this.getMessage(option.getLabel()));
165 }
166 control.setOptions(this.getOptions());
167
168 this.drawHtmlPre(out);
169 out.write(control.getHtml());
170 this.drawHtmlPost(out);
171 }
172 }