KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > jsp > taglib > IndexTag


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10
11 package org.mmbase.bridge.jsp.taglib;
12 import org.mmbase.bridge.jsp.taglib.util.Attribute;
13 import org.mmbase.bridge.jsp.taglib.containers.*;
14 import org.mmbase.bridge.Query;
15
16 import javax.servlet.jsp.JspTagException JavaDoc;
17 import javax.servlet.jsp.tagext.Tag JavaDoc;
18 import javax.servlet.jsp.JspException JavaDoc;
19 import javax.servlet.jsp.jstl.core.*;
20
21 /**
22  * The index of current item of a list.
23  *
24  * @author Michiel Meeuwissen
25  * @version $Id: IndexTag.java,v 1.21 2005/12/15 21:47:27 michiel Exp $
26  */

27
28 public class IndexTag extends ListReferrerTag implements Writer, QueryContainerReferrer {
29
30     private Attribute container = Attribute.NULL;
31
32     /**
33      * @since MMBase-1.7.1
34      */

35     public void setContainer(String JavaDoc c) throws JspTagException JavaDoc {
36         container = getAttribute(c);
37     }
38
39
40     private Attribute offset = Attribute.NULL;
41
42     public void setOffset(String JavaDoc o) throws JspTagException JavaDoc {
43         offset = getAttribute(o);
44     }
45     protected int getOffset() throws JspTagException JavaDoc {
46         return offset.getInt(this, getList().getIndexOffset()); // start counting at list's offset on default (normally 1)
47
}
48
49     public int doStartTag() throws JspTagException JavaDoc{
50
51         int index;
52         if (container != Attribute.NULL) {
53             if (parentListId != Attribute.NULL) {
54                 throw new JspTagException JavaDoc("Cannot specify both 'container' and 'list' attributes");
55             }
56             QueryContainer c = (QueryContainer) findParentTag(QueryContainer.class, (String JavaDoc) container.getValue(this));
57             Query query = c.getQuery();
58             index = query.getOffset() / query.getMaxNumber() + offset.getInt(this, 0);
59         } else if (parentListId != Attribute.NULL) {
60             index = getList().getIndex() + getOffset();;
61         } else {
62             Tag JavaDoc tag = findLoopOrQuery(null, true);
63             if (tag instanceof QueryContainer) {
64                 Query query = ((QueryContainer) tag).getQuery();
65                 index = query.getOffset() / query.getMaxNumber() + offset.getInt(this, 0);
66             } else {
67                 LoopTagStatus status = ((LoopTag) tag).getLoopStatus();
68                 if (status == null) throw new TaglibException("The tag " + tag + " return loop status 'null'");
69                 index = status.getIndex();
70                 if (tag instanceof ListProvider) {
71                     index += offset.getInt(this, ((ListProvider) tag).getIndexOffset());
72                 } else {
73                     index += offset.getInt(this, 0);
74                 }
75             }
76         }
77
78         helper.setValue(new Integer JavaDoc(index));
79         if (getId() != null) {
80             getContextProvider().getContextContainer().register(getId(), helper.getValue());
81         }
82         return EVAL_BODY_BUFFERED;
83     }
84
85
86     public int doAfterBody() throws JspException JavaDoc {
87         return helper.doAfterBody();
88     }
89
90     /**
91      *
92      **/

93     public int doEndTag() throws JspTagException JavaDoc {
94         helper.doEndTag();
95         return super.doEndTag();
96     }
97
98 }
99
Popular Tags