概要:本教程,演示了如何将ADF Faces components定义为drag和drop targets,并实现事件监听。创建Managed bean MyPageInfo并且实现handleItemDrop监听器来处理drop event,并且在配置文件faces-config.xml定义一个实例dnd。faces-config.xml定义了,应用程序需要的资源,例如页面请求的files和beans等。事件处理函数handleItemDrop当拖动coffee到Beverage choices 列表时会出现多个相同内容的项,修改后的代码如下所示。
Target URL -- http://127.0.0.1:7101/DndApp-Project1-context-root/faces/MyPage.jspx
----------------------Start of Drop event handler-------------------------
====The value need to drop is Coffee
Have added to the list....
----------------------End of Drop event handler-------------------------
----------------------Start of Drop event handler-------------------------
====The value need to drop is Coffee
Have already in the list....
----------------------End of Drop event handler-------------------------
----------------------Start of Drop event handler-------------------------
====The value need to drop is Coffee
Have already in the list....
----------------------End of Drop event handler-------------------------
修改后的handleItemDrop
参考【1】http://docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_41/jdtut_11r2_41.html
运行结果:
Target URL -- http://127.0.0.1:7101/DndApp-Project1-context-root/faces/MyPage.jspx
----------------------Start of Drop event handler-------------------------
====The value need to drop is Coffee
Have added to the list....
----------------------End of Drop event handler-------------------------
----------------------Start of Drop event handler-------------------------
====The value need to drop is Coffee
Have already in the list....
----------------------End of Drop event handler-------------------------
----------------------Start of Drop event handler-------------------------
====The value need to drop is Coffee
Have already in the list....
----------------------End of Drop event handler-------------------------
修改后的handleItemDrop
package project1;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.model.SelectItem;
import oracle.adf.view.rich.datatransfer.DataFlavor;
import oracle.adf.view.rich.dnd.DnDAction;
import oracle.adf.view.rich.event.DropEvent;
//@ManagedBean(name="dnd")
//@SessionScoped
public class MyPageInfo implements Serializable {
public MyPageInfo() {
}
/**
* @return the beverage items
*/
private List<SelectItem> _choices;
boolean ifContain = false;
public List<SelectItem> getChoices() {
if (_choices == null) {
_choices = new ArrayList<SelectItem>();
_choices.add(new SelectItem("Cocoa", "Cocoa"));
_choices.add(new SelectItem("Tea", "Tea"));
_choices.add(new SelectItem("Wine", "Wine"));
}
return _choices;
}
/**
* Drop event handler
*/
public DnDAction handleItemDrop(DropEvent dropEvent) {
System.out.println("----------------------Start of Drop event handler-------------------------");
try {
DataFlavor<String> df = DataFlavor.getDataFlavor(String.class);
String droppedValue = dropEvent.getTransferable().getData(df);
System.out.println("====The value need to drop is " +
droppedValue);
if (droppedValue == null) {
return DnDAction.NONE;
} else {
//If the item is already in the list, then do nothing;else append it to the list
if (ifContain) {
System.out.println("Have already in the list....");
System.out.println("----------------------End of Drop event handler-------------------------");
return DnDAction.COPY;
}
for (SelectItem item : _choices) {
if (item.getLabel().equals(droppedValue)) {
ifContain = false;
}
}
if (!ifContain) {
_choices.add(new SelectItem(droppedValue, droppedValue));
System.out.println("Have added to the list....");
ifContain = true;
}
System.out.println("----------------------End of Drop event handler-------------------------");
}
return DnDAction.COPY;
} catch (Exception ex) {
System.out.println("item drop failed with : " + ex.getMessage());
return DnDAction.NONE;
}
}
}
参考【1】http://docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_41/jdtut_11r2_41.html