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

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

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

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import org.apache.wicket.Component;
import org.apache.wicket.model.IComponentAssignedModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.IWrapModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.util.lang.Args;

/**
 * A model that lists the items given in the {@code model} parameter by creating a transient {@link org.apache.wicket.model.StringResourceModel}
 * for each item in the list with the given {@code messageKey}. If no entries are in the list, {@code defaultValue} is shown. Example
 *
 * 
 * SomeComponent.properties
 * ---
 * stringLength=len: ${length}
 *
 * SomeComponent.java
 * ---
 * List<String> list = Arrays.asList("A", "BC", "DEF");
 * add(new Label("label", new ListingStringResourceModel<String>("stringLength", Model.ofList(list));
 *
 * Renders the text as:
 * ---
 * len: 1, len: 2, len: 3
 *
 * 
* The model implements {@link org.apache.wicket.model.IComponentAssignedModel} and thus, retrieves the correct * message based on component hierarchy, when the model is assigned to a component. * * * @param the type of a single item in the list * @see org.apache.wicket.model.StringResourceModel#StringResourceModel(String, org.apache.wicket.model.IModel, Object...) */ public class ListingStringResourceModel extends LoadableDetachableModel implements IComponentAssignedModel { private static final long serialVersionUID = 714388179253718513L; public static final String DEFAULT_SEPARATOR = ", "; private final IModel defaultMessage; private final String messageKey; private final IModel> model; private final Component component; private String separator = DEFAULT_SEPARATOR; @SuppressWarnings("unchecked") public ListingStringResourceModel(String messageKey, IModel> model, IModel defaultMessage, Component component) { this.component = component; this.defaultMessage = Args.notNull(defaultMessage, "defaultMessage"); this.messageKey = Args.notNull(messageKey, "message key"); this.model = Args.notNull(model, "list model"); } public ListingStringResourceModel(String messageKey, IModel> model) { this(messageKey, model, Model.of(""), null); } @Override public void detach() { super.detach(); defaultMessage.detach(); model.detach(); } private List> getWrappedModels(Component component) { List> list = new ArrayList>(); for (T item : model.getObject()) { list.add(new StringResourceModel(messageKey, component, Model.of(item))); } return list; } private String getString(Component component) { List> models = getWrappedModels(component); if (models.isEmpty()) { return defaultMessage.getObject(); } List strings = new ArrayList(); for (IModel stringIModel : models) { strings.add(stringIModel.getObject()); } return StringUtils.join(strings, separator); } private String getString() { return getString(component); } public String getSeparator() { return separator; } public ListingStringResourceModel setSeparator(String separator) { this.separator = separator; return this; } @Override protected String load() { return getString(); } @Override public IWrapModel wrapOnAssignment(Component component) { return new AssignmentWrapper(component); } private class AssignmentWrapper extends LoadableDetachableModel implements IWrapModel { private static final long serialVersionUID = -7181093446442329177L; private final Component assignedComponent; public AssignmentWrapper(Component component) { this.assignedComponent = component; } @Override public void detach() { super.detach(); ListingStringResourceModel.this.detach(); } @Override protected void onDetach() { if (ListingStringResourceModel.this.component == null) { ListingStringResourceModel.this.onDetach(); } } @Override public IModel getWrappedModel() { return ListingStringResourceModel.this; } @Override protected String load() { if (ListingStringResourceModel.this.component != null) { return ListingStringResourceModel.this.getObject(); } else { return getString(assignedComponent); } } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy