Pages

Wednesday, November 7, 2007

Drag and drop, update department of employees by dropping it on a department

In JDeveloper 11g supports drag and drop which gives us nice new possibilities. There are different types of drag sources and drop targets. This great oracle example website shows the combinations.
In this example I make on the rich employee table a collection Drag Source and on the department form a drop target for this collection. I changed the employee table so it support multiple selection so I can update the department of many employees at the same time

first we drop from the component palette collectionDragSource on the table

On the form layout we put a droptarget with a dataflavor

Now we are ready to make the backing bean


public class empDndBean {

public DnDAction drop(DropEvent dropEvent) {
try {

DataFlavor df = DataFlavor.getDataFlavor(RowKeySet.class);
RowKeySet droppedValue = dropEvent.getTransferable().getData(df);
if (droppedValue == null) {
return DnDAction.NONE;
} else {
// get the current department
DCIteratorBinding departmentsIter = (DCIteratorBinding) getBindings().get("DeptView1Iterator");
Row rw = departmentsIter.getCurrentRow();
Number department = (oracle.jbo.domain.Number)rw.getAttribute("Deptno");

AppModuleImpl am = getAm();

Object[] keys = droppedValue.toArray();
Key empKey= null;
// get the employee which we want to update the department
for (int i = 0; i < keys.length; i++) {
List l = (List)keys[i];
empKey = (Key)l.get(0);
EmpViewRowImpl empRow = (EmpViewRowImpl) am.getEmpView1().getRow(empKey);
empRow.setDeptno(department);
}
am.getDBTransaction().commit();
// requery the employees view
am.getEmpView2().clearCache();
am.getEmpView2().executeQuery();
am.getEmpView1().clearCache();
am.getEmpView1().executeQuery();

}
return DnDAction.COPY;
} catch (Exception ex) {
System.out.println("drop failed with : " + ex.getMessage());
return DnDAction.NONE;
}
}

private BindingContainer getBindings() {
FacesContext fc = FacesContext.getCurrentInstance();
Application app = fc.getApplication();
ExpressionFactory elFactory = app.getExpressionFactory();
ELContext elContext = fc.getELContext();
ValueExpression valueExp = elFactory.createValueExpression(elContext, "#{bindings}", Object.class);
return (BindingContainer) valueExp.getValue(elContext);
}

private AppModuleImpl getAm(){
FacesContext fc = FacesContext.getCurrentInstance();
Application app = fc.getApplication();
ExpressionFactory elFactory = app.getExpressionFactory();
ELContext elContext = fc.getELContext();
ValueExpression valueExp =
elFactory.createValueExpression(elContext,
"#{data.AppModuleDataControl.dataProvider}",
Object.class);
return (AppModuleImpl)valueExp.getValue(elContext);
}

}

Now you can run it.

Here is the workspace

No comments:

Post a Comment