View Javadoc

1   package de.matthias_burbach.mosaique.core.model;
2   
3   import java.util.ArrayList;
4   import java.util.Collections;
5   import java.util.Comparator;
6   import java.util.HashMap;
7   import java.util.Iterator;
8   import java.util.List;
9   import java.util.Map;
10  
11  /***
12   * Represents the complete Tiles context that is composed of the attributes
13   * defined along the definition hierarchy of a Tile plus the attributes that
14   * may be defined in the insert element inserting this tile in some JSP.
15   *
16   * @author Matthias Burbach
17   */
18  public class TilesContext {
19      /***
20       * The put objects of type {@link Put} keyed by their names.
21       */
22      private Map puts = new HashMap();
23  
24      /***
25       * @return The put objects of type {@link Put} keyed by their names.
26       */
27      public Map getPuts() {
28          return puts;
29      }
30  
31      /***
32       * @return The put objects of type {@link Put} ordered ascendingly by the
33       *         levels of the definitions defining them.
34       *         See {@link Definition#getLevel()} to understand the meaning of
35       *         levels.
36       */
37      public List getPutsOrderedByLevel() {
38          List result = new ArrayList();
39          for (Iterator iter = puts.values().iterator(); iter.hasNext();) {
40              Put put = (Put) iter.next();
41              result.add(put);
42          }
43          Collections.sort(result, new Comparator() {
44                  /*(non-Javadoc)
45                   * @see java.util.Comparator#compare(
46                   *          java.lang.Object, java.lang.Object)
47                   */
48                  /***
49                   * {@inheritDoc}
50                   */
51                  public int compare(final Object o1, final Object o2) {
52                      Put put1 = (Put) o1;
53                      Put put2 = (Put) o2;
54                      int compareResult = put1.getLevel() - put2.getLevel();
55                      if (compareResult == 0) {
56                          /*
57                           * order by occurence within definition
58                           */
59                          compareResult = put1.getBegin() - put2.getBegin();
60                      }
61                      return compareResult;
62                  }
63              }
64          );
65          return result;
66      }
67  
68      /***
69       * @param additionalValues Adds all puts of type {@link Put} keyed by their
70       *                         names of type {@link String} to the map of
71       *                         already puts. Will override existing puts with
72       *                         same names, so make sure to add in good order
73       *                         that reflects the definition hierarchy.
74       */
75      public void addPuts(final Map additionalValues) {
76          puts.putAll(additionalValues);
77      }
78  
79      /***
80       * @param name The name of a put attribute.
81       * @return The value of a put attribute or <code>null</code> if the name
82       *         is unknown.
83       */
84      public String getValue(final String name) {
85          String result = null;
86          Put put = ((Put) puts.get(name));
87          if (put != null) {
88              result = put.getValue();
89          }
90          return result;
91      }
92  }