001: /*
002: * Copyright 2002-2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package info.jtrac.wicket;
018:
019: import java.util.HashSet;
020: import java.util.List;
021: import java.util.Locale;
022: import org.apache.wicket.markup.ComponentTag;
023: import org.apache.wicket.markup.MarkupStream;
024: import org.apache.wicket.markup.html.form.IChoiceRenderer;
025: import org.apache.wicket.markup.html.form.ListMultipleChoice;
026: import org.apache.wicket.util.string.Strings;
027:
028: /**
029: * custom multo select list / check box control that
030: * is scrollable and selected entries "float" to the top
031: */
032: public class JtracCheckBoxMultipleChoice extends ListMultipleChoice {
033:
034: private boolean isForSet;
035:
036: public JtracCheckBoxMultipleChoice(String id, List choices,
037: IChoiceRenderer renderer) {
038: super (id, choices, renderer);
039: }
040:
041: public JtracCheckBoxMultipleChoice(String id, List choices,
042: IChoiceRenderer renderer, boolean isForSet) {
043: super (id, choices, renderer);
044: this .isForSet = isForSet;
045: }
046:
047: @Override
048: protected java.lang.Object convertValue(String[] ids) {
049: List list = (List) super .convertValue(ids);
050: if (isForSet) {
051: return new HashSet(list);
052: } else {
053: return list;
054: }
055: }
056:
057: /**
058: * code adapted from onComponentTagBody implementation of wicket's built-in
059: * CheckBoxMultipleChoice component
060: */
061: @Override
062: protected void onComponentTagBody(final MarkupStream markupStream,
063: final ComponentTag openTag) {
064:
065: final List choices = getChoices();
066: boolean scrollable = choices.size() > 6;
067:
068: final StringBuilder buffer = new StringBuilder();
069: final StringBuilder selectedBuffer = new StringBuilder();
070:
071: if (scrollable) {
072: selectedBuffer
073: .append("<div class=\"multiselect scrollable\">");
074: } else {
075: selectedBuffer.append("<div class=\"multiselect\">");
076: }
077:
078: final String selected = getValue();
079:
080: boolean hasSelected = false;
081:
082: Locale locale = getLocale();
083:
084: for (int index = 0; index < choices.size(); index++) {
085:
086: final Object choice = choices.get(index);
087:
088: final String label = getConverter(String.class)
089: .convertToString(
090: getChoiceRenderer().getDisplayValue(choice),
091: locale);
092:
093: if (label != null) {
094:
095: String id = getChoiceRenderer().getIdValue(choice,
096: index);
097: final String idAttr = getInputName() + "_" + id;
098:
099: String display = label;
100: if (localizeDisplayValues()) {
101: display = getLocalizer().getString(label, this ,
102: label);
103: }
104: CharSequence escaped = Strings.escapeMarkup(display,
105: false, true);
106: boolean isSelected = false;
107: StringBuilder whichBuffer = buffer;
108: if (isSelected(choice, index, selected)) {
109: isSelected = true;
110: if (scrollable) {
111: hasSelected = true;
112: whichBuffer = selectedBuffer;
113: }
114: }
115: whichBuffer.append("<input name=\"").append(
116: getInputName()).append("\"").append(
117: " type=\"checkbox\"").append(
118: isSelected ? " checked=\"checked\"" : "")
119: .append(
120: (isEnabled() ? ""
121: : " disabled=\"disabled\""))
122: .append(" value=\"").append(id).append(
123: "\" id=\"").append(idAttr).append(
124: "\"/>").append("<label for=\"").append(
125: idAttr).append("\">").append(escaped)
126: .append("</label>").append("<br/>");
127: }
128: }
129:
130: if (hasSelected) {
131: selectedBuffer.append("<hr/>");
132: }
133:
134: selectedBuffer.append(buffer).append("</div>");
135:
136: replaceComponentTagBody(markupStream, openTag, selectedBuffer);
137:
138: }
139:
140: }
|