2010-07-01 9 views
12

J'essaie de créer une boîte de dialogue de confirmation modale. Je voudrais que cela fonctionne comme Window.confirm(""), où je peux juste l'appeler, et obtenir une réponse booléenne.Boîte de dialogue de confirmation GWT

Mon problème est que je ne suis pas sûr de savoir comment le faire. J'essaie d'utiliser MVP dans mon application. Voici le code que j'ai jusqu'à présent:

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

et moi:

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

} 

Répondre

19

Tu ne vas pas être en mesure de le faire fonctionner exactement de la même manière que Window.confirm(). Le problème est que tout le javascript dans une page Web s'exécute dans un seul fil. Vous remarquerez que tant qu'une boîte de dialogue de confirmation standard est ouverte, le reste de la page disparaît. C'est parce que le thread javascript est bloqué, en attente de confirm() pour retourner. Si vous deviez créer une méthode similaire pour votre boîte de dialogue, tant qu'elle attendait que cette méthode ne renvoie aucun événement généré par l'utilisateur, elle ne serait pas traitée. J'espère que cela à du sens. Le mieux que vous puissiez faire est similaire à ce que fait la bibliothèque GWT pour les appels RPC - l'interface AsyncCallback. Vous pouvez même réutiliser que vous l'interface, ou vous pouvez préférer rouler votre propre:

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

Au lieu de Window.confirm(String), votre signature de la méthode sera plus comme Dialog.confirm(String,DialogCallback). Ensuite, votre boîte de dialogue garde juste une référence au rappel qui est passé, et où vous avez // do something dans votre code, vous faites des appels à onOk et onCancel.

+0

Cela fonctionne. Merci Monsieur. Je posterai mon code dans une autre réponse pour tous ceux qui sont curieux. – KevMo

8

Voici le code que je travaille si quelqu'un est curieux.

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

Qu'est-ce que LensooConstant? Merci. – Eugen

+0

Oh, désolé. Je n'avais pas réalisé que j'avais laissé ça là-dedans. C'est juste une interface qui contenait mes Constantes de Chaîne pour différents support de langue. C'était une façon stupide de l'implémenter, et cela a été changé depuis. – KevMo

+0

Super exemple MVP simple! – MatejC

Questions connexes