Coverage Report - javax.faces.component.UIData
 
Classes in this File Line Coverage Branch Coverage Complexity
UIData
96%
195/203
86%
60/70
1.833
UIData$FacesEventWrapper
50%
8/16
N/A
1.833
 
 1  
 /*
 2  
  * Copyright 2004-2011 the Seasar Foundation and the Others.
 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,
 13  
  * either express or implied. See the License for the specific language
 14  
  * governing permissions and limitations under the License.
 15  
  */
 16  
 package javax.faces.component;
 17  
 
 18  
 import java.io.IOException;
 19  
 import java.util.Iterator;
 20  
 import java.util.Map;
 21  
 
 22  
 import javax.faces.context.FacesContext;
 23  
 import javax.faces.el.ValueBinding;
 24  
 import javax.faces.event.AbortProcessingException;
 25  
 import javax.faces.event.FacesEvent;
 26  
 import javax.faces.event.FacesListener;
 27  
 import javax.faces.event.PhaseId;
 28  
 import javax.faces.internal.ComponentStates;
 29  
 import javax.faces.internal.NamingContainerUtil;
 30  
 import javax.faces.internal.UIDataUtil;
 31  
 import javax.faces.model.DataModel;
 32  
 
 33  
 import org.seasar.framework.util.AssertionUtil;
 34  
 
 35  
 /**
 36  
  * @author shot
 37  
  * @author manhole
 38  
  */
 39  
 public class UIData extends UIComponentBase implements NamingContainer {
 40  
 
 41  
     public static final String COMPONENT_FAMILY = "javax.faces.Data";
 42  
 
 43  
     public static final String COMPONENT_TYPE = "javax.faces.Data";
 44  
 
 45  
     private static final String FOOTER_FACET_NAME = "footer";
 46  
 
 47  
     private static final String HEADER_FACET_NAME = "header";
 48  
 
 49  
     private static final String FIRST_BINDING_NAME = "first";
 50  
 
 51  
     private static final String VALUE_BINDING_NAME = "value";
 52  
 
 53  
     private static final String ROWS_BINDING_NAME = "rows";
 54  
 
 55  419
     private int first = 0;
 56  
 
 57  419
     private int rowIndex = 0;
 58  
 
 59  419
     private int rows = 0;
 60  
 
 61  419
     private boolean firstSet = false;
 62  
 
 63  419
     private boolean rowsSet = false;
 64  
 
 65  419
     private transient DataModel model = null;
 66  
 
 67  419
     private Object value = null;
 68  
 
 69  419
     private String var = null;
 70  
 
 71  419
     private ComponentStates componentStates = new ComponentStates();
 72  
 
 73  
     private static final String DEFAULT_RENDER_TYPE = "javax.faces.Table";
 74  
 
 75  
     public UIData() {
 76  419
         super();
 77  419
         setRendererType(DEFAULT_RENDER_TYPE);
 78  419
     }
 79  
 
 80  
     public String getFamily() {
 81  34
         return COMPONENT_FAMILY;
 82  
     }
 83  
 
 84  
     public void setId(String id) {
 85  75
         super.setId(id);
 86  63
         NamingContainerUtil.refreshDescendantComponentClientId(this);
 87  63
     }
 88  
 
 89  
     public int getFirst() {
 90  27
         if (firstSet) {
 91  10
             return first;
 92  
         }
 93  17
         Integer firstValue = (Integer) ComponentUtil_.getValueBindingValue(
 94  
                 this, FIRST_BINDING_NAME);
 95  17
         return (firstValue != null) ? firstValue.intValue() : first;
 96  
     }
 97  
 
 98  
     public void setFirst(int first) {
 99  11
         AssertionUtil.assertIntegerNotNegative("first", first);
 100  8
         this.first = first;
 101  8
         firstSet = true;
 102  8
     }
 103  
 
 104  
     public UIComponent getFooter() {
 105  35
         return getFacet(FOOTER_FACET_NAME);
 106  
     }
 107  
 
 108  
     public void setFooter(UIComponent footer) {
 109  13
         AssertionUtil.assertNotNull("footer", footer);
 110  10
         getFacets().put(FOOTER_FACET_NAME, footer);
 111  10
     }
 112  
 
 113  
     public UIComponent getHeader() {
 114  35
         return getFacet(HEADER_FACET_NAME);
 115  
     }
 116  
 
 117  
     public void setHeader(UIComponent header) {
 118  13
         AssertionUtil.assertNotNull("header", header);
 119  10
         getFacets().put(HEADER_FACET_NAME, header);
 120  10
     }
 121  
 
 122  
     public boolean isRowAvailable() {
 123  112
         return getDataModel().isRowAvailable();
 124  
     }
 125  
 
 126  
     public int getRowCount() {
 127  16
         return getDataModel().getRowCount();
 128  
     }
 129  
 
 130  
     public Object getRowData() {
 131  43
         return getDataModel().getRowData();
 132  
     }
 133  
 
 134  
     public int getRowIndex() {
 135  85
         return rowIndex;
 136  
     }
 137  
 
 138  
     public void setRowIndex(int rowIndex) {
 139  117
         saveDescendantState();
 140  117
         this.rowIndex = rowIndex;
 141  117
         DataModel model = getDataModel();
 142  117
         model.setRowIndex(rowIndex);
 143  
 
 144  117
         String var = getVar();
 145  117
         if (var != null) {
 146  47
             Map requestMap = getFacesContext().getExternalContext()
 147  
                     .getRequestMap();
 148  47
             if (rowIndex == -1) {
 149  5
                 requestMap.remove(var);
 150  42
             } else if (isRowAvailable()) {
 151  36
                 requestMap.put(var, getRowData());
 152  
             } else {
 153  6
                 requestMap.remove(var);
 154  
             }
 155  
         }
 156  117
         restoreDescendantState();
 157  117
     }
 158  
 
 159  
     public int getRows() {
 160  30
         if (rowsSet) {
 161  9
             return rows;
 162  
         }
 163  21
         Integer value = (Integer) ComponentUtil_.getValueBindingValue(this,
 164  
                 ROWS_BINDING_NAME);
 165  21
         return (value != null) ? value.intValue() : rows;
 166  
     }
 167  
 
 168  
     public void setRows(int rows) {
 169  13
         AssertionUtil.assertIntegerNotNegative("rows", rows);
 170  10
         this.rows = rows;
 171  10
         rowsSet = true;
 172  10
     }
 173  
 
 174  
     public String getVar() {
 175  125
         return var;
 176  
     }
 177  
 
 178  
     public void setVar(String var) {
 179  17
         this.var = var;
 180  17
     }
 181  
 
 182  
     public void restoreState(FacesContext context, Object state) {
 183  6
         Object[] values = (Object[]) state;
 184  6
         super.restoreState(context, values[0]);
 185  6
         first = ((Integer) values[1]).intValue();
 186  6
         firstSet = ((Boolean) values[2]).booleanValue();
 187  6
         rowIndex = ((Integer) values[3]).intValue();
 188  6
         rows = ((Integer) values[4]).intValue();
 189  6
         rowsSet = ((Boolean) values[5]).booleanValue();
 190  6
         value = (Object) values[6];
 191  6
         var = (String) values[7];
 192  6
     }
 193  
 
 194  
     public Object saveState(FacesContext context) {
 195  9
         Object[] values = new Object[8];
 196  9
         values[0] = super.saveState(context);
 197  9
         values[1] = new Integer(first);
 198  9
         values[2] = firstSet ? Boolean.TRUE : Boolean.FALSE;
 199  9
         values[3] = new Integer(rowIndex);
 200  9
         values[4] = new Integer(rows);
 201  9
         values[5] = rowsSet ? Boolean.TRUE : Boolean.FALSE;
 202  9
         values[6] = value;
 203  9
         values[7] = var;
 204  9
         return values;
 205  
     }
 206  
 
 207  
     public Object getValue() {
 208  58
         if (value != null) {
 209  51
             return value;
 210  
         }
 211  7
         return ComponentUtil_.getValueBindingValue(this, VALUE_BINDING_NAME);
 212  
     }
 213  
 
 214  
     public void setValue(Object value) {
 215  52
         this.value = value;
 216  52
         model = null;
 217  52
     }
 218  
 
 219  
     public void setValueBinding(String name, ValueBinding vb) {
 220  71
         AssertionUtil.assertNotNull("name", name);
 221  68
         if (name.equals("var") || name.equals("rowIndex")) {
 222  6
             throw new IllegalArgumentException("setValueBinding");
 223  62
         } else if (name.equals(VALUE_BINDING_NAME)) {
 224  3
             model = null;
 225  
         }
 226  62
         super.setValueBinding(name, vb);
 227  53
     }
 228  
 
 229  
     public String getClientId(FacesContext context) {
 230  78
         AssertionUtil.assertNotNull("context", context);
 231  75
         String clientId = super.getClientId(context);
 232  75
         if (rowIndex >= 0) {
 233  73
             clientId = clientId + NamingContainer.SEPARATOR_CHAR + rowIndex;
 234  
         }
 235  75
         return clientId;
 236  
     }
 237  
 
 238  
     public void queueEvent(FacesEvent event) {
 239  9
         AssertionUtil.assertNotNull("event", event);
 240  6
         super.queueEvent(new FacesEventWrapper(event, getRowIndex(), this));
 241  3
     }
 242  
 
 243  
     public void broadcast(FacesEvent event) throws AbortProcessingException {
 244  9
         AssertionUtil.assertNotNull("event", event);
 245  6
         if (!(event instanceof FacesEventWrapper)) {
 246  3
             super.broadcast(event);
 247  3
             return;
 248  
         }
 249  
 
 250  3
         FacesEventWrapper e = (FacesEventWrapper) event;
 251  3
         int rowIndex = getRowIndex();
 252  3
         setRowIndex(e.getRowIndex());
 253  3
         FacesEvent orgEvent = e.getFacesEvent();
 254  3
         orgEvent.getComponent().broadcast(orgEvent);
 255  3
         setRowIndex(rowIndex);
 256  3
     }
 257  
 
 258  
     public void encodeBegin(FacesContext context) throws IOException {
 259  5
         AssertionUtil.assertNotNull("context", context);
 260  2
         resetModelAndSavedState();
 261  2
         super.encodeBegin(context);
 262  2
     }
 263  
 
 264  
     public void processDecodes(FacesContext context) {
 265  9
         AssertionUtil.assertNotNull("context", context);
 266  6
         if (!isRendered()) {
 267  3
             return;
 268  
         }
 269  3
         processAppropriateAction(context, PhaseId.APPLY_REQUEST_VALUES);
 270  
         try {
 271  3
             decode(context);
 272  0
         } catch (RuntimeException e) {
 273  0
             context.renderResponse();
 274  0
             throw e;
 275  3
         }
 276  3
     }
 277  
 
 278  
     public void processUpdates(FacesContext context) {
 279  9
         AssertionUtil.assertNotNull("context", context);
 280  6
         if (!isRendered()) {
 281  3
             return;
 282  
         }
 283  3
         processAppropriateAction(context, PhaseId.UPDATE_MODEL_VALUES);
 284  3
         resetModelAndSavedState();
 285  3
     }
 286  
 
 287  
     public void processValidators(FacesContext context) {
 288  9
         AssertionUtil.assertNotNull("context", context);
 289  6
         if (!isRendered()) {
 290  3
             return;
 291  
         }
 292  3
         processAppropriateAction(context, PhaseId.PROCESS_VALIDATIONS);
 293  3
     }
 294  
 
 295  
     protected void processAppropriateAction(FacesContext context,
 296  
             PhaseId phaseId) {
 297  9
         processFacets(context, phaseId);
 298  9
         processColumns(context, phaseId);
 299  9
         processColumnsChildren(context, phaseId);
 300  9
     }
 301  
 
 302  
     protected void processFacets(FacesContext context, PhaseId phaseId) {
 303  9
         setRowIndex(-1);
 304  9
         for (Iterator facets = getFacets().keySet().iterator(); facets
 305  18
                 .hasNext();) {
 306  9
             UIComponent facet = (UIComponent) getFacets().get(facets.next());
 307  9
             ComponentUtil_.processAppropriatePhaseAction(context, facet,
 308  
                     phaseId);
 309  
         }
 310  9
     }
 311  
 
 312  
     protected void processColumns(FacesContext context, PhaseId phaseId) {
 313  9
         setRowIndex(-1);
 314  9
         for (Iterator columns = getChildren().iterator(); columns.hasNext();) {
 315  9
             UIComponent column = (UIComponent) columns.next();
 316  9
             if (!(column instanceof UIColumn)) {
 317  0
                 continue;
 318  
             }
 319  9
             if (!column.isRendered()) {
 320  0
                 continue;
 321  
             }
 322  9
             for (Iterator columnFacets = column.getFacets().keySet().iterator(); columnFacets
 323  18
                     .hasNext();) {
 324  9
                 UIComponent columnFacet = (UIComponent) column.getFacets().get(
 325  
                         columnFacets.next());
 326  9
                 ComponentUtil_.processAppropriatePhaseAction(context,
 327  
                         columnFacet, phaseId);
 328  
             }
 329  
         }
 330  9
     }
 331  
 
 332  
     protected void processColumnsChildren(FacesContext context, PhaseId phaseId) {
 333  9
         int first = getFirst();
 334  9
         int rows = getRows();
 335  9
         int last = (rows != 0) ? (first + rows) : getRowCount();
 336  36
         for (int rowIndex = first; rowIndex < last; rowIndex++) {
 337  27
             setRowIndex(rowIndex);
 338  27
             if (!isRowAvailable()) {
 339  0
                 continue;
 340  
             }
 341  27
             for (Iterator children = getChildren().iterator(); children
 342  54
                     .hasNext();) {
 343  27
                 UIComponent child = (UIComponent) children.next();
 344  27
                 if (!(child instanceof UIColumn)) {
 345  0
                     continue;
 346  
                 }
 347  27
                 for (Iterator grandChildren = child.getChildren().iterator(); grandChildren
 348  54
                         .hasNext();) {
 349  27
                     UIComponent grandChild = (UIComponent) grandChildren.next();
 350  27
                     if (!grandChild.isRendered()) {
 351  0
                         continue;
 352  
                     }
 353  27
                     ComponentUtil_.processAppropriatePhaseAction(context,
 354  
                             grandChild, phaseId);
 355  
                 }
 356  
             }
 357  
         }
 358  9
         setRowIndex(-1);
 359  9
     }
 360  
 
 361  
     protected void saveDescendantState() {
 362  117
         FacesContext context = getFacesContext();
 363  117
         for (Iterator itr = getChildren().iterator(); itr.hasNext();) {
 364  130
             UIComponent child = (UIComponent) itr.next();
 365  130
             if (child instanceof UIColumn) {
 366  130
                 saveDescendantState(child, context);
 367  
             }
 368  
         }
 369  117
     }
 370  
 
 371  
     protected void saveDescendantState(UIComponent component,
 372  
             FacesContext context) {
 373  130
         componentStates.saveDescendantComponentStates(context, component);
 374  130
     }
 375  
 
 376  
     protected void restoreDescendantState() {
 377  117
         FacesContext context = getFacesContext();
 378  117
         for (Iterator itr = getChildren().iterator(); itr.hasNext();) {
 379  130
             UIComponent child = (UIComponent) itr.next();
 380  130
             if (child instanceof UIColumn) {
 381  130
                 restoreDescendantState(child, context);
 382  
             }
 383  
         }
 384  117
     }
 385  
 
 386  
     protected void restoreDescendantState(UIComponent component,
 387  
             FacesContext context) {
 388  130
         componentStates.restoreDescendantState(context, component);
 389  130
     }
 390  
 
 391  
     protected DataModel getDataModel() {
 392  288
         if (model != null) {
 393  241
             return model;
 394  
         }
 395  47
         model = UIDataUtil.getSuitableDataModel(getValue());
 396  47
         return model;
 397  
     }
 398  
 
 399  
     private void resetModelAndSavedState() {
 400  5
         model = null;
 401  5
         componentStates.clear();
 402  5
     }
 403  
 
 404  
     static class FacesEventWrapper extends FacesEvent {
 405  
 
 406  
         private static final long serialVersionUID = 1L;
 407  
 
 408  9
         private FacesEvent event_ = null;
 409  
 
 410  9
         private int index_ = -1;
 411  
 
 412  
         public FacesEventWrapper(FacesEvent event, int index,
 413  
                 UIComponent component) {
 414  9
             super(component);
 415  9
             event_ = event;
 416  9
             index_ = index;
 417  9
         }
 418  
 
 419  
         public FacesEvent getFacesEvent() {
 420  6
             return event_;
 421  
         }
 422  
 
 423  
         public int getRowIndex() {
 424  3
             return index_;
 425  
         }
 426  
 
 427  
         public PhaseId getPhaseId() {
 428  0
             return event_.getPhaseId();
 429  
         }
 430  
 
 431  
         public void setPhaseId(PhaseId phaseId) {
 432  0
             event_.setPhaseId(phaseId);
 433  0
         }
 434  
 
 435  
         public boolean isAppropriateListener(FacesListener listener) {
 436  0
             return event_.isAppropriateListener(listener);
 437  
         }
 438  
 
 439  
         public void processListener(FacesListener listener) {
 440  0
             event_.processListener(listener);
 441  0
         }
 442  
 
 443  
         public void queue() {
 444  0
             event_.queue();
 445  0
         }
 446  
     }
 447  
 }