2010-07-01 5 views
12

Próbuję utworzyć okno dialogowe z potwierdzeniem modalnym. Chciałbym, żeby działało jak Window.confirm(""), gdzie mogę go po prostu wywołać i uzyskać odpowiedź boolowską.Okno dialogowe potwierdzenia GWT

Mój kłopot polega na tym, że nie jestem pewien, jak to zrobić. Próbuję użyć MVP w mojej aplikacji. Oto kod mam tak daleko:

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()); 
    } 
} 

i mój widok:

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); 
    } 

} 

Odpowiedz

19

Nie będziemy mieć możliwość to działa dokładnie w taki sam sposób jak Window.confirm(). Problem polega na tym, że cały javascript na stronie działa w jednym wątku. Zauważysz, że tak długo, jak standardowe okno dialogowe potwierdzenia jest otwarte, reszta strony zanika. Dzieje się tak, ponieważ jeden wątek javascript jest zablokowany, czekając na powrót do confirm(). Jeśli miałbyś stworzyć podobną metodę dla twojego okna dialogowego, tak długo, jak długo czekał na tę metodę, aby powrócić, żadne zdarzenia generowane przez użytkownika nie byłyby przetwarzane, więc twoje okno dialogowe nie działałoby. Mam nadzieję, że ma to sens.

Najlepsze, co można zrobić, jest podobne do tego, co robi biblioteka GWT dla wywołań RPC - interfejs AsyncCallback. Można nawet ponowne wykorzystanie tego interfejsu siebie, lub może wolisz, aby rzucić swój własny:

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

Zamiast Window.confirm(String), podpis metoda będzie bardziej jak Dialog.confirm(String,DialogCallback). Następnie w oknie dialogowym znajduje się odniesienie do wywołania zwrotnego, które zostało przekazane i gdzie w kodzie jest // do something nawiązywane są połączenia z onOk i onCancel.

+0

To działa. Dziękuję Panu. Opublikuję mój kod w innej odpowiedzi dla każdego, kto jest ciekawy. – KevMo

8

Oto kod, nad którym pracuję, jeśli ktoś jest ciekawy.

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

Co to jest LensooConstant? Dzięki. – Eugen

+0

Och, przepraszam. Nie wiedziałem, że to zostawiłem. To tylko interfejs, który zawierał Moje Stałe Stałe do obsługi różnych języków. To był głupi sposób na jego wdrożenie i od tego czasu został zmieniony. – KevMo

+0

Świetny przykład MVP prosty! – MatejC