package com.atlassian.plugin.web.descriptors;

import com.atlassian.plugin.Plugin;
import com.atlassian.plugin.PluginParseException;
import com.atlassian.plugin.web.model.DefaultWebIcon;
import com.atlassian.plugin.web.model.DefaultWebLink;
import com.atlassian.plugin.web.model.WebIcon;
import com.atlassian.plugin.web.model.WebLink;
import com.atlassian.plugin.web.WebInterfaceManager;
import org.dom4j.Element;

/**
 * Represents a pluggable link.
 */
public class DefaultWebItemModuleDescriptor extends AbstractWebFragmentModuleDescriptor implements WebItemModuleDescriptor
{
    private String section;
    private WebIcon icon;
    private DefaultWebLink link;
    private String styleClass;

    public DefaultWebItemModuleDescriptor(WebInterfaceManager webInterfaceManager)
    {
        super(webInterfaceManager);
    }

    public DefaultWebItemModuleDescriptor()
    {
    }

    public void init(Plugin plugin, Element element) throws PluginParseException
    {
        super.init(plugin, element);

        section = element.attributeValue("section");

        if (element.element("icon") != null)
        {
            icon = new DefaultWebIcon(element.element("icon"), webInterfaceManager.getWebFragmentHelper(), contextProvider, this);
        }

        if (element.element("styleClass") != null)
        {
            styleClass = element.element("styleClass").getTextTrim();
        }
        else
        {
            styleClass = "";
        }

        if (element.element("link") != null)
        {
            link = new DefaultWebLink(element.element("link"), webInterfaceManager.getWebFragmentHelper(), contextProvider, this);
        }
    }

    public String getSection()
    {
        return section;
    }

    public WebLink getLink()
    {
        return link;
    }

    public WebIcon getIcon()
    {
        return icon;
    }

    public String getStyleClass()
    {
        return styleClass;
    }
}

@@ -1089,20 +1089,26 @@
 lement(%22
-icon
+styleClass
 %22) != nu
@@ -1137,125 +1137,126 @@
     
-icon = new DefaultWebIcon(element.element(%22icon%22), webInterfaceManager.getWebFragmentHelper(), contextProvider, this)
+styleClass = element.element(%22styleClass%22).getTextTrim();%0A        %7D%0A        else%0A        %7B%0A            styleClass = %22%22
 ;%0A  
@@ -1267,212 +1267,464 @@
   %7D%0A
-%0A
     
+%7D%0A%0A
     
-if (element.element(%22styleClass%22) != null
+public String getSection()%0A    %7B%0A        return section;%0A    %7D%0A%0A    public WebLink getLink()%0A    %7B%0A        return link;%0A    %7D%0A%0A    public WebIcon getIcon(
 )%0A    
+%7B%0A
     
-%7B%0A
     
-        styleClass = element.element(%22styleClass%22).getTextTrim();%0A    
+return icon;%0A    %7D%0A%0A    public String getStyleClass()%0A    %7B%0A        return styleClass;%0A
     %7D%0A
+%0A
     
-    else%0A        %7B%0A            styleClass = %22%22;%0A        %7D%0A
+public void enabled()%0A    %7B%0A        super.enabled();%0A%0A        // contextProvider is not available until the module is enabled because they may need to have dependencies injected
 %0A   
@@ -1749,20 +1749,20 @@
 lement(%22
-link
+icon
 %22) != nu
@@ -1787,20 +1787,20 @@
         
-link
+icon
  = new D
@@ -1808,20 +1808,20 @@
 faultWeb
-Link
+Icon
 (element
@@ -1830,20 +1830,20 @@
 lement(%22
-link
+icon
 %22), webI
@@ -1924,271 +1924,199 @@
     
-%7D%0A%0A
     
-public String getSection(
+if (element.element(%22link%22) != null
 )%0A    
-%7B%0A
     
+%7B%0A
     
-return section;%0A    %7D%0A%0A    public WebLink getLink()%0A    %7B%0A        return link;%0A    %7D%0A%0A    public WebIcon getIcon()%0A    %7B%0A        return icon;%0A    %7D%0A%0A    public String getStyleClass()%0A    %7B%0A        return styleClass;
+        link = new DefaultWebLink(element.element(%22link%22), webInterfaceManager.getWebFragmentHelper(), contextProvider, this);%0A        %7D
 %0A   

move creation of icons and links to enabled() so contextProvider is available