KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > custom > tabbedpane > HtmlTabbedPaneRenderer


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.custom.tabbedpane;
17
18 import org.apache.myfaces.renderkit.RendererUtils;
19 import org.apache.myfaces.renderkit.html.HTML;
20 import org.apache.myfaces.renderkit.html.HtmlRenderer;
21 import org.apache.myfaces.renderkit.html.HtmlRendererUtils;
22 import org.apache.myfaces.component.UserRoleUtils;
23
24 import javax.faces.application.ViewHandler;
25 import javax.faces.component.NamingContainer;
26 import javax.faces.component.UIComponent;
27 import javax.faces.component.UIForm;
28 import javax.faces.component.UIViewRoot;
29 import javax.faces.component.UINamingContainer;
30 import javax.faces.context.FacesContext;
31 import javax.faces.context.ResponseWriter;
32 import java.io.IOException JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
35
36 /**
37  * @author Manfred Geiler (latest modification by $Author: oros $)
38  * @version $Revision: 1.10 $ $Date: 2005/02/16 00:50:37 $
39  * $Log: HtmlTabbedPaneRenderer.java,v $
40  * Revision 1.10 2005/02/16 00:50:37 oros
41  * SF issue #1043331: replaced all   by the corresponding numeric entity   so safari users will be happy, too, with MyFaces output
42  *
43  * Revision 1.9 2005/02/11 16:03:00 mmarinschek
44  * solve bug in tabbed panel when datatable was displayed not on tab, but at the bottom of the datatable...
45  *
46  * Revision 1.8 2005/01/24 12:20:11 mmarinschek
47  * Changed the TabbedPane component to only decode components which are on the visible tags - other components are not processed in the decode phase. Changed the HtmlRendererUtils back to submit empty strings for components which should be posted back but have null values.
48  *
49  * Revision 1.7 2004/11/26 14:29:12 oros
50  * bug fix #1006636: VisibleOnUserRole attribute for x:panelTab tag
51  *
52  * Revision 1.6 2004/10/13 11:50:58 matze
53  * renamed packages to org.apache
54  *
55  * Revision 1.5 2004/08/09 10:52:25 manolito
56  * RFE #975649 - Enhance HtmlTabbedPaneRenderer (rendered attribute for TabbedPane component)
57  *
58  * Revision 1.4 2004/07/10 17:11:02 o_rossmueller
59  * added attributes activeTabStyleClass, inactiveTabStyleClass, activeSubStyleClass, inactiveSubStyleClass, tagContentStyleClass to overwrite style attributes using css
60  *
61  * Revision 1.3 2004/07/01 21:53:05 mwessendorf
62  * ASF switch
63  *
64  * Revision 1.2 2004/05/18 12:02:13 manolito
65  * getActionURL and getResourceURL must not call encodeActionURL or encodeResourceURL
66  *
67  */

68 public class HtmlTabbedPaneRenderer
69         extends HtmlRenderer
70 {
71     //private static final Log log = LogFactory.getLog(HtmlTabbedPaneRenderer.class);
72

73     private static final String JavaDoc TABLE_STYLE =
74         "border-style: none; " +
75         "padding: 0px; " +
76         "border-spacing: 0px; " +
77         "empty-cells: show; ";
78
79     private static final String JavaDoc ACTIVE_HEADER_CELL_STYLE =
80         "border-top: 2px outset #CCCCCC; " +
81         "border-right: 2px outset #CCCCCC; " +
82         "border-bottom: 0px none; " +
83         "border-left: 2px outset #CCCCCC; " +
84         "text-align: center; ";
85
86     private static final String JavaDoc INACTIVE_HEADER_CELL_STYLE =
87         "border-top: 1px outset #CCCCCC; " +
88         "border-right: 1px outset #CCCCCC; " +
89         "border-bottom: 0px none; " +
90         "border-left: 1px outset #CCCCCC; " +
91         "text-align: center; " +
92         "background-color: #CCCCCC; ";
93
94     private static final String JavaDoc EMPTY_HEADER_CELL_STYLE =
95         "border-top: 0px none; " +
96         "border-right: 0px none; " +
97         "border-bottom: 0px none; " +
98         "border-left: 0px none; ";
99
100     private static final String JavaDoc SUB_HEADER_CELL_STYLE =
101         "height: 2px; " +
102         "line-height: 0px; font-size: 0px; " +
103         "border-bottom: 0px none; ";
104
105     private static final String JavaDoc TAB_CELL_STYLE =
106         "border-top: 0px none; " +
107         "border-right: 2px outset #CCCCCC; " +
108         "border-bottom: 2px outset #CCCCCC; " +
109         "border-left: 2px outset #CCCCCC; " +
110         "padding: 10px; ";
111
112     private static final String JavaDoc NO_BORDER_STYLE =
113         "0px none; ";
114
115     private static final String JavaDoc BORDER_STYLE =
116         "2px outset #CCCCCC; ";
117
118     private static final String JavaDoc BUTTON_STYLE_ACTIVE
119         = "border-style:none; width:100%; cursor:pointer;";
120     private static final String JavaDoc BUTTON_STYLE_INACTIVE
121         = "border-style:none; width:100%; cursor:pointer; background-color:#CCCCCC;";
122
123     private static final String JavaDoc BUTTON_STYLE_DISABLED
124         = "border-style:none; width:100%; cursor:normal;";
125
126     private static final String JavaDoc DEFAULT_BG_COLOR = "#FFFFFF";
127
128     private static final String JavaDoc AUTO_FORM_SUFFIX = ".autoform";
129
130
131     public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) throws IOException JavaDoc
132     {
133     }
134
135     public boolean getRendersChildren()
136     {
137         return true;
138     }
139     
140     public void encodeChildren(FacesContext facescontext, UIComponent uicomponent) throws IOException JavaDoc
141     {
142     }
143
144     public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) throws IOException JavaDoc
145     {
146         RendererUtils.checkParamValidity(facesContext, uiComponent, HtmlPanelTabbedPane.class);
147
148         ResponseWriter writer = facesContext.getResponseWriter();
149
150         HtmlRendererUtils.writePrettyLineSeparator(facesContext);
151
152         HtmlPanelTabbedPane tabbedPane = (HtmlPanelTabbedPane)uiComponent;
153         int selectedIndex = tabbedPane.getSelectedIndex();
154
155         if (tabbedPane.getBgcolor() == null)
156         {
157             tabbedPane.setBgcolor(DEFAULT_BG_COLOR);
158         }
159
160         UIForm parentForm = RendererUtils.findParentForm(tabbedPane);
161         if (parentForm == null)
162         {
163             writeFormStart(writer, facesContext, tabbedPane);
164         }
165
166         writeTableStart(writer, facesContext, tabbedPane);
167         HtmlRendererUtils.writePrettyLineSeparator(facesContext);
168         writer.startElement(HTML.TR_ELEM, uiComponent);
169
170         //Tab headers
171
int tabIdx = 0;
172         int visibleTabCount = 0;
173         int visibleTabSelectedIdx = -1;
174         List JavaDoc children = tabbedPane.getChildren();
175         for (int i = 0, len = children.size(); i < len; i++)
176         {
177             UIComponent child = getUIComponent((UIComponent)children.get(i));
178             if (child instanceof HtmlPanelTab)
179             {
180                 if (child.isRendered())
181                 {
182                     writeHeaderCell(writer, facesContext, tabbedPane,
183                                     (HtmlPanelTab)child, tabIdx, tabIdx == selectedIndex, isDisabled(facesContext, child));
184                     if (tabIdx == selectedIndex)
185                     {
186                         visibleTabSelectedIdx = visibleTabCount;
187                     }
188                     visibleTabCount++;
189                 }
190                 tabIdx++;
191             }
192         }
193
194         //Empty tab cell on the right for better look
195
HtmlRendererUtils.writePrettyLineSeparator(facesContext);
196         HtmlRendererUtils.writePrettyIndent(facesContext);
197         writer.startElement(HTML.TD_ELEM, uiComponent);
198         writer.writeAttribute(HTML.STYLE_ATTR, EMPTY_HEADER_CELL_STYLE, null);
199         writer.write("&#160;");
200         writer.endElement(HTML.TD_ELEM);
201         HtmlRendererUtils.writePrettyLineSeparator(facesContext);
202         writer.endElement(HTML.TR_ELEM);
203
204         //Sub header cells
205
HtmlRendererUtils.writePrettyLineSeparator(facesContext);
206         writer.startElement(HTML.TR_ELEM, uiComponent);
207         writeSubHeaderCells(writer, facesContext, tabbedPane, visibleTabCount, visibleTabSelectedIdx, false);
208         HtmlRendererUtils.writePrettyLineSeparator(facesContext);
209         writer.endElement(HTML.TR_ELEM);
210
211         //Tab
212
HtmlRendererUtils.writePrettyLineSeparator(facesContext);
213         writer.startElement(HTML.TR_ELEM, uiComponent);
214         writeTabCell(writer, facesContext, tabbedPane, visibleTabCount, selectedIndex);
215         HtmlRendererUtils.writePrettyLineSeparator(facesContext);
216         writer.endElement(HTML.TR_ELEM);
217
218         HtmlRendererUtils.writePrettyLineSeparator(facesContext);
219         writer.endElement(HTML.TABLE_ELEM);
220
221         if (parentForm == null)
222         {
223             writeFormEnd(writer, facesContext);
224         }
225     }
226
227
228     public void decode(FacesContext facesContext, UIComponent uiComponent)
229     {
230         RendererUtils.checkParamValidity(facesContext, uiComponent, HtmlPanelTabbedPane.class);
231
232         HtmlPanelTabbedPane tabbedPane = (HtmlPanelTabbedPane)uiComponent;
233
234         Map JavaDoc paramMap = facesContext.getExternalContext().getRequestParameterMap();
235
236         int tabIdx = 0;
237         List JavaDoc children = tabbedPane.getChildren();
238         for (int i = 0, len = children.size(); i < len; i++)
239         {
240             UIComponent child = getUIComponent((UIComponent)children.get(i));
241             if (child instanceof HtmlPanelTab)
242             {
243                 String JavaDoc paramName = tabbedPane.getClientId(facesContext) + "." + tabIdx;
244                 String JavaDoc paramValue = (String JavaDoc)paramMap.get(paramName);
245                 if (paramValue != null && paramValue.length() > 0)
246                 {
247                     tabbedPane.queueEvent(new TabChangeEvent(tabbedPane,
248                                                              tabbedPane.getSelectedIndex(),
249                                                              tabIdx));
250                     return;
251                 }
252                 tabIdx++;
253             }
254         }
255     }
256
257
258     protected void writeFormStart(ResponseWriter writer,
259                                   FacesContext facesContext,
260                                   UIComponent tabbedPane)
261         throws IOException JavaDoc
262     {
263         ViewHandler viewHandler = facesContext.getApplication().getViewHandler();
264         String JavaDoc viewId = facesContext.getViewRoot().getViewId();
265         String JavaDoc actionURL = viewHandler.getActionURL(facesContext, viewId);
266
267         //write out auto form
268
writer.startElement(HTML.FORM_ELEM, null);
269         writer.writeAttribute(HTML.NAME_ATTR, tabbedPane.getClientId(facesContext) + AUTO_FORM_SUFFIX, null);
270         writer.writeAttribute(HTML.STYLE_ATTR, "display:inline", null);
271         writer.writeAttribute(HTML.METHOD_ATTR, "post", null);
272         writer.writeURIAttribute(HTML.ACTION_ATTR,
273                                  facesContext.getExternalContext().encodeActionURL(actionURL),
274                                  null);
275         writer.flush();
276     }
277
278
279     protected void writeTableStart(ResponseWriter writer,
280                                    FacesContext facesContext,
281                                    HtmlPanelTabbedPane tabbedPane)
282         throws IOException JavaDoc
283     {
284         String JavaDoc oldStyle = tabbedPane.getStyle();
285         if (oldStyle == null)
286         {
287             tabbedPane.setStyle(TABLE_STYLE);
288         }
289         else
290         {
291             tabbedPane.setStyle(TABLE_STYLE + "; " + oldStyle);
292         }
293
294         String JavaDoc oldBgColor = tabbedPane.getBgcolor();
295         tabbedPane.setBgcolor(null);
296
297         writer.startElement(HTML.TABLE_ELEM, tabbedPane);
298         writer.writeAttribute(HTML.CELLSPACING_ATTR, "0", null);
299         HtmlRendererUtils.renderHTMLAttributes(writer, tabbedPane, HTML.TABLE_PASSTHROUGH_ATTRIBUTES);
300         writer.flush();
301
302         tabbedPane.setStyle(oldStyle);
303         tabbedPane.setBgcolor(oldBgColor);
304     }
305
306
307     protected void writeHeaderCell(ResponseWriter writer,
308                                    FacesContext facesContext,
309                                    HtmlPanelTabbedPane tabbedPane,
310                                    HtmlPanelTab tab,
311                                    int tabIndex,
312                                    boolean active,
313                                    boolean disabled)
314         throws IOException JavaDoc
315     {
316         HtmlRendererUtils.writePrettyLineSeparator(facesContext);
317         HtmlRendererUtils.writePrettyIndent(facesContext);
318         writer.startElement(HTML.TD_ELEM, tabbedPane);
319         if (active)
320         {
321             writer.writeAttribute(HTML.STYLE_ATTR,
322                                   ACTIVE_HEADER_CELL_STYLE + "background-color:" + tabbedPane.getBgcolor(),
323                                   null);
324
325             HtmlRendererUtils.renderHTMLAttribute(writer, tabbedPane, "activeTabStyleClass", HTML.STYLE_CLASS_ATTR);
326         }
327         else if (disabled)
328         {
329             writer.writeAttribute(HTML.STYLE_ATTR,
330                                   INACTIVE_HEADER_CELL_STYLE,
331                                   null);
332             HtmlRendererUtils.renderHTMLAttribute(writer, tabbedPane, "disabledTabStyleClass", HTML.STYLE_CLASS_ATTR);
333         }
334         else
335         {
336             writer.writeAttribute(HTML.STYLE_ATTR,
337                                   INACTIVE_HEADER_CELL_STYLE,
338                                   null);
339             HtmlRendererUtils.renderHTMLAttribute(writer, tabbedPane, "inactiveTabStyleClass", HTML.STYLE_CLASS_ATTR);
340         }
341
342
343         String JavaDoc label = tab.getLabel();
344         if (label == null || label.length() == 0)
345         {
346             label = "Tab " + tabIndex;
347         }
348
349         if (disabled) {
350             writer.startElement(HTML.LABEL_ELEM, tabbedPane);
351             writer.writeAttribute(HTML.NAME_ATTR, tabbedPane.getClientId(facesContext) + "." + tabIndex, null);
352             writer.writeAttribute(HTML.STYLE_ATTR, BUTTON_STYLE_DISABLED, null);
353             writer.writeText(label, null);
354             writer.endElement(HTML.LABEL_ELEM);
355         } else {
356             //Button
357
writer.startElement(HTML.INPUT_ELEM, tabbedPane);
358             writer.writeAttribute(HTML.TYPE_ATTR, "submit", null);
359             writer.writeAttribute(HTML.NAME_ATTR, tabbedPane.getClientId(facesContext) + "." + tabIndex, null);
360             writer.writeAttribute(HTML.VALUE_ATTR, label, null);
361
362             if (active)
363             {
364                 writer.writeAttribute(HTML.STYLE_ATTR,
365                     BUTTON_STYLE_ACTIVE + "background-color:" + tabbedPane.getBgcolor(),
366                     null);
367             }
368             else
369             {
370                 writer.writeAttribute(HTML.STYLE_ATTR,
371                     BUTTON_STYLE_INACTIVE,
372                     null);
373             }
374             writer.endElement(HTML.INPUT_ELEM);
375         }
376         writer.endElement(HTML.TD_ELEM);
377     }
378
379
380     protected void writeSubHeaderCells(ResponseWriter writer,
381                                        FacesContext facesContext,
382                                        HtmlPanelTabbedPane tabbedPane,
383                                        int visibleTabCount,
384                                        int visibleTabSelectedIndex,
385                                        boolean disabled)
386             throws IOException JavaDoc
387     {
388         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
389         for (int i = 0, cnt = visibleTabCount + 1; i < cnt; i++)
390         {
391             HtmlRendererUtils.writePrettyLineSeparator(facesContext);
392             HtmlRendererUtils.writePrettyIndent(facesContext);
393             writer.startElement(HTML.TD_ELEM, tabbedPane);
394             buf.setLength(0);
395             buf.append(SUB_HEADER_CELL_STYLE);
396             buf.append("border-top:").append(i == visibleTabSelectedIndex ? NO_BORDER_STYLE : BORDER_STYLE);
397             buf.append("border-right:").append(i + 1 < cnt ? NO_BORDER_STYLE : BORDER_STYLE);
398             buf.append("border-left:").append(i > 0 ? NO_BORDER_STYLE : BORDER_STYLE);
399             buf.append("background-color:").append(tabbedPane.getBgcolor());
400             writer.writeAttribute(HTML.STYLE_ATTR, buf.toString(), null);
401
402             if (i == visibleTabSelectedIndex) {
403                 HtmlRendererUtils.renderHTMLAttribute(writer, tabbedPane, "activeSubStyleClass", HTML.STYLE_CLASS_ATTR);
404             } else {
405                 HtmlRendererUtils.renderHTMLAttribute(writer, tabbedPane, "inactiveSubStyleClass", HTML.STYLE_CLASS_ATTR);
406             }
407
408             writer.write("&#160;");
409             writer.endElement(HTML.TD_ELEM);
410         }
411     }
412
413
414     protected void writeTabCell(ResponseWriter writer,
415                                 FacesContext facesContext,
416                                 HtmlPanelTabbedPane tabbedPane,
417                                 int tabCount,
418                                 int selectedIndex)
419         throws IOException JavaDoc
420     {
421         HtmlRendererUtils.writePrettyLineSeparator(facesContext);
422         HtmlRendererUtils.writePrettyIndent(facesContext);
423         writer.startElement(HTML.TD_ELEM, tabbedPane);
424         writer.writeAttribute(HTML.COLSPAN_ATTR, Integer.toString(tabCount + 1), null);
425         writer.writeAttribute(HTML.STYLE_ATTR, TAB_CELL_STYLE + "background-color:" + tabbedPane.getBgcolor(), null);
426         HtmlRendererUtils.renderHTMLAttribute(writer, tabbedPane, "tabContentStyleClass", HTML.STYLE_CLASS_ATTR);
427
428         int tabIdx = 0;
429         List JavaDoc children = tabbedPane.getChildren();
430         for (int i = 0, len = children.size(); i < len; i++)
431         {
432             UIComponent child = getUIComponent((UIComponent)children.get(i));
433             if (child instanceof HtmlPanelTab)
434             {
435                 if (tabIdx == selectedIndex)
436                 {
437                     RendererUtils.renderChild(facesContext, child);
438                 }
439                 tabIdx++;
440             }
441             else
442             {
443                 RendererUtils.renderChild(facesContext, child);
444             }
445         }
446
447         writer.endElement(HTML.TD_ELEM);
448     }
449
450     private UIComponent getUIComponent(UIComponent uiComponent)
451     {
452         if (uiComponent instanceof UIForm || uiComponent instanceof UINamingContainer)
453         {
454             List JavaDoc children = uiComponent.getChildren();
455             for (int i = 0, len = children.size(); i < len; i++)
456             {
457                 uiComponent = getUIComponent((UIComponent)children.get(i));
458             }
459         }
460         return uiComponent;
461     }
462
463
464     protected void writeFormEnd(ResponseWriter writer,
465                                 FacesContext facesContext)
466         throws IOException JavaDoc
467     {
468         //write state marker
469
ViewHandler viewHandler = facesContext.getApplication().getViewHandler();
470         viewHandler.writeState(facesContext);
471
472         writer.endElement(HTML.FORM_ELEM);
473     }
474
475
476     protected boolean isDisabled(FacesContext facesContext, UIComponent uiComponent)
477     {
478         return !UserRoleUtils.isEnabledOnUserRole(uiComponent);
479     }
480 }
481
Popular Tags