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.event;
21  
22  import org.apache.myfaces.tobago.internal.component.AbstractUICollapsiblePanel;
23  import org.apache.myfaces.tobago.util.ComponentUtils;
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  import javax.faces.component.StateHolder;
28  import javax.faces.component.UIComponent;
29  import javax.faces.component.UIViewRoot;
30  import javax.faces.context.FacesContext;
31  import javax.faces.event.AbortProcessingException;
32  import javax.faces.event.ActionEvent;
33  import javax.faces.event.ActionListener;
34  import java.lang.invoke.MethodHandles;
35  
36  public class CollapsibleActionListener implements ActionListener, StateHolder {
37  
38    private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
39  
40    private String forId;
41  
42    private boolean transientFlag;
43  
44    public CollapsibleActionListener() {
45      // for state holder
46    }
47  
48    public CollapsibleActionListener(final String forId) {
49      this.forId = forId;
50    }
51  
52    @Override
53    public void processAction(final ActionEvent actionEvent) throws AbortProcessingException {
54      final FacesContext facesContext = FacesContext.getCurrentInstance();
55      final UIViewRoot viewRoot = facesContext.getViewRoot();
56      if (viewRoot != null) {
57        final String forClientId = ComponentUtils.evaluateClientId(facesContext, actionEvent.getComponent(), forId);
58  
59        final UIComponent component = viewRoot.findComponent(forClientId);
60        if (component instanceof AbstractUICollapsiblePanel) {
61          ((AbstractUICollapsiblePanel) component).processState();
62        } else {
63          LOG.error("Wrong component class for id: '{}'. Type is {} but expected type is {}",
64              forClientId, component.getClass().getName(), AbstractUICollapsiblePanel.class.getName());
65        }
66      }
67    }
68  
69    @Override
70    public Object saveState(final FacesContext context) {
71      return forId;
72    }
73  
74    @Override
75    public void restoreState(final FacesContext context, final Object state) {
76      this.forId = (String) state;
77    }
78  
79    @Override
80    public boolean isTransient() {
81      return transientFlag;
82    }
83  
84    @Override
85    public void setTransient(final boolean newTransientValue) {
86      this.transientFlag = newTransientValue;
87    }
88  }