Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/webobjects/woextensions/WOTable.java


1   /*
2    * WOTable.java
3    * © Copyright 2001 Apple Computer, Inc. All rights reserved.
4    * This a modified version.
5    * Original license: http://www.opensource.apple.com/apsl/
6    */
7   
8   package com.webobjects.woextensions;
9   
10  import com.webobjects.appserver.*;
11  import java.util.*;
12  import com.webobjects.foundation.*;
13  
14  public class WOTable extends WOComponent {
15      protected NSArray _list;
16      protected int _maxColumns;
17      public int currentRow;
18      public int currentCol;
19      protected int _rowCount;
20      protected int _colCount;
21  
22      public WOTable(WOContext aContext)  {
23          super(aContext);
24          _resetInternalCaches();
25      }
26  
27      public boolean isStateless() {
28          return true;
29      }
30  
31      public NSArray list()  {
32          if (_list==null) {
33              _list = (NSArray)_WOJExtensionsUtil.valueForBindingOrNull("list",this);
34              if (_list == null) {
35                  _list = NSArray.EmptyArray;
36              }
37          }
38          return _list;
39      }
40  
41      public int maxColumns()  {
42          if (_maxColumns == -1) {
43              Object maxStr = valueForBinding("maxColumns");
44              if (maxStr != null) {
45                  try {
46                      _maxColumns = Integer.parseInt(maxStr.toString());
47                  } catch (NumberFormatException e) {
48                      throw new IllegalStateException("WOTable - problem parsing int from maxColumns binding "+e);
49                  }
50              }
51              if (_maxColumns <= 0)
52                  _maxColumns=1;
53         }
54          return _maxColumns;
55      }
56  
57  
58      public int rowCount()  {
59          if (_rowCount == -1) {
60              NSArray aList = list();
61              int aMaxColCount = maxColumns();
62              int aListCount = aList.count();
63              int aRemainder = 0;
64              if (aMaxColCount!=0)  {
65                  _rowCount = aListCount / aMaxColCount;
66                  aRemainder = aListCount % aMaxColCount;
67              }
68              if (aRemainder!=0) {
69                  _rowCount++;
70              }
71          }
72          return _rowCount;
73      }
74  
75      public int colCount()  {
76          if (_colCount == -1) {
77              int aMaxColumns = maxColumns();
78              NSArray aList = list();
79              if (currentRow < (rowCount() - 1)) {
80                  _colCount = aMaxColumns;
81              } else {
82                  if (aMaxColumns!=0)
83                      _colCount = aList.count() % aMaxColumns;
84                  if (_colCount == 0) {
85                      _colCount = aMaxColumns;
86                  }
87              }
88          }
89          return _colCount;
90      }
91  
92      public void setCurrentRow(Number newValue) {
93          if (newValue!=null) {
94              currentRow=newValue.intValue();
95              _colCount=-1;
96              currentCol=-1;
97          }
98      }
99  
100 
101     public void pushItem()  {
102         NSArray aList = list();
103         int index = currentCol+maxColumns()*currentRow;
104         Object item = index < aList.count() ? aList.objectAtIndex(index) : null;
105         setValueForBinding(item, "item");
106         if (canSetValueForBinding("row")) {
107             setValueForBinding(new Integer(currentRow), "row");
108         }
109         if (canSetValueForBinding("col")) {
110             setValueForBinding(new Integer(currentCol), "col");
111         }
112         if (canSetValueForBinding("index")) {
113             setValueForBinding(new Integer(index), "index");
114         }
115     }
116 
117     public void setCurrentCol(Number newValue){
118         currentCol=newValue.intValue();
119         pushItem();
120     }
121 
122     protected void _resetInternalCaches() {
123         _list=null;
124         _rowCount=-1;
125         _colCount=-1;
126         currentCol=-1;
127         currentRow=-1;
128         _maxColumns = -1;
129     }
130 
131     public void takeValuesFromRequest(WORequest aRequest, WOContext aContext)  {
132         _resetInternalCaches();
133         super.takeValuesFromRequest(aRequest, aContext);
134     }
135 
136     public void reset() {
137         _resetInternalCaches();
138     }
139 }