@Test public void testSingleOption() throws Exception { DropdownWidget widget = new DropdownWidget(); List<Option> option = new ArrayList<Option>(); option.add(new Option("label1", "value1", false)); widget.setOptions(option); widget.setInitialValue("choose one"); FormEntryContext formEntryContext = mock(FormEntryContext.class); when(formEntryContext.getFieldName(widget)).thenReturn("w1"); String html = widget.generateHtml(formEntryContext); assertThat(html, is("<select id=\"w1\" name=\"w1\"><option value=\"value1\">label1</option></select>")); }
@Override public String generateHtml(FormEntryContext context) { StringBuilder sb = new StringBuilder(); if (context.getMode() == Mode.VIEW) { String toPrint = ""; if (getInitialValue() != null) { boolean found = false; for (Option o : getOptions()) { if (getInitialValue().equals(o.getValue())) { toPrint = o.getLabel(); found = true; break; } } if (!found) toPrint = getInitialValue(); return WidgetFactory.displayValue(toPrint); } else { return WidgetFactory.displayDefaultEmptyValue(); } } else { String id = context.getFieldName(this); sb.append("<select id=\"" + id + "\" name=\"" + id + "\""); if (size != null) { if (size == 999) { size = getOptions().size() + 1; } sb.append(" size=").append("\"" + size.intValue() + "\""); } sb.append(">"); for (int i = 0; i < getOptions().size(); ++i) { Option option = getOptions().get(i); boolean selected = option.isSelected(); if (!selected) selected = getInitialValue() == null ? option.getValue().equals("") : getInitialValue().equals(option.getValue()); sb.append("<option value=\"").append(option.getValue()).append("\""); if (selected) sb.append(" selected=\"true\""); sb.append(">"); sb.append(option.getLabel()); sb.append("</option>"); } sb.append("</select>"); return sb.toString(); } }
DropdownWidget extends SingleOptionWidget { @Override public String generateHtml(FormEntryContext context) { StringBuilder sb = new StringBuilder(); if (context.getMode() == Mode.VIEW) { String toPrint = ""; if (getInitialValue() != null) { boolean found = false; for (Option o : getOptions()) { if (getInitialValue().equals(o.getValue())) { toPrint = o.getLabel(); found = true; break; } } if (!found) toPrint = getInitialValue(); return WidgetFactory.displayValue(toPrint); } else { return WidgetFactory.displayDefaultEmptyValue(); } } else { String id = context.getFieldName(this); sb.append("<select id=\"" + id + "\" name=\"" + id + "\""); if (size != null) { if (size == 999) { size = getOptions().size() + 1; } sb.append(" size=").append("\"" + size.intValue() + "\""); } sb.append(">"); for (int i = 0; i < getOptions().size(); ++i) { Option option = getOptions().get(i); boolean selected = option.isSelected(); if (!selected) selected = getInitialValue() == null ? option.getValue().equals("") : getInitialValue().equals(option.getValue()); sb.append("<option value=\"").append(option.getValue()).append("\""); if (selected) sb.append(" selected=\"true\""); sb.append(">"); sb.append(option.getLabel()); sb.append("</option>"); } sb.append("</select>"); return sb.toString(); } } }
DropdownWidget extends SingleOptionWidget { @Override public String generateHtml(FormEntryContext context) { StringBuilder sb = new StringBuilder(); if (context.getMode() == Mode.VIEW) { String toPrint = ""; if (getInitialValue() != null) { boolean found = false; for (Option o : getOptions()) { if (getInitialValue().equals(o.getValue())) { toPrint = o.getLabel(); found = true; break; } } if (!found) toPrint = getInitialValue(); return WidgetFactory.displayValue(toPrint); } else { return WidgetFactory.displayDefaultEmptyValue(); } } else { String id = context.getFieldName(this); sb.append("<select id=\"" + id + "\" name=\"" + id + "\""); if (size != null) { if (size == 999) { size = getOptions().size() + 1; } sb.append(" size=").append("\"" + size.intValue() + "\""); } sb.append(">"); for (int i = 0; i < getOptions().size(); ++i) { Option option = getOptions().get(i); boolean selected = option.isSelected(); if (!selected) selected = getInitialValue() == null ? option.getValue().equals("") : getInitialValue().equals(option.getValue()); sb.append("<option value=\"").append(option.getValue()).append("\""); if (selected) sb.append(" selected=\"true\""); sb.append(">"); sb.append(option.getLabel()); sb.append("</option>"); } sb.append("</select>"); return sb.toString(); } } DropdownWidget();  DropdownWidget(Integer size); }
DropdownWidget extends SingleOptionWidget { @Override public String generateHtml(FormEntryContext context) { StringBuilder sb = new StringBuilder(); if (context.getMode() == Mode.VIEW) { String toPrint = ""; if (getInitialValue() != null) { boolean found = false; for (Option o : getOptions()) { if (getInitialValue().equals(o.getValue())) { toPrint = o.getLabel(); found = true; break; } } if (!found) toPrint = getInitialValue(); return WidgetFactory.displayValue(toPrint); } else { return WidgetFactory.displayDefaultEmptyValue(); } } else { String id = context.getFieldName(this); sb.append("<select id=\"" + id + "\" name=\"" + id + "\""); if (size != null) { if (size == 999) { size = getOptions().size() + 1; } sb.append(" size=").append("\"" + size.intValue() + "\""); } sb.append(">"); for (int i = 0; i < getOptions().size(); ++i) { Option option = getOptions().get(i); boolean selected = option.isSelected(); if (!selected) selected = getInitialValue() == null ? option.getValue().equals("") : getInitialValue().equals(option.getValue()); sb.append("<option value=\"").append(option.getValue()).append("\""); if (selected) sb.append(" selected=\"true\""); sb.append(">"); sb.append(option.getLabel()); sb.append("</option>"); } sb.append("</select>"); return sb.toString(); } } DropdownWidget();  DropdownWidget(Integer size); @Override String generateHtml(FormEntryContext context); DropdownWidget clone(); }
DropdownWidget extends SingleOptionWidget { @Override public String generateHtml(FormEntryContext context) { StringBuilder sb = new StringBuilder(); if (context.getMode() == Mode.VIEW) { String toPrint = ""; if (getInitialValue() != null) { boolean found = false; for (Option o : getOptions()) { if (getInitialValue().equals(o.getValue())) { toPrint = o.getLabel(); found = true; break; } } if (!found) toPrint = getInitialValue(); return WidgetFactory.displayValue(toPrint); } else { return WidgetFactory.displayDefaultEmptyValue(); } } else { String id = context.getFieldName(this); sb.append("<select id=\"" + id + "\" name=\"" + id + "\""); if (size != null) { if (size == 999) { size = getOptions().size() + 1; } sb.append(" size=").append("\"" + size.intValue() + "\""); } sb.append(">"); for (int i = 0; i < getOptions().size(); ++i) { Option option = getOptions().get(i); boolean selected = option.isSelected(); if (!selected) selected = getInitialValue() == null ? option.getValue().equals("") : getInitialValue().equals(option.getValue()); sb.append("<option value=\"").append(option.getValue()).append("\""); if (selected) sb.append(" selected=\"true\""); sb.append(">"); sb.append(option.getLabel()); sb.append("</option>"); } sb.append("</select>"); return sb.toString(); } } DropdownWidget();  DropdownWidget(Integer size); @Override String generateHtml(FormEntryContext context); DropdownWidget clone();  }