001: /* Rows.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Tue Oct 25 16:02:39 2005, Created by tomyeh
010: }}IS_NOTE
011:
012: Copyright (C) 2005 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: This program is distributed under GPL Version 2.0 in the hope that
016: it will be useful, but WITHOUT ANY WARRANTY.
017: }}IS_RIGHT
018: */
019: package org.zkoss.zul;
020:
021: import java.util.Set;
022: import java.util.HashSet;
023: import java.util.Iterator;
024:
025: import org.zkoss.zk.ui.Component;
026: import org.zkoss.zk.ui.UiException;
027: import org.zkoss.zk.ui.ext.render.Cropper;
028:
029: import org.zkoss.zul.impl.XulElement;
030: import org.zkoss.zul.ext.Paginal;
031:
032: /**
033: * Defines the rows of a grid.
034: * Each child of a rows element should be a {@link Row} element.
035: *
036: * @author tomyeh
037: */
038: public class Rows extends XulElement {
039: /** Returns the grid that contains this rows.
040: * <p>It is the same as {@link #getParent}.
041: */
042: public Grid getGrid() {
043: return (Grid) getParent();
044: }
045:
046: //Paging//
047: /** Returns the index of the first visible child.
048: * <p>Used only for component development, not for application developers.
049: */
050: public int getVisibleBegin() {
051: final Grid grid = getGrid();
052: if (grid == null || !grid.inPagingMold())
053: return 0;
054: final Paginal pgi = grid.getPaginal();
055: return pgi.getActivePage() * pgi.getPageSize();
056: }
057:
058: /** Returns the index of the last visible child.
059: * <p>Used only for component development, not for application developers.
060: */
061: public int getVisibleEnd() {
062: final Grid grid = getGrid();
063: if (grid == null || !grid.inPagingMold())
064: return Integer.MAX_VALUE;
065: final Paginal pgi = grid.getPaginal();
066: return (pgi.getActivePage() + 1) * pgi.getPageSize() - 1; //inclusive
067: }
068:
069: //-- Component --//
070: public void setParent(Component parent) {
071: if (parent != null && !(parent instanceof Grid))
072: throw new UiException("Unsupported parent for rows: "
073: + parent);
074: super .setParent(parent);
075: }
076:
077: public boolean insertBefore(Component child, Component insertBefore) {
078: if (!(child instanceof Row))
079: throw new UiException("Unsupported child for rows: "
080: + child);
081: return super .insertBefore(child, insertBefore);
082: }
083:
084: public void onChildAdded(Component child) {
085: super .onChildAdded(child);
086:
087: final Grid grid = getGrid();
088: if (grid != null && grid.inPagingMold())
089: grid.getPaginal().setTotalSize(getChildren().size());
090: }
091:
092: public void onChildRemoved(Component child) {
093: super .onChildRemoved(child);
094:
095: final Grid grid = getGrid();
096: if (grid != null && grid.inPagingMold())
097: grid.getPaginal().setTotalSize(getChildren().size());
098: }
099:
100: //-- ComponentCtrl --//
101: protected Object newExtraCtrl() {
102: return new ExtraCtrl();
103: }
104:
105: /** A utility class to implement {@link #getExtraCtrl}.
106: * It is used only by component developers.
107: */
108: protected class ExtraCtrl extends XulElement.ExtraCtrl implements
109: Cropper {
110: //--Cropper--//
111: public boolean isCropper() {
112: final Grid grid = getGrid();
113: return grid != null && grid.inPagingMold();
114: }
115:
116: public Set getAvailableAtClient() {
117: final Grid grid = getGrid();
118: if (grid == null || !grid.inPagingMold())
119: return null;
120:
121: final Set avail = new HashSet(37);
122: final Paginal pgi = grid.getPaginal();
123: int pgsz = pgi.getPageSize();
124: final int ofs = pgi.getActivePage() * pgsz;
125: for (final Iterator it = getChildren().listIterator(ofs); --pgsz >= 0
126: && it.hasNext();)
127: avail.add(it.next());
128: return avail;
129: }
130: }
131: }
|