Commits

Stefan Saasen committed e01aea0

Add example to show a panel on the profile page.

Comments (0)

Files changed (6)

     </build>
 
     <properties>
-        <confluence.version>4.1.6</confluence.version>
+        <confluence.version>4.2-m8</confluence.version>
         <confluence.data.version>3.5</confluence.data.version>
         <amps.version>3.8</amps.version>
     </properties>

src/main/java/com/atlassian/example/DynamicPanelContextProvider.java

+package com.atlassian.example;
+
+import com.atlassian.bandana.BandanaManager;
+import com.atlassian.confluence.renderer.PageContext;
+import com.atlassian.plugin.PluginParseException;
+import com.atlassian.plugin.web.ContextProvider;
+import com.atlassian.renderer.WikiStyleRenderer;
+import com.google.common.collect.ImmutableMap;
+
+import java.util.Map;
+
+public class DynamicPanelContextProvider implements ContextProvider {
+    private final WikiStyleRenderer wikiStyleRenderer;
+    private final BandanaManager bandanaManager;
+
+    public DynamicPanelContextProvider(BandanaManager bandanaManager, WikiStyleRenderer wikiStyleRenderer) {
+        this.bandanaManager = bandanaManager;
+        this.wikiStyleRenderer = wikiStyleRenderer;
+    }
+
+    @Override
+    public void init(Map<String, String> params) throws PluginParseException {
+    }
+
+    @Override
+    public Map<String, Object> getContextMap(Map<String, Object> context) {
+
+        final String macro = "{cheese}"; // Use bandanaManager.getValue() to retrieve the macro content
+
+        // Create a custom context map for the velocity template
+        context.putAll(ImmutableMap.of(
+                "panelContent", wikiStyleRenderer.convertWikiToXHtml(new PageContext(), macro)
+        ));
+        return context;
+    }
+}

src/main/java/com/atlassian/example/ExampleMacro.java

-package com.atlassian.example;
-
-import java.util.Map;
-import java.util.List;
-
-import com.atlassian.confluence.content.render.xhtml.ConversionContext;
-import com.atlassian.confluence.macro.Macro;
-import com.atlassian.confluence.macro.MacroExecutionException;
-import com.atlassian.confluence.pages.PageManager;
-import com.atlassian.confluence.pages.Page;
-import com.atlassian.confluence.spaces.SpaceManager;
-import com.atlassian.confluence.user.AuthenticatedUserThreadLocal;
-import com.atlassian.user.User;
-import com.opensymphony.util.TextUtils;
-
-/**
- * This very simple macro shows you the very basic use-case of displaying *something* on the Confluence page where it is used.
- * Use this example macro to toy around, and then quickly move on to the next example - this macro doesn't
- * really show you all the fun stuff you can do with Confluence.
- */
-public class ExampleMacro implements Macro
-{
-
-    // We just have to define the variables and the setters, then Spring injects the correct objects for us to use. Simple and efficient.
-    // You just need to know *what* you want to inject and use.
-
-    private final PageManager pageManager;
-    private final SpaceManager spaceManager;
-
-    public ExampleMacro(PageManager pageManager, SpaceManager spaceManager)
-    {
-        this.pageManager = pageManager;
-        this.spaceManager = spaceManager;
-    }
-
-    /**
-     * This method returns XHTML to be displayed on the page that uses this macro
-     * we just do random stuff here, trying to show how you can access the most basic
-     * managers and model objects. No emphasis is put on beauty of code nor on
-     * doing actually useful things :-)
-     */
-    @Override
-    public String execute(Map<String, String> parameters, String body, ConversionContext context) throws MacroExecutionException
-    {
-        // in this most simple example, we build the result in memory, appending HTML code to it at will.
-        // this is something you absolutely don't want to do once you start writing plugins for real. Refer
-        // to the next example for better ways to render content.
-        StringBuffer result = new StringBuffer();
-
-        // get the currently logged in user and display his name
-        User user = AuthenticatedUserThreadLocal.getUser();
-        if (user != null)
-        {
-            String greeting = "<p>Hello <b>" + TextUtils.htmlEncode(user.getFullName()) + "</b>.</p>";
-            result.append(greeting);
-        }
-
-        //get the pages added in the last 55 days to the DS space ("Demo Space"), and display them
-        List<Page> list = pageManager.getRecentlyAddedPages(55, "DS");
-        result.append("<p>");
-        result.append("Some stats for <b>Demonstration Space</b>:");
-        result.append("<table class=\"confluenceTable\">");
-        result.append("<thead><tr><th class=\"confluenceTh\">Page</th><th class=\"confluenceTh\">Number of children</th></tr></thead>");
-        result.append("<tbody>");
-        for (Page page : list)
-        {
-            int numberOfChildren = page.getChildren().size();
-            String pageWithChildren = "<tr><td class=\"confluenceTd\">" + TextUtils.htmlEncode(page.getTitle()) + "</td><td class=\"confluenceTd\" style=\"text-align:right\">" + numberOfChildren + "</td></tr>";
-            result.append(pageWithChildren);
-        }
-        result.append("</tbody>");
-        result.append("</table>");
-        result.append("</p>");
-
-        // and show the number of all spaces in this installation.
-        String spaces = "<p>Altogether, this installation has <b>" + spaceManager.getAllSpaces().size() + "</b> spaces.</p>";
-        result.append(spaces);
-
-        // this concludes our little demo. Now you should understand the basics of code injection use in Confluence, and how
-        // to get a really simple macro running.
-
-        return result.toString();
-    }
-
-    @Override
-    public BodyType getBodyType()
-    {
-        return BodyType.NONE;
-    }
-
-    @Override
-    public OutputType getOutputType()
-    {
-        return OutputType.BLOCK;
-    }
-
-}

src/main/resources/atlassian-plugin.xml

     <plugin-info>
         <description>${project.description}</description>
         <version>${project.version}</version>
-        <vendor name="${project.organization.name}" url="${project.organization.url}" />
+        <vendor name="${project.organization.name}" url="${project.organization.url}"/>
     </plugin-info>
 
-    <xhtml-macro name="panel-example" class="com.atlassian.example.ExampleMacro" key="my-macro">
-        <parameters/>
-    </xhtml-macro>
+    <resource type="i18n" name="i18n" location="i18n.panel-example"/>
+
+    <!-- The atl.confluence.userprofile location is defined in viewmyprofile.vm -->
+    <!-- The atl.userprofile location is defined in profile.vmd -->
+    <web-panel key="myPanel" location="atl.confluence.userprofile">
+        <resource name="view" type="confluence-velocity" location="/templates/panel/panelcontent.vm"/>
+        <context-provider class="com.atlassian.example.DynamicPanelContextProvider"/>
+    </web-panel>
 </atlassian-plugin>

src/main/resources/i18n/panel-example.properties

+my-dynamic-panel.headline=My Example Panel Output

src/main/resources/templates/panel/panelcontent.vm

+<div class="profile-section" id="my-dynamic-panel">
+    <form class="aui">## so the header gets the aui form styles.
+        <h2>$action.getText("my-dynamic-panel.headline")</h2>
+    </form>
+    $!panelContent
+</div>