### Navigation tree for a registered user

As it was mentioned before a navigation tree for a registered user has two main features: drag-and-drop and context menu.

Drag-and-drop feature supported in the Photo Album application is not so complicated as it may seem from the first view. In this application we can mark out two types of drag-and-drop: one type takes place only inside the tree (between tree nodes) and another one - between the watching area and the tree. The difference is not considerable enough to describe two types separately, but also not at all insignificant to be omitted here.

There are two components that provide drag-and-drop functionality: <rich:dragSource> which takes care of the drag and <rich:dropTarget> which takes care with of the drop operations.

## Note:

Due to "Album Groups - Albums - Photos" hierarchy we can say that photos could be moved between albums, albums could be moved between album groups. To avoid a mishmash, it's not allowed to place photos directly in album groups as well as nesting album groups inside album groups or albums inside albums.

Let's explore how drag-and-drop works for albums.

All albums, that are represented as TreeNodes, must be marked somehow for dragging. For this purpose we use previously mentioned <rich:dragSource> and specify albums with "type" attribute. Let's have a look at the `src/main/webapp/includes/index/tree.xhtml` file:

                <rich:treeModelAdaptor var="album" nodes="#{'#{'}treeNode.albums}">
                  <rich:treeNode … >
                      …
                    <rich:dragSource type="album" dragIndicator="dragIndicator" dragValue="#{'#{'}treeNode}" />
                        …
                    </a4j:outputPanel>
                  </rich:treeNode>
                    …
                </rich:treeModelAdaptor>

To provide drop functionality for the marked albums, we should mark Album Groups as drop zones in the application code too. For this purpose we add the <rich:dropTarget> with "acceptedTypes" attribute to the "Shelf" node and in order to process drop on the server side we specify a listener with the "dropListener" attribute in the same `src/main/webapp/includes/index/tree.xhtml` file:

                <rich:treeModelAdaptor nodes="#{'#{'}userBean.user.shelves}">
                  <rich:treeNode … >
                      …
                     <rich:dropTarget acceptedTypes="album" dropValue="#{'#{'}treeNode}" dropListener="#{'#{'}dndManager.processDrop}"
                         render="treePanel, mainArea" />
                         …
                  </rich:treeNode>
                    …
                </rich:treeModelAdaptor>

The "acceptedTypes" attribute tells the "Shelf" node what types of dragged zones (albums in this case) it can accept. We have specified "Album" node "dragType" as "album", so the "Shelf" node can accept it.

The code for the <rich:dragIndicator> looks like the following:

                <rich:dragIndicator id="dragIndicator">
                  <rich:panel>
                    [Album]
                  </rich:panel>
                </rich:dragIndicator>

The `processDrop()` method of `DnDManager.java` class is shown in the listing below:

                public void processDrop(DropEvent dropEvent) {
                    if (user == null) return;

                    Object dragValue = dropEvent.getDragValue();
                    Object dropValue = dropEvent.getDropValue();
                    if (dragValue instanceof Image) {
                        if (!((Album) dropValue).getOwner().getLogin().equals(user.getLogin())) {
                            error.fire(new SimpleEvent(Constants.DND_PHOTO_ERROR));
                            return;
                        }
                        handleImage((Image) dragValue, (Album) dropValue);
                    } else if (dragValue instanceof Album) {
                        if (!((Shelf) dropValue).getOwner().getLogin().equals(user.getLogin())) {
                            error.fire(new SimpleEvent(Constants.DND_ALBUM_ERROR));
                            return;
                        }
                        handleAlbum((Album) dragValue, (Shelf) dropValue);
                    }
                }

Here is the whole example of the "Navigation tree for a registered user":

                <rich:dragIndicator id="dragIndicator">
                  <rich:panel>
                    [Album]
                  </rich:panel>
                </rich:dragIndicator>
                <rich:dragIndicator id="iDragIndicator">
                  <rich:panel>
                      [Image]
                  </rich:panel>
                </rich:dragIndicator>
                <rich:tree id="userTree" var="treeNode" toggleType="client">
                  <rich:treeModelAdaptor nodes="#{'#{'}userBean.user.shelves}">
                    <rich:treeNode style="cursor:pointer" selectedClass="tree-selected-node"
                        iconExpanded="/resources/img/shell/tree_icon_shelf.png" iconCollapsed="/resources/img/shell/tree_icon_shelf.png" id="shelf">
                      <ui:include src="/includes/contextMenu/CMForShelf.xhtml">
                        <ui:param name="shelf" value="#{'#{'}treeNode}" />
                        <ui:param name="target" value="shelf" />
                      </ui:include>
                      <a4j:outputPanel layout="block" style="cursor:pointer;width:200px; overflow-x:hidden;">
                        <h:outputText value="#{'#{'}treeNode.name}" />
                        <h:outputText rendered="#{'#{'}fn:length(treeNode.unvisitedImages) > 0}" value=" #{'#{'}messages['delimiter']} " />
                        <h:outputText rendered="#{'#{'}fn:length(treeNode.unvisitedImages) > 0}" styleClass="bold"
                            value="#{'#{'}fn:length(treeNode.unvisitedImages)}" />
                        <h:outputText rendered="#{'#{'}fn:length(treeNode.unvisitedImages) > 0}" value=" #{'#{'}messages['new']}" />
                        <a4j:ajax render="treePanel, mainArea" event="click" listener="#{'#{'}controller.showShelf(treeNode)}" />

                        <rich:dropTarget acceptedTypes="album" dropValue="#{'#{'}treeNode}" dropListener="#{'#{'}dndManager.processDrop}"
                            render="treePanel, mainArea" />

                        <rich:tooltip direction="topRight" showDelay="1000">
                          <span style="white-space: nowrap">#{'#{'}treeNode.name}</span>
                        </rich:tooltip>
                      </a4j:outputPanel>
                    </rich:treeNode>

                    <rich:treeModelAdaptor nodes="#{'#{'}treeNode.albums}">
                      <rich:treeNode style="cursor:pointer" render="mainArea, treePanel" selectedClass="tree-selected-node"
                          iconLeaf="resources/img/shell/tree_icon_album.png" id="album">
                        <ui:include src="/includes/contextMenu/CMForAlbum.xhtml">
                          <ui:param name="album" value="#{'#{'}treeNode}" />
                          <ui:param name="target" value="album" />
                        </ui:include>
                        <a4j:outputPanel layout="block" style="cursor:pointer;width:200px; overflow-x:hidden;">
                          <h:outputText value="#{'#{'}treeNode.name}" />
                          <h:outputText rendered="#{'#{'}fn:length(treeNode.unvisitedImages) > 0}" value=" #{'#{'}messages['delimiter']} " />
                          <h:outputText rendered="#{'#{'}fn:length(treeNode.unvisitedImages) > 0}" styleClass="bold"
                              value="#{'#{'}fn:length(treeNode.unvisitedImages)}" />
                          <h:outputText rendered="#{'#{'}fn:length(treeNode.unvisitedImages) > 0}" value=" #{'#{'}messages['new']}" />
                          <a4j:ajax render="treePanel, mainArea" event="click" listener="#{'#{'}controller.showAlbum(treeNode)}" />

                          <rich:dragSource type="album" dragIndicator="dragIndicator" dragValue="#{'#{'}treeNode}" />
                            <rich:dropTarget acceptedTypes="image" dropValue="#{'#{'}treeNode}"
                                dropListener="#{'#{'}dndManager.processDrop}" render="treePanel, mainArea" />

                          <rich:tooltip direction="topRight" showDelay="1000">
                            <span style="white-space: nowrap"> #{'#{'}treeNode.name} </span>
                          </rich:tooltip>
                        </a4j:outputPanel>
                      </rich:treeNode>

                    </rich:treeModelAdaptor>
                  </rich:treeModelAdaptor>

                </rich:tree>

The image below shows how the described above drag-and-drop features are rendered in the Photo Album.

Visit following pages at RichFaces Showcase for more information, examples and sources on the components used in the application and described in this chapter:

- Tree for the <rich:tree> component;

- Drag and Drop for the <rich:dragSource> and <rich:dropTarget> components;