Monday, March 4, 2013

ADF 11G : How to work with Drop functionality?

ADF 11G : How to work with Drop functionality?









































package view;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.faces.component.ContextCallback;
import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;

import oracle.adf.view.rich.component.rich.data.RichTable;
import oracle.adf.view.rich.context.AdfFacesContext;
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 oracle.jbo.Row;
import oracle.jbo.uicli.binding.JUCtrlHierNodeBinding;

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

public class NeelmaniDropHandlerBean {
    public NeelmaniDropHandlerBean() {
        super();
    }
    
    public DnDAction neelDropHandler(DropEvent dropEvent) {
        RichTable table = (RichTable) dropEvent.getDragComponent();
        Transferable t = dropEvent.getTransferable();
        DataFlavor df =
            DataFlavor.getDataFlavor(RowKeySet.class, "rowmove");
        RowKeySet rks = t.getData(df);
        Iterator iter = rks.iterator();
        while (iter.hasNext()) {
            //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();
            //DCDataRow is the generic row class representation in in ADF. It is
            //the super class of oracle.jbo.Row, which you use with ADF BC services
            Row row = (Row) rowBinding.getRow();
            //delete row
            row.remove();
            //activate animation           
        }
       
      Map viewScope = AdfFacesContext.getCurrentInstance().getViewScope();
      //show different icon for bin colelctor. The new icon
      //does not take drop payloads and is shown for 1 second
      viewScope.put("binState", "fullRecycle");
      //refresh garbage bin parent (panelGrouplLayout)
      refreshComponentContainer("pgl1"); 
      AdfFacesContext.getCurrentInstance().addPartialTarget(table);
      //table source is manually refreshed, so we ca return NONE
      return DnDAction.NONE;
    }
   
    private void refreshComponentContainer(String id){
      String comp = id;
      //see ADF Code Corner sample #58 to read about the code
      //used in the search below
      FacesContext fctx = FacesContext.getCurrentInstance();
      UIViewRoot root = fctx.getViewRoot();           
         root.invokeOnComponent( fctx,comp,
          new ContextCallback(){
             public void invokeContextCallback(FacesContext facesContext,
                                               UIComponent uiComponent) {
                //refresh parent container                
                 AdfFacesContext.getCurrentInstance().addPartialTarget(uiComponent);
            }        
      });
    }
   
    public void onPoll(PollEvent pollEvent) {
        Map viewScope = AdfFacesContext.getCurrentInstance().getViewScope();
        //show different icon for bin colelctor. The new icon
        //does not take drop payloads and is shown for 1 second
        viewScope.put("binState", "emptyRecycle");
       //refresh garbage bin parent (panelGrouplLayout)
       refreshComponentContainer("pgl1");
    }
}