View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.client.enduser.panels;
20  
21  import java.io.Serializable;
22  import org.apache.syncope.client.enduser.pages.BasePage;
23  import org.apache.wicket.PageReference;
24  import org.apache.wicket.ajax.AjaxRequestTarget;
25  import org.apache.wicket.ajax.markup.html.form.AjaxButton;
26  import org.apache.wicket.markup.html.form.Button;
27  import org.apache.wicket.markup.html.form.Form;
28  import org.apache.wicket.model.CompoundPropertyModel;
29  
30  public abstract class AbstractAnyFormPanel<T extends Serializable> extends AbstractFormPanel<T> {
31  
32      private static final long serialVersionUID = -5976166731584959275L;
33  
34      protected final Form<T> form;
35  
36      public AbstractAnyFormPanel(final String id, final T defaultItem, final PageReference pageReference) {
37          super(id, defaultItem, pageReference);
38  
39          form = new Form<>("form");
40          form.setOutputMarkupId(true);
41          add(form);
42          AjaxButton submitButton = new AjaxButton("submit") {
43  
44              private static final long serialVersionUID = 4284361595033427185L;
45  
46              @Override
47              protected void onSubmit(final AjaxRequestTarget target) {
48                  onFormSubmit(target);
49              }
50  
51              @Override
52              protected void onError(final AjaxRequestTarget target) {
53                  ((BasePage) getPage()).getNotificationPanel().refresh(target);
54              }
55          };
56  
57          submitButton.setOutputMarkupId(true);
58          submitButton.setDefaultFormProcessing(true);
59          form.add(submitButton);
60  
61          Button cancel = new Button("cancel") {
62  
63              private static final long serialVersionUID = 3669569969172391336L;
64  
65              @Override
66              public void onSubmit() {
67                  setResponsePage(getApplication().getHomePage());
68              }
69  
70          };
71          cancel.setOutputMarkupId(true);
72          cancel.setDefaultFormProcessing(false);
73          form.add(cancel);
74      }
75  
76      public Form<T> getForm() {
77          return form;
78      }
79  
80      public void setFormModel(final T modelObject) {
81          form.setModel(new CompoundPropertyModel<>(modelObject));
82      }
83  
84      protected void onCancelInternal(final T modelObject) {
85      }
86  
87      protected Serializable onApplyInternal(final T modelObject) {
88          // do nothing
89          return null;
90      }
91  
92      protected abstract void buildLayout(T modelObject);
93  
94      protected abstract void onFormSubmit(AjaxRequestTarget target);
95  }