View Javadoc

1   package com.atlassian.plugin.web.model;
2   
3   import com.atlassian.plugin.PluginParseException;
4   import com.atlassian.plugin.web.WebFragmentHelper;
5   import com.atlassian.plugin.web.ContextProvider;
6   import com.atlassian.plugin.web.descriptors.WebFragmentModuleDescriptor;
7   import org.dom4j.Element;
8   
9   import javax.servlet.http.HttpServletRequest;
10  import java.util.*;
11  
12  /**
13   * A simple bean to represent labels in the web interface.
14   */
15  public class DefaultWebLabel extends DefaultWebParam implements WebLabel
16  {
17      private final String key;
18      private final String noKeyValue;
19  
20      public DefaultWebLabel(Element labelEl, WebFragmentHelper webFragmentHelper, ContextProvider contextProvider, WebFragmentModuleDescriptor descriptor) throws PluginParseException
21      {
22          super(labelEl, webFragmentHelper, contextProvider, descriptor);
23          if (labelEl == null)
24          {
25              throw new PluginParseException("You must specify a label for the section.");
26          }
27          else
28          {
29              this.key = labelEl.attributeValue("key");
30  
31              if (this.key == null)
32              {
33                  this.noKeyValue = labelEl.getTextTrim();
34              }
35              else
36              {
37                  this.noKeyValue = null;
38              }
39          }
40      }
41  
42      public String getKey()
43      {
44          return key;
45      }
46  
47      public String getNoKeyValue()
48      {
49          return noKeyValue;
50      }
51  
52      public String getDisplayableLabel(HttpServletRequest req, Map<String,Object> origContext)
53      {
54          Map<String,Object> tmpContext = new HashMap<String,Object>(origContext);
55          tmpContext.putAll(getContextMap(tmpContext));
56          if (key != null)
57          {
58              if (params == null || params.isEmpty())
59              {
60                  return getWebFragmentHelper().getI18nValue(key, null, tmpContext);
61              }
62              else
63              {
64                  List arguments = new ArrayList();
65  
66                  // we know here because it's a tree map that the params are in alphabetical order
67                  for (Iterator iterator = params.keySet().iterator(); iterator.hasNext();)
68                  {
69                      String key = (String) iterator.next();
70                      if (key.startsWith("param"))
71                          arguments.add(getWebFragmentHelper().renderVelocityFragment(params.get(key), tmpContext));
72                  }
73  
74                  return getWebFragmentHelper().getI18nValue(key, arguments, tmpContext);
75              }
76          }
77          else
78          {
79              return getWebFragmentHelper().renderVelocityFragment(noKeyValue, tmpContext);
80          }
81      }
82  }