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.hostcontainer.HostContainer;
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.model.WebPanel;
10 import com.google.common.base.Supplier;
11 import com.google.common.collect.Maps;
12 import org.dom4j.Element;
13
14 import java.io.IOException;
15 import java.io.Writer;
16 import java.util.Map;
17
18 import static com.atlassian.plugin.util.validation.ValidationPattern.test;
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85 public class DefaultWebPanelModuleDescriptor extends AbstractWebFragmentModuleDescriptor<WebPanel> implements WebPanelModuleDescriptor {
86
87
88
89
90 public static final String XML_ELEMENT_NAME = "web-panel";
91
92 private WebPanelSupplierFactory webPanelSupplierFactory;
93
94
95
96
97
98 private Supplier<WebPanel> webPanelFactory;
99
100 private int weight;
101 private String location;
102
103 public DefaultWebPanelModuleDescriptor(final HostContainer hostContainer, final ModuleFactory moduleClassFactory, final WebInterfaceManager webInterfaceManager) {
104 super(moduleClassFactory, webInterfaceManager);
105 this.webPanelSupplierFactory = new WebPanelSupplierFactory(this, hostContainer, moduleFactory);
106 this.webInterfaceManager = webInterfaceManager;
107 }
108
109 @Override
110 public void init(final Plugin plugin, final Element element) throws PluginParseException {
111 super.init(plugin, element);
112
113 weight = WeightElementParser.getWeight(element);
114 location = element.attributeValue("location");
115
116 webPanelFactory = webPanelSupplierFactory.build(moduleClassName);
117 }
118
119 private class ContextAwareWebPanel implements WebPanel {
120 private final WebPanel delegate;
121
122 private ContextAwareWebPanel(WebPanel delegate) {
123 this.delegate = delegate;
124 }
125
126 public String getHtml(final Map<String, Object> context) {
127 return delegate.getHtml(getContextProvider().getContextMap(Maps.newHashMap(context)));
128 }
129
130 public void writeHtml(Writer writer, Map<String, Object> context) throws IOException {
131 delegate.writeHtml(writer, getContextProvider().getContextMap(Maps.newHashMap(context)));
132 }
133 }
134
135 @Override
136 protected void provideValidationRules(final ValidationPattern pattern) {
137 super.provideValidationRules(pattern);
138 pattern.rule(test("@location").withError("The Web Panel location attribute is required."));
139 }
140
141 public String getLocation() {
142 return location;
143 }
144
145 public int getWeight() {
146 return weight;
147 }
148
149 @Override
150 public WebPanel getModule() {
151 return new ContextAwareWebPanel(webPanelFactory.get());
152 }
153
154 @Override
155 public void enabled() {
156 super.enabled();
157 webInterfaceManager.refresh();
158 }
159
160 @Override
161 public void disabled() {
162 webInterfaceManager.refresh();
163 super.disabled();
164 }
165 }