Changeset 4882


Ignore:
Timestamp:
04/02/14 15:17:26 (11 years ago)
Author:
Twan Goosen
Message:

simplified getting of string for resource type counts, now completely via StringResourceModel? (instead of converter)

Location:
vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/pojo/ResourceTypeCount.java

    r4620 r4882  
    2626public class ResourceTypeCount implements Serializable {
    2727
     28    /**
     29     * Enumeration for singular/plural distinction. {@link #toString() }
     30     * returns "singular" or "plural" respectively to make the resource bundle
     31     * files look clean (no screaming)
     32     */
     33    public enum Number {
     34
     35        SINGULAR("singular"),
     36        PLURAL("plural");
     37
     38        private final String numberString;
     39
     40        Number(String numberString) {
     41            this.numberString = numberString;
     42        }
     43
     44        @Override
     45        public String toString() {
     46            return numberString;
     47        }
     48
     49    }
     50
    2851    private final ResourceType resourceType;
    2952    private final Integer count;
     
    4063    public ResourceType getResourceType() {
    4164        return resourceType;
     65    }
     66
     67    /**
     68     *
     69     * @return {@link Number#SINGULAR } or {@link Number#PLURAL } depending on
     70     * value of {@link #getCount() }
     71     */
     72    public Number getNumber() {
     73        return getCount() == 1 ? Number.SINGULAR : Number.PLURAL;
    4274    }
    4375
  • vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/wicket/panels/search/SearchResultItemCollapsedPanel.java

    r4881 r4882  
    2525import eu.clarin.cmdi.vlo.wicket.model.SolrFieldModel;
    2626import eu.clarin.cmdi.vlo.wicket.provider.ResouceTypeCountDataProvider;
    27 import java.util.Locale;
    2827import org.apache.solr.common.SolrDocument;
    2928import org.apache.wicket.markup.html.WebMarkupContainer;
     
    3534import org.apache.wicket.markup.repeater.data.IDataProvider;
    3635import org.apache.wicket.model.IModel;
    37 import org.apache.wicket.model.Model;
    3836import org.apache.wicket.model.StringResourceModel;
    3937import org.apache.wicket.spring.injection.annot.SpringBean;
    40 import org.apache.wicket.util.convert.ConversionException;
    41 import org.apache.wicket.util.convert.IConverter;
    4238
    4339/**
     
    4743public class SearchResultItemCollapsedPanel extends Panel {
    4844
    49     private final ResourceTypeCountConverter resourceTypeCountConverter = new ResourceTypeCountConverter();
    5045    private static final int MAX_DESCRIPTION_LENGTH = 350;
    5146    private static final int LONG_DESCRIPTION_TRUNCATE_POINT = 320;
     
    10398        protected void populateItem(Item<ResourceTypeCount> item) {
    10499            final Link resourceLink = new RecordPageLink("recordLink", documentModel, selectionModel);
    105             final Label label = new Label("resourceCountLabel", item.getModel()) {
    106 
    107                 @Override
    108                 public <C> IConverter<C> getConverter(Class<C> type) {
    109                     if (type == ResourceTypeCount.class) {
    110                         return (IConverter<C>) resourceTypeCountConverter;
    111                     } else {
    112                         return super.getConverter(type);
    113                     }
    114                 }
    115 
    116             };
    117 
     100            final Label label = new Label("resourceCountLabel", getResourceCountModel(item.getModel()));
    118101            resourceLink.add(label);
    119102            item.add(resourceLink);
    120103        }
     104
     105        /**
     106         *
     107         * @param resourceTypeCountModel
     108         * @return a string model that conveys the type of resource and number
     109         * of instances
     110         */
     111        private IModel<String> getResourceCountModel(final IModel<ResourceTypeCount> resourceTypeCountModel) {
     112            // first create a string model that provides the type of resources
     113            // in the right number (plural or singular, conveniently supplied by ResourceTypeCount)
     114            final StringResourceModel resourceTypeModel = new StringResourceModel("resourcetype.${resourceType}.${number}", resourceTypeCountModel, "?");
     115            // inject this into the resource string that combines it with count
     116            return new StringResourceModel("resources.typecount", this, resourceTypeCountModel, resourceTypeModel);
     117        }
    121118    }
    122119
    123     /**
    124      * One-way converter for resource type counts that generates string
    125      * representations like "1 video file", "2 video files" or "3 text
    126      * documents"
    127      */
    128     private static class ResourceTypeCountConverter implements IConverter<ResourceTypeCount> {
    129 
    130         @Override
    131         public ResourceTypeCount convertToObject(String value, Locale locale) throws ConversionException {
    132             throw new UnsupportedOperationException("Not supported");
    133         }
    134 
    135         @Override
    136         public String convertToString(ResourceTypeCount value, Locale locale) {
    137             final String resourceTypeString = getResourceTypeString(value);
    138             return String.format("%d %s", value.getCount(), resourceTypeString);
    139         }
    140 
    141         private String getResourceTypeString(ResourceTypeCount value) {
    142             final String count = value.getCount() == 1 ? "singular" : "plural";
    143             final StringResourceModel resourceModel = new StringResourceModel("resourcetype.${resourceType}." + count, Model.of(value), "?");
    144             return resourceModel.getObject();
    145         }
    146 
    147     }
    148120}
Note: See TracChangeset for help on using the changeset viewer.