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

Quick Search    Search Deep

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


1   /*
2    * WOSimpleArrayDisplay.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 com.webobjects.foundation.*;
12  import com.webobjects.eocontrol.*;
13  
14  import java.util.*;
15  
16  public class WOSimpleArrayDisplay extends WOComponent
17  {
18      // required
19      public NSArray list;
20      // optional
21      public int numberToDisplay;
22      public String itemDisplayKey;
23      public String listAction;
24      public String listActionString;
25      public Object currentItem; // used by subclasses
26      // internal/private
27      protected int _realSize;
28      protected NSArray _subList;
29  
30      public WOSimpleArrayDisplay(WOContext aContext)  {
31          super(aContext);
32          numberToDisplay = -1;
33      }
34  
35      public boolean isStateless() {
36          return true;
37      }
38  
39      public void reset()  {
40          _invalidateCaches();
41      }
42  
43      public NSArray list()  {
44          list = (list != null) ? list : (NSArray)_WOJExtensionsUtil.valueForBindingOrNull("list",this);
45          if (list == null) {
46              throw new IllegalStateException("<"+getClass().getName()+" list binding required. list value is null or missing>");
47          }
48          return list;
49      }
50  
51      public String itemDisplayKey()  {
52          if (null==itemDisplayKey) {
53              if (hasBinding("itemDisplayKey")) {
54                  itemDisplayKey = (String)_WOJExtensionsUtil.valueForBindingOrNull("itemDisplayKey",this);
55              }
56          }
57          return itemDisplayKey;
58      }
59  
60      public String listAction()  {
61          if (null==listAction)
62              listAction=(String)_WOJExtensionsUtil.valueForBindingOrNull("listAction",this);
63          return listAction;
64      }
65  
66      public String listActionString() {
67          if (null==listActionString)
68              listActionString=(String)_WOJExtensionsUtil.valueForBindingOrNull("listActionString",this);
69          return listActionString;
70      }
71  
72      
73      public int numberToDisplay()  {
74          if (numberToDisplay == -1) {
75              Object numStr = valueForBinding("numberToDisplay");
76              if (numStr != null) {
77                  try {
78                      numberToDisplay = Integer.parseInt(numStr.toString());
79                  } catch (NumberFormatException e) {
80                      throw new IllegalStateException("WOSimpleArrayDisplay - problem parsing int from numberToDisplay binding "+e);
81                  }
82              } else {
83                  numberToDisplay = 5;
84              }
85              if (numberToDisplay <= 0) {
86                  throw new RuntimeException ("<"+getClass().getName()+" numberToDisplay can not be <=0 !");
87              }
88          }
89          return numberToDisplay;
90      }
91  
92  
93  
94      public int realSize()  {
95          return _realSize;
96      }
97  
98      public NSArray subList()  {
99          if (null==_subList) {
100             _realSize = list().count();
101             if (_realSize > numberToDisplay()) {
102                 int anIndex;
103                 int count = numberToDisplay();
104                 NSMutableArray aSubList = new NSMutableArray(count);
105                 for (anIndex = 0; anIndex < count ; anIndex++) {
106                     aSubList.addObject(list().objectAtIndex(anIndex));
107                 }
108                 _subList = aSubList;
109             } else {
110                 _subList = list();
111             }
112         }
113         return _subList;
114     }
115 
116     public String displayStringForItem()  {
117         String displayStringForItem = null;
118         if (itemDisplayKey()!=null) {
119             displayStringForItem = (String) NSKeyValueCodingAdditions.Utility.valueForKeyPath(currentItem, itemDisplayKey());
120         } else {
121             displayStringForItem = (String) NSKeyValueCoding.Utility.valueForKey(currentItem, "userPresentableDescription");
122         }
123         return displayStringForItem;
124     }
125 
126     public boolean isDisplayingSubset()  {
127         return (realSize() > numberToDisplay());
128     }
129 
130     public WOActionResults listActionClicked()  {
131         return performParentAction(listAction());
132     }
133 
134     public boolean hasItems()  {
135         return (list().count()!=0);
136     }
137 
138     protected void _invalidateCaches() {
139         // ** By setting these to null, we allow for cycling of the page)
140         _subList = null;
141         list = null;
142         itemDisplayKey = null;
143         listAction = null;
144         listActionString = null;
145         numberToDisplay=-1;
146     }
147 }