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.examples.repeater;
018:
019: import org.apache.wicket.AttributeModifier;
020: import org.apache.wicket.Component;
021: import org.apache.wicket.extensions.markup.html.repeater.data.sort.OrderByBorder;
022: import org.apache.wicket.markup.html.basic.Label;
023: import org.apache.wicket.markup.html.link.Link;
024: import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
025: import org.apache.wicket.markup.repeater.Item;
026: import org.apache.wicket.markup.repeater.ReuseIfModelsEqualStrategy;
027: import org.apache.wicket.markup.repeater.data.DataView;
028: import org.apache.wicket.model.AbstractReadOnlyModel;
029: import org.apache.wicket.model.IModel;
030: import org.apache.wicket.model.Model;
031:
032: /**
033: * page that demonstrates dataview with ReuseIfModelsEqualStrategy
034: *
035: * @author igor
036: */
037: public class OIRPage extends BasePage {
038: private static class HighlitableDataItem extends Item {
039: private boolean highlite = false;
040:
041: /**
042: * toggles highlite
043: */
044: public void toggleHighlite() {
045: highlite = !highlite;
046: }
047:
048: /**
049: * Constructor
050: *
051: * @param id
052: * @param index
053: * @param model
054: */
055: public HighlitableDataItem(String id, int index, IModel model) {
056: super (id, index, model);
057: add(new AttributeModifier("style", true, new Model(
058: "background-color:#80b6ed;")) {
059: public boolean isEnabled(Component component) {
060: return HighlitableDataItem.this .highlite;
061: }
062: });
063: }
064: }
065:
066: /**
067: * Constructor
068: */
069: public OIRPage() {
070: SortableContactDataProvider dp = new SortableContactDataProvider();
071:
072: final DataView dataView = new DataView("oir", dp) {
073: protected void populateItem(final Item item) {
074: Contact contact = (Contact) item.getModelObject();
075: item.add(new ActionPanel("actions", item.getModel()));
076: item.add(new Link("toggleHighlite") {
077: public void onClick() {
078: HighlitableDataItem hitem = (HighlitableDataItem) item;
079: hitem.toggleHighlite();
080: }
081: });
082: item.add(new Label("contactid", String.valueOf(contact
083: .getId())));
084: item
085: .add(new Label("firstname", contact
086: .getFirstName()));
087: item.add(new Label("lastname", contact.getLastName()));
088: item
089: .add(new Label("homephone", contact
090: .getHomePhone()));
091: item
092: .add(new Label("cellphone", contact
093: .getCellPhone()));
094:
095: item.add(new AttributeModifier("class", true,
096: new AbstractReadOnlyModel() {
097: public Object getObject() {
098: return (item.getIndex() % 2 == 1) ? "even"
099: : "odd";
100: }
101: }));
102: }
103:
104: protected Item newItem(String id, int index, IModel model) {
105: return new HighlitableDataItem(id, index, model);
106: }
107: };
108:
109: dataView.setItemsPerPage(8);
110: dataView.setItemReuseStrategy(ReuseIfModelsEqualStrategy
111: .getInstance());
112:
113: add(new OrderByBorder("orderByFirstName", "firstName", dp) {
114: protected void onSortChanged() {
115: dataView.setCurrentPage(0);
116: }
117: });
118:
119: add(new OrderByBorder("orderByLastName", "lastName", dp) {
120: protected void onSortChanged() {
121: dataView.setCurrentPage(0);
122: }
123: });
124:
125: add(dataView);
126: add(new PagingNavigator("navigator", dataView));
127: }
128: }
|