KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > jwi > jgallery > tags > IndexIteratorTag


1 package de.jwi.jgallery.tags;
2
3 /*
4  * jGallery - Java web application to display image galleries
5  *
6  * Copyright (C) 2004 Juergen Weber
7  *
8  * This file is part of jGallery.
9  *
10  * jGallery is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later
12  * version.
13  *
14  * jGallery is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
15  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License along with jGallery; if not, write to the Free
19  * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston
20  */

21
22 import java.io.IOException JavaDoc;
23
24 import javax.servlet.jsp.JspTagException JavaDoc;
25 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
26 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
27
28 import de.jwi.jgallery.Folder;
29
30 /**
31  * @author Juergen Weber
32  * Source file created on 10.03.2004
33  *
34  */

35 public class IndexIteratorTag extends BodyTagSupport JavaDoc
36 {
37
38     public static final String JavaDoc CURRENTINDEX = "index";
39
40     private int totalIndexes;
41
42     private int index;
43
44     private Folder folder;
45
46     private IndexIteratorCursor cursor;
47
48     private void updateCursor()
49     {
50         int i = totalIndexes - index + 1;
51
52         String JavaDoc page = folder.getCalculatedIndexPage(i);
53         String JavaDoc number = Integer.toString(i);
54         String JavaDoc selected = folder.getIndexNum().equals(number) ? "selected" : "";
55
56         cursor.setPage(page);
57         cursor.setNumber(number);
58         cursor.setSelected(selected);
59     }
60
61     public int doStartTag() throws JspTagException JavaDoc
62     {
63         folder = (Folder) pageContext.getRequest().getAttribute("folder");
64
65         if (null == folder) { throw new JspTagException JavaDoc(
66                 "no folder Attribute found in request"); }
67
68         int i = 1;
69         try
70         {
71             i = Integer.parseInt(folder.getTotalIndexes());
72         }
73         catch (NumberFormatException JavaDoc e1)
74         {
75             // empty String, leave preset 1
76
}
77         index = totalIndexes = i;
78
79         if (index > 0)
80         {
81             cursor = new IndexIteratorCursor();
82             
83             updateCursor();
84             pageContext.setAttribute(CURRENTINDEX, cursor);
85
86             index--;
87
88             return (EVAL_BODY_AGAIN);
89         }
90         else
91         {
92             return SKIP_BODY;
93         }
94     }
95
96     public int doAfterBody() throws JspTagException JavaDoc
97     {
98         BodyContent JavaDoc body = getBodyContent();
99         try
100         {
101             body.writeOut(getPreviousOut());
102         }
103         catch (IOException JavaDoc e)
104         {
105             throw new JspTagException JavaDoc("IterationTag: " + e.getMessage());
106         }
107
108         body.clearBody();
109         if (index > 0)
110         {
111             updateCursor();
112             pageContext.setAttribute(CURRENTINDEX, cursor);
113
114             index--;
115
116             return (EVAL_BODY_AGAIN);
117         }
118         else
119         {
120             return SKIP_BODY;
121         }
122     }
123 }
124
Popular Tags