org.apache.tapestry5.corelib.components
public class: Palette [javadoc |
source]
java.lang.Object
org.apache.tapestry5.corelib.base.AbstractField
org.apache.tapestry5.corelib.components.Palette
All Implemented Interfaces:
Field
Multiple selection component. Generates a UI consisting of two <select> elements configured for multiple
selection; the one on the left is the list of "available" elements, the one on the right is "selected". Elements can
be moved between the lists by clicking a button, or double clicking an option (and eventually, via drag and drop).
The items in the available list are kept ordered as per
SelectModel order. When items are moved from the
selected list to the available list, they items are inserted back into their proper positions.
The Palette may operate in normal or re-orderable mode, controlled by the reorder parameter.
In normal mode, the items in the selected list are kept in the same "natural" order as the items in the available
list.
In re-order mode, items moved to the selected list are simply added to the bottom of the list. In addition, two extra
buttons appear to move items up and down within the selected list.
Much of the look and feel is driven by CSS, the default Tapestry CSS is used to set up the columns, etc. By default,
the <select> element's widths are 200px, and it is common to override this to a specific value:
<style>
DIV.t-palette SELECT { width: 300px; }
</style>
You'll want to ensure that both <select> in each column is the same width, otherwise the display will update
poorly as options are moved from one column to the other.
Option groups within the
SelectModel will be rendered, but are not supported by many browsers, and are not
fully handled on the client side.
Methods from org.apache.tapestry5.corelib.base.AbstractField: |
---|
afterDecorator, beforeDecorator, createDefaultParameterBinding, decorateInsideField, defaultLabel, getClientId, getControlName, getLabel, isDisabled, isRequired, processSubmission, setDecorator, setFormSupport, setup |
Methods from java.lang.Object: |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method from org.apache.tapestry5.corelib.components.Palette Detail: |
boolean beforeRenderBody() {
return false;
}
Prevent the body from rendering. |
void beginRender(MarkupWriter writer) {
JSONArray selectedValues = new JSONArray();
for (OptionModel selected : selectedOptions)
{
Object value = selected.getValue();
String clientValue = encoder.toClient(value);
selectedValues.put(clientValue);
}
JSONArray naturalOrder = new JSONArray();
for (String value : this.naturalOrder)
{
naturalOrder.put(value);
}
String clientId = getClientId();
renderSupport.addScript("new Tapestry.Palette('%s', %s, %s);", clientId, reorder, naturalOrder);
writer.element("input", "type", "hidden", "id", clientId + ":values", "name", getControlName() + ":values",
"value", selectedValues);
writer.end();
}
|
public Renderable getAvailableRenderer() {
return new AvailableRenderer();
}
|
List<Object> getSelected() {
if (selected == null) return Collections.emptyList();
return selected;
}
|
public Renderable getSelectedRenderer() {
return new SelectedRenderer();
}
|
int getSize() {
return size;
}
|
protected void processSubmission(String elementName) {
String parameterValue = request.getParameter(elementName + ":values");
JSONArray values = new JSONArray(parameterValue);
// Use a couple of local variables to cut down on access via bindings
List< Object > selected = this.selected;
if (selected == null) selected = newList();
else selected.clear();
ValueEncoder encoder = this.encoder;
int count = values.length();
for (int i = 0; i < count; i++)
{
String value = values.getString(i);
Object objectValue = encoder.toValue(value);
selected.add(objectValue);
}
this.selected = selected;
}
|
void setupRender(MarkupWriter writer) {
valueToOptionModel = CollectionFactory.newMap();
availableOptions = CollectionFactory.newList();
selectedOptions = CollectionFactory.newList();
naturalOrder = CollectionFactory.newList();
renderer = new SelectModelRenderer(writer, encoder);
final Set selectedSet = newSet(getSelected());
SelectModelVisitor visitor = new SelectModelVisitor()
{
public void beginOptionGroup(OptionGroupModel groupModel)
{
availableOptions.add(new OptionGroupStart(groupModel));
}
public void endOptionGroup(OptionGroupModel groupModel)
{
availableOptions.add(new OptionGroupEnd(groupModel));
}
public void option(OptionModel optionModel)
{
Object value = optionModel.getValue();
boolean isSelected = selectedSet.contains(value);
String clientValue = toClient(value);
naturalOrder.add(clientValue);
if (isSelected)
{
selectedOptions.add(optionModel);
valueToOptionModel.put(value, optionModel);
return;
}
availableOptions.add(new RenderOption(optionModel));
}
};
model.visit(visitor);
}
|
String toClient(Object value) {
return encoder.toClient(value);
}
|