对话框 函数_通过函数式编程实现动态对话框处理程序
對話框 函數
在我以前的文章中,我提到了一個常見的用例,當我們需要以編程方式檢查當前事務是否臟了,并在做某件事之前通知用戶有關該事務的信息。 就像“您尚未保存的更改將丟失,您要繼續嗎?”。
假設我們需要在應用程序的許多位置,從一個視圖導航到另一個視圖,單擊“搜索”按鈕,調用業務服務方法等時,通知用戶有關交易不干凈的情況。因此,在每種情況下,我們都需要做在用戶確認他們要繼續之后的不同操作。 這意味著我們的對話偵聽器應該以某種方式知道它的全部內容以及下一步要做什么。
解決方案可能是向af:dialog組件添加自定義屬性,該自定義屬性指向當用戶在對話框中單擊“是”時將要調用的函數:
<af:popup id="pDirtyTransaction" contentDelivery="lazyUncached"><af:dialog title="Warning" type="yesNo" closeIconVisible="false"id="dDirtyTransaction"dialogListener="#{theBean.dirtyTransactionDialogListener}"><af:outputText value="You have unsaved changes, do you want to continue?"id="ot1"/><f:attribute name="dialogHandler" value=""/> ? ? ? ? ? ? ? ? ? </af:dialog> </af:popup>在這種情況下,對話框偵聽器可能如下所示:
public void dirtyTransactionDialogListener(DialogEvent dialogEvent) { ? ? ? Map attrs = dialogEvent.getComponent().getAttributes();Consumer<Boolean> dialogHandler = (Consumer) attrs.get("dialogHandler");if (dialogHandler != null) {dialogHandler.accept(dialogEvent.getOutcome() == DialogEvent.Outcome.yes);attrs.put("dialogHandler",null);} ? ? ? ? ? ? ? ? ? }我們在這里期望dialogHandler屬性指向實現Consumer功能接口的對象。
在我們的utils中有一個方法顯示帶有對話框的彈出窗口:
public static void showDirtyTransactionPopup(Consumer dialogHandler) {if (dialogHandler != null) {JSFUtil.findComponent("dDirtyTransaction").getAttributes().put("dialogHandler",dialogHandler);}RichPopup popup =(RichPopup) JSFUtil.findComponent("pDirtyTransaction");popup.show(new RichPopup.PopupHints()); }讓我們在一個簡單的場景中使用這種方法。 在我們的任務流View1和View2中有兩個視圖活動。 用戶單擊按鈕以從一個視圖導航到另一個視圖。 導航時,我們需要檢查當前事務是否臟了,是否詢問用戶是否要繼續。 我們可以利用Java 8 Lambda表達式的功能并實現按鈕動作偵聽器,如下所示:
public void buttonActionListener(ActionEvent actionEvent) {if (Utils.isTransactionDirty()) {? ? ? ?Utils.showDirtyTransactionPopup((yesOutcome) -> {? ? ? ? ? //the code below will be invoked by the dialog listener//when the user clicks a button on the dialog ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if ((Boolean) yesOutcome) {//the user has agreed to proceed,//so let's rollback the current transactionUtils.getCurrentRootDataControl().rollbackTransaction();? ? ? ? ? ? //and queue an action event for this button againnew ActionEvent(actionEvent.getComponent()).queue();} });} else//just navigate to View2Utils.handleNavigation("goView2"); }基于此技術,我們可以實現一個聲明性組件,用作具有動態內容和動態處理程序的對話框。
而已!
翻譯自: https://www.javacodegeeks.com/2017/11/implementing-dynamic-dialog-handler-functional-programming.html
對話框 函數
總結
以上是生活随笔為你收集整理的对话框 函数_通过函数式编程实现动态对话框处理程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 蒸汽世界ol(蒸汽世界备案)
- 下一篇: 纹理和基元_通过粘性仙人掌基元进行延迟加