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.collapsiblepanel;
20  
21  import javax.faces.component.UIComponent;
22  import javax.faces.context.FacesContext;
23  
24  import org.apache.myfaces.component.UserRoleAware;
25  import org.apache.myfaces.component.html.ext.HtmlCommandLink;
26  
27  /**
28   * Link used to collapse or expand a t:collapsiblePanel. 
29   * 
30   * Unless otherwise specified, all attributes accept static values or EL expressions.
31   * 
32   * @JSFComponent
33   *   name = "t:headerLink"
34   *   class = "org.apache.myfaces.custom.collapsiblepanel.HtmlHeaderLink"
35   *   tagClass = "org.apache.myfaces.custom.collapsiblepanel.HtmlHeaderLinkTag"
36   *
37   * @since 1.1.7
38   * @author Martin Marinschek (latest modification by $Author: lu4242 $)
39   * @version $Revision: 691856 $ $Date: 2008-09-03 21:40:30 -0500 (Wed, 03 Sep 2008) $
40   *
41   */
42  public abstract class AbstractHtmlHeaderLink extends HtmlCommandLink
43  {
44      public static final String COMPONENT_TYPE = "org.apache.myfaces.HtmlHeaderLink";
45      public static final String DEFAULT_RENDERER_TYPE = "org.apache.myfaces.HeaderLink";
46      
47      private static final String LINK_ID = "ToggleCollapsed".intern();
48      
49      public String getClientId(FacesContext context)
50      {
51          if (context == null) throw new NullPointerException("context");
52  
53          String clientId;
54          
55          //Try to find its nearest parent that extends form HtmlCollapsiblePanel
56          //to calculate its id.
57          UIComponent collapsiblePanel = findParentCollapsiblePanel(this);
58          
59          if (collapsiblePanel == null)
60          {
61              //Calculate its id normally
62              clientId = super.getClientId(context);
63          }
64          else
65          {
66              //Create its id based on collapsiblePanel id.
67              //There only could exists one headerLink per collapsiblePanel.
68              String calculatedId = collapsiblePanel.getClientId(context) + LINK_ID;
69              
70              //Get the original
71              clientId = super.getClientId(context);
72              
73              //just change the final id with the calculated id.
74              int lastDoublePointLocation = clientId.lastIndexOf(':');
75              clientId = clientId.substring(0,lastDoublePointLocation) +
76                  calculatedId.substring(lastDoublePointLocation);
77          }
78          
79          return clientId;
80      }
81      
82      protected static UIComponent findParentCollapsiblePanel(UIComponent component)
83      {
84          UIComponent currentComponent = component;
85  
86          // Search for an ancestor that is a instance of HtmlCollapsiblePanel
87          while (null != (currentComponent = currentComponent.getParent()))
88          {
89              if (currentComponent instanceof HtmlCollapsiblePanel)
90              {
91                  break;
92              }
93          }
94          return currentComponent;
95      }
96      
97      /**
98       * This property is no longer available since getClientId()
99       * is overridden to proper working of collapsiblePanel
100      * 
101      * @JSFProperty
102      *   tagExcluded = "true"
103      *   
104      * @return
105      */
106     public Boolean getForceId()
107     {
108         return Boolean.FALSE;
109     }
110     
111     public void setForceId(Boolean forceId){
112         throw new UnsupportedOperationException(); 
113     }
114     
115     /**
116      * This property is no longer available since getClientId()
117      * is overridden to proper working of collapsiblePanel
118      * 
119      * @JSFProperty
120      *   tagExcluded = "true"
121      *   
122      * @return
123      */
124     public Boolean getForceIdIndex()
125     {
126         return Boolean.TRUE;
127     }
128     
129     public void setForceIdIndex(Boolean forceIdIndex)
130     {
131         throw new UnsupportedOperationException();
132     }
133 
134 }