1 package com.atlassian.plugin.web.descriptors;
2
3 import static com.atlassian.plugin.util.validation.ValidationPattern.test;
4
5 import com.atlassian.plugin.web.conditions.ConditionLoadingException;
6 import org.dom4j.Element;
7
8 import com.atlassian.plugin.Plugin;
9 import com.atlassian.plugin.PluginParseException;
10 import com.atlassian.plugin.descriptors.AbstractModuleDescriptor;
11 import com.atlassian.plugin.hostcontainer.HostContainer;
12 import com.atlassian.plugin.module.ModuleFactory;
13 import com.atlassian.plugin.util.validation.ValidationPattern;
14 import com.atlassian.plugin.web.Condition;
15 import com.atlassian.plugin.web.ContextProvider;
16 import com.atlassian.plugin.web.WebInterfaceManager;
17 import com.atlassian.plugin.web.model.WebPanel;
18 import com.google.common.base.Supplier;
19
20 import java.io.IOException;
21 import java.io.Writer;
22 import java.util.HashMap;
23 import java.util.Map;
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
86
87
88
89
90
91
92
93
94
95 public class DefaultWebPanelModuleDescriptor extends AbstractWebFragmentModuleDescriptor<WebPanel> implements WebPanelModuleDescriptor
96 {
97
98
99
100
101 public static final String XML_ELEMENT_NAME = "web-panel";
102
103 private WebPanelSupplierFactory webPanelSupplierFactory;
104
105
106
107
108
109 private Supplier<WebPanel> webPanelFactory;
110
111 private int weight;
112 private String location;
113
114 public DefaultWebPanelModuleDescriptor(final HostContainer hostContainer, final ModuleFactory moduleClassFactory, final WebInterfaceManager webInterfaceManager)
115 {
116 super(moduleClassFactory, webInterfaceManager);
117 this.webPanelSupplierFactory = new WebPanelSupplierFactory(this, hostContainer, moduleFactory);
118 this.webInterfaceManager = webInterfaceManager;
119 }
120
121 @Override
122 public void init(final Plugin plugin, final Element element) throws PluginParseException
123 {
124 super.init(plugin, element);
125
126 weight = WeightElementParser.getWeight(element);
127 location = element.attributeValue("location");
128
129 webPanelFactory = webPanelSupplierFactory.build(moduleClassName);
130 }
131
132 private class ContextAwareWebPanel implements WebPanel
133 {
134 private final WebPanel delegate;
135
136 private ContextAwareWebPanel(WebPanel delegate)
137 {
138 this.delegate = delegate;
139 }
140
141 public String getHtml(final Map<String, Object> context)
142 {
143 return delegate.getHtml(getContextProvider().getContextMap(new HashMap<String, Object>(context)));
144 }
145
146 public void writeHtml(Writer writer, Map<String, Object> context) throws IOException
147 {
148 delegate.writeHtml(writer, context);
149 }
150 }
151
152 @Override
153 protected void provideValidationRules(final ValidationPattern pattern)
154 {
155 super.provideValidationRules(pattern);
156 pattern.rule(test("@location").withError("The Web Panel location attribute is required."));
157 }
158
159 public String getLocation()
160 {
161 return location;
162 }
163
164 public int getWeight()
165 {
166 return weight;
167 }
168
169 @Override
170 public WebPanel getModule()
171 {
172 return new ContextAwareWebPanel(webPanelFactory.get());
173 }
174
175 @Override
176 public void enabled()
177 {
178 super.enabled();
179 webInterfaceManager.refresh();
180 }
181
182 @Override
183 public void disabled()
184 {
185 webInterfaceManager.refresh();
186 super.disabled();
187 }
188 }