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.renderkit.html;
21  
22  import java.util.Collections;
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  /**
27   * Map of commands to be send to the user agent.
28   * It contains the command which shall be executed by click or other events.
29   *
30   * @since 2.0.0
31   */
32  public class CommandMap {
33  
34    private Command click;
35    private Map<String, Command> other;
36  
37    /**
38     * Creates an empty command map, which may hold different command triggered by different keys.
39     */
40    public CommandMap() {
41    }
42  
43    /**
44     * Creates a command map, which hold the given command triggered by "click".
45     */
46    public CommandMap(Command click) {
47      this.click = click;
48    }
49  
50    public void setClick(Command click) {
51      this.click = click;
52    }
53  
54    public Command getClick() {
55      return click;
56    }
57  
58    public void addCommand(String name, Command command) {
59      if (name.equals("click")) {
60        setClick(command);
61      } else {
62        if (other == null) {
63          other = new HashMap<String, Command>();
64        }
65  
66        assert name.matches("[a-z]+");
67  
68        other.put(name, command);
69      }
70    }
71  
72    public Map<String, Command> getOther() {
73      if (other != null) {
74        return Collections.unmodifiableMap(other);
75      } else {
76        return null;
77      }
78    }
79  }