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.module.templatingkit.dam.dialog;
35
36 import info.magnolia.cms.core.Content;
37 import info.magnolia.cms.gui.control.Button;
38 import info.magnolia.cms.gui.control.ButtonSet;
39 import info.magnolia.cms.gui.control.ControlImpl;
40 import info.magnolia.cms.gui.control.Hidden;
41 import info.magnolia.cms.gui.dialog.DialogBox;
42 import info.magnolia.cms.gui.dialog.DialogButtonSet;
43 import info.magnolia.cms.gui.dialog.DialogControl;
44 import info.magnolia.cms.gui.dialog.DialogControlImpl;
45 import info.magnolia.cms.gui.dialog.DialogFactory;
46 import info.magnolia.cms.gui.misc.CssConstants;
47 import info.magnolia.module.templatingkit.STKModule;
48 import info.magnolia.module.templatingkit.dam.DAMHandler;
49 import info.magnolia.objectfactory.Components;
50
51 import java.io.IOException;
52 import java.io.Writer;
53 import java.util.ArrayList;
54 import java.util.Collection;
55 import java.util.Iterator;
56 import java.util.List;
57
58 import javax.jcr.PropertyType;
59 import javax.jcr.RepositoryException;
60 import javax.servlet.http.HttpServletRequest;
61 import javax.servlet.http.HttpServletResponse;
62
63 import org.apache.commons.lang.StringUtils;
64 import org.slf4j.Logger;
65 import org.slf4j.LoggerFactory;
66
67
68
69
70
71
72
73
74
75 public class DialogDAM extends DialogControlImpl {
76
77 private static final String ORIGINAL_NAME = "originalName";
78 private static final Logger log = LoggerFactory.getLogger(DialogDAM.class);
79 private DialogBox box;
80 private Collection<DAMHandler> handlers = null;
81
82 @Override
83 public void init(HttpServletRequest request, HttpServletResponse response,
84 Content storageNode, Content configNode) throws RepositoryException {
85
86 super.init(request, response, storageNode, configNode);
87
88 this.handlers = Components.getComponent(STKModule.class).getDamSupport().getHandlers().values();
89 loadSubs();
90 }
91
92 @Override
93 public void drawHtmlPreSubs(Writer out) throws IOException {
94 if(handlers.size() > 1) {
95 drawRadio(out);
96 }
97 else if(handlers.size() == 1){
98 Hidden control = new Hidden(getName(), handlers.iterator().next().getName());
99 out.write(control.getHtml());
100 }
101 }
102
103 @Override
104 protected void drawSubs(Writer out) throws IOException {
105 String aName = this.getName();
106
107 String originalName = this.getConfigValue(ORIGINAL_NAME);
108 String locale = "";
109 if (!StringUtils.isBlank(originalName)) {
110 locale = StringUtils.substringAfter(aName, originalName);
111 if (locale.startsWith("_")) {
112 aName = originalName;
113 } else {
114 locale = "";
115 }
116 }
117
118 for(DAMHandler handler: handlers) {
119
120 if(handlers.size() > 1) {
121 String style = "style=\"display:none;\"";
122 out.write("<div id=\"" + aName + locale + "_" + handler.getName() + "_dam_div\" " + style + " >");
123
124 out.write("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" style=\"table-layout:fixed\" >");
125 out.write("<col width=\"200\" /><col />");
126
127 }
128 Iterator it = handler.getControls().iterator();
129
130 while (it.hasNext()) {
131 Content controlNodeConfig = (Content) it.next();
132
133
134 DialogControlImpl sub = this.getSub(aName + controlNodeConfig.getName() + locale);
135 if (sub == null) {
136 sub = this.getSub(aName + controlNodeConfig.getName() );
137 }
138 sub.drawHtml(out);
139 }
140
141 if(handlers.size() > 1) {
142 out.write("</table></div>");
143 }
144 }
145 }
146
147 private void loadSubs() {
148 for(DAMHandler handler: handlers) {
149
150 for (Object c : handler.getControls()) {
151 Content controlNodeConfig = (Content) c;
152 try {
153 DialogControl control = DialogFactory.loadDialog(this.getRequest(), this.getResponse(), this.getStorageNode(),
154 controlNodeConfig);
155 String name = ((DialogControlImpl) control).getName();
156 ((DialogControlImpl) control).setName(this.getName() + name);
157
158 this.addSub(control);
159
160 } catch (RepositoryException e) {
161
162 log.debug(e.getMessage(), e);
163 }
164 }
165 }
166 }
167
168 @Override
169 public void drawHtmlPostSubs(Writer out) throws IOException {
170 if(box != null) {
171 out.append("<script type=\"text/javascript\"> ");
172 out.append("mgnl.dam.DAMDialog.onSelectionChanged('" + this.getName() + "','" + this.getValue() + "');");
173 out.append("</script>");
174 box.drawHtmlPost(out);
175 }
176 }
177
178 private List<Button> getDamRadioOptions() {
179 List<Button> selectOptions = new ArrayList<Button>();
180 for(DAMHandler handler: handlers) {
181 Button option = new Button(this.getName(), handler.getName());
182 option.setLabel(this.getMessage(handler.getDamSelectorOptionLabel()));
183 option.setOnclick("mgnl.dam.DAMDialog.onSelectionChanged('" + this.getName() + "','" + handler.getName() + "');");
184 selectOptions.add(option);
185 }
186 return selectOptions;
187 }
188
189 public void drawRadio(Writer out) throws IOException {
190
191 box = new DialogButtonSet();
192 try {
193 box.init(this.getRequest(), this.getResponse(), null, null);
194 } catch (RepositoryException e) {
195
196 }
197 box.setLabel(this.getMessage(this.getLabel()));
198 box.setSaveInfo(true);
199
200 ButtonSet control;
201
202 control = new ButtonSet(this.getName(), this.getValue());
203
204 control.setButtonType(ControlImpl.BUTTONTYPE_RADIO);
205
206 control.setCssClass(this.getConfigValue("cssClass", CssConstants.CSSCLASS_BUTTONSETBUTTON));
207
208 control.setSaveInfo(true);
209
210 control.setType(PropertyType.TYPENAME_STRING);
211
212 control.setButtonHtmlPre("<tr><td class=\"" + CssConstants.CSSCLASS_BUTTONSETBUTTON + "\">");
213 control.setButtonHtmlInter("</td><td class=\"" + CssConstants.CSSCLASS_BUTTONSETLABEL + "\">");
214 control.setButtonHtmlPost("</td></tr>");
215 List<Button> selectOptions = getDamRadioOptions();
216 int cols = selectOptions.size();
217 if (cols > 1) {
218 control.setHtmlPre(control.getHtmlPre() + "<tr>");
219 control.setHtmlPost("</tr>" + control.getHtmlPost());
220 int item = 1;
221 int itemsPerCol = (int) Math.ceil(selectOptions.size() / ((double) cols));
222 for (Object selectOption : selectOptions) {
223 Button b = (Button) selectOption;
224 if (item == 1) {
225 b.setHtmlPre("<td><table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" >"
226 + control.getButtonHtmlPre());
227 }
228 if (item == itemsPerCol) {
229 b.setHtmlPost(control.getButtonHtmlPost() + "</table></td><td class=\""
230 + CssConstants.CSSCLASS_BUTTONSETINTERCOL
231 + "\"></td>");
232 item = 1;
233 } else {
234 item++;
235 }
236 }
237
238 int lastIndex = selectOptions.size() - 1;
239
240 if (lastIndex > -1) {
241 selectOptions.get(lastIndex).setHtmlPost(control.getButtonHtmlPost() + "</table>");
242 }
243 }
244
245 int width = 100;
246 if(cols < 5) {
247 width = cols * 20;
248 }
249 control.setHtmlPre("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"" + width + "%\">");
250
251 control.setButtons(selectOptions);
252 box.drawHtmlPre(out);
253 out.write(control.getHtml());
254
255 }
256
257 @Override
258 public void setName(String s) {
259
260 if (!StringUtils.isBlank(s)) {
261 String originalName = super.getName();
262 String locale = StringUtils.substringAfter(s, originalName);
263 if (!StringUtils.isBlank(originalName) && locale.startsWith("_")) {
264
265 this.setConfig(ORIGINAL_NAME, originalName);
266
267 List<DialogControlImpl> subs = this.getSubs();
268 for (DialogControlImpl sub: subs) {
269 sub.setName(sub.getName()+locale);
270 }
271 }
272 }
273 super.setName(s);
274 }
275 }