View Javadoc

1   package com.atlassian.plugin.web.model;
2   
3   import com.atlassian.plugin.Plugin;
4   import com.atlassian.plugin.PluginAccessor;
5   import com.atlassian.plugin.web.renderer.RendererException;
6   import com.atlassian.plugin.web.renderer.StaticWebPanelRenderer;
7   import com.atlassian.plugin.web.renderer.WebPanelRenderer;
8   import com.google.common.base.Preconditions;
9   
10  import java.io.IOException;
11  import java.io.Writer;
12  import java.util.Map;
13  
14  /**
15   * @since   2.5.0
16   */
17  public abstract class AbstractWebPanel implements WebPanel
18  {
19      private final PluginAccessor pluginAccessor;
20      protected Plugin plugin;
21      private String resourceType;
22  
23      protected AbstractWebPanel(PluginAccessor pluginAccessor)
24      {
25          this.pluginAccessor = pluginAccessor;
26      }
27  
28      public void setPlugin(Plugin plugin)
29      {
30          this.plugin = plugin;
31      }
32  
33      public void setResourceType(String resourceType)
34      {
35          this.resourceType = Preconditions.checkNotNull(resourceType);
36      }
37  
38      /**
39       * Default implementation of {@link WebPanel#writeHtml(java.io.Writer, java.util.Map)} that delegates to
40       * {@link WebPanel#getHtml(java.util.Map)}. This method is provided for backwards compatibility with
41       * pre-2.11 implementations of WebPanel. Panels SHOULD be upgraded to support this method natively
42       * for performance reasons.
43       *
44       * @param writer the writer to append the panel output to
45       * @param context the contextual information that can be used during
46       *  rendering. Context elements are not standardized and are
47       *  application-specific, so refer to your application's documentation to
48       *  learn what is available.
49       * @throws IOException
50       */
51      public void writeHtml(Writer writer, Map<String, Object> context) throws IOException
52      {
53          writer.write(getHtml(context));
54      }
55  
56      protected final WebPanelRenderer getRenderer()
57      {
58          if (StaticWebPanelRenderer.RESOURCE_TYPE.equals(resourceType))
59          {
60              return StaticWebPanelRenderer.RENDERER;
61          }
62          else
63          {
64              for (WebPanelRenderer webPanelRenderer : pluginAccessor.getEnabledModulesByClass(WebPanelRenderer.class))
65              {
66                  if (Preconditions.checkNotNull(resourceType).equals(webPanelRenderer.getResourceType()))
67                  {
68                      return webPanelRenderer;
69                  }
70              }
71              throw new RendererException("No renderer found for resource type: " + resourceType);
72          }
73      }
74  }