View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.custom.suggestajax.tablesuggestajax;
20  
21  
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  
28  import javax.faces.component.UIComponent;
29  import javax.faces.component.UIViewRoot;
30  import javax.faces.context.FacesContext;
31  import javax.faces.context.ResponseWriter;
32  
33  import org.apache.myfaces.custom.inputAjax.Listener;
34  import org.apache.myfaces.custom.util.ComponentUtils;
35  import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
36  
37  /**
38   * Extending the outputText component in order to introduce the for attribute.
39   * 
40   * @JSFComponent
41   *   name = "s:outputText"
42   *   tagClass = "org.apache.myfaces.custom.suggestajax.tablesuggestajax.HtmlOutputTextTag"
43   *   class = "org.apache.myfaces.custom.suggestajax.tablesuggestajax.HtmlOutputText"
44   *   
45   * @author Gerald Muellan
46   *         Date: 15.02.2006
47   *         Time: 13:30:43
48   */
49  public abstract class AbstractHtmlOutputText extends org.apache.myfaces.component.html.ext.HtmlOutputText
50  {
51      public static final String COMPONENT_TYPE = "org.apache.myfaces.HtmlOutputTextFor";
52  
53      public AbstractHtmlOutputText()
54      {
55      }
56  
57      public void encodeBegin(FacesContext facesContext) throws IOException
58      {
59          super.encodeBegin(facesContext);
60      }
61  
62      public void encodeEnd(FacesContext facesContext) throws IOException
63      {
64          ResponseWriter writer = facesContext.getResponseWriter();
65          writer.startElement(HTML.SPAN_ELEM, null);
66          writer.writeAttribute("id", this.getClientId(facesContext), null);
67          super.encodeEnd(facesContext);
68          writer.endElement(HTML.SPAN_ELEM);
69      }
70  
71      public boolean getRendersChildren()
72      {
73          return true;
74      }
75  
76      public void encodeChildren(FacesContext facesContext) throws IOException
77      {
78          checkForListeners(facesContext, this);
79          super.encodeChildren(facesContext);
80      }
81  
82      private void checkForListeners(FacesContext context, UIComponent component)
83      {
84          // TODO: MOVE THIS UP THE TREE OR TO SOME OTHER LEVEL SO IT CAN WORK ON ANY COMPONENT
85          List children = component.getChildren();
86          if(children != null){
87              for (int i=0; i<children.size(); i++)
88              {
89                  UIComponent child = (UIComponent) children.get(i);
90                  if(child instanceof Listener){
91                      Listener listener = (Listener) child;
92                      Map rmap = context.getExternalContext().getRequestMap();
93                      List listeners = (List) rmap.get(Listener.LISTENER_MAP_ENTRY);
94                      if(listeners == null){
95                          listeners = new ArrayList();
96                          rmap.put(Listener.LISTENER_MAP_ENTRY, listeners);
97                      }
98                      // find component
99                      UIViewRoot root = context.getViewRoot();
100                     UIComponent ajaxComponent; //= component.findComponent(listener.getOn());
101                     //System.out.println("FINDING COMPONENT TO LISTEN ON: " + ajaxComponent);
102                     ajaxComponent = ComponentUtils.findComponentById(context, root, listener.getOn());
103                     if(ajaxComponent != null){
104                         //System.out.println("FINDING COMPONENT TO LISTEN ON: " + ajaxComponent);
105                         Map listenerItem = new HashMap();
106                         listenerItem.put("listenOn", ajaxComponent.getClientId(context));
107                         listenerItem.put("listenerId", component.getClientId(context));
108                         listenerItem.put("action", listener.getAction());
109                         listenerItem.put("eventType", listener.getEventType());
110                         listeners.add(listenerItem);
111                     }
112                 }
113             }
114         }
115     }
116 
117     /**
118      * Specify the id of the dom element where the label should be put in
119      * 
120      * @JSFProperty
121      */
122     public abstract String getFor();
123 
124     /**
125      * Specify the id of the dom element where the value should be put in. 
126      * This value is also included in the suggested table, but only in a 
127      * hidden span element following the span for the label in one row.
128      * 
129      * @JSFProperty
130      */
131     public abstract String getForValue();
132 
133     /**
134      * To provide a second value in form of a label. Usage like SelectItem. 
135      * Label is brought to client in a hidden span element near the value.
136      * 
137      * @JSFProperty
138      */
139     public abstract String getLabel();
140     
141 }