Advertisement
Guest User

display modal dialog only if validation is sucessful

a guest
May 22nd, 2014
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.05 KB | None | 0 0
  1. import org.apache.wicket.ajax.AjaxRequestTarget;
  2. import org.apache.wicket.ajax.markup.html.AjaxLink;
  3. import org.apache.wicket.ajax.markup.html.form.AjaxButton;
  4. import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
  5. import org.apache.wicket.markup.html.WebPage;
  6. import org.apache.wicket.markup.html.form.Form;
  7. import org.apache.wicket.markup.html.form.TextField;
  8. import org.apache.wicket.model.CompoundPropertyModel;
  9.  
  10. import java.io.Serializable;
  11.  
  12. public class FormTestPage extends WebPage {
  13.  
  14.     public FormTestPage() {
  15.     }
  16.  
  17.     @Override
  18.     protected void onInitialize() {
  19.         super.onInitialize();
  20.         add(new TheForm("TheForm", new CompoundPropertyModel<TestModel>(new TestModel())));
  21.     }
  22.  
  23.     private class TheForm extends Form<TestModel> {
  24.  
  25.         public TheForm(String myForm, CompoundPropertyModel<TestModel> model) {
  26.             super(myForm, model);
  27.  
  28.             TextField<String> textField = new TextField<String>("name");
  29.             add(textField);
  30.             textField.setRequired(true);
  31.  
  32.             final ModalWindow modalWindow = new ModalWindow("ModalWindow");
  33.             add(modalWindow);
  34.  
  35.             add(new AjaxButton("AjaxButton", this) {
  36.  
  37.                 @Override
  38.                 protected void onSubmit(AjaxRequestTarget target, Form form) {
  39.  
  40.                     // process your model;
  41.                     xyz();
  42.  
  43.                     // create the close link for the SuccessPanel
  44.                     AjaxLink closeLinkForSuccessPanel = createCloseLinkForSuccessPanel(modalWindow);
  45.  
  46.                     // set the Content
  47.                     modalWindow.setContent(new SuccessPanel(ModalWindow.CONTENT_ID, closeLinkForSuccessPanel));
  48.  
  49.                     // set the call back
  50.                     modalWindow.setWindowClosedCallback(new ModalWindow.WindowClosedCallback() {
  51.  
  52.                         @Override
  53.                         public void onClose(AjaxRequestTarget target) {
  54.                                 setResponsePage(new AnyPage());
  55.                         }
  56.                     });
  57.  
  58.                     // show the panel
  59.                     modalWindow.show(target);
  60.                 }
  61.  
  62.             });
  63.         }
  64.     }
  65.  
  66.     private AjaxLink createCloseLinkForSuccessPanel(final ModalWindow modalWindow) {
  67.  
  68.         return new AjaxLink("close") {
  69.  
  70.             @Override
  71.             public void onClick(AjaxRequestTarget target) {
  72.                 modalWindow.close(target);
  73.             }
  74.         };
  75.     }
  76.  
  77. }
  78.  
  79. //
  80. //
  81. //
  82. public class TestModel implements Serializable {
  83.  
  84.     private String name;
  85.  
  86.     public TestModel() {
  87.     }
  88.  
  89.     public String getName() {
  90.         return name;
  91.     }
  92.  
  93.     public void setName(String name) {
  94.         this.name = name;
  95.     }
  96. }
  97.  
  98. //
  99. // FormTestPage.html
  100. //
  101. <?xml version="1.0" encoding="UTF-8"?>
  102. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  103.         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  104. <html xmlns="http://www.w3.org/1999/xhtml"
  105.       xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd"
  106.       xml:lang="fr" lang="fr">
  107.  
  108. <body>
  109.  
  110.     <form wicket:id="TheForm">
  111.  
  112.         <span style="display: none;" wicket:id="ModalWindow"></span>
  113.  
  114.         <input wicket:id="name" type="text" size="40">
  115.  
  116.         <input wicket:id="AjaxButton" type="submit" value="submit"/>
  117.  
  118.     </form>
  119.  
  120. </body>
  121.  
  122. </html>
  123.  
  124.  
  125. //
  126. //
  127. //
  128. public class SuccessPanel extends Panel {
  129.     public SuccessPanel(String wid, AjaxLink closeLinkForSuccessPanel) {
  130.         super(wid);
  131.         add( closeLinkForSuccessPanel );
  132.     }
  133. }
  134.  
  135. //
  136. // SuccessPanel.html
  137. //
  138. <?xml version="1.0" encoding="UTF-8"?>
  139. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  140.         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  141. <html xmlns="http://www.w3.org/1999/xhtml"
  142.       xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd"
  143.       xml:lang="fr" lang="fr">
  144.  
  145. <wicket:panel>
  146.  
  147.     success
  148.  
  149.     <br>
  150.  
  151.     <a wicket:id="close">close</a>
  152.  
  153. </wicket:panel>
  154.  
  155. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement