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.descriptors.AbstractModuleDescriptor;
6   import com.atlassian.plugin.module.ModuleFactory;
7   import com.atlassian.plugin.util.validation.ValidationPattern;
8   import com.atlassian.plugin.web.WebInterfaceManager;
9   import com.atlassian.plugin.web.api.provider.WebSectionProvider;
10  import com.google.common.base.Supplier;
11  import org.dom4j.Element;
12  
13  import javax.annotation.Nonnull;
14  
15  import static com.atlassian.plugin.util.validation.ValidationPattern.test;
16  import static com.google.common.base.Suppliers.memoize;
17  
18  public class WebSectionProviderModuleDescriptor extends AbstractModuleDescriptor<WebSectionProvider> {
19      private final WebInterfaceManager webInterfaceManager;
20      private Supplier<WebSectionProvider> sectionSupplier;
21      private String location;
22  
23      public WebSectionProviderModuleDescriptor(final ModuleFactory moduleFactory, final WebInterfaceManager webInterfaceManager) {
24          super(moduleFactory);
25          this.webInterfaceManager = webInterfaceManager;
26      }
27  
28      @Override
29      protected void provideValidationRules(ValidationPattern pattern) {
30          super.provideValidationRules(pattern);
31          pattern.rule(test("@class").withError("The web section provider class is required"));
32          pattern.rule(test("@location").withError("Must provide a location that sections should be added to."));
33      }
34  
35      @Override
36      public void init(@Nonnull final Plugin plugin, @Nonnull final Element element) throws PluginParseException {
37          super.init(plugin, element);
38          final WebSectionProviderModuleDescriptor that = this;
39          sectionSupplier = memoize(new Supplier<WebSectionProvider>() {
40              @Override
41              public WebSectionProvider get() {
42                  return moduleFactory.createModule(moduleClassName, that);
43              }
44          });
45          location = element.attributeValue("location");
46      }
47  
48      public String getLocation() {
49          return location;
50      }
51  
52      @Override
53      public WebSectionProvider getModule() {
54          return sectionSupplier.get();
55      }
56  
57      @Override
58      public void enabled() {
59          super.enabled();
60          webInterfaceManager.refresh();
61      }
62  
63      @Override
64      public void disabled() {
65          super.disabled();
66          webInterfaceManager.refresh();
67      }
68  }