KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > gui > dialog > DialogSelect


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.gui.dialog;
14
15 import info.magnolia.cms.beans.config.ContentRepository;
16 import info.magnolia.cms.core.Content;
17 import info.magnolia.cms.gui.control.Select;
18 import info.magnolia.cms.gui.control.SelectOption;
19 import info.magnolia.cms.gui.misc.CssConstants;
20 import info.magnolia.cms.security.AccessDeniedException;
21 import info.magnolia.cms.util.ContentUtil;
22 import info.magnolia.cms.util.NodeDataUtil;
23
24 import java.io.IOException JavaDoc;
25 import java.io.Writer JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30
31 import javax.jcr.PathNotFoundException;
32 import javax.jcr.PropertyType;
33 import javax.jcr.RepositoryException;
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35 import javax.servlet.http.HttpServletResponse JavaDoc;
36
37 import org.apache.commons.lang.StringUtils;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41
42 /**
43  * @author Vinzenz Wyser
44  * @version 2.0
45  */

46 public class DialogSelect extends DialogBox {
47
48     /**
49      * Logger.
50      */

51     private static Logger log = LoggerFactory.getLogger(DialogSelect.class);
52
53     /**
54      * Empty constructor should only be used by DialogFactory.
55      */

56     protected DialogSelect() {
57     }
58
59     public void setOptions(Content configNode) {
60         List JavaDoc options = new ArrayList JavaDoc();
61         try {
62             Iterator JavaDoc it = this.getOptionNodes(configNode).iterator(); //$NON-NLS-1$
63
while (it.hasNext()) {
64                 Content n = (Content) it.next();
65                 String JavaDoc valueNodeData = this.getConfigValue("valueNodeData", "value");
66                 String JavaDoc labelNodeData = this.getConfigValue("labelNodeData", "label");
67
68                 String JavaDoc value = NodeDataUtil.getString(n, valueNodeData);//$NON-NLS-1$
69
String JavaDoc label = NodeDataUtil.getString(n, labelNodeData, value);//$NON-NLS-1$
70

71                 SelectOption option = new SelectOption(label, value);
72                 if (n.getNodeData("selected").getBoolean()) { //$NON-NLS-1$
73
option.setSelected(true);
74                 }
75                 options.add(option);
76             }
77         }
78         catch (RepositoryException e) {
79             if (log.isDebugEnabled()) {
80                 log.debug("Exception caught: " + e.getMessage(), e); //$NON-NLS-1$
81
}
82         }
83         this.setOptions(options);
84     }
85     
86     protected Collection JavaDoc getOptionNodes(Content configNode) throws PathNotFoundException, RepositoryException, AccessDeniedException {
87         Content optionsNode = null;
88         
89         if(configNode.hasContent("options")){
90             optionsNode = configNode.getContent("options"); //$NON-NLS-1$
91
}
92         else{
93             String JavaDoc repository = this.getConfigValue("repository", ContentRepository.WEBSITE);
94             String JavaDoc path = this.getConfigValue("path");
95             if(StringUtils.isNotEmpty(path)){
96                 optionsNode = ContentUtil.getContent(repository, path);
97             }
98         }
99         
100         if(optionsNode != null){
101             return ContentUtil.getAllChildren(optionsNode);
102         }
103         return new ArrayList JavaDoc();
104     }
105
106     /**
107      * @see info.magnolia.cms.gui.dialog.DialogControl#init(HttpServletRequest, HttpServletResponse, Content, Content)
108      */

109     public void init(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, Content websiteNode, Content configNode)
110         throws RepositoryException {
111         super.init(request, response, websiteNode, configNode);
112         if (configNode != null) {
113             setOptions(configNode);
114         }
115     }
116
117     /**
118      * @see info.magnolia.cms.gui.dialog.DialogControl#drawHtml(Writer)
119      */

120     public void drawHtml(Writer JavaDoc out) throws IOException JavaDoc {
121         Select control = new Select(this.getName(), this.getValue());
122         control.setType(this.getConfigValue("type", PropertyType.TYPENAME_STRING)); //$NON-NLS-1$
123
if (this.getConfigValue("saveInfo").equals("false")) { //$NON-NLS-1$ //$NON-NLS-2$
124
control.setSaveInfo(false);
125         }
126         control.setCssClass(CssConstants.CSSCLASS_SELECT);
127         control.setCssStyles("width", this.getConfigValue("width", "100%")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
128

129         // translate (not possible in init since not a sub of the tab then)
130
for (Iterator JavaDoc iter = this.getOptions().iterator(); iter.hasNext();) {
131             SelectOption option = (SelectOption) iter.next();
132             control.setOptions(this.getMessage(option.getLabel()), option.getValue());
133         }
134
135         this.drawHtmlPre(out);
136         out.write(control.getHtml());
137         this.drawHtmlPost(out);
138     }
139 }
Popular Tags