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.core.Content;
37 import info.magnolia.cms.gui.control.ControlImpl;
38 import info.magnolia.cms.gui.control.Select;
39 import info.magnolia.cms.gui.control.SelectOption;
40 import info.magnolia.cms.gui.misc.CssConstants;
41 import info.magnolia.cms.security.AccessDeniedException;
42 import info.magnolia.cms.util.ContentUtil;
43 import info.magnolia.cms.util.NodeDataUtil;
44 import info.magnolia.repository.RepositoryConstants;
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", RepositoryConstants.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 @Override
130 public void init(HttpServletRequest request, HttpServletResponse response, Content websiteNode, Content configNode)
131 throws RepositoryException {
132 super.init(request, response, websiteNode, configNode);
133 if (configNode != null) {
134 setOptions(configNode);
135 }
136 }
137
138
139
140
141 @Override
142 public void drawHtml(Writer out) throws IOException {
143 Select control;
144 if (this.getConfigValue("valueType").equals("multiple")) {
145 List values = this.getValues();
146 if ((values == null || values.isEmpty()) && getValue() != null) {
147 values = Collections.singletonList(getValue());
148 }
149 control = new Select(this.getName(), values);
150 control.setValueType(ControlImpl.VALUETYPE_MULTIPLE);
151 }
152 else {
153 control = new Select(this.getName(), this.getValue());
154 }
155 control.setType(this.getConfigValue("type", PropertyType.TYPENAME_STRING));
156 if (this.getConfigValue("saveInfo").equals("false")) {
157 control.setSaveInfo(false);
158 }
159 control.setCssClass(CssConstants.CSSCLASS_SELECT);
160 control.setMultiple(this.getConfigValue("multiple", "false"));
161 control.setCssStyles("width", this.getConfigValue("width", "100%"));
162
163
164 for (Iterator iter = this.getOptions().iterator(); iter.hasNext();) {
165 SelectOption option = (SelectOption) iter.next();
166 option.setLabel(this.getMessage(option.getLabel()));
167 }
168 control.setOptions(this.getOptions());
169
170 this.drawHtmlPre(out);
171 out.write(control.getHtml());
172 this.drawHtmlPost(out);
173 }
174 }