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.example.demo;
21  
22  import org.apache.myfaces.tobago.util.AjaxUtils;
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  import javax.el.ELContext;
27  import javax.el.ValueExpression;
28  import javax.enterprise.context.RequestScoped;
29  import javax.faces.context.FacesContext;
30  import javax.faces.event.AjaxBehaviorEvent;
31  import javax.inject.Inject;
32  import javax.inject.Named;
33  import java.lang.invoke.MethodHandles;
34  import java.util.Date;
35  
36  @RequestScoped
37  @Named
38  public class PartialReloadController {
39  
40    private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
41  
42    private String navigateActionValue;
43  
44    @Inject
45    private NavigationState navigationState;
46  
47    public Date getCurrentDate() {
48      return new Date();
49    }
50  
51    public String reload() {
52      return logAndNavigate(null);
53    }
54  
55    public String error() {
56      throw new DemoException("Test Exception");
57    }
58  
59    public String waitAndReload3() {
60      return waitAndReload(3000);
61    }
62  
63    public String waitAndReload7() {
64      return waitAndReload(7000);
65    }
66  
67    private String waitAndReload(final long delay) {
68      LOG.info("I'm waiting now for {} ms.", delay);
69      synchronized (this) {
70        try {
71          wait(delay);
72        } catch (final InterruptedException e) {
73          //
74        }
75      }
76      LOG.info("My waiting for {} ms is done.", delay);
77      return logAndNavigate(null);
78    }
79  
80    public void navigateAction(final AjaxBehaviorEvent event) {
81      final FacesContext facesContext = FacesContext.getCurrentInstance();
82  
83      if (navigationState == null) {
84        final ELContext elContext = facesContext.getELContext();
85        final ValueExpression expression = facesContext.getApplication().getExpressionFactory()
86            .createValueExpression(elContext, "#{navigationState}", NavigationState.class);
87        navigationState = (NavigationState) expression.getValue(elContext);
88      }
89  
90      LOG.info("navigateActionValue = \"" + navigateActionValue + "\"");
91      if (navigateActionValue == null) {
92        logAndNavigate(null);
93      } else if ("left".equals(navigateActionValue)) {
94        AjaxUtils.addRenderIds("page:mainForm:left");
95        navigateActionValue = null;
96        logAndNavigate(null);
97      } else if ("right".equals(navigateActionValue)) {
98        AjaxUtils.addRenderIds("page:mainForm:right");
99        navigateActionValue = null;
100       logAndNavigate(null);
101     } else if ("both".equals(navigateActionValue)) {
102       AjaxUtils.addRenderIds("page:mainForm:left", "page:mainForm:right");
103       navigateActionValue = null;
104       logAndNavigate(null);
105     } else if ("parent".equals(navigateActionValue)) {
106       navigateActionValue = null;
107       AjaxUtils.addRenderIds("page:mainForm:parent");
108       logAndNavigate(null);
109     } else if ("prev".equals(navigateActionValue)) {
110       navigateActionValue = null;
111       AjaxUtils.navigate(facesContext, logAndNavigate(navigationState.gotoPrevious()));
112     } else if ("next".equals(navigateActionValue)) {
113       navigateActionValue = null;
114       AjaxUtils.navigate(facesContext, logAndNavigate(navigationState.gotoNext()));
115     }
116     logAndNavigate(null);
117   }
118 
119   private String logAndNavigate(final String navValue) {
120     LOG.info("Return navigate value: " + navValue + "");
121     return navValue;
122   }
123 
124 
125   public String getNavigateActionValue() {
126     return navigateActionValue;
127   }
128 
129   public void setNavigateActionValue(final String navigateActionValue) {
130     this.navigateActionValue = navigateActionValue;
131   }
132 }