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.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   * Map of commands to be send to the user agent.
31   * It contains the command which shall be executed by click or other events.
32   *
33   * @since 2.0.0
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     * Creates an empty command map, which may hold different command triggered by different keys.
44     */
45    public CommandMap() {
46    }
47  
48    /**
49     * Creates a command map, which hold the given command triggered by "click".
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     * Merges these two maps.
85     * If one is null, the other one will be return. It may also return null.
86     * If both are not null, m1 will be filled with the data of m2.
87     * @param m1 map 1
88     * @param m2 map 2
89     * @return m1 or m2
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 }