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.ui.commons.wizards;
20  
21  import java.io.Serializable;
22  import org.apache.syncope.client.ui.commons.pages.BaseWebPage;
23  import org.apache.wicket.Component;
24  import org.apache.wicket.MarkupContainer;
25  import org.apache.wicket.ajax.AjaxRequestTarget;
26  import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;
27  import org.apache.wicket.ajax.form.AjaxFormSubmitBehavior;
28  import org.apache.wicket.extensions.wizard.FinishButton;
29  import org.apache.wicket.extensions.wizard.IWizard;
30  import org.apache.wicket.extensions.wizard.IWizardStep;
31  import org.apache.wicket.extensions.wizard.Wizard;
32  import org.apache.wicket.extensions.wizard.WizardButton;
33  import org.apache.wicket.extensions.wizard.WizardButtonBar;
34  import org.apache.wicket.markup.ComponentTag;
35  
36  public class AjaxWizardMgtButtonBar<T extends Serializable> extends WizardButtonBar {
37  
38      private static final long serialVersionUID = 7453943437344127136L;
39  
40      private final AjaxWizard.Mode mode;
41  
42      protected boolean completed = false;
43  
44      public AjaxWizardMgtButtonBar(final String id, final AjaxWizard<T> wizard, final AjaxWizard.Mode mode) {
45          super(id, wizard);
46          this.mode = mode;
47          wizard.setOutputMarkupId(true);
48      }
49  
50      @Override
51      public MarkupContainer add(final Component... childs) {
52          for (Component component : childs) {
53              if (component instanceof WizardButton) {
54                  ajaxify((WizardButton) component);
55              }
56          }
57          return super.add(childs);
58      }
59  
60      private void ajaxify(final WizardButton button) {
61          button.add(new AjaxFormSubmitBehavior("click") {
62  
63              private static final long serialVersionUID = 18163421824742L;
64  
65              @Override
66              protected void updateAjaxAttributes(final AjaxRequestAttributes attributes) {
67                  super.updateAjaxAttributes(attributes);
68  
69                  AjaxWizardMgtButtonBar.this.updateAjaxAttributes(attributes);
70              }
71  
72              @Override
73              public boolean getDefaultProcessing() {
74                  return button.getDefaultFormProcessing();
75              }
76  
77              @Override
78              protected void onSubmit(final AjaxRequestTarget target) {
79                  target.add(findParent(Wizard.class));
80                  button.onSubmit();
81              }
82  
83              @Override
84              protected void onAfterSubmit(final AjaxRequestTarget target) {
85                  button.onAfterSubmit();
86              }
87  
88              @Override
89              protected void onError(final AjaxRequestTarget target) {
90                  target.add(findParent(Wizard.class));
91                  button.onError();
92                  ((BaseWebPage) getPage()).getNotificationPanel().refresh(target);
93              }
94  
95              @Override
96              protected void onComponentTag(final ComponentTag tag) {
97                  tag.put("type", "button");
98              }
99          });
100     }
101 
102     protected void updateAjaxAttributes(final AjaxRequestAttributes attributes) {
103     }
104 
105     @Override
106     protected FinishButton newFinishButton(final String id, final IWizard wizard) {
107         return new FinishButton(id, wizard) {
108 
109             private static final long serialVersionUID = 864248301720764819L;
110 
111             @Override
112             public boolean isEnabled() {
113                 switch (mode) {
114                     case EDIT:
115                     case TEMPLATE:
116                         return true;
117                     case READONLY:
118                         return false;
119                     default:
120                         if (!completed) {
121                             final IWizardStep activeStep = getWizardModel().getActiveStep();
122                             completed = (activeStep != null)
123                                     && getWizardModel().isLastStep(activeStep)
124                                     && super.isEnabled();
125                         }
126                         return completed;
127                 }
128             }
129 
130             @Override
131             public boolean isVisible() {
132                 switch (mode) {
133                     case READONLY:
134                         return false;
135                     default:
136                         return true;
137                 }
138             }
139         };
140     }
141 }