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
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
40
41
42
43
44
45
46
47
48
49
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 }