KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > custom > collapsiblepanel > HtmlCollapsiblePanelRenderer


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.collapsiblepanel;
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
23 import javax.faces.application.Application;
24 import javax.faces.application.ViewHandler;
25 import javax.faces.component.UIComponent;
26 import javax.faces.component.html.HtmlCommandLink;
27 import javax.faces.component.html.HtmlOutputText;
28 import javax.faces.context.FacesContext;
29 import javax.faces.context.ResponseWriter;
30
31 import java.io.IOException JavaDoc;
32 import java.util.List JavaDoc;
33
34 /**
35  * @author Kalle Korhonen (latest modification by $Author: matzew $)
36  * @version $Revision: 1.1 $ $Date: 2005/01/10 08:22:17 $
37  * $Log: HtmlCollapsiblePanelRenderer.java,v $
38  * Revision 1.1 2005/01/10 08:22:17 matzew
39  * added new component CollapsiblePanel (contributed by Kalle Korhonen) (MYFACES-68)
40  *
41  *
42  */

43 public class HtmlCollapsiblePanelRenderer extends HtmlRenderer {
44     //private static final Log log = LogFactory.getLog(HtmlCollapsiblePanel.class);
45
private static final String JavaDoc LINK_ID = "ToggleCollapsed".intern();
46
47     public boolean getRendersChildren()
48     {
49         return true;
50     }
51     
52     public void encodeChildren(FacesContext facesContext, UIComponent uiComponent) throws IOException JavaDoc
53     {
54       // RendererUtils.checkParamValidity(facesContext, uiComponent, HtmlCollapsiblePanel.class);
55
ResponseWriter writer = facesContext.getResponseWriter();
56       HtmlCollapsiblePanel collapsiblePanel = (HtmlCollapsiblePanel)uiComponent;
57       
58       HtmlCommandLink link = getLink(facesContext, collapsiblePanel);
59       collapsiblePanel.getChildren().add(link);
60       
61       // Always render the link to toggle the collapsed state
62
RendererUtils.renderChild(facesContext, link);
63       link.setRendered(false);
64       
65       // conditionally render the rest of the children
66
if (!collapsiblePanel.isCollapsed()) {
67         HtmlRendererUtils.writePrettyLineSeparator(facesContext);
68         // TODO apply styles from the parent element to this DIV
69
writer.startElement(HTML.DIV_ELEM, null);
70         RendererUtils.renderChildren(facesContext, uiComponent);
71         writer.endElement(HTML.DIV_ELEM );
72         HtmlRendererUtils.writePrettyLineSeparator(facesContext);
73       }
74
75       link.setRendered(true);
76     }
77
78     public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) throws IOException JavaDoc
79     {
80       RendererUtils.checkParamValidity(facesContext, uiComponent, HtmlCollapsiblePanel.class);
81       ResponseWriter writer = facesContext.getResponseWriter();
82
83       HtmlRendererUtils.writePrettyLineSeparator(facesContext);
84         writer.startElement(HTML.DIV_ELEM, null);
85         
86       HtmlCollapsiblePanel collapsiblePanel = (HtmlCollapsiblePanel)uiComponent;
87       ViewHandler viewHandler = facesContext.getApplication().getViewHandler();
88       String JavaDoc viewId = facesContext.getViewRoot().getViewId();
89       String JavaDoc actionURL = viewHandler.getActionURL(facesContext, viewId);
90
91       Application application = facesContext.getApplication();
92     }
93     
94     
95     public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) throws IOException JavaDoc
96     {
97       //RendererUtils.checkParamValidity(facesContext, uiComponent, HtmlCollapsiblePanel.class);
98
ResponseWriter writer = facesContext.getResponseWriter();
99       writer.endElement(HTML.DIV_ELEM );
100       HtmlRendererUtils.writePrettyLineSeparator(facesContext);
101     }
102     
103     public void decode(FacesContext facesContext, UIComponent uiComponent)
104     {
105             RendererUtils.checkParamValidity(facesContext, uiComponent, HtmlCollapsiblePanel.class);
106         HtmlCollapsiblePanel collapsiblePanel = (HtmlCollapsiblePanel)uiComponent;
107         String JavaDoc reqValue = (String JavaDoc)facesContext.getExternalContext().getRequestParameterMap().get(HtmlRendererUtils.getHiddenCommandLinkFieldName(HtmlRendererUtils.getFormName(collapsiblePanel, facesContext)));
108             //log.debug("new component's id is " + collapsiblePanel.getClientId(facesContext) + ", req id is " + reqValue);
109
if ((collapsiblePanel.getClientId(facesContext) + LINK_ID).equals(reqValue)) collapsiblePanel.setCollapsed(!collapsiblePanel.isCollapsed() );
110     }
111     
112     protected HtmlCommandLink getLink(FacesContext facesContext, HtmlCollapsiblePanel collapsiblePanel)
113         throws IOException JavaDoc
114     {
115         Application application = facesContext.getApplication();
116         HtmlCommandLink link = (HtmlCommandLink)application.createComponent(HtmlCommandLink.COMPONENT_TYPE);
117         link.setId(collapsiblePanel.getId() + LINK_ID );
118         link.setTransient(true);
119         link.setImmediate(true);
120         //link.addActionListener(new ChangeCollapsedHandler());
121

122         List JavaDoc children = link.getChildren();
123                 // Create the indicator. You could later make this conditional and render optional images instead
124
HtmlOutputText uiText = (HtmlOutputText)application.createComponent(HtmlOutputText.COMPONENT_TYPE);
125                 uiText.setTransient(true);
126                 uiText.setValue(collapsiblePanel.isCollapsed() ? ">" : "ν");
127                 uiText.setEscape(false);
128                 uiText.setStyleClass(collapsiblePanel.getStyleClass());
129                 uiText.setStyle(collapsiblePanel.getStyle());
130                 children.add(uiText);
131                 
132                 // Create the optional label
133
String JavaDoc label = collapsiblePanel.getValue();
134                 if (label != null) {
135                     uiText = (HtmlOutputText)application.createComponent(HtmlOutputText.COMPONENT_TYPE);
136                     uiText.setTransient(true);
137                     uiText.setValue(" " + label);
138                     uiText.setStyleClass(collapsiblePanel.getStyleClass());
139                     uiText.setStyle(collapsiblePanel.getStyle());
140                     children.add(uiText);
141                 }
142                 return link;
143     }
144    
145     
146     // Couldn't get an ActionListner for a link to work properly. With each page submit, one more
147
// event was fired. I assume it is because the link component was set to transparent, and I didn't
148
// know how to get a reference to it back in a new encoding phase
149
/*
150     public static class ChangeCollapsedHandler implements ActionListener {
151         // Can't make this class anonymous, because it won't work with state saving
152         // refer to http://forum.java.sun.com/thread.jspa?messageID=2885214&#2885214
153         
154           /* (non-Javadoc)
155           * @see javax.faces.event.ActionListener#processAction(javax.faces.event.ActionEvent)
156           */

157     /*
158       public void processAction(ActionEvent actionEvent) throws AbortProcessingException {
159             log.info("Got action event, processing " + actionEvent.getComponent().getId() + ", phase id " + actionEvent.getPhaseId() );
160             if (!(actionEvent.getComponent().getParent() instanceof HtmlCollapsiblePanel) )
161                 throw new AbortProcessingException("The parent of the action source was of unexpected type, HtmlCollapsiblePanel was expected");
162             HtmlCollapsiblePanel collapsiblePanel = (HtmlCollapsiblePanel)actionEvent.getComponent().getParent();
163             collapsiblePanel.setCollapsed(!collapsiblePanel.isCollapsed() );
164         // Note that we need to remove the listeners here, otherwise they will be fired again for old components,
165         // don't quite understand why
166         ActionListener[] listeners = collapsiblePanel.getLink().getActionListeners();
167         for (int i= 0; i< listeners.length; i++) collapsiblePanel.getLink().removeActionListener(listeners[i]);
168         }
169         }
170     */

171 }
172
Popular Tags