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.internal.renderkit;
21
22 import org.apache.myfaces.tobago.component.ClientBehaviors;
23
24 import javax.faces.context.FacesContext;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.Map;
28
29
30
31
32
33
34
35 public class CommandMap {
36
37 private static final String KEY = CommandMap.class.getName() + ".KEY";
38
39 private Command click;
40 private Map<ClientBehaviors, Command> other;
41
42
43
44
45 public CommandMap() {
46 }
47
48
49
50
51 public CommandMap(final Command click) {
52 this.click = click;
53 }
54
55 public void setClick(final Command click) {
56 this.click = click;
57 }
58
59 public Command getClick() {
60 return click;
61 }
62
63 public void addCommand(final ClientBehaviors name, final Command command) {
64 if (name == ClientBehaviors.click) {
65 setClick(command);
66 } else {
67 if (other == null) {
68 other = new HashMap<>();
69 }
70
71 other.put(name, command);
72 }
73 }
74
75 public Map<ClientBehaviors, Command> getOther() {
76 if (other != null) {
77 return Collections.unmodifiableMap(other);
78 } else {
79 return null;
80 }
81 }
82
83
84
85
86
87
88
89
90
91 public static CommandMap merge(final CommandMap m1, final CommandMap m2) {
92 if (m1 == null) {
93 return m2;
94 } else {
95 if (m2 == null) {
96 return m1;
97 } else {
98 final Command c2 = m2.getClick();
99 if (c2 != null) {
100 final Command c1 = m1.getClick();
101 if (c1 == null) {
102 m1.setClick(c2);
103 } else {
104 c1.merge(c2);
105 }
106 } else {
107 if (m1.other != null && m2.getOther() != null) {
108 for (final Map.Entry<ClientBehaviors, Command> entry : m2.getOther().entrySet()) {
109 final ClientBehaviors key = entry.getKey();
110 final Command value = entry.getValue();
111 if (m1.other.containsKey(key)) {
112 final Command command = m1.other.get(key);
113 command.merge(value);
114 } else {
115 m1.addCommand(key, value);
116 }
117 }
118 }
119 }
120 return m1;
121 }
122 }
123 }
124
125 public static CommandMap restoreCommandMap(final FacesContext facesContext) {
126 return (CommandMap) facesContext.getAttributes().get(KEY);
127 }
128
129 public static void storeCommandMap(final FacesContext facesContext, final CommandMap map) {
130 facesContext.getAttributes().put(KEY, map);
131 }
132
133 public boolean isEmpty() {
134 return click == null && other == null;
135 }
136 }