Hi,

if someone is interested in a LoadableDetachableListModel where every single 
entity in the List is loaded per request by its own detachable model, see 
below :). 

I found it quite useful. I needed for a multi file upload panel where all 
changes entities shall be saved in one step.

public class DocumentModel<Document> extends 
LoadableDetachableListModel<Document>{

        public DDE(List<Document> entities) {
                super(entities);
        }

        protected IModel<T> createModel(Document entity) {
                return new EntityBaseLoadableDetachableModel<Document>(entity);
        }

}

package de.binaerebauten.app.presentation.wicket.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;

@SuppressWarnings("serial")
public abstract class LoadableDetachableListModel<T> extends 
LoadableDetachableModel<List<T>>{

        private List<IModel<T>> modelsList = new ArrayList<IModel<T>>();

        private List<T> entities;

        public LoadableDetachableListModel() {
                
        }

        public LoadableDetachableListModel(List<T> entities) {
                addAll(entities);
        }
        
        protected void addAll(List<T> entities) {
                for(T entity: entities) {
                        add(entity);
                }
        }

        public void onDetach() {
                for(IModel<T> model : modelsList) {
                        model.detach();
                }
                entities = null;
        }
        
        private List<T> loadAll() {
                List<T> rv = new ArrayList<T>();
                for(IModel<T> model : modelsList) {
                        rv.add(model.getObject());
                }
                return rv;
        }

        protected List<T> load() {
                if(entities != null) {
                        return entities;
                }
                entities = new List<T>() {

                        public boolean add(T o) {
                                return modelsList.add(createModel(o));
                        }


                        public void add(int index, T element) {
                                modelsList.add(index, createModel(element));
                        }

                        public boolean addAll(Collection<? extends T> c) {
                                throw new UnsupportedOperationException();
                        }

                        public boolean addAll(int index, Collection<? extends 
T> c) {
                                throw new UnsupportedOperationException();
                        }

                        public void clear() {
                                modelsList.clear();
                        }

                        public boolean contains(Object o) {
                                return indexOf(o) > -1;
                        }

                        public boolean containsAll(Collection<?> c) {
                                throw new UnsupportedOperationException();
                        }

                        public T get(int index) {
                                IModel<T> model = modelsList.get(index);
                                return model.getObject();
                        }

                        public int indexOf(Object o) {
                                int pos = 0;
                                for(IModel<T> model : modelsList) {
                                        T o1 = model.getObject();
                                        if(o1.equals(o)) {
                                                return pos;
                                        }
                                        pos++;
                                }
                                return -1;
                        }

                        public boolean isEmpty() {
                                return modelsList.isEmpty();
                        }

                        public Iterator<T> iterator() {
                                return new Iterator<T>() {
                                        Iterator<IModel<T>> delegate = 
modelsList.iterator();
                                        
                                        public boolean hasNext() {
                                                return delegate.hasNext();
                                        }

                                        public T next() {
                                                IModel<T> model = 
delegate.next();
                                                return model == null ? null : 
model.getObject();
                                        }

                                        public void remove() {
                                                delegate.remove();
                                        }
                                };
                        }

                        public int lastIndexOf(Object o) {
                                throw new UnsupportedOperationException();
                        }

                        public ListIterator<T> listIterator() {
                                throw new UnsupportedOperationException();
                        }

                        public ListIterator<T> listIterator(int index) {
                                throw new UnsupportedOperationException();
                        }

                        public boolean remove(Object o) {
                                int idx = indexOf(o);
                                if(idx == -1) {
                                        return false;
                                }
                                remove(idx);
                                return true;
                        }

                        public T remove(int index) {
                                IModel<T> model = modelsList.remove(index);
                                return model == null ? null : model.getObject();
                        }

                        public boolean removeAll(Collection<?> c) {
                                throw new UnsupportedOperationException();
                        }

                        public boolean retainAll(Collection<?> c) {
                                throw new UnsupportedOperationException();      
                
                        }

                        public T set(int index, T element) {
                                IModel<T> model = modelsList.set(index, 
createModel(element));
                                return model == null ? null : model.getObject();
                        }

                        public int size() {
                                return modelsList.size();
                        }

                        public List<T> subList(int fromIndex, int toIndex) {
                                return loadAll().subList(fromIndex, toIndex);
                        }

                        public Object[] toArray() {
                                throw new UnsupportedOperationException();
                        }

                        public <U> U[] toArray(U[] a) {
                                throw new UnsupportedOperationException();
                        }
                };
                return entities;
        }
        
        public boolean add(T entity) {
                return modelsList.add(createModel(entity));
        }

        protected abstract IModel<T> createModel(T entity);

}



-- 
binaere bauten gmbh · tempelhofer ufer 1a · 10961 berlin

   +49 · 171 · 9342 465

Handelsregister: HRB 115854 - Amtsgericht Charlottenburg
Geschäftsführer: Dipl.-Inform. Ilja Pavkovic, Dipl.-Inform. Jost Becker

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org

Reply via email to