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.ifmessage;
20  
21  import java.io.IOException;
22  import java.util.StringTokenizer;
23  
24  import javax.faces.component.UIComponent;
25  import javax.faces.component.UIComponentBase;
26  import javax.faces.context.FacesContext;
27  
28  /**
29   * Provide a component that will optionally render its children if the specified component(s) specified has a message.
30   * 
31   * Renders children if any of the component(s) specified in "for" has a message in the context.
32   * 
33   * @JSFComponent
34   *   name = "s:ifMessage"
35   *   class = "org.apache.myfaces.custom.ifmessage.IfMessage"
36   *   tagClass = "org.apache.myfaces.custom.ifmessage.IfMessageTag"
37   *   
38   * @author Mike Youngstrom (latest modification by $Author: skitching $)
39   * @version $Revision: 673833 $ $Date: 2008-07-03 16:58:05 -0500 (Thu, 03 Jul 2008) $
40   */
41  public abstract class AbstractIfMessage extends UIComponentBase {
42      
43      public static final String COMPONENT_TYPE = "org.apache.myfaces.IfMessage";
44      public static final String DEFAULT_RENDERER_TYPE = "org.apache.myfaces.IfMessageRenderer";
45      public static final String COMPONENT_FAMILY = "javax.faces.Panel";
46          
47      /**
48       * @JSFProperty
49       * @return
50       */
51      public abstract String getFor();
52      
53      private boolean isMessageForId(String id) {
54          UIComponent component = findComponent(id); 
55          if(component != null) {
56              String clientId = component.getClientId(FacesContext.getCurrentInstance());
57              return FacesContext.getCurrentInstance().getMessages(clientId).hasNext();
58          }
59          return false;
60      }
61      
62      public boolean getRendersChildren() {
63          return true;
64      }
65      
66      public void encodeChildren(FacesContext context) throws IOException {
67          if(getFor() != null) {
68              StringTokenizer tokenizer = new StringTokenizer(getFor(), ",");
69              while(tokenizer.hasMoreTokens()) {
70                  if(isMessageForId(tokenizer.nextToken().trim())) {
71                      super.encodeChildren(context);
72                      break;
73                  }
74              }
75          } else {
76              if(FacesContext.getCurrentInstance().getMessages().hasNext()) {
77                  super.encodeChildren(context);
78              }
79          }
80      }
81      
82  }
83  
84