Drag And Drop in Oracle Forms and Java

Posted by Tobias on November 30, 2010 00:00 CET Updated on April 22, 2013 21:51 CEST 7 Comments Add a comment

Contents

I came across an example of how to implement drag and drop in Oracle Forms. It kind of worked but did not impress me, so I decided to do my own implementation. When I read the source code for the other example one thought crossed my mind which I would like to share; do not reinvent the wheel unless you have a gun to your head. If you do have a gun to your head, expect to end up with a not so round wheel.

Because the presentation layer in Oracle Forms is written in Java a lot of information in this article also apply on standard Java applications.

Research

The first task when doing something like this is research. Maybe drag and drop is already implemented in Forms? Actually, it is! Open the file ‘frmall.jar’ with some archiving software, like the excellent 7-Zip application, and have a look at the ‘oracle.ewt.dnd’ package. There are several classes which handle drag and drop but there is one catch; we don’t know how they work. Why? Because Oracle doesn’t like to share their knowledge with their customers. Scratch that! Oracle doesn’t like to share their knowledge with anyone!

Fig. 1
Figure 1. oracle.ewt.dnd package

So without any documentation or source code we would have a really hard time implementing drag and drop using the classes from Oracle. I mentioned earlier that one should not reinvent the wheel and we don’t have to in this case. Java comes with full drag and drop support out of the box and it’s up to you to make the best of it. I also mentioned earlier that the other example I found on the web kind of worked. It did, within Forms but it could not communicate with other applications. If we use the built in support in Java we’ll end up with drag and drop in the Forms application as well as drag and drop from and to other applications.

So let’s start with the first task: research.

//download.oracle.com/javase/tutorial/uiswing/dnd/intro.html

//download.oracle.com/javase/1.5.0/docs/guide/dragndrop/spec/dnd1.html

Already done? Don’t cheat!

Ok, so basically we need three things: a drag source, a drop target and something to transfer. In this example we’ll use text boxes as drag sources and drop targets and text strings as transfer data. You could have almost anything as transfer data as you can transfer binary data between objects.

While doing research, we’ll also find out how dragging text in other applications work. Notice when you select text in Internet Explorer and hover your mouse over it the cursor will be a standard cursor. Now try selecting text in Firefox or Chrome, the cursor will not change into the standard arrow but you can still drag the text. So now we know there are multiple ways of visualizing draggable text. Which one is the correct method? Maybe someone will write an article about it, not me though. In the example I have implemented both visualizing methods; arrow for TextFields and text selector for TextAreas.

Implementation

The TextField gets created by setting the implementation class in Forms. In the initialization of the TextField it will create an instance of DnDHandler which will handle drag and drop for the component. When the DnDHandler gets created its first task is to set up the component which created it as a DropTarget. The DnDHandlers second task is to create a listener for the component which will trigger when a DragGesture is recognized. That’s it for initialization. Now the application is running and waiting for the user to start dragging something or drop something from the outside.

When a user triggers a DragGestureEvent, a method in the FormsDragGestureListener gets called which enables us to do various things but eventually we’ll decide to start a DragEvent. How do Forms know that we have started dragging? Remember the wheel? This is the rewards we receive by using an existing wheel, i.e. it gets handled for us. All we need to know is that the dragGestureRecognized() method gets called. While starting the DragEvent we’ll create the transferable data object which in this case will be an instance of StringTransferable. This class is a container for a string but could just as much be a container for an image or perhaps an episode of Family Guy. You decide.

The user now drags the text into another text box; this is where the FormsDropTargetListener comes into action. Some required methods need to be implemented here but the most important ones are dragOver() and drop(). The dragOver() method is where we can accept or reject the item being dragged. We can only accept strings in this case so look into the StringTransferable instance and decide if we can accept it. If we have to reject the StringTransferable we’ll call rejectDrop() in the DropTargetDropEvent. This will make the mouse pointer turn into a stop cursor. Again, this is handled for us because we are clever.

When the user finally releases the mouse button the drop() method in FormsDropTargetListener is called. This is where we unpack the transferable object and process the content. In this case the transferable is a string and we will only replace the current content of our TextField with the transfer string. We could do more advanced things like inserting the transfer string at the position of the mouse cursor. After we process the transfer object we need to let the drag source know that we are finished, we do this by calling dropComplete() in the DropTargetContext. This triggers a call to dragDropEnd() in FormsDragSourceListener which enables us to process the event on the drag source. Here we can remove the selected text from the TextField if this was a move event.

Fig. 2
Figure 2. Drag And Drop in Oracle Forms

The downloadable code also demonstrates how to make PJCs (Pluggable Java Components) send custom events in Forms 10. If you’re running Forms 11 PJCs already knows how to send custom events.

Installation

Copy the DragnDrop.jar file to your ‘forms\java’ folder then add it to your ‘formsweb.cfg’ file.

Download

Download binaries and sources v1.0.0 (44KB)

Subversion repository at code.google.com

Post your comment

7 Comments (newest first)

Posted by Stephan on May 11, 2017 14:55 CEST

Hi, is there any chance I can obtain the source code for this? The download does only contain the jar-file and the subversion repository doesn't seem to contain anything either. Thanks :-)

Posted by Tobias on June 22, 2012 16:54 CEST

...and when the drag and drop is complete and the drag source want to find out where we dropped the transferable we would call DragSourceDropEvent#getSource() method in the FormsDragSourceListener#dragDropEnd() method. Best

Posted by Tobias on June 22, 2012 16:48 CEST

Kevin, I assume you want to find the relation between the drag source and the drop target. So in the case when "this" is the drop target and we want to get a reference to the drag source we need to call DropTargetDropEvent#getSource() which in this implementation would be in the FormsDropTargetListener#processDropTargetDropEvent() method. To get a reference to the drop target in this case we would need to call DropTargetDropEvent#getDropTargetContext()#getComponent() in the FormsDropTargetListener#processDropTargetDropEvent() method. In this example we use this method to set the dropped text into the text box, see the FormsDropTargetListener#setText() method for an example of this. Best

Posted by Kevin on June 22, 2012 11:04 CEST

Very nice, it works just great! But how can I know from which item I started to drag and which item I did the drop? Best Regards, Kevin kevin.lommens@hotmail.com

Posted by Tobias on May 22, 2012 21:37 CEST

This article was written for Forms 10, which version are you running? Also, please post any stack traces if you are getting crashes. Best

Posted by Rupali on May 18, 2012 15:48 CEST

Hi Tobias, I have downloaded code attached in the above document & also followed steps mentioned in the Installation. However I was not able to drag & drop a text. Could you please tell me what could be the possible solution. My email is: rupali.dayama@kpitcummins.com

Posted by BqwcPCSARn on July 30, 2011 06:01 CEST

These topics are so confusnig but this helped me get the job done.

Read More