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.commons.lang3.SerializationUtils;
23  import org.apache.wicket.PageReference;
24  import org.apache.wicket.event.IEventSink;
25  
26  public abstract class AbstractModalPanelBuilder<T extends Serializable> implements ModalPanelBuilder<T> {
27  
28      private static final long serialVersionUID = 5241745929825564456L;
29  
30      protected final PageReference pageRef;
31  
32      private final T defaultItem;
33  
34      private T item;
35  
36      protected IEventSink eventSink;
37  
38      /**
39       * Construct.
40       *
41       * @param defaultItem default item.
42       * @param pageRef Caller page reference.
43       */
44      public AbstractModalPanelBuilder(final T defaultItem, final PageReference pageRef) {
45          this.defaultItem = defaultItem;
46          this.pageRef = pageRef;
47      }
48  
49      protected void onCancelInternal(final T modelObject) {
50      }
51  
52      protected Serializable onApplyInternal(final T modelObject) {
53          // do nothing
54          return null;
55      }
56  
57      protected T getOriginalItem() {
58          return item;
59      }
60  
61      @Override
62      public T getDefaultItem() {
63          return defaultItem;
64      }
65  
66      protected T newModelObject() {
67          if (item == null) {
68              // keep the original item: the which one before the changes performed during wizard browsing
69              item = SerializationUtils.clone(defaultItem);
70          }
71  
72          // instantiate a new model object and return it
73          return SerializationUtils.clone(item);
74      }
75  
76      @Override
77      public AbstractModalPanelBuilder<T> setItem(final T item) {
78          this.item = item;
79          return this;
80      }
81  
82      @Override
83      public PageReference getPageReference() {
84          return pageRef;
85      }
86  
87      @Override
88      public ModalPanelBuilder<T> setEventSink(final IEventSink eventSink) {
89          this.eventSink = eventSink;
90          return this;
91      }
92  
93      @Override
94      public IEventSink getEventSink() {
95          return eventSink;
96      }
97  }