View Javadoc

1   /**
2    * This file Copyright (c) 2008-2012 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * This dialog is loaded when control=dam is selected. It's configuration is complemented
69   * with the controls defined for each (DAMSupport) handler
70   * Once rendered this subcontrols will have as name: "dam control name + handler control name"
71   * i.e. dam control name "image" handler control name "dmsUUID"
72   * @author tmiyar
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          // TODO dlipp - quick hack
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         //TODO: would it not be more systematic to set (and keep) locale in separate variable and to be able to still retrieve unlocalized name? Maybe for 5.0 ...
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                 // since the sub name is constructed as controlName+handlerName, we need to use uni18ned name
133                 // to construct sub name and attach locale afterwards
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                     // ignore
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             //ignore
196         }
197         box.setLabel(this.getMessage(this.getLabel()));
198         box.setSaveInfo(true);
199 
200         ButtonSet control;
201         // radio
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             // very last button: close table
238             int lastIndex = selectOptions.size() - 1;
239             // avoid ArrayIndexOutOfBoundsException, but should not happen
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         // on name update made due to i18n, relay updated names also to all subs
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                 // keep the original name for later
265                 this.setConfig(ORIGINAL_NAME, originalName);
266                 // also update all subs since they have been set all by now
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 }