View Javadoc

1   /**
2    * This file Copyright (c) 2003-2013 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.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   * @author Vinzenz Wyser
67   * @version 2.0
68   */
69  public class DialogSelect extends DialogBox {
70  
71      /**
72       * Logger.
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(); //$NON-NLS-1$
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);//$NON-NLS-1$
90  
91                  SelectOption option = new SelectOption(label, value);
92                  if (n.getNodeData("selected").getBoolean()) { //$NON-NLS-1$
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); //$NON-NLS-1$
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"); //$NON-NLS-1$
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      * @see info.magnolia.cms.gui.dialog.DialogControl#init(HttpServletRequest, HttpServletResponse, Content, Content)
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      * @see info.magnolia.cms.gui.dialog.DialogControl#drawHtml(Writer)
140      */
141     @Override
142     public void drawHtml(Writer out) throws IOException {
143         Select control;
144         if (this.getConfigValue("valueType").equals("multiple")) { //$NON-NLS-1$ //$NON-NLS-2$
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)); //$NON-NLS-1$
156         if (this.getConfigValue("saveInfo").equals("false")) { //$NON-NLS-1$ //$NON-NLS-2$
157             control.setSaveInfo(false);
158         }
159         control.setCssClass(CssConstants.CSSCLASS_SELECT);
160         control.setMultiple(this.getConfigValue("multiple", "false")); //$NON-NLS-1$ //$NON-NLS-2$
161         control.setCssStyles("width", this.getConfigValue("width", "100%")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
162 
163         // translate (not possible in init since not a sub of the tab then)
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 }