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

com.databasesandlife.util.wicket.SingleEntryModelAdaptor Maven / Gradle / Ivy

There is a newer version: 21.0.1
Show newest version
package com.databasesandlife.util.wicket;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.model.IModel;

/**
 * Wraps a model of a single value, in a model capable of storing a list of values.
 *    

* Multiple-entry text fields can allow the user to select multiple values. * However, if they have a "non-multiple" version, the user can only select one value. * The appropriate model for such a field is a Model<X> but the multiple-entry text field will * require a Model<List<X>>. *

* An object of this class allows the application to provide a Model<X> but the multiple-value * text field (which is operating in single-value mode) to see a Model<List<X>>. * * @author This source is copyright Adrian Smith and licensed under the LGPL 3. * @see Project on GitHub */ @SuppressWarnings("serial") public class SingleEntryModelAdaptor implements IModel> { protected IModel singleEntryModel; public SingleEntryModelAdaptor(IModel x) { singleEntryModel = x; } @Override public void detach() { singleEntryModel.detach(); } @Override public List getObject() { // return List.of(..) doesn't work: // the resulting array is actually MODIFIED by Wicket's Select object before being passed to setObject var object = singleEntryModel.getObject(); var result = new ArrayList(); if (object != null) result.add(object); return result; } @Override public void setObject(List newList) { if (newList == null || newList.isEmpty()) singleEntryModel.setObject(null); else if (newList.size() == 1) singleEntryModel.setObject(newList.get(0)); else throw new IllegalArgumentException("Expected <= 1 elements, found " + newList.size()); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy