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.component.html.ext;
20  
21  import javax.faces.component.UINamingContainer;
22  import javax.faces.component.UIPanel;
23  import javax.faces.context.FacesContext;
24  
25  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFComponent;
26  import org.apache.myfaces.component.NewspaperTable;
27  
28  /**
29   * This component is used to render one row in a t:dataTable, when
30   * ajaxRowRender="true" is set.
31   * 
32   * @author Leonardo Uribe
33   */
34  @JSFComponent
35  public class HtmlTableRow extends UIPanel
36  {
37      public static final String COMPONENT_FAMILY = "org.apache.myfaces.HtmlTableRow"; 
38      public static final String COMPONENT_TYPE = "org.apache.myfaces.HtmlTableRow";
39      public static final String DEFAULT_RENDERER_TYPE = "org.apache.myfaces.HtmlTableRow";
40  
41      private String _alternateClientId;
42      
43      public HtmlTableRow()
44      {
45          setRendererType(DEFAULT_RENDERER_TYPE);
46      }
47      
48      @Override
49      public String getClientId(FacesContext context)
50      {
51          String inheritedClientId = super.getClientId(context);
52          if (getParent() instanceof HtmlDataTable)
53          {
54              HtmlDataTable dataTable = (HtmlDataTable) getParent();
55              if (dataTable.getNewspaperColumns() > 1)
56              {
57                  if (_alternateClientId == null)
58                  {
59                      char separator = UINamingContainer.getSeparatorChar(context); 
60                      _alternateClientId = dataTable.getClientId() + separator + 
61                                           getNewspaperRowIndex(dataTable) + separator + getId(); 
62                      return _alternateClientId;
63                  }
64                  else
65                  {
66                      return _alternateClientId;
67                  }
68              }
69              else
70              {
71                  return inheritedClientId;
72              }
73          }
74          else
75          {
76              return inheritedClientId;
77          }
78      }
79      
80      public int getNewspaperRowIndex(HtmlDataTable dataTable)
81      {
82          int rowCount = dataTable.getRowCount();
83          
84          int first = dataTable.getFirst();
85          int rows = dataTable.getRows();
86          int last;
87  
88          if (rows <= 0)
89          {
90             last = rowCount;
91          }
92          else
93          {
94             last = first + rows;
95             if (last > rowCount)
96             {
97                 last = rowCount;
98             }
99          }
100 
101         int newspaperColumns = dataTable.getNewspaperColumns();
102         int newspaperRows;
103         if((last - first) % newspaperColumns == 0)
104             newspaperRows = (last - first) / newspaperColumns;
105         else newspaperRows = ((last - first) / newspaperColumns) + 1;
106 
107         int currentRow = dataTable.getRowIndex();
108         int nr = NewspaperTable.NEWSPAPER_HORIZONTAL_ORIENTATION.equals(dataTable.getNewspaperOrientation()) ? 
109                 (currentRow-first)/newspaperColumns :
110                 (currentRow-first) % newspaperRows;
111         
112         return nr;
113     }
114     
115     @Override
116     public void setId(String id)
117     {
118         super.setId(id);
119         _alternateClientId = null;
120     }
121 
122     public String getFamily()
123     {
124         return COMPONENT_FAMILY;
125     }
126 }