From e1ae6df9d23a77a999d2869074fee019069023b4 Mon Sep 17 00:00:00 2001 From: "Howard M. Lewis Ship" Date: Thu, 12 Jul 2012 15:51:01 -0700 Subject: [PATCH] Add support for a sortProperty parameter Use the sort property when sorting on the client --- .../tapx/core/components/MultipleSelect.java | 115 ++++++++++++++---- .../tapx/core/tapx-multiselect.js | 12 +- 2 files changed, 99 insertions(+), 28 deletions(-) diff --git a/tapx-core/src/main/java/com/howardlewisship/tapx/core/components/MultipleSelect.java b/tapx-core/src/main/java/com/howardlewisship/tapx/core/components/MultipleSelect.java index fe727fb..7e64bd9 100644 --- a/tapx-core/src/main/java/com/howardlewisship/tapx/core/components/MultipleSelect.java +++ b/tapx-core/src/main/java/com/howardlewisship/tapx/core/components/MultipleSelect.java @@ -1,4 +1,4 @@ -// Copyright 2011 Howard M. Lewis Ship +// Copyright 2011, 2012 Howard M. Lewis Ship // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -29,10 +29,8 @@ import org.apache.tapestry5.ioc.annotations.Inject; import org.apache.tapestry5.json.JSONArray; import org.apache.tapestry5.json.JSONObject; -import org.apache.tapestry5.services.BeanModelSource; -import org.apache.tapestry5.services.ComponentDefaultProvider; -import org.apache.tapestry5.services.FormSupport; -import org.apache.tapestry5.services.Request; +import org.apache.tapestry5.plastic.PlasticUtils; +import org.apache.tapestry5.services.*; import org.apache.tapestry5.services.javascript.JavaScriptSupport; import java.util.ArrayList; @@ -44,17 +42,19 @@ * Both a simplification of the Tapestry {@link Palette} component, and an extension in that it supports * adding new values on the fly, using a form located inside a Modalbox modal dialog. Specifically * limited to editing a Set of values: element order is immaterial, and the UI keeps - * the values sorted in alphabetical order by {@linkplain MultipleSelectModel#toLabel(Object) label}. - *

+ * the values sorted in alphabetical order by {@linkplain MultipleSelectModel#toLabel(Object) label}, + * or via an alternate property (defined by the sortProperty parameter). + *

* The UI includes an "Add" button to add a new value of the type appropriate to the set. This sets up a modal dialog on * the client side, and a uses a server-side {@link BeanEditor} to render the form. Informal blocks bound do this * component will, in turn, be provided to the BeanEditor component for use as property overrides. - *

+ *

* TODO: Rename this to SetEditor and delete the current SetEditor. */ @Import(stack = "tapx-core") @SuppressWarnings("rawtypes") @SupportsInformalParameters +@Events("configureNewValue: allows the container to configure a new value prior to it being saved") public class MultipleSelect implements Field { /** @@ -100,6 +100,14 @@ public class MultipleSelect implements Field @Parameter(defaultPrefix = BindingConstants.LITERAL, value = "message:tapx-multiple-select-available-column-label") private String availableColumnLabel; + /** + * Property used when sorting the options. If null (the default), the sorting is based on the label. + * The property should be a String property, or a numeric value. Numbers are converted to zero-padded strings. + * Really only meant for positive numbers. Floats or decimals will cause a runtime exception. + */ + @Parameter(defaultPrefix = BindingConstants.LITERAL) + private String sortProperty; + /** * Alternate label used to represent a "single" instance of the value; this is used as part of * button labels, and in the title of the modal dialog. @@ -146,6 +154,10 @@ public class MultipleSelect implements Field @Inject private BeanModelSource beanModelSource; + @Inject + + private PropertyConduitSource propertyConduitSource; + Object defaultBeanModel() { return new AbstractBinding() @@ -158,40 +170,79 @@ public boolean isInvariant() public Object get() { - if (newValue == null) - return null; + Object instance = + newValue != null ? newValue : model.createEmptyInstance(); - return beanModelSource.createEditModel(newValue.getClass(), resources.getContainerMessages()); + return beanModelSource.createEditModel(instance.getClass(), resources.getContainerMessages()); } }; } - private static class Pair implements Comparable + private static class Option implements Comparable