1 24 package org.riotfamily.forms.element.select; 25 26 import java.io.PrintWriter ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 30 import org.riotfamily.common.markup.Html; 31 import org.riotfamily.common.markup.TagWriter; 32 33 34 37 public class SelectBox extends AbstractSingleSelectElement { 38 39 private static final String DEFAULT_CHOOSE_LABEL_KEY = 40 "label.selectBox.choose"; 41 42 private String chooseLabelKey; 43 44 private String chooseLabel; 45 46 public SelectBox() { 47 setOptionRenderer(new OptionTagRenderer()); 48 } 49 50 54 public void setShowChooseOption(boolean show) { 55 if (show) { 56 chooseLabelKey = DEFAULT_CHOOSE_LABEL_KEY; 57 } 58 else { 59 chooseLabelKey = null; 60 chooseLabel = null; 61 } 62 } 63 64 public void setChooseLabel(String chooseLabel) { 65 this.chooseLabel = chooseLabel; 66 } 67 68 public void setChooseLabelKey(String chooseLabelKey) { 69 this.chooseLabelKey = chooseLabelKey; 70 } 71 72 protected List createOptions() { 73 List options = super.createOptions(); 74 if (chooseLabelKey != null) { 75 chooseLabel = getFormContext().getMessageResolver() 76 .getMessage(chooseLabelKey); 77 } 78 if (chooseLabel != null) { 79 Option chooseOption = new Option(null, null, chooseLabel, this); 80 options.add(0, chooseOption); 81 } 82 return options; 83 } 84 85 public void renderInternal(PrintWriter writer) { 86 TagWriter selectTag = new TagWriter(writer); 87 88 selectTag.start(Html.SELECT); 89 selectTag.attribute(Html.INPUT_NAME, getParamName()); 90 selectTag.attribute(Html.COMMON_ID, getId()); 91 selectTag.attribute(Html.SELECT_SIZE, 1); 92 selectTag.body(); 93 94 Iterator it = getOptions().iterator(); 95 while (it.hasNext()) { 96 ((Option) it.next()).render(); 97 } 98 99 selectTag.end(); 100 } 101 102 } 103 | Popular Tags |