KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > taglib > common > SlotsTag


1 /* ===============================================================================
2 *
3 * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4 *
5 * ===============================================================================
6 *
7 * Copyright (C)
8 *
9 * This program is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License version 2, as published by the
11 * Free Software Foundation. See the file LICENSE.html for more information.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19 * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20 *
21 * ===============================================================================
22 */

23 package org.infoglue.deliver.taglib.common;
24
25 import java.util.List JavaDoc;
26
27 import javax.servlet.jsp.JspException JavaDoc;
28 import javax.servlet.jsp.JspTagException JavaDoc;
29
30 import org.infoglue.deliver.taglib.AbstractTag;
31 import org.infoglue.deliver.util.Slots;
32
33 /**
34  *
35  */

36 public class SlotsTag extends AbstractTag {
37     /**
38      *
39      */

40     private static final long serialVersionUID = 3257849891731681845L;
41
42     /**
43      *
44      */

45     private String JavaDoc visibleElementsId;
46     
47     /**
48      *
49      */

50     private String JavaDoc visibleSlotsId;
51     
52     /**
53      *
54      */

55     private String JavaDoc lastSlotId;
56     
57     /**
58      *
59      */

60     private List JavaDoc elements;
61
62     /**
63      *
64      */

65     private int currentSlot = 1;
66     
67     /**
68      *
69      */

70     private int slotSize;
71     
72     /**
73      *
74      */

75     private int slotCount;
76     
77     /**
78      *
79      */

80     private int maxSlots;
81     
82     
83     /**
84      *
85      */

86     public int doEndTag() throws JspException JavaDoc
87     {
88         calculateSlots();
89         return EVAL_PAGE;
90     }
91     
92     /**
93      *
94      */

95     private void calculateSlots() throws JspException JavaDoc
96     {
97         try
98         {
99             if(elements != null && visibleElementsId != null)
100             {
101                 Slots slots = new Slots(elements, currentSlot, slotSize, slotCount);
102                 setResultAttribute(visibleElementsId, slots.getVisibleElements());
103                 setResultAttribute(visibleSlotsId, slots.getVisibleSlots());
104                 setResultAttribute(lastSlotId, slots.getLastSlot());
105             }
106             else if(maxSlots > 0)
107             {
108                 Slots slots = new Slots(currentSlot, slotSize, slotCount, maxSlots);
109                 setResultAttribute(visibleSlotsId, slots.getVisibleSlots());
110                 setResultAttribute(lastSlotId, slots.getLastSlot());
111             }
112             else
113                 throw new JspTagException JavaDoc("Either elements/visibleElementsId or maxSlots must be specified.");
114         }
115         catch(Exception JavaDoc e)
116         {
117             e.printStackTrace();
118             throw new JspTagException JavaDoc(e.getMessage());
119         }
120     }
121
122     /**
123      *
124      */

125     protected void setResultAttribute(final String JavaDoc id, final Object JavaDoc value)
126     {
127         if(value == null)
128             pageContext.removeAttribute(id);
129         else
130             pageContext.setAttribute(id, value);
131     }
132
133     /**
134      *
135      */

136     public void setVisibleElementsId(final String JavaDoc id)
137     {
138         this.visibleElementsId = id;
139     }
140     
141     /**
142      *
143      */

144     public void setVisibleSlotsId(final String JavaDoc id)
145     {
146         this.visibleSlotsId = id;
147     }
148     
149     /**
150      *
151      */

152     public void setLastSlotId(final String JavaDoc id)
153     {
154         this.lastSlotId = id;
155     }
156     
157     /**
158      *
159      */

160     public void setElements(final String JavaDoc elements) throws JspException JavaDoc
161     {
162         this.elements = evaluateList("slots", "elements", elements);
163     }
164
165     /**
166      *
167      */

168     public void setCurrentSlot(final String JavaDoc currentSlot) throws JspException JavaDoc
169     {
170         this.currentSlot = Math.max(1, evaluateInteger("slots", "currentSlot", currentSlot).intValue());
171     }
172
173     /**
174      *
175      */

176     public void setMaxSlots(final String JavaDoc maxSlots) throws JspException JavaDoc
177     {
178         this.maxSlots = evaluateInteger("slots", "maxSlots", maxSlots).intValue();
179     }
180     
181     /**
182      *
183      */

184     public void setSlotSize(final String JavaDoc slotSize) throws JspException JavaDoc
185     {
186         this.slotSize = evaluateInteger("slots", "slotSize", slotSize).intValue();
187     }
188
189     /**
190      *
191      */

192     public void setSlotCount(final String JavaDoc slotCount) throws JspException JavaDoc
193     {
194         this.slotCount = evaluateInteger("slots", "slotCount", slotCount).intValue();
195     }
196 }
197
Popular Tags