Thursday, May 15, 2014

Drag and Drop feature in ADF table-Moving a row from one table to another

In the previous exercise we saw how we can drag and drop the rows in a table and while dropping how we can change the column values.

In this exercise we will see how we can manipulate the same code to drag rows from one table and drop it in another table.

The first thing is to drag and drop the CollectionDropTarget from table1 to table2 as shown in the figure below



Next is go to your java bean and update the binding for the second table.

We were accessing the table1 using following code

Dcbc.findIteratorBinding("Emptable1View1Iterator");

The only change in this will to point it to the executable of second table so the overall code will be


package view;


import oracle.adf.view.rich.component.rich.data.RichTable;
import oracle.adf.view.rich.datatransfer.DataFlavor;
import oracle.adf.view.rich.datatransfer.Transferable;
import oracle.adf.view.rich.dnd.DnDAction;
import oracle.adf.view.rich.event.DropEvent;

import java.util.List;

import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.model.binding.DCIteratorBinding;

import oracle.jbo.Row;
import oracle.jbo.RowSetIterator;
import oracle.jbo.uicli.binding.JUCtrlHierNodeBinding;

import org.apache.myfaces.trinidad.model.RowKeySet;

public class DragDropRows {
public DragDropRows() {
}

public void dropListener(DropEvent dropEvent) {
// Add event code here...
DCBindingContainer bc =
(DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
DCIteratorBinding dcib =
bc.findIteratorBinding("Emptable1View1Iterator");
RowSetIterator iter = dcib.getRowSetIterator();


// while (iter.hasNext()) {
iter.removeCurrentRow();
//get next selected row key
// List key = (List)iter.next();
// //make row current so we can access it
// table.setRowKey(key);
// //the table model represents its row by the ADF binding class,
// //which is JUCtrlHierNodeBinding
// JUCtrlHierNodeBinding rowBinding =
// (JUCtrlHierNodeBinding) table.getRowData();
// Row row = (Row) rowBinding.getRow();
// //delete row
// row.remove();
// //activate animation
// }
}

public DnDAction dropRows(DropEvent dropEvent) {
// Add event code here...

RichTable rt = (RichTable)dropEvent.getDragComponent();
Transferable t = dropEvent.getTransferable();
DataFlavor df =
DataFlavor.getDataFlavor(RowKeySet.class, "MoveRows");
RowKeySet rowKeySet = t.getData(df);
List key = (List)rowKeySet.iterator().next();
rt.setRowKey(key);
JUCtrlHierNodeBinding rowBinding =
(JUCtrlHierNodeBinding)rt.getRowData();
Row row = (Row)rowBinding.getRow();
//addRow(row);
DCBindingContainer Dcbc =
(DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
DCIteratorBinding dcib =
Dcbc.findIteratorBinding("Emptable2View1Iterator");
RowSetIterator iter = dcib.getRowSetIterator();
// while (iter.hasNext()) {
Row newRow = iter.createRow();
newRow.setAttribute("Name", row.getAttribute("Name"));
newRow.setAttribute("Empid", row.getAttribute("Empid"));
newRow.setAttribute("Dept", row.getAttribute("Dept"));
iter.insertRowAtRangeIndex(iter.getRowCount(), newRow);
// return DnDAction.MOVE;
// }
return DnDAction.MOVE;
}


}

Here Emptable2View1Iterator is nothing but the executable for the second table



These are the only two changes that needs to be done.

Now deploy the code to integrated weblogic server.

Once the page comes up try to drag and drop row from one table to another this should work fine.

No comments: