2010-07-01 39 views
12

Tôi đang cố gắng tạo một hộp thoại xác nhận phương thức. Tôi muốn nó hoạt động như Window.confirm(""), nơi tôi có thể gọi nó, và nhận được một phản hồi boolean.Hộp thoại xác nhận GWT

Sự cố của tôi là tôi không chắc chắn cách thực hiện. Tôi đang cố gắng sử dụng MVP trong ứng dụng của mình. Dưới đây là đoạn code tôi có cho đến nay:

public class DialogBoxPresenter implements Presenter { 

    public interface Display { 

     Label getDialogText(); 

     Button getAffirmativeButton(); 

     Button getCancelButton(); 

     Widget asWidget(); 

     public void center(); 

     public void hide(); 

     public void setHeader(String text); 
    } 
    private Display display; 
    private String header; 
    private String dialogText; 
    private String cancelButtonText; 
    private String affirmativeButtonText; 

    protected DialogBoxPresenter() { 
    } 

    public DialogBoxPresenter(Display display, String header, String dialogText, String cancelButtonText, String affirmativeButtonText) { 
     this.display = display; 
     this.header = header; 
     this.dialogText = dialogText; 
     this.cancelButtonText = cancelButtonText; 
     this.affirmativeButtonText = affirmativeButtonText; 

     bind(); 
    } 

    public DialogBoxPresenter(Display display, String header, String dialogText) { 
     this.display = display; 
     this.header = header; 
     this.dialogText = dialogText; 
     this.cancelButtonText = "Cancel"; 
     this.affirmativeButtonText = "OK"; 

     bind(); 
    } 

    private void bind() { 

     this.display.getDialogText().setText(dialogText); 
     this.display.getAffirmativeButton().setText(affirmativeButtonText); 
     this.display.getCancelButton().setText(cancelButtonText); 
     this.display.setHeader(header); 

     addClickHandlers(); 

    } 

    private void addClickHandlers() { 
     this.display.getAffirmativeButton().addClickHandler(new ClickHandler() { 

      @Override 
      public void onClick(ClickEvent event) { 
       doAffirmative(); 
      } 
     }); 

     this.display.getCancelButton().addClickHandler(new ClickHandler() { 

      @Override 
      public void onClick(ClickEvent event) { 
       doCancel(); 
      } 
     }); 
    } 

    private void doAffirmative() { 
     //do something 
     display.hide(); 
    } 

    private void doCancel() { 
     //do something 
     display.hide(); 
    } 

    public void init() { 
     display.center(); 
    } 

    @Override 
    public void go(HasWidgets container) { 
     container.clear(); 
     container.add(display.asWidget()); 
    } 
} 

và quan điểm của tôi:

public class DialogBoxView extends DialogBox implements DialogBoxPresenter.Display { 

    private Label dialogText; 
    private Button affirmativeButton; 
    private Button cancelButton; 
    private VerticalPanel container; 

    public DialogBoxView() { 
     //init items 
     dialogText = new Label(); 

     affirmativeButton = new Button(); 
     cancelButton = new Button(); 

     container = new VerticalPanel(); 

     setGlassEnabled(true); 
     setAnimationEnabled(true); 
     setModal(false); 

     init(); 
    } 

    private void init() { 
     //add items 
     container.add(dialogText); 

     HorizontalPanel hp = new HorizontalPanel(); 
     hp.add(affirmativeButton); 
     hp.add(cancelButton); 

     container.add(hp); 
     this.add(container); 
    } 

    @Override 
    public Widget asWidget() { 
     return this; 
    } 

    @Override 
    public Label getDialogText() { 
     return dialogText; 
    } 

    @Override 
    public Button getAffirmativeButton() { 
     return affirmativeButton; 
    } 

    @Override 
    public Button getCancelButton() { 
     return cancelButton; 
    } 

    @Override 
    public void setHeader(String text) { 
     this.setText(text); 
    } 

} 

Trả lời

19

Bạn sẽ không thể có nó làm việc trong chính xác cùng một cách như Window.confirm(). Vấn đề là tất cả các javascript trong một trang web chạy trong một chủ đề duy nhất. Bạn sẽ nhận thấy rằng miễn là hộp thoại xác nhận chuẩn đang mở, phần còn lại của trang sẽ chết. Đó là vì một chuỗi javascript bị chặn, chờ confirm() trả lại. Nếu bạn tạo một phương thức tương tự cho hộp thoại của mình, miễn là phương thức đó trả về không có sự kiện do người dùng tạo sẽ được xử lý và do đó hộp thoại của bạn sẽ không hoạt động. Tôi hy vọng điều đó đúng.

Điều tốt nhất bạn có thể thực hiện tương tự với thư viện GWT thực hiện cho các cuộc gọi RPC - giao diện AsyncCallback. Bạn thậm chí có thể sử dụng lại giao diện chính mình, hoặc bạn có thể thích cuộn của riêng bạn:

public interface DialogCallback { 
    void onOk(); 
    void onCancel(); 
} 

Thay vì Window.confirm(String), chữ ký phương pháp của bạn sẽ được nhiều hơn như Dialog.confirm(String,DialogCallback). Sau đó, hộp thoại của bạn chỉ giữ tham chiếu đến cuộc gọi lại được chuyển vào và nơi bạn có // do something trong mã của mình, bạn thực hiện cuộc gọi đến onOkonCancel.

+0

Điều đó có hiệu quả. Cảm ơn ngài. Tôi sẽ đăng mã của tôi trong câu trả lời khác cho bất kỳ ai tò mò. – KevMo

8

Đây là mã tôi đã làm việc nếu có ai đó tò mò.

public class DialogBoxPresenter implements Presenter { 

     public interface Display { 

      Label getDialogText(); 

      Button getAffirmativeButton(); 

      Button getCancelButton(); 

      Widget asWidget(); 

      public void center(); 

      public void hide(); 

      public void setHeader(String text); 
     } 
     private Display display; 
     private String header; 
     private String dialogText; 
     private String cancelButtonText; 
     private String affirmativeButtonText; 
     private ConfirmDialogCallback confirmCallback; 
     private AlertDialogCallback alertCallback; 

     protected DialogBoxPresenter() { 
     } 

     public DialogBoxPresenter(Display display, String header, String dialogText, String cancelButtonText, String affirmativeButtonText, ConfirmDialogCallback callback) { 
      this.display = display; 
      this.header = header; 
      this.dialogText = dialogText; 
      this.cancelButtonText = cancelButtonText; 
      this.affirmativeButtonText = affirmativeButtonText; 
      this.confirmCallback = callback; 

      bind(); 
     } 

     public DialogBoxPresenter(Display display, String header, String dialogText, String affirmativeButtonText, AlertDialogCallback callback) { 
      this.display = display; 
      this.header = header; 
      this.dialogText = dialogText; 
      this.affirmativeButtonText = affirmativeButtonText; 
      this.alertCallback = callback; 

      this.display.getCancelButton().setVisible(false); 

      bind(); 
     } 

     private void bind() { 

      this.display.getDialogText().setText(dialogText); 
      this.display.getAffirmativeButton().setText(affirmativeButtonText); 
      this.display.getCancelButton().setText(cancelButtonText); 
      this.display.setHeader(header); 

      addClickHandlers(); 

     } 

     private void addClickHandlers() { 
      this.display.getAffirmativeButton().addClickHandler(new ClickHandler() { 

       @Override 
       public void onClick(ClickEvent event) { 
        doAffirmative(); 
       } 
      }); 

      this.display.getCancelButton().addClickHandler(new ClickHandler() { 

       @Override 
       public void onClick(ClickEvent event) { 
        doCancel(); 
       } 
      }); 
     } 

     private void doAffirmative() { 
      if (confirmCallback != null) { 
       confirmCallback.onAffirmative(); 
      } else { 
       alertCallback.onAffirmative(); 
      } 
      display.hide(); 
     } 

     private void doCancel() { 
      confirmCallback.onCancel(); 
      display.hide(); 
     } 

     public void init() { 
      display.center(); 
     } 

     @Override 
     public void go(HasWidgets container) { 
      container.clear(); 
      container.add(display.asWidget()); 
     } 
    } 




    public class DialogBoxView extends DialogBox implements DialogBoxPresenter.Display { 

     private Label dialogText; 
     private Button affirmativeButton; 
     private Button cancelButton; 
     private VerticalPanel container; 

     public DialogBoxView() { 
      //init items 
      dialogText = new Label(); 

      affirmativeButton = new Button(); 
      cancelButton = new Button(); 

      container = new VerticalPanel(); 

      setGlassEnabled(true); 
      setAnimationEnabled(true); 
      setModal(false); 

      init(); 
     } 

     private void init() { 
      //add items 
      container.add(dialogText); 

      HorizontalPanel hp = new HorizontalPanel(); 
      hp.add(affirmativeButton); 
      hp.add(cancelButton); 

      container.add(hp); 
      this.add(container); 
     } 

     @Override 
     public Widget asWidget() { 
      return this; 
     } 

     @Override 
     public Label getDialogText() { 
      return dialogText; 
     } 

     @Override 
     public Button getAffirmativeButton() { 
      return affirmativeButton; 
     } 

     @Override 
     public Button getCancelButton() { 
      return cancelButton; 
     } 

     @Override 
     public void setHeader(String text) { 
      this.setText(text); 
     } 

    } 


    public class DialogBoxWidget implements LensooConstant { 

     private static DialogBoxView view = null; 
     private static DialogBoxPresenter presenter = null; 

     public static DialogBoxPresenter confirm(String header, String dialogText, String cancelButtonText, String affirmativeButtonText, ConfirmDialogCallback callback) { 
      view = new DialogBoxView(); 
      presenter = new DialogBoxPresenter(view, header, dialogText, cancelButtonText, affirmativeButtonText, callback); 

      presenter.init(); 

      return presenter; 
     } 

     public static DialogBoxPresenter confirm(String header, String dialogText, ConfirmDialogCallback callback) { 
      return DialogBoxWidget.confirm(header, dialogText, constants.cancelButton(), constants.okButton(), callback); 
     } 

     public static DialogBoxPresenter alert(String header, String dialogText, String affirmativeButtonText, AlertDialogCallback callback) { 
      view = new DialogBoxView(); 
      presenter = new DialogBoxPresenter(view, header, dialogText, affirmativeButtonText, callback); 

      presenter.init(); 

      return presenter; 
     } 

     public static DialogBoxPresenter alert(String header, String dialogText, AlertDialogCallback callback) { 
      return DialogBoxWidget.alert(header, dialogText, constants.okButton(), callback); 
     } 

     protected DialogBoxWidget() { 
     } 
    } 

public interface AlertDialogCallback { 

    void onAffirmative(); 

} 

public interface ConfirmDialogCallback { 

    void onAffirmative(); 

    void onCancel(); 
} 
+0

LensooConstant là gì? Cảm ơn. – Eugen

+0

Ồ, xin lỗi. Tôi không nhận ra là tôi đã để nó ở đó. Nó chỉ là một giao diện chứa các chuỗi ký tự của tôi để hỗ trợ ngôn ngữ khác nhau. Đó là một cách ngớ ngẩn để thực hiện nó, và nó đã được thay đổi. – KevMo

+0

Ví dụ tuyệt vời MVP đơn giản! – MatejC

Các vấn đề liên quan