1 13 package info.magnolia.cms.gui.dialog; 14 15 import info.magnolia.cms.core.Content; 16 import info.magnolia.cms.core.ContentHandler; 17 import info.magnolia.cms.core.ItemType; 18 import info.magnolia.cms.gui.control.Button; 19 import info.magnolia.cms.gui.control.ButtonSet; 20 import info.magnolia.cms.gui.control.ControlSuper; 21 import info.magnolia.cms.gui.control.Hidden; 22 import info.magnolia.cms.gui.misc.CssConstants; 23 import info.magnolia.cms.i18n.TemplateMessagesUtil; 24 25 import java.io.IOException ; 26 import java.io.Writer ; 27 import java.util.ArrayList ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 31 import javax.jcr.PropertyType; 32 import javax.jcr.RepositoryException; 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.http.HttpServletResponse ; 35 36 import org.apache.commons.lang.StringUtils; 37 import org.apache.log4j.Logger; 38 39 40 44 public class DialogButtonSet extends DialogBox { 45 46 49 private static Logger log = Logger.getLogger(DialogButtonSet.class); 50 51 private int buttonType = ControlSuper.BUTTONTYPE_RADIO; 52 53 56 protected DialogButtonSet() { 57 } 58 59 public void setOptions(Content configNode, boolean setDefaultSelected) { 60 List options = new ArrayList (); 63 try { 64 Iterator it = configNode.getContent("options") .getChildren(ItemType.CONTENTNODE.getSystemName(), ContentHandler.SORT_BY_SEQUENCE).iterator(); 66 while (it.hasNext()) { 67 Content n = ((Content) it.next()); 68 String value = n.getNodeData("value").getString(); String label = n.getNodeData("label").getString(); label = TemplateMessagesUtil.get(this.getRequest(), label); 71 Button button = new Button(this.getName(), value); 72 button.setLabel(label); 74 75 String iconSrc = n.getNodeData("iconSrc").getString(); if (StringUtils.isNotEmpty(iconSrc)) { 77 button.setIconSrc(iconSrc); 78 } 79 80 if (setDefaultSelected && n.getNodeData("selected").getBoolean()) { button.setState(ControlSuper.BUTTONSTATE_PUSHED); 82 } 83 options.add(button); 84 } 85 } 86 catch (RepositoryException e) { 87 log.debug("Exception caught: " + e.getMessage(), e); } 89 this.setOptions(options); 90 } 91 92 public void setOption(Content configNode) { 93 List options = new ArrayList (); 95 Button button = new Button(this.getName() + "_dummy", StringUtils.EMPTY); String label = configNode.getNodeData("buttonLabel").getString(); label = TemplateMessagesUtil.get(this.getRequest(), label); 98 button.setLabel(label); 99 100 if (configNode.getNodeData("selected").getBoolean()) { button.setState(ControlSuper.BUTTONSTATE_PUSHED); 102 } 103 104 button.setValue("true"); button.setOnclick("mgnlDialogShiftCheckboxSwitch('" + this.getName() + "');"); options.add(button); 107 this.setOptions(options); 108 } 109 110 113 public void init(HttpServletRequest request, HttpServletResponse response, Content websiteNode, Content configNode) 114 throws RepositoryException { 115 super.init(request, response, websiteNode, configNode); 116 117 if (configNode != null) { 119 String controlType = configNode.getNodeData("controlType").getString(); 121 if (log.isDebugEnabled()) { 122 log.debug("Init DialogButtonSet with type=" + controlType); } 124 125 if (controlType.equals("radio")) { setButtonType(ControlSuper.BUTTONTYPE_RADIO); 128 setOptions(configNode, true); 129 } 130 else if (controlType.equals("checkbox")) { setButtonType(ControlSuper.BUTTONTYPE_CHECKBOX); 132 setOptions(configNode, false); 133 setConfig("valueType", "multiple"); } 135 else if (controlType.equals("checkboxSwitch")) { setButtonType(ControlSuper.BUTTONTYPE_CHECKBOX); 137 setOption(configNode); 138 } 139 } 140 } 141 142 public void drawHtmlPreSubs(Writer out) throws IOException { 143 this.drawHtmlPre(out); 144 } 145 146 public void drawHtmlPostSubs(Writer out) throws IOException { 147 this.drawHtmlPost(out); 148 } 149 150 public void setButtonType(int i) { 151 this.buttonType = i; 152 } 153 154 public int getButtonType() { 155 return this.buttonType; 156 } 157 158 161 public void drawHtml(Writer out) throws IOException { 162 this.drawHtmlPre(out); 163 ButtonSet control; 164 if (this.getConfigValue("valueType").equals("multiple")) { control = new ButtonSet(this.getName(), this.getValues()); 167 control.setValueType(ControlSuper.VALUETYPE_MULTIPLE); 168 } 169 else if (this.getButtonType() == ControlSuper.BUTTONTYPE_CHECKBOX) { 170 control = new ButtonSet(this.getName() + "_SWITCH", this.getValue()); } 173 else { 174 control = new ButtonSet(this.getName(), this.getValue()); 176 } 177 control.setButtonType(this.getButtonType()); 178 179 control.setCssClass(this.getConfigValue("cssClass", CssConstants.CSSCLASS_BUTTONSETBUTTON)); 182 if (this.getConfigValue("saveInfo").equals("false")) { control.setSaveInfo(false); 184 } 185 control.setType(this.getConfigValue("type", PropertyType.TYPENAME_STRING)); String width = this.getConfigValue("width", null); control.setButtonHtmlPre("<tr><td class=\"" + CssConstants.CSSCLASS_BUTTONSETBUTTON + "\">"); control.setButtonHtmlInter("</td><td class=\"" + CssConstants.CSSCLASS_BUTTONSETLABEL + "\">"); control.setButtonHtmlPost("</td></tr>"); int cols = Integer.valueOf(this.getConfigValue("cols", "1")).intValue(); if (cols > 1) { 192 width = "100%"; control.setHtmlPre(control.getHtmlPre() + "<tr>"); control.setHtmlPost("</tr>" + control.getHtmlPost()); int item = 1; 196 int itemsPerCol = (int) Math.ceil(this.getOptions().size() / ((double) cols)); 197 for (int i = 0; i < this.getOptions().size(); i++) { 198 Button b = (Button) this.getOptions().get(i); 199 if (item == 1) { 200 b.setHtmlPre("<td><table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">" + control.getButtonHtmlPre()); 202 } 203 if (item == itemsPerCol) { 204 b.setHtmlPost(control.getButtonHtmlPost() + "</table></td><td class=\"" + CssConstants.CSSCLASS_BUTTONSETINTERCOL + "\"></td>"); item = 1; 207 } 208 else { 209 item++; 210 } 211 } 212 int lastIndex = this.getOptions().size() - 1; 214 if (lastIndex > -1) { 216 ((Button) this.getOptions().get(lastIndex)).setHtmlPost(control.getButtonHtmlPost() + "</table>"); } 218 } 219 if (width != null) { 220 control.setHtmlPre("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"" + width + "\">"); } 222 control.setButtons(this.getOptions()); 223 out.write(control.getHtml()); 224 if (control.getButtonType() == ControlSuper.BUTTONTYPE_CHECKBOX 225 && control.getValueType() != ControlSuper.VALUETYPE_MULTIPLE) { 226 String value = this.getValue(); 228 if (StringUtils.isEmpty(value)) { 229 if (this.getConfigValue("selected").equals("true")) { value = "true"; } 232 else { 233 value = "false"; } 235 } 236 out.write(new Hidden(this.getName(), value).getHtml()); 237 } 238 this.drawHtmlPost(out); 239 } 240 } 241 | Popular Tags |