001: /*
002: * $Id: Item.java 458267 2005-12-09 07:31:35Z ivaynberg $
003: * $Revision: 458267 $
004: * $Date: 2005-12-09 08:31:35 +0100 (Fri, 09 Dec 2005) $
005: *
006: * ====================================================================
007: * Licensed under the Apache License, Version 2.0 (the "License");
008: * you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019: package wicket.extensions.markup.html.repeater.refreshing;
020:
021: import java.util.Comparator;
022:
023: import wicket.markup.html.WebMarkupContainer;
024: import wicket.model.IModel;
025: import wicket.version.undo.Change;
026:
027: /**
028: * Container that holds components in a RefreshingView. One Item represents one
029: * entire row of the view. Users should add all containing components to the
030: * Item instead of the view, this is accomplished by implementing
031: * refreshingView.populateItem(Item item).
032: *
033: * @see RefreshingView
034: *
035: * @author Igor Vaynberg (ivaynberg)
036: */
037: public class Item extends WebMarkupContainer {
038: private static final long serialVersionUID = 1L;
039:
040: /** relative index of this item */
041: private int index;
042:
043: /**
044: * @param id
045: * component id
046: * @param index
047: * relative index of this item in the pageable view
048: * @param model
049: * model for this item
050: */
051: public Item(final String id, int index, final IModel model) {
052: super (id, model);
053: this .index = index;
054: }
055:
056: /**
057: * Sets the index of this item
058: *
059: * @param index
060: * new index
061: */
062: public void setIndex(int index) {
063: if (this .index != index) {
064: if (isVersioned()) {
065: addStateChange(new Change() {
066: final int oldIndex = Item.this .index;
067: private static final long serialVersionUID = 1L;
068:
069: public void undo() {
070: Item.this .index = oldIndex;
071: }
072:
073: public String toString() {
074: return "IndexChange[component: " + getPath()
075: + ", index: " + oldIndex + "]";
076: }
077: });
078: }
079: this .index = index;
080: }
081: }
082:
083: /**
084: * @return the index assigned to this item
085: */
086: public int getIndex() {
087: return index;
088: }
089:
090: /**
091: * @return the primary key assigned to this item
092: */
093: public String getPrimaryKey() {
094: return getId();
095: }
096:
097: /**
098: * Comparator that compares Items by their index property
099: *
100: * @author Igor Vaynberg (ivaynberg)
101: *
102: */
103: public static class IndexComparator implements Comparator {
104: private static final Comparator instance = new IndexComparator();
105:
106: /**
107: * @return static instance of the comparator
108: */
109: public static final Comparator getInstance() {
110: return instance;
111: }
112:
113: /**
114: * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
115: */
116: public int compare(Object o1, Object o2) {
117: Item lhs = (Item) o1;
118: Item rhs = (Item) o2;
119: return lhs.getIndex() - rhs.getIndex();
120: }
121:
122: };
123:
124: }
|