KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > improve > struts > taglib > layout > tab > TabsTag


1 package fr.improve.struts.taglib.layout.tab;
2
3 import java.util.Comparator JavaDoc;
4 import java.util.Map JavaDoc;
5 import java.util.TreeMap JavaDoc;
6
7 import javax.servlet.jsp.JspException JavaDoc;
8
9 import fr.improve.struts.taglib.layout.BodyLayoutTagSupport;
10 import fr.improve.struts.taglib.layout.event.EndLayoutEvent;
11 import fr.improve.struts.taglib.layout.event.ErrorEvent;
12 import fr.improve.struts.taglib.layout.event.ErrorEventListener;
13 import fr.improve.struts.taglib.layout.event.StartLayoutEvent;
14 import fr.improve.struts.taglib.layout.util.LayoutUtils;
15 import fr.improve.struts.taglib.layout.util.TabsInterface;
16 import fr.improve.struts.taglib.layout.util.TagUtils;
17
18 /**
19  * @author jnribette
20  *
21  * To change this generated comment edit the template variable "typecomment":
22  * Window>Preferences>Java>Templates.
23  * To enable and disable the creation of type comments go to
24  * Window>Preferences>Java>Code Generation.
25  */

26 public class TabsTag extends BodyLayoutTagSupport implements ErrorEventListener {
27     /**
28      * Special comparator for the tabs Map.
29      * For historic reasons, the Map keys are
30      * String representation of numbers, which
31      * causes sort problems when there are more than 10 tab.
32      * This comparator is used to sort the String correctly.
33      */

34     private class TabComparator implements Comparator JavaDoc {
35         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
36             String JavaDoc s1 = (String JavaDoc) o1;
37             String JavaDoc s2 = (String JavaDoc) o2;
38             int l1 = s1.length();
39             int l2 = s2.length();
40             if (l1 == l2) {
41                 return s1.compareTo(s2);
42             } else {
43                 return l1 - l2;
44             }
45         }
46     }
47     
48     /**
49      * List of the tab titleKeys.
50      */

51     private Map JavaDoc tabs = new TreeMap JavaDoc(new TabComparator());
52     
53     /**
54      * Name of the last attributed tabs in the request.
55      */

56     private String JavaDoc LAST_TAB_KEY = "fr.improve.struts.taglib.layout.tab.TabsTag.KEY";
57
58     /**
59      * Id of the current tab group.
60      */

61     private String JavaDoc currentId;
62
63     /**
64      * Number of the current tab in the group.
65      */

66     private int currentTab = -1;
67
68     /**
69      * Tabs renderer.
70      */

71     private TabsInterface tabsRenderer;
72     
73     /**
74      * StyleClass
75      */

76     private String JavaDoc styleClass;
77         
78     /**
79      * Width
80      */

81     private String JavaDoc width;
82     
83     /**
84      * Name in the pageContext of the selected tab key.
85      */

86     private String JavaDoc selectedTabKeyName;
87     
88     
89     /**
90      * Name in the pageContext of the selected tab key.
91      */

92     private String JavaDoc selectedTabKey;
93         
94     /**
95      * true if a tab has already been selected.
96      */

97     private boolean tabAlreadySelected = false;
98     
99     /**
100      * Called by the <layout:tab> tags to add a tab.
101      * @param in_tabKey the key of the tab title.
102      * @return the id of the tab.
103      */

104     public String JavaDoc addTab(String JavaDoc in_titleKey, String JavaDoc in_title, String JavaDoc in_href, String JavaDoc in_reqCode, String JavaDoc in_width, boolean in_selected) {
105         currentTab++;
106         StringBuffer JavaDoc lc_id = new StringBuffer JavaDoc("tabs");
107         lc_id.append(currentId);
108         lc_id.append("tab");
109         lc_id.append(currentTab);
110         TabHeader lc_header = new TabHeader();
111         lc_header.width = in_width;
112         lc_header.title = in_title;
113         lc_header.titleKey = in_titleKey;
114         lc_header.href = in_href;
115         lc_header.reqCode = in_reqCode;
116         if (in_selected) {
117             lc_header.styleClass = tabsRenderer.getHeaderEnabledStyle();
118             tabAlreadySelected = true;
119         } else {
120             lc_header.styleClass = tabsRenderer.getHeaderDisabledStyle();
121         }
122         tabs.put(String.valueOf(currentTab), lc_header);
123         return lc_id.toString();
124     }
125     /**
126      * Used internally to compute a unique id for each tab in a request.
127      * @return An array containing the id of the tabs and the tab number.
128      */

129     private String JavaDoc computeNewTabId() {
130         Integer JavaDoc lc_id = (Integer JavaDoc) pageContext.getRequest().getAttribute(LAST_TAB_KEY);
131         if (lc_id==null) {
132             lc_id = new Integer JavaDoc(0);
133         }
134         pageContext.getRequest().setAttribute(LAST_TAB_KEY, new Integer JavaDoc(lc_id.intValue()+1));
135         return lc_id.toString();
136     }
137     
138     /**
139      * Called by the inner input field tag to notify an error.
140      */

141     public void processErrorEvent(ErrorEvent in_event) {
142         TabHeader lc_header = (TabHeader) tabs.get(String.valueOf(currentTab));
143         lc_header.styleClass = tabsRenderer.getHeaderErrorStyle();
144     }
145
146     /**
147      * Start the tag. Init the tabs renderer.
148      */

149     public int doStartLayoutTag() throws JspException JavaDoc {
150         try {
151             tabsRenderer = (TabsInterface) LayoutUtils.getSkin(pageContext.getSession()).getTabsClass().newInstance();
152             tabsRenderer.init(pageContext, styleClass, this);
153             currentId = computeNewTabId();
154         } catch (Exception JavaDoc e) {
155             System.out.println(e);
156             // PENDING
157
//tabsRenderer = new fr.improve.struts.taglib.layout.util.BasicPanel();
158
}
159         
160         new StartLayoutEvent(this, new StringBuffer JavaDoc("<td colspan=\"").append(LayoutUtils.getSkin(pageContext.getSession()).getFieldInterface().getColumnNumber()).append("\">").toString()).send();
161         
162         return EVAL_BODY_TAG;
163     }
164
165     /**
166      * End of the body. Print everything.
167      */

168     public int doAfterBody() throws JspException JavaDoc {
169         StringBuffer JavaDoc lc_buffer = new StringBuffer JavaDoc();
170         tabsRenderer.doStartPanel(lc_buffer, "center", width);
171         tabsRenderer.doStartHeaders(lc_buffer);
172         tabsRenderer.doPrintHeaders(lc_buffer, currentId, tabs);
173         tabsRenderer.doEndHeaders(lc_buffer);
174         tabsRenderer.doBeforeBody(lc_buffer, "center");
175         TagUtils.writePrevious(pageContext, lc_buffer.toString());
176         lc_buffer.setLength(0);
177
178         if (bodyContent != null) {
179             TagUtils.writePrevious(pageContext, bodyContent.getString());
180             bodyContent.clearBody();
181         }
182         tabsRenderer.doAfterBody(lc_buffer);
183         tabsRenderer.doEndPanel(lc_buffer);
184         TagUtils.writePrevious(pageContext, lc_buffer.toString());
185                         
186         return SKIP_BODY;
187     }
188     
189     /**
190      * End the tag. Reset the tag.
191      */

192     public int doEndLayoutTag() throws JspException JavaDoc {
193         
194         new EndLayoutEvent(this, "</td>").send();
195         
196         tabsRenderer = null;
197         currentId = null;
198         currentTab = -1;
199         tabs.clear();
200         tabAlreadySelected = false;
201         return EVAL_PAGE;
202     }
203     public void release() {
204         super.release();
205         selectedTabKeyName = null;
206         styleClass = null;
207         width = null;
208     }
209     
210     /**
211      * Returns the styleClass.
212      * @return String
213      */

214     public String JavaDoc getStyleClass() {
215         return styleClass;
216     }
217
218     /**
219      * Sets the styleClass.
220      * @param styleClass The styleClass to set
221      */

222     public void setStyleClass(String JavaDoc styleClass) {
223         this.styleClass = styleClass;
224     }
225
226     /**
227      * Returns the width.
228      * @return String
229      */

230     public String JavaDoc getWidth() {
231         return width;
232     }
233
234     /**
235      * Sets the width.
236      * @param width The width to set
237      */

238     public void setWidth(String JavaDoc width) {
239         this.width = width;
240     }
241
242     /**
243      * Returns the selectedTabKeyName.
244      * @return String
245      */

246     public String JavaDoc getSelectedTabKeyName() {
247         return selectedTabKeyName;
248     }
249
250     /**
251      * Sets the selectedTabKeyName.
252      * @param selectedTabKeyName The selectedTabKeyName to set
253      */

254     public void setSelectedTabKeyName(String JavaDoc selectedTabKeyName) {
255         this.selectedTabKeyName = selectedTabKeyName;
256     }
257
258     /**
259      * Returns the tabAlreadySelected.
260      * @return boolean
261      */

262     public boolean isTabAlreadySelected() {
263         return tabAlreadySelected;
264     }
265     
266     public String JavaDoc getSelectedTabKey(){
267         return selectedTabKey;
268     }
269     public void setSelectedTabKey(String JavaDoc in_name){
270         selectedTabKey = in_name;
271     }
272 }
273
Popular Tags