View Javadoc

1   package net.sf.chainedoptions.support;
2   
3   import java.util.Collections;
4   import java.util.LinkedList;
5   import java.util.List;
6   
7   import net.sf.chainedoptions.ChainedOptionStrategy;
8   import net.sf.chainedoptions.LabelValueBean;
9   
10  import org.springframework.beans.factory.InitializingBean;
11  
12  
13  /***
14   * Strategy that adds a set of extra items to a given list of options. The set
15   * of extra items is configurable. The strategy also provides a default value,
16   * which is also configurable.
17   * 
18   * @author Mattias Arthursson
19   * @author Ulrik Sandberg
20   */
21  public class ExtraItemsStrategy implements ChainedOptionStrategy,
22          InitializingBean {
23  
24      private List extraItems;
25  
26      private LabelValueBean defaultItem;
27  
28      /*
29       * @see net.sf.chainedoptions.ChainedOptionStrategy#adjustAndSort(java.util.List, java.lang.Object)
30       */
31      public List adjustAndSort(List options, Object context) {
32          List adjusted = new LinkedList(options);
33          adjusted.addAll(extraItems);
34          Collections.sort(adjusted);
35          return adjusted;
36      }
37  
38      /*
39       * @see net.sf.chainedoptions.ChainedOptionStrategy#getDefaultValue(java.util.List, java.lang.Object)
40       */
41      public String getDefaultValue(List options, Object context) {
42          return getDefaultItem().getValue();
43      }
44  
45      /***
46       * Extra items are items that signify special values, like ANY, EMPTY,
47       * NOCHANGE etc.
48       * 
49       * @param extraItems
50       *            The extra items to set as a list of LabelValueBean objects.
51       */
52      public void setExtraItems(List extraItems) {
53          this.extraItems = extraItems;
54      }
55  
56      /***
57       * The default item is the item in the list that should be selected when the
58       * list is first displayed.
59       * 
60       * @return Returns the default item as a LabelValueBean.
61       */
62      public LabelValueBean getDefaultItem() {
63          return defaultItem;
64      }
65  
66      /***
67       * The default item is the item in the list that should be selected when the
68       * list is first displayed.
69       * 
70       * @param defaultItem
71       *            The default item to set as a LabelValueBean.
72       */
73      public void setDefaultItem(LabelValueBean defaultItem) {
74          this.defaultItem = defaultItem;
75      }
76  
77      /*
78       * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
79       */
80      public void afterPropertiesSet() throws Exception {
81          if (defaultItem == null) {
82              throw new IllegalArgumentException(
83                      "Property 'defaultItem' must be set");
84          }
85          if (!extraItems.contains(defaultItem)) {
86              throw new IllegalArgumentException(
87                      "Property 'defaultItem' does not exist in 'extraItems'");
88          }
89      }
90  }