Drag and Drop has always been a great user interface functionality that is easy and intuitive for the user for performing a number of actions in all sorts of applications. ICEfaces has always provided Drag and Drop support in regular Java applications and in portlet applications as well. However, before ICEfaces 3.3, the Drag and Drop functionality in portlets was constrained to working only within one portlet. Fortunately, a new feature in ICEfaces 3.3 allows developers to implement Drag and Drop functionality across different portlets on a page. This opens a world of possibilities in portlet applications. It is now possible to share the exact data object or POJO via Drag and Drop with different portlets, which can operate on the same object in different ways. All of this is now possible without having to make big changes to existing portlets or pages, and their beans, to accomplish this. This tutorial illustrates an easy technique for inter-portlet Drag and Drop functionality.
This tutorial was developed using Liferay 6. However, the technique illustrated here can be used in any portal container that supports JSF. It is assumed that the reader is familiar with basic portlet development.
In this tutorial we will create 3 different portlets: one that contains draggable items, and two similar portlets that will be used as drop zones. The first portlet will simply contain a one-column table with a few rows. Each of those rows will be draggable. Rather than rows, we will view them as items to drag. The other two portlets will be almost identical. Each of them will contain a one-column table, initially empty, that will be populated with the items that we drop on them. Thus, the items we drop will be removed from the first portlet and will be added to the portlet where we dropped them.
The technique is based on the new dragStartListener and datasource attributes supported by ace:draggable. We will create three different beans and one simple data object. The technique uses a window-scoped bean as a managed property in two view-scoped beans to allow communication between them. In other words, the window-scoped bean is shared among portlets on the same page. Each portlet in this tutorial uses a view-scoped bean, whose spatial scope is the portlet itself and nothing outside of it. While it would be possible to accomplish inter-portlet Drag and Drop with a window-scoped bean alone, portlets are more likely to be designed with view-scoped beans, so this technique shows how to easily add inter-portlet communication to existing portlets. At the same time, working with view-scoped beans will allow us to reuse those beans either on the same page or in a different page, allowing us to leverage the full potential or portlet development, rather than having a page-design mentality, where we could end up with less flexibility and less reusability.
We will now describe the different facelets and beans that we will use and explain their purposes. We will just highlight the core parts of the code. The full source code is available in a zip file with this tutorial, where you can also see the file structure. At the end, we'll go through the required XML configuration to register these portlets in Liferay.
This portlet will simply contain a one-column table with five rows. Each of those rows will be draggable. We will create a file named drag.xhtml, whose body will contain the following markup:
The key point in the markup above is the use of a dragStartListener handler method and the reference to the table in the datasource attribute of ace:draggable. This will cause that the DragDropEvent object of the handler method contain a reference to the same data object corresponding to the "row" being dragged. The for atribute specifies the id of the component that will receive the draggable functionality, in this case, an h:outputText component. The scope attribute specifies a string for the draggable that must match the same scope string of the droppable component in order to be a valid drop action. In this case we simply use the string 'tutorial' for our scope. The other attributes simply have to do with the way things look. When revert is true, the draggable item will return to its initial position if it's not dropped on a valid drop target. When using helper set to 'clone', a new HTML node, based on the original one, will be created and dragged, instead of the original. The opacity attribute simply specifies the opacity of this helper node to distinguish it from the original one. The bean for this portlet will be named DraggableBean.java. It will be view-scoped, and initially it will look like this:
We will add more to it later. At this point, it is only necessary to know that this bean simply contains a list of items that serves as a model for our data table. It also contains the handler method that will be invoked when we start to drag an item. Notice that in this handler, we are retrieving the actual data object, corresponding to the item, from the event object.
The entry for this bean in faces-config.xml would look like this:
We will create two of these portlets. These portlets will simply contain a panel that will serve as a drop zone and a table inside of it, initially empty, that will display the items that have been dropped on it. We will create a file named drop1.xhtml and a file named drop2.xhtml. Their bodies will contain the following markup:
The only difference between drop1.xhtml and drop2.xhtml is that drop2.xhtml will have a different header label in the container panel, for the sole purpose of distinguishing between both of them (i.e. <ace:panel header="Drop Target 2">).
In this case, by nesting the ace:droppable component inside of the ace:panel component, we make the panel a drop target on which we can drop our draggables. The dropListener attribute will be fired whenever a valid drop action occurs. Again, we're using the scope attribute with the 'tutorial' string to only allow draggables with the same scope string to be dropped in this drop target. The tolerance attribute simply determines whether the draggable must be completely contained in the drop target in order to be dropped or if it is enough to be touching it.
Both of these portlets will use the same view-scoped bean. We only need to write one. We don't have to make any modification for it to work with a different portlet. This bean will be named DroppableBean.java, and its initial contents will be the following:
We will also add more to this bean later. We will explain in the next section how the magic happens and how the drop handler will eventually work to accomplish what we described at the beginning of the tutorial.
The entry for this bean in faces-config.xml would look like this:
This is the most important part of the technique. We will create a window-scoped bean, named WindowScopedBean.java, that will allow our two view-scoped beans described above to share objects. The bean itself is quite simple and looks like this:
For more information about the window scope offered by ICEfaces, please visit this wiki page. The entry for this bean in faces-config.xml would look like this:
The important part is how the view-scoped beans are going to use this bean. So, we will add the following code to both DraggableBean.java and DroppableBean.java:
We will make this bean a managed property of DraggableBean.java and DroppableBean.java. Thus, it is important to write the setter method for this property, as we did above, so that the framework can actually add to our view-scoped beans a reference to this window-scoped bean. To make this bean a managed property of our view-scoped beans, just add the following markup inside their managed-bean entries in faces-config.xml:
Now, we will add two more lines to our drag-start handler in DraggableBean.java. These lines will save the object, corresponding to the item being dragged, in the window-scoped bean, which will be later retrieved by the drop handler in DroppableBean.java. We will also add a reference to the original list of this item, so it can be removed after the drop operation. At the end, this method will look like this:
Now, we will finally add some code to our drop handler in DroppableBean.java. The first line will retrieve the object we just saved in the window-scoped bean. Then, we check if the object is not null, and if it is not, we add it to this portlet's list and remove it from the draggables portlet list. At the end, this method will look like this:
The sequence of events should be clearer now. First, when a draggable item from the draggables portlet starts being dragged, the handleDrag method is fired, saving references for the item object and a the original list in the window-scoped bean. Then, when the item is dropped on a drop target portlet, the handleDrop method is fired, retrieving the objects that were set in the window-scoped bean by handleDrag and moving the item object from one original list to the drop portlet list.
It might seem like it is unnecessary to save a reference to the original list every time an item starts being dragged, since there's only one draggables portlet. However, this is done to illustrate that there could be many different draggables portlets, and, by using this technique, it would be easy to move items from one portlet to another.
We have been using a very simple data object, just to avoid directing the attention of the reader away from the main point of this tutorial, but of course, more complex data objects can be used. The code for this class is the following:
At this point, we have already accomplished our goal of facilitating inter-portlet Drag and Drop functionality. However, from the user perspective, the effects might not be very visible, since when we add an item to a drop target, the table of that portlet is updated to reflect the fact that it now contains a new item, but that same item doesn't seem to disappear from the draggables portlet. We can't use an <ace:ajax> tag to cause an ajax update in a different portlet. However, we can use ICEpush to accomplish this, and fortunately, it is very easy to do.
We will import the class org.icefaces.application.PushRenderer to our two view-scoped beans. Then, we will add the following line at the end of the constructor in DraggableBean.java: PushRenderer.addCurrentView("tutorial");. Finally, we will add the following line at the end of the drop handler in DroppableBean.java: PushRenderer.render("tutorial");. That's all we need to dynamically update our draggables portlet after a drop event in our drop target portlets.
This is a very basic ICEpush illustration, since ICEpush is out of the scope of this tutorial. For a more complete introduction to ICEpush, please look for the tutorial named Easy Ajax Push.
Portlet development basics are also out of the scope of this tutorial. More information can be found on this wiki by searching for the 'portlet' keyword. However, we will show how the Liferay XML configuration files look like for this tutorial. Again, the full source code needed for this tutorial, including all the XML configuration can be found in the zip file on this page.
Once everything is finished, just compile the project to produce a war file. Then, in Liferay, install these portlets by uploading this war file in the administrator interface. Finally, create a new page in Liferay and add all the three portlets you just installed to that page and see for yourself how inter-portlet drag and drop is now possible.
© Copyright 2021 ICEsoft Technologies Canada Corp.