View Javadoc

1   package com.atlassian.plugin.web.descriptors;
2   
3   import com.atlassian.plugin.Plugin;
4   import com.atlassian.plugin.PluginParseException;
5   import com.atlassian.plugin.StateAware;
6   import com.atlassian.plugin.descriptors.AbstractModuleDescriptor;
7   import com.atlassian.plugin.descriptors.ModuleDescriptors;
8   import com.atlassian.plugin.module.ModuleFactory;
9   import com.atlassian.plugin.web.Condition;
10  import com.atlassian.plugin.web.ContextProvider;
11  import com.atlassian.plugin.web.WebInterfaceManager;
12  import com.atlassian.plugin.web.conditions.ConditionLoadingException;
13  import com.atlassian.plugin.web.model.DefaultWebLabel;
14  import com.atlassian.plugin.web.model.DefaultWebParam;
15  import com.atlassian.plugin.web.model.WebLabel;
16  import com.atlassian.plugin.web.model.WebParam;
17  import org.dom4j.Element;
18  
19  import java.util.List;
20  
21  /**
22   * An abstract convenience class for web fragment descriptors.
23   */
24  public abstract class AbstractWebFragmentModuleDescriptor<T> extends AbstractModuleDescriptor<T> implements StateAware, WebFragmentModuleDescriptor<T>
25  {
26      protected WebInterfaceManager webInterfaceManager;
27      protected Element element;
28      protected int weight;
29  
30      protected Condition condition;
31      protected ContextProvider contextProvider;
32      protected DefaultWebLabel label;
33      protected DefaultWebLabel tooltip;
34      protected WebParam params;
35      private ConditionElementParser conditionElementParser;
36      private ContextProviderElementParser contextProviderElementParser;
37  
38      protected AbstractWebFragmentModuleDescriptor(final WebInterfaceManager webInterfaceManager)
39      {
40          super(ModuleFactory.LEGACY_MODULE_FACTORY);
41          setWebInterfaceManager(webInterfaceManager);
42      }
43  
44      public AbstractWebFragmentModuleDescriptor()
45      {
46          super(ModuleFactory.LEGACY_MODULE_FACTORY);
47      }
48  
49      public AbstractWebFragmentModuleDescriptor(ModuleFactory moduleClassFactory, WebInterfaceManager webInterfaceManager) {
50          super(moduleClassFactory);
51          setWebInterfaceManager(webInterfaceManager);
52      }
53  
54      @Override
55      public void init(final Plugin plugin, final Element element) throws PluginParseException
56      {
57          super.init(plugin, element);
58  
59          this.element = element;
60          weight = WeightElementParser.getWeight(element);
61      }
62  
63      /**
64       * Create a condition for when this web fragment should be displayed
65       * 
66       * @param element Element of web-section or web-item
67       * @param type logical operator type
68       * @throws PluginParseException
69       */
70      protected Condition makeConditions(final Element element, final int type) throws PluginParseException
71      {
72          return getRequiredConditionElementParser().makeConditions(plugin, element, type);
73      }
74  
75      @SuppressWarnings("unchecked")
76      protected Condition makeConditions(final List elements, final int type) throws PluginParseException
77      {
78          return getRequiredConditionElementParser().makeConditions(plugin, elements, type);
79      }
80  
81      protected Condition makeCondition(final Element element) throws PluginParseException
82      {
83          return getRequiredConditionElementParser().makeCondition(plugin, element);
84      }
85  
86      protected ContextProvider makeContextProvider(final Element element) throws PluginParseException
87      {
88          return contextProviderElementParser.makeContextProvider(plugin, element.getParent());
89      }
90  
91      private ConditionElementParser getRequiredConditionElementParser()
92      {
93          if (conditionElementParser == null)
94          {
95              throw new IllegalStateException("ModuleDescriptorHelper not available because the WebInterfaceManager has not been injected.");
96          }
97          else
98          {
99              return conditionElementParser;
100         }
101     }
102 
103     @Override
104     public void enabled()
105     {
106         super.enabled();
107         // this was moved to the enabled() method because spring beans declared
108         // by the plugin are not available for injection during the init() phase
109         try
110         {
111             contextProvider = contextProviderElementParser.makeContextProvider(plugin, element);
112 
113             if (element.element("label") != null)
114             {
115                 label = new DefaultWebLabel(element.element("label"), webInterfaceManager.getWebFragmentHelper(), contextProvider, this);
116             }
117 
118             if (element.element("tooltip") != null)
119             {
120                 tooltip = new DefaultWebLabel(element.element("tooltip"), webInterfaceManager.getWebFragmentHelper(), contextProvider, this);
121             }
122 
123             if (getParams() != null)
124             {
125                 params = new DefaultWebParam(getParams(), webInterfaceManager.getWebFragmentHelper(), contextProvider, this);
126             }
127 
128             condition = makeConditions(element, ConditionElementParser.CompositeType.AND);
129         }
130         catch (final PluginParseException e)
131         {
132             // is there a better exception to throw?
133             throw new RuntimeException("Unable to enable web fragment", e);
134         }
135 
136         webInterfaceManager.refresh();
137     }
138 
139     @Override
140     public void disabled()
141     {
142         condition = null;
143         webInterfaceManager.refresh();
144         super.disabled();
145     }
146 
147     public int getWeight()
148     {
149         return weight;
150     }
151 
152     public WebLabel getWebLabel()
153     {
154         return label;
155     }
156 
157     public WebLabel getTooltip()
158     {
159         return tooltip;
160     }
161 
162     public void setWebInterfaceManager(final WebInterfaceManager webInterfaceManager)
163     {
164         this.webInterfaceManager = webInterfaceManager;
165         this.conditionElementParser = new ConditionElementParser(new ConditionElementParser.ConditionFactory()
166         {
167             public Condition create(String className, Plugin plugin) throws ConditionLoadingException
168             {
169                 return webInterfaceManager.getWebFragmentHelper().loadCondition(className, plugin);
170             }
171         });
172         this.contextProviderElementParser = new ContextProviderElementParser(webInterfaceManager.getWebFragmentHelper());
173     }
174 
175     public Condition getCondition()
176     {
177         return condition;
178     }
179 
180     public ContextProvider getContextProvider()
181     {
182         return contextProvider;
183     }
184 
185     public WebParam getWebParams()
186     {
187         return params;
188     }
189 
190     @Override
191     public boolean equals(Object obj)
192     {
193         return new ModuleDescriptors.EqualsBuilder().descriptor(this).isEqualTo(obj);
194     }
195 
196     @Override
197     public int hashCode()
198     {
199         return new ModuleDescriptors.HashCodeBuilder().descriptor(this).toHashCode();
200     }
201 }