13 Replies Latest reply on Jul 13, 2006 11:48 AM by D Johnson

    Customize Navigation Portlet

    Alexander Grünewald Newbie

      Hello Developers, Hello Community,

      is there a way to customize the NavigationPortlet? I would like to change the order of items displayed in the tab navigation. Or should i write my own Portlet which renders the links in the right order and place it in the navigation region of the page?

      Best regards,
      Alexander

        • 1. Re: Customize Navigation Portlet
          noicangi Novice

          hi i was looking for the same;)

          + when i add a portlet to hellowold page and reload the jboss the page resets.
          how can i solve this? i tried to change the security settings.

          • 2. Re: Customize Navigation Portlet
            Roy Russo Master

            The order in the nav portlet is by name.

            So if you want your own ordering, I would suggest you fork the nav portlet, so you don't have to hack at the code, which will make upgrading a problem later on.

            Just remember to assign the nav portlet to the nav region, and set no decorations on it.

            • 3. Re: Customize Navigation Portlet
              Viet Master

              a better suggestion than roy's one is :

              contribute to the portlet and add a portlet preference for it that says how to display nodes.

              • 4. Re: Customize Navigation Portlet
                Vince Marco Newbie

                 

                "julien@jboss.com" wrote:
                a better suggestion than roy's one is :

                contribute to the portlet and add a portlet preference for it that says how to display nodes.



                My question then is how do you decide on a design for things like this, or are they just thrown together by whomever decides to join in.

                I've created a custom NavbarPortlet (named it different to avoid upgrade problems) which does have a preference for ordering and a preference for overriding the name. I'll gladly contribute, but I'm just not sure of the process. Guide me.

                Cheers,

                Vince


                • 5. Re: Customize Navigation Portlet
                  Viet Master

                  For small things like that you can come up with fresh ideas.

                  You need to sign the contributor agreement http://labs.jboss.com/portal/?ctrl:id=page.default.con&noproject=true

                  For small contributions you don't need access to the CVS, so you give us the patch. If you want to contribute furthermore then we grant you CVS access, then you can do it yourself and contribute more.

                  Any other question ?

                  • 6. Re: Customize Navigation Portlet
                    Alexander Grünewald Newbie

                    Maybe you could simply post the source code of your NavigationBar Portlet in this thread (if it isn't to much in size), because I would like to know, how you implemented it. If your portlet is not accepted to be a part of the portal core, it couldn't however be a good candidate for the JBoss Portlet Swap website? So everyone who is interested in this portlet, could integrate it in its portal.

                    Best regards,
                    Alexander

                    • 7. Re: Customize Navigation Portlet
                      Viet Master

                      if you plan to do this, then create a JIRA issue and attach the code to the jira issue, then post the jira issue link here.

                      • 8. Re: Customize Navigation Portlet
                        Doug Schnelzer Newbie

                        I added the ability to sort tabs and specify tab labels using a resource properties file. The JIRA task is http://jira.jboss.com/jira/browse/JBPORTAL-585. Let me know if you have any questions on how to use it.

                        • 9. Re: Customize Navigation Portlet
                          Vince Marco Newbie

                          Beat me to it. Here is a segment from my *-objects.xml file related to my version of the NavbarPortlet and the code.

                          <page>
                           <page-name>default</page-name>
                           <properties>
                           <property>
                           <name>navbar.DisplayName</name>
                           <value>Home</value>
                           </property>
                           <property>
                           <name>navbar.SortOrder</name>
                           <value>1</value>
                           </property>
                           </properties>
                          
                          import org.jboss.portal.core.api.JBossPortalNode;
                          import org.jboss.portal.core.impl.model.portal.PortalObjectImpl;
                          import org.jboss.portal.core.model.portal.Context;
                          import org.jboss.portal.core.model.portal.Page;
                          import org.jboss.portal.core.model.portal.Portal;
                          import org.jboss.portal.core.model.portal.PortalObject;
                          import org.jboss.portal.core.model.portal.PortalObjectContainer;
                          import org.jboss.portal.core.model.portal.Window;
                          import org.jboss.portal.security.PortalPermissionFactory;
                          import org.jboss.portal.security.PortalPolicyException;
                          import org.jboss.portlet.JBossPortlet;
                          import org.jboss.portlet.JBossRenderRequest;
                          import org.jboss.portlet.JBossRenderResponse;
                          import org.jboss.portlet.PortalNode;
                          import org.jboss.portlet.PortalNodeURL;
                          
                          import javax.portlet.PortletException;
                          import javax.portlet.PortletPreferences;
                          import java.io.IOException;
                          import java.util.ArrayList;
                          import java.util.Collections;
                          import java.util.Comparator;
                          import java.util.HashMap;
                          import java.util.Iterator;
                          import java.util.List;
                          import java.util.Map;
                          
                          /**
                           *
                           * @author Vince Marco
                           */
                          public class NavbarPortlet extends JBossPortlet
                           {
                           private PortalPermissionFactory permissionFactory;
                           private PortalObjectContainer container;
                           private Map nodemap = new HashMap();
                           private static final String NAME_PROPERTY = "navbar.DisplayName";
                           private static final String ORDER_PROPERTY = "navbar.SortOrder";
                          
                           public NavbarPortlet()
                           {
                           super();
                           }
                          
                           public void init() throws PortletException
                           {
                           permissionFactory = (PortalPermissionFactory) this.getPortletContext().getAttribute("PortalPermissionFactory");
                           container = (PortalObjectContainer) this.getPortletContext().getAttribute("PortalObjectContainer");
                           if (permissionFactory == null)
                           {
                           throw new PortletException("No portal permission factory");
                           }
                           if (container == null)
                           {
                           throw new PortletException("No portal object container");
                           }
                           }
                          
                           public void destroy()
                           {
                           super.destroy();
                          
                           //
                           permissionFactory = null;
                           container = null;
                           }
                          
                           public void render(JBossRenderRequest req, JBossRenderResponse resp) throws IOException,
                           PortletException
                           {
                           resp.setContentType("text/html");
                           resp.setTitle("Portals");
                           try
                           {
                           PortletPreferences prefs = req.getPreferences();
                           String rootLevel = prefs.getValue("navigation-root-level", "page");
                           Context context = container.getContext();
                           List navElements = new ArrayList();
                          
                           // get the list of portal nodes to display (portals or pages, based on
                           // preference)
                           Iterator portalObjectIterator;
                           if ("page".equalsIgnoreCase(rootLevel))
                           {
                           // if the pages of the current portal are requested, then read up
                           // the hierarchie to the current portal, to
                           // get the children from there (which are the pages to display)
                           PortalNode current = req.getPortalNode();
                           while (current != null && current.getType() != PortalNode.TYPE_PORTAL)
                           {
                           current = current.getParent();
                           }
                           if (current != null && current.getType() == PortalNode.TYPE_PORTAL)
                           {
                           PortalObject root = context.getChild(current.getName());
                           portalObjectIterator = root.getChildren().iterator();
                           }
                           else
                           {
                           throw new PortletException("the current node has no parent of type 'portal'");
                           }
                           }
                           else
                           {
                           // ok, we are to display portals then; they are the children of the
                           // container root
                           portalObjectIterator = context.getChildren().iterator();
                           }
                           // now build the list
                           while (portalObjectIterator.hasNext())
                           {
                           PortalObject child = (PortalObject) portalObjectIterator.next();
                           if (child.getType() == PortalNode.TYPE_PAGE || child.getType() == PortalNode.TYPE_PORTAL)
                           {
                           PortalObjectImpl portalObject = (PortalObjectImpl) child;
                           if (allowed(portalObject.getPortalNode()))
                           {
                           navElements.add(portalObject);
                           }
                           }
                           }
                          
                           Collections.sort(navElements, new PortalNodeComparator());
                          
                           // find the selected item in the level that was selected to be the root
                           // from the current portal node walk up the hierarchie until the node
                           // type matches
                           // that of the nav elements; if there is a match, hold on to it to mark
                           // the selected item later on
                           PortalNode selectedNode = null;
                           if (navElements.size() > 0)
                           {
                           PortalObject first = (PortalObject) navElements.get(0);
                           int navElementType = first.getType();
                           PortalNode current = req.getPortalNode();
                           while (current != null && current.getType() != navElementType)
                           {
                           current = current.getParent();
                           }
                          
                           // now read up the hierarchie within the same node type until the
                           // type changes, to find the top level node of that type
                           if (current.getParent() != null)
                           {
                           PortalNode sameTypeLevelUp = current;
                           while (sameTypeLevelUp != null && sameTypeLevelUp.getType() == current.getType())
                           {
                           current = sameTypeLevelUp;
                           sameTypeLevelUp = current.getParent();
                           }
                           }
                          
                           if (current != null && current.getType() == navElementType)
                           {
                           selectedNode = current;
                           }
                           }
                          
                           StringBuffer html = new StringBuffer();
                           html.append("<ul id=\"tabsHeader\">");
                           for (Iterator i = navElements.iterator(); i.hasNext();)
                           {
                           PortalObjectImpl navElement = (PortalObjectImpl) i.next();
                          
                           // build up Marks markup for the navigation , based on these nodes
                           String name = navElement.getDeclaredProperty(NAME_PROPERTY);
                           if (name == null)
                           name = navElement.getName();
                           JBossPortalNode urlNode = null;
                           if (navElement instanceof Portal)
                           {
                           // get the default page node for this portal to get a valid URL
                           // from it
                           Page defaultPage = ((Portal) navElement).getDefaultPage();
                           if (defaultPage != null)
                           {
                           PortalObject node = navElement.getChild(defaultPage.getName());
                           if (node instanceof PortalObjectImpl)
                           {
                           urlNode = ((PortalObjectImpl) node).getPortalNode();
                           }
                           }
                           }
                           else if (navElement instanceof Page)
                           {
                           urlNode = navElement.getPortalNode();
                           }
                          
                           if (urlNode != null)
                           {
                           PortalNodeURL childURL = resp.createRenderURL(urlNode);
                           html.append("<li");
                           if (selectedNode != null && selectedNode.equals(navElement.getPortalNode()))
                           {
                           html.append(" id=\"current\"");
                           }
                           html.append(" onmouseover=\"this.className='hoverOn'\"");
                           html.append(" onmouseout=\"this.className='hoverOff'\"><a href='");
                           html.append(childURL).append("'>").append(name).append("</a></li>");
                           }
                           }
                           html.append("</ul>");
                           resp.getWriter().write(html.toString());
                           }
                           catch (Exception e)
                           {
                           // todo: handle error case : write it out
                           e.printStackTrace();
                           }
                          
                           }
                          
                           private boolean allowed(JBossPortalNode node) throws PortalPolicyException
                           {
                           return permissionFactory.checkPermission("portalobject", node.getHandle(), "view");
                           }
                          
                           private static int getOrder(Object o)
                           {
                           if (o instanceof Context)
                           {
                           return 0;
                           }
                           if (o instanceof Portal)
                           {
                           return 1;
                           }
                           if (o instanceof Page)
                           {
                           return 2;
                           }
                           if (o instanceof Window)
                           {
                           return 3;
                           }
                           return 4;
                           }
                          
                           class PortalNodeComparator implements Comparator
                           {
                           public int compare(Object arg0, Object arg1)
                           {
                           int result = 0;
                           int high1 = getOrder(arg0);
                           int high2 = getOrder(arg1);
                           if (high1 == high2)
                           {
                           PortalObject p0 = (PortalObject)arg0;
                           PortalObject p1 = (PortalObject)arg1;
                           String sort0 = p0.getDeclaredProperty(ORDER_PROPERTY);
                           String sort1 = p1.getDeclaredProperty(ORDER_PROPERTY);
                           if (sort0 == null)
                           sort0 = p0.getName();
                           if (sort1 == null)
                           sort1 = p1.getName();
                           result = (sort0.compareTo(sort1));
                           }
                           else
                           result = high1 - high2;
                           return result;
                           }
                           }
                           }
                          
                          
                          


                          • 10. Re: Customize Navigation Portlet
                            Alexander Grünewald Newbie

                            Hi vmarco, Hi schnelzer,

                            These jboss portlets are exactly what i need! I would like to see at least one of them in the portal core. Also very nice, is the ability of these portlets to display an alternative page name.

                            Thank you,
                            Alexander

                            • 11. Re: Customize Navigation Portlet
                              Yogesh Novice

                              Hi,
                              I was trying to implement the Navigation Portlet but could not find the following in the portal librairies ??

                              import org.jboss.portal.security.PortalPermissionFactory;
                              import org.jboss.portal.security.PortalPolicyException;


                              could you tell me which jar file contain these ??

                              I am using jboss-portal 2.2.0 on Fedora Core 5 and MySQL 4.1.2.

                              Thnx,
                              Yogesh


                              • 13. Re: Customize Navigation Portlet
                                D Johnson Newbie


                                Just another suggestion would be to assign the tab order or arrange by sorting.

                                Ordered:

                                i.e. TabA order=1, TabB order=2 etc.

                                I like the idea of sorting but , I would like to have finer control over how the tabs are arranged.

                                I don't think it would be that much of a leap to add this.