1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.custom.ppr;
20
21 import org.apache.commons.lang.StringUtils;
22
23 import javax.faces.FacesException;
24 import javax.faces.component.UICommand;
25 import javax.faces.component.UIComponent;
26 import javax.faces.context.FacesContext;
27 import javax.faces.render.Renderer;
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.Iterator;
31 import java.util.List;
32
33
34
35
36
37
38
39
40
41
42
43
44
45 public class PPRSubmitRenderer extends Renderer
46 {
47 public void encodeBegin(FacesContext context, UIComponent component) throws IOException
48 {
49 super.encodeBegin(context, component);
50
51 final PPRSubmit pprSubmit = (PPRSubmit) component;
52 UICommand command = findCommandComponent(false, component);
53 if (!StringUtils.isEmpty(pprSubmit.getProcessComponentIds()) &&
54 (command == null || command.isImmediate())) {
55 throw new FacesException("PPRSubmit must embed a command component with immedate='false'.");
56 }
57 }
58
59 public void encodeEnd(FacesContext context, UIComponent component) throws IOException
60 {
61 UICommand command = findCommandComponent(true, component);
62
63 if (command != null) {
64 List panelGroups = new ArrayList(5);
65 String id = command.getId();
66 addPPRPanelGroupComponents(context.getViewRoot(), panelGroups);
67 for (int i = 0; i < panelGroups.size(); i++) {
68 PPRPanelGroup pprGroup = (PPRPanelGroup) panelGroups.get(i);
69
70 if (!PPRSupport.isPartialRequest(context)) {
71 PPRSupport.initPPR(context, pprGroup);
72 }
73
74 List triggers = pprGroup.parsePartialTriggers();
75 for (int j = 0; j < triggers.size(); j++) {
76 PartialTriggerParser.PartialTrigger trigger = (PartialTriggerParser.PartialTrigger) triggers.get(j);
77
78
79 if (trigger.getPartialTriggerId().equals(id)) {
80 PPRSupport.encodeJavaScriptTriggerOnly(context, command, pprGroup, trigger);
81 }
82 }
83 }
84 }
85 else {
86 throw new FacesException("PPRSubmitRenderer must be embedded in or embed a command component.");
87 }
88 }
89
90
91
92
93
94 private UICommand findCommandComponent(boolean checkParent, UIComponent component)
95 {
96 if (checkParent) {
97 UIComponent parent = component.getParent();
98 if (parent instanceof UICommand) {
99 return (UICommand) parent;
100 }
101 }
102
103 if (component.getChildCount() > 0) {
104 UIComponent child = (UIComponent) component.getChildren().get(0);
105 if (child instanceof UICommand) {
106 return (UICommand) child;
107 }
108 }
109
110 return null;
111 }
112
113 public void addPPRPanelGroupComponents(UIComponent component, List list)
114 {
115
116 for (Iterator it = component.getChildren().iterator(); it.hasNext();) {
117 UIComponent c = (UIComponent) it.next();
118 if (c instanceof PPRPanelGroup) {
119 list.add(c);
120 }
121 if (c.getChildCount() > 0) {
122 addPPRPanelGroupComponents(c, list);
123 }
124 }
125 }
126 }