1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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 }