View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 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.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   * @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", 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      * @see info.magnolia.cms.gui.dialog.DialogControl#init(HttpServletRequest, HttpServletResponse, Content, Content)
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      * @see info.magnolia.cms.gui.dialog.DialogControl#drawHtml(Writer)
139      */
140     public void drawHtml(Writer out) throws IOException {
141         Select control;
142         if (this.getConfigValue("valueType").equals("multiple")) { //$NON-NLS-1$ //$NON-NLS-2$
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)); //$NON-NLS-1$
154         if (this.getConfigValue("saveInfo").equals("false")) { //$NON-NLS-1$ //$NON-NLS-2$
155             control.setSaveInfo(false);
156         }
157         control.setCssClass(CssConstants.CSSCLASS_SELECT);
158         control.setMultiple(this.getConfigValue("multiple", "false")); //$NON-NLS-1$ //$NON-NLS-2$
159         control.setCssStyles("width", this.getConfigValue("width", "100%")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
160 
161         // translate (not possible in init since not a sub of the tab then)
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 }