package org.apache.wicket.examples.tree.content;
import org.apache.wicket.Component;
import org.apache.wicket.examples.tree.Foo;
import org.apache.wicket.extensions.markup.html.repeater.tree.AbstractTree;
import org.apache.wicket.extensions.markup.html.repeater.tree.ITreeProvider;
import org.apache.wicket.extensions.markup.html.repeater.tree.content.CheckedFolder;
import org.apache.wicket.extensions.markup.html.repeater.util.ProviderSubset;
import org.apache.wicket.model.IModel;
@author
public class CheckedFolderContent extends Content
{
private static final long serialVersionUID = 1L;
private ProviderSubset<Foo> checked;
public CheckedFolderContent(ITreeProvider<Foo> provider)
{
checked = new ProviderSubset<>(provider, false);
}
@Override
public void detach()
{
checked.detach();
}
protected boolean isChecked(Foo foo)
{
return checked.contains(foo);
}
protected void check(Foo foo, boolean check)
{
if (check)
{
checked.add(foo);
}
else
{
checked.remove(foo);
}
}
@Override
public Component newContentComponent(String id, final AbstractTree<Foo> tree, IModel<Foo> model)
{
return new CheckedFolder<Foo>(id, tree, model)
{
private static final long serialVersionUID = 1L;
@Override
protected IModel<Boolean> newCheckBoxModel(final IModel<Foo> model)
{
return new IModel<Boolean>()
{
private static final long serialVersionUID = 1L;
@Override
public Boolean getObject()
{
return isChecked(model.getObject());
}
@Override
public void setObject(Boolean object)
{
check(model.getObject(), object);
}
};
}
};
}
}