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  
20  package org.apache.myfaces.tobago.internal.component;
21  
22  import org.apache.myfaces.tobago.layout.LayoutComponent;
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  import javax.faces.context.FacesContext;
27  import javax.faces.event.AbortProcessingException;
28  import javax.faces.event.FacesEvent;
29  import java.io.IOException;
30  import java.util.Map;
31  
32  public abstract class AbstractUIMediator extends AbstractUIPanelBase implements LayoutComponent {
33  
34    private static final Logger LOG = LoggerFactory.getLogger(AbstractUIMediator.class);
35  
36    private String var;
37  
38    @Override
39    public void encodeBegin(final FacesContext facesContext) throws IOException {
40      super.encodeBegin(facesContext);
41      if (var != null) {
42        facesContext.getExternalContext().getRequestMap().put(var, this);
43      }
44    }
45  
46    @Override
47    public void encodeEnd(final FacesContext facesContext) throws IOException {
48      if (var != null) {
49        facesContext.getExternalContext().getRequestMap().remove(var);
50      }
51      super.encodeEnd(facesContext);
52    }
53  
54    @Override
55    public void broadcast(final FacesEvent event) throws AbortProcessingException {
56      final Map requestMap = FacesContext.getCurrentInstance().getExternalContext().getRequestMap();
57      requestMap.put(var, this);
58      try {
59        super.broadcast(event);
60      } finally {
61        requestMap.remove(var);
62      }
63    }
64  
65    @Override
66    public Object saveState(final FacesContext context) {
67      final Object[] state = new Object[2];
68      state[0] = super.saveState(context);
69      state[1] = var;
70      return state;
71    }
72  
73    @Override
74    public void restoreState(final FacesContext context, final Object state) {
75      final Object[] values = (Object[]) state;
76      super.restoreState(context, values[0]);
77      var = (String) values[1];
78    }
79  
80    public String getVar() {
81      return var;
82    }
83  
84    public void setVar(final String var) {
85      this.var = var;
86    }
87  }