1 13 package info.magnolia.cms.gui.dialog; 14 15 import info.magnolia.cms.gui.control.Button; 16 import info.magnolia.cms.gui.control.ControlImpl; 17 import info.magnolia.cms.util.FreeMarkerUtil; 18 19 import java.io.IOException ; 20 import java.io.Writer ; 21 import java.util.ArrayList ; 22 import java.util.Arrays ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Map ; 27 28 import org.apache.commons.lang.BooleanUtils; 29 import org.apache.commons.lang.StringUtils; 30 31 32 37 public class DialogMultiSelect extends DialogBox { 38 39 42 public static final String SAVE_MODE_MULTIPLE = "multiple"; 43 44 47 public static final String SAVE_MODE_JSON = "json"; 48 49 52 public static final String SAVE_MODE_LIST = "list"; 53 54 57 public static final String CONFIG_SAVE_MODE = "saveMode"; 58 59 62 private static final String CONFIG_CHOOSE_ONCLICK = "chooseOnclick"; 63 64 67 private static final String CONFIG_TREE = "tree"; 68 69 72 public void drawHtml(Writer out) throws IOException { 73 this.drawHtmlPre(out); 74 out.write(FreeMarkerUtil.process(DialogMultiSelect.class, this)); 75 this.drawHtmlPost(out); 76 } 77 78 81 public String getAddButton() { 82 Button add = new Button(); 83 add.setLabel(getMessage("buttons.add")); add.setOnclick(this.getName() + "DynamicTable.addNew();"); add.setSmall(true); 86 return add.getHtml(); 87 } 88 89 92 public String getChooseButton() { 93 94 String chooseOnclick = this.getConfigValue(CONFIG_CHOOSE_ONCLICK); 95 if(StringUtils.isEmpty(chooseOnclick)){ 96 String tree = this.getConfigValue(CONFIG_TREE); 97 if(StringUtils.isNotEmpty(tree)){ 98 chooseOnclick = "mgnlOpenTreeBrowserWithControl($('${prefix}'), '" + tree + "');"; 99 100 } 101 } 102 103 if (StringUtils.isNotEmpty(chooseOnclick)) { 104 Button choose = new Button(); 105 choose.setLabel(this.getMessage("buttons.choose")); choose.setOnclick(chooseOnclick); 107 108 choose.setSmall(true); 109 return choose.getHtml(); 110 } 111 return ""; 112 } 113 114 117 public String getDeleteButton() { 118 Button delete = new Button(); 119 delete.setLabel(this.getMessage("buttons.delete")); delete 121 .setOnclick(this.getName() + "DynamicTable.del('${index}');" + this.getName() + "DynamicTable.persist();"); delete.setSmall(true); 123 return delete.getHtml(); 124 } 125 126 129 public String getInnerHtml() { 130 String name = "/" + StringUtils.replace(DialogMultiSelect.class.getName(), ".", "/") + "Inner.html"; 131 Map map = new HashMap (); 132 map.put("this", this); 133 return FreeMarkerUtil.process(name, map); 134 } 135 136 139 public String getGetObjectFunction() { 140 return "function(prefix, index){return {value: $(prefix).value }}"; 141 } 142 143 146 public String getNewObjectFunction() { 147 return "function(){return {value: ''}}"; 148 } 149 150 153 public String getJSON() { 154 if (this.isSaveAsJSON()) { 155 return this.getValue(); 156 } 157 158 List values; 159 if (this.isSaveAsList()) { 160 values = Arrays.asList(this.getValue().split(",")); 161 } 162 else { 163 values = this.getValues(); 164 } 165 166 if (values.size() == 0) { 167 return "[{value:''}]"; 168 } 169 170 List objects = new ArrayList (); 171 for (Iterator iter = values.iterator(); iter.hasNext();) { 172 String value = (String ) iter.next(); 173 objects.add("{value: '" + value + "'}"); 174 } 175 return "[" + StringUtils.join(objects.iterator(), ",") + "]"; 176 } 177 178 public String getSaveInfo() { 179 Boolean renderSaveInfo = BooleanUtils.toBooleanObject(this.getConfigValue("saveInfo")); 180 if (BooleanUtils.toBooleanDefaultIfNull(renderSaveInfo, true)) { 181 ControlImpl dummy = new ControlImpl(this.getName(), (String ) null); 182 if (!isSaveAsList() && !isSaveAsJSON()) { 183 dummy.setValueType(ControlImpl.VALUETYPE_MULTIPLE); 184 } 185 return dummy.getHtmlSaveInfo(); 186 } 187 return ""; 189 } 190 191 public boolean isSaveAsList() { 192 return StringUtils.equals(this.getConfigValue(CONFIG_SAVE_MODE), SAVE_MODE_LIST); 193 } 194 195 public boolean isSaveAsJSON() { 196 return StringUtils.equals(this.getConfigValue(CONFIG_SAVE_MODE), SAVE_MODE_JSON); 197 } 198 199 204 public String getHiddenFieldName() { 205 if (this.isSaveAsList()) { 206 return this.getName(); 207 } 208 209 return this.getName() + "Persisted"; 210 } 211 212 } 213 | Popular Tags |