All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.nitorcreations.wicket.model.JoiningPropertyModel Maven / Gradle / Ivy

There is a newer version: 1.7
Show newest version
package com.nitorcreations.wicket.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import org.apache.wicket.core.util.lang.PropertyResolver;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;

/**
 * A model that uses {@link org.apache.wicket.core.util.lang.PropertyResolver} to fetch the given property of each
 * of the given collection model's items and join them to a String array.
 * 

* Uses natural ordering of strings to represent the collection. * Example: *

 * List<String> list = Arrays.asList("DEF", "A", "BC");
 * add(new Label("label", new JoiningPropertyModel<String>(Model.ofList(list), "length")));
 *
 * Would render the label's text as "1, 2, 3"
 * 
* * @param */ public class JoiningPropertyModel extends AbstractReadOnlyModel { private static final long serialVersionUID = -2810734991454424296L; private final IModel> collectionModel; private final String property; public JoiningPropertyModel(IModel> collectionModel, String property) { this.collectionModel = collectionModel; this.property = property; } @Override public String getObject() { final Collection collection = this.collectionModel.getObject(); if (null == collection) { return null; } final List list = new ArrayList(); for (T object : collection) { Object value = PropertyResolver.getValue(property, object); if (null != value) { list.add(String.valueOf(value)); } } Collections.sort(list); return StringUtils.join(list, ", "); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy