001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.wicket.markup.repeater.data;
018:
019: import java.util.Iterator;
020:
021: import org.apache.wicket.markup.html.navigation.paging.IPageable;
022: import org.apache.wicket.markup.repeater.AbstractPageableView;
023: import org.apache.wicket.markup.repeater.RefreshingView;
024:
025: /**
026: * Base class for data views.
027: *
028: * Data views aim to make it very simple to populate your repeating view from a
029: * database by utilizing {@link IDataProvider} to act as an interface between
030: * the database and the dataview.
031: *
032: * @see IDataProvider
033: * @see DataView
034: * @see IPageable
035: * @see RefreshingView
036: *
037: * @author Igor Vaynberg (ivaynberg)
038: *
039: */
040: public abstract class DataViewBase extends AbstractPageableView {
041: private final IDataProvider dataProvider;
042:
043: /**
044: * @param id
045: * component id
046: * @param dataProvider
047: * data provider
048: */
049: public DataViewBase(String id, IDataProvider dataProvider) {
050: super (id);
051: if (dataProvider == null) {
052: throw new IllegalArgumentException(
053: "argument [dataProvider] cannot be null");
054: }
055: this .dataProvider = dataProvider;
056: }
057:
058: /**
059: * @return data provider associated with this view
060: */
061: protected final IDataProvider internalGetDataProvider() {
062: return dataProvider;
063: }
064:
065: protected final Iterator getItemModels(int offset, int count) {
066: return new ModelIterator(internalGetDataProvider(), offset,
067: count);
068: }
069:
070: /**
071: * Helper class that converts input from IDataProvider to an iterator over
072: * view items.
073: *
074: * @author Igor Vaynberg (ivaynberg)
075: *
076: */
077: private static final class ModelIterator implements Iterator {
078: private final Iterator items;
079: private final IDataProvider dataProvider;
080: private final int max;
081: private int index;
082:
083: /**
084: * Constructor
085: *
086: * @param dataProvider
087: * data provider
088: * @param offset
089: * index of first item
090: * @param count
091: * max number of items to return
092: */
093: public ModelIterator(IDataProvider dataProvider, int offset,
094: int count) {
095: items = dataProvider.iterator(offset, count);
096: this .dataProvider = dataProvider;
097: max = count;
098: }
099:
100: /**
101: * @see java.util.Iterator#remove()
102: */
103: public void remove() {
104: throw new UnsupportedOperationException();
105: }
106:
107: /**
108: * @see java.util.Iterator#hasNext()
109: */
110: public boolean hasNext() {
111: return items.hasNext() && (index < max);
112: }
113:
114: /**
115: * @see java.util.Iterator#next()
116: */
117: public Object next() {
118: index++;
119: return dataProvider.model(items.next());
120: }
121: }
122:
123: protected final int internalGetItemCount() {
124: return internalGetDataProvider().size();
125: }
126:
127: /**
128: * @see org.apache.wicket.markup.repeater.AbstractPageableView#onDetach()
129: */
130: protected void onDetach() {
131: dataProvider.detach();
132: super.onDetach();
133: }
134: }
|