1 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 ; 25 import java.io.Writer ; 26 import java.util.ArrayList ; 27 import java.util.Collection ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 31 import javax.jcr.PathNotFoundException; 32 import javax.jcr.PropertyType; 33 import javax.jcr.RepositoryException; 34 import javax.servlet.http.HttpServletRequest ; 35 import javax.servlet.http.HttpServletResponse ; 36 37 import org.apache.commons.lang.StringUtils; 38 import org.slf4j.Logger; 39 import org.slf4j.LoggerFactory; 40 41 42 46 public class DialogSelect extends DialogBox { 47 48 51 private static Logger log = LoggerFactory.getLogger(DialogSelect.class); 52 53 56 protected DialogSelect() { 57 } 58 59 public void setOptions(Content configNode) { 60 List options = new ArrayList (); 61 try { 62 Iterator it = this.getOptionNodes(configNode).iterator(); while (it.hasNext()) { 64 Content n = (Content) it.next(); 65 String valueNodeData = this.getConfigValue("valueNodeData", "value"); 66 String labelNodeData = this.getConfigValue("labelNodeData", "label"); 67 68 String value = NodeDataUtil.getString(n, valueNodeData); String label = NodeDataUtil.getString(n, labelNodeData, value); 71 SelectOption option = new SelectOption(label, value); 72 if (n.getNodeData("selected").getBoolean()) { 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); } 82 } 83 this.setOptions(options); 84 } 85 86 protected Collection getOptionNodes(Content configNode) throws PathNotFoundException, RepositoryException, AccessDeniedException { 87 Content optionsNode = null; 88 89 if(configNode.hasContent("options")){ 90 optionsNode = configNode.getContent("options"); } 92 else{ 93 String repository = this.getConfigValue("repository", ContentRepository.WEBSITE); 94 String 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 (); 104 } 105 106 109 public void init(HttpServletRequest request, HttpServletResponse 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 120 public void drawHtml(Writer out) throws IOException { 121 Select control = new Select(this.getName(), this.getValue()); 122 control.setType(this.getConfigValue("type", PropertyType.TYPENAME_STRING)); if (this.getConfigValue("saveInfo").equals("false")) { control.setSaveInfo(false); 125 } 126 control.setCssClass(CssConstants.CSSCLASS_SELECT); 127 control.setCssStyles("width", this.getConfigValue("width", "100%")); 129 for (Iterator 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 |