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.renderkit;
21
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.myfaces.tobago.internal.util.FacesContextUtils;
24 import org.apache.myfaces.tobago.layout.Measure;
25 import org.apache.myfaces.tobago.model.PageState;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.apache.myfaces.tobago.internal.component.AbstractUIPage;
29 import org.apache.myfaces.tobago.layout.Box;
30 import org.apache.myfaces.tobago.util.ComponentUtils;
31
32 import javax.faces.component.UIComponent;
33 import javax.faces.context.FacesContext;
34 import java.util.StringTokenizer;
35
36 public class PageRendererBase extends LayoutComponentRendererBase {
37
38 private static final Logger LOG = LoggerFactory.getLogger(PageRendererBase.class);
39
40 public void decode(FacesContext facesContext, UIComponent component) {
41 if (component instanceof AbstractUIPage) {
42 AbstractUIPage page = (AbstractUIPage) component;
43
44 decodeActionPosition(facesContext, page);
45 decodePageState(facesContext, page);
46 }
47 }
48
49 private void decodeActionPosition(FacesContext facesContext, AbstractUIPage page) {
50 String actionIdName = page.getClientId(facesContext) + ComponentUtils.SUB_SEPARATOR + "form-action";
51 String newActionId = (String) facesContext.getExternalContext().getRequestParameterMap().get(actionIdName);
52 if (LOG.isDebugEnabled()) {
53 LOG.debug("action = " + newActionId);
54 }
55 page.setActionId(newActionId);
56 FacesContextUtils.setActionId(facesContext, newActionId);
57 try {
58 String actionPositionName = page.getClientId(facesContext) + ComponentUtils.SUB_SEPARATOR + "action-position";
59 String actionPositionString = (String)
60 facesContext.getExternalContext().getRequestParameterMap().get(actionPositionName);
61 if (LOG.isDebugEnabled()) {
62 LOG.debug("actionPosition='" + actionPositionString + "'");
63 }
64 if (StringUtils.isNotEmpty(actionPositionString)) {
65 Box actionPosition = new Box(actionPositionString);
66 page.setActionPosition(actionPosition);
67 } else {
68 page.setActionPosition(null);
69 }
70 } catch (Exception e) {
71 LOG.warn("Can't analyse parameter for action-position", e);
72 }
73 }
74
75 @SuppressWarnings("unchecked")
76 private void decodePageState(FacesContext facesContext, AbstractUIPage page) {
77 String name;
78 String value = null;
79 try {
80 name = page.getClientId(facesContext) + ComponentUtils.SUB_SEPARATOR + "form-clientDimension";
81 value = (String) facesContext.getExternalContext().getRequestParameterMap().get(name);
82 if (value != null) {
83 StringTokenizer tokenizer = new StringTokenizer(value, ";");
84 Measure width = Measure.parse(tokenizer.nextToken());
85 Measure height = Measure.parse(tokenizer.nextToken());
86
87 PageState pageState = page.getPageState(facesContext);
88 if (pageState != null) {
89 pageState.setClientWidth(width.getPixel());
90 pageState.setClientHeight(height.getPixel());
91 }
92 facesContext.getExternalContext().getRequestMap().put("tobago-page-clientDimension-width", width);
93 facesContext.getExternalContext().getRequestMap().put("tobago-page-clientDimension-height", height);
94 }
95 } catch (Exception e) {
96 LOG.error("Error in decoding state: value='" + value + "'", e);
97 }
98 }
99 }