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.extensions.markup.html.repeater.data.table;
018:
019: import org.apache.wicket.IClusterable;
020: import org.apache.wicket.markup.html.basic.Label;
021: import org.apache.wicket.markup.repeater.data.DataView;
022: import org.apache.wicket.model.Model;
023: import org.apache.wicket.model.StringResourceModel;
024:
025: /**
026: * Label that provides Showing x to y of z message given for a DataTable
027: *
028: * @author Igor Vaynberg (ivaynberg)
029: *
030: */
031: public class NavigatorLabel extends Label {
032: private static final long serialVersionUID = 1L;
033:
034: // TODO Factor this interface out and let dataview/datatable implement it
035: private static interface PageableComponent extends IClusterable {
036: /**
037: * @return total number of rows across all pages
038: */
039: int getRowCount();
040:
041: /**
042: * @return current page
043: */
044: int getCurrentPage();
045:
046: /**
047: * @return rows per page
048: */
049: int getRowsPerPage();
050: }
051:
052: /**
053: * @param id
054: * component id
055: * @param table
056: * dataview
057: */
058: public NavigatorLabel(final String id, final DataTable table) {
059: this (id, new PageableComponent() {
060:
061: /**
062: *
063: */
064: private static final long serialVersionUID = 1L;
065:
066: public int getCurrentPage() {
067: return table.getCurrentPage();
068: }
069:
070: public int getRowCount() {
071: return table.getRowCount();
072: }
073:
074: public int getRowsPerPage() {
075: return table.getRowsPerPage();
076: }
077:
078: });
079:
080: }
081:
082: /**
083: * @param id
084: * component id
085: * @param table
086: * pageable view
087: */
088: public NavigatorLabel(final String id, final DataView table) {
089: this (id, new PageableComponent() {
090:
091: /**
092: *
093: */
094: private static final long serialVersionUID = 1L;
095:
096: public int getCurrentPage() {
097: return table.getCurrentPage();
098: }
099:
100: public int getRowCount() {
101: return table.getRowCount();
102: }
103:
104: public int getRowsPerPage() {
105: return table.getItemsPerPage();
106: }
107:
108: });
109:
110: }
111:
112: private NavigatorLabel(final String id,
113: final PageableComponent table) {
114: super (id);
115: setModel(new StringResourceModel("NavigatorLabel", this ,
116: new Model(new LabelModelObject(table)),
117: "Showing ${from} to ${to} of ${of}"));
118: }
119:
120: private class LabelModelObject implements IClusterable {
121: private static final long serialVersionUID = 1L;
122: private PageableComponent table;
123:
124: /**
125: * Construct.
126: * @param table
127: */
128: public LabelModelObject(PageableComponent table) {
129: this .table = table;
130: }
131:
132: /**
133: * @return "z" in "Showing x to y of z"
134: */
135: public int getOf() {
136: return table.getRowCount();
137: }
138:
139: /**
140: * @return "x" in "Showing x to y of z"
141: */
142: public int getFrom() {
143: if (getOf() == 0) {
144: return 0;
145: }
146: return (table.getCurrentPage() * table.getRowsPerPage()) + 1;
147: }
148:
149: /**
150: * @return "y" in "Showing x to y of z"
151: */
152: public int getTo() {
153: if (getOf() == 0) {
154: return 0;
155: }
156: return Math.min(getOf(), getFrom() + table.getRowsPerPage()
157: - 1);
158: }
159:
160: }
161: }
|