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  
11  /***
12   * A ChainedOptionStrategy that adds no extra items and returns the first in the
13   * option list as default value.
14   * 
15   * @author Mattias Arthursson
16   */
17  public class FirstItemStrategy implements ChainedOptionStrategy {
18  
19      /***
20       * Just sort the options for this implementation.
21       * 
22       * @param options
23       *            the options to sort.
24       * @param context
25       *            ignored for this implementation.
26       * 
27       * @return a new Sorted list containing the options.
28       */
29      public List adjustAndSort(List options, Object context) {
30          LinkedList result = new LinkedList();
31          result.addAll(options);
32          Collections.sort(result);
33          return result;
34      }
35  
36      /***
37       * Return the value of the first item as default value. If the list is
38       * empty, return empty String.
39       * 
40       * @param options
41       *            the list of options.
42       * @param context
43       *            ignored for this implementation.
44       * @return the value of the first item in the List if the List is not empty,
45       *         empty String otherwise.
46       */
47      public String getDefaultValue(List options, Object context) {
48          if (options.size() > 0) {
49              LabelValueBean bean = (LabelValueBean) options.get(0);
50              return bean.getValue();
51          } else {
52              return "";
53          }
54      }
55  
56  }