Friday, May 16, 2014

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

In the last exercise we saw how we can move one row from one table to another, this exercise we will extend the previous example and enhance it so that it can now allow multiple rows to be dragged and dropped among tables.

There is a small code change for this we will have to loop through all the rows in the collection to be inserted in the second table.

so my exact code will look like this

package view;


import java.util.Iterator;

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();

}

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);
Iterator itr = rowKeySet.iterator();
RichTable dropTable = (RichTable)dropEvent.getDragComponent();

while (itr.hasNext()) {
List key = (List)itr.next();
rt.setRowKey(key);
JUCtrlHierNodeBinding rowBinding =
(JUCtrlHierNodeBinding)rt.getRowData();
Row row = (Row)rowBinding.getRow();

//addRow(row);

DCBindingContainer Dcbc =
(DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
DCIteratorBinding dc =
Dcbc.findIteratorBinding("Emptable2View1Iterator");
RowSetIterator iter = dc.getRowSetIterator();
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;
}

}

Here as you can observer we are getting iterator and for all the values we are inserting data into other table.

After making the change deploy the code to integrate weblogic server.

Select multiple rows



After dropping it to other table




Here you can observer that i am not deleting the records after dropping them into table 2.

If required that functionality can also be achieved by implementing change in code in dropListener method.

Now if we wanted to remove the rows that we have dragged from the source table there is a small change in the code.

Infact we do not need the dragListener for this purpose so you can comment out the code for dragListener and remove the reference of dragListener method in the JSP page.



A small change in code to implement removal of data

package view;


import java.util.Iterator;
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 iterator = dcib.getRowSetIterator();
//
//
// }

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);
Iterator itr = rowKeySet.iterator();
RichTable dropTable = (RichTable)dropEvent.getDragComponent();

while (itr.hasNext()) {
List key = (List)itr.next();
rt.setRowKey(key);
JUCtrlHierNodeBinding rowBinding =
(JUCtrlHierNodeBinding)rt.getRowData();
Row row = (Row)rowBinding.getRow();

DCBindingContainer Dcbc =
(DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
DCIteratorBinding dc =
Dcbc.findIteratorBinding("Emptable2View1Iterator");
RowSetIterator iter = dc.getRowSetIterator();
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);
row.remove();
}
return DnDAction.MOVE;
}

}


And deploy the code to the server.

Now select few rows in the source table and drag and drop it to the target table.

Drag



After dropping




One important thing to consider while dropping the data controls in ADF page is that since you want to select multiple rows you have to select that option while dropping the table as shown.

No comments: