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  package org.apache.myfaces.shared.util.el;
20  
21  import java.util.Collection;
22  import java.util.Collections;
23  import java.util.HashSet;
24  import java.util.Iterator;
25  import java.util.Map;
26  import java.util.Set;
27  
28  /**
29   * You can use this class to trigger an action when a boolean is set to true.
30   *
31   * Example : in JSF pages, for dataTable, to remove elements :
32   * Backing bean (#{inboxFace}).
33   * public ActionsMap getRemoveEmailUnid(){
34   *         return new ActionsMap(){
35   *            public void performAction(String unid) {
36   *                InboxMailDAO<TInboxMail> dao = getInboxMailDAO();
37   *                TInboxMail email = dao.getByPrimaryKey( unid );
38   *                dao.remove( email );
39   *            }
40   *        };
41   *    }
42   * JSF page :
43   * &lt;h:selectBooleanCheckbox value="#{inboxFace.removeEmailUnid[email.unid]}"/&gt;
44   */
45  public abstract class ActionsMap implements Map
46  {
47  
48      private Set keys;
49  
50      public ActionsMap()
51      {
52          // NoOp
53      }
54  
55      public ActionsMap(Set keys)
56      {
57          this.keys = keys;
58      }
59  
60      /**
61       * This method should fire the command.
62       */
63      public abstract void performAction(String command);
64  
65      public int size()
66      {
67          return keys.size();
68      }
69  
70      public boolean isEmpty()
71      {
72          return keys.isEmpty();
73      }
74  
75      public boolean containsKey(Object key)
76      {
77          return keys.contains( key );
78      }
79  
80      public boolean containsValue(Object value)
81      {
82          if( ! (value instanceof Boolean) )
83          {
84              return false;
85          }
86          return ((Boolean)value).booleanValue();
87      }
88  
89      public Object get( Object key)
90      {
91          return Boolean.FALSE;
92      }
93  
94      public Boolean put(String key, Boolean value)
95      {
96          if( value!=null && value.booleanValue() )
97          {
98              performAction(key);
99          }
100         return Boolean.FALSE;
101     }
102 
103     public Object remove(Object key)
104     {
105         if( keys.remove( key ) )
106         {
107             return Boolean.FALSE;
108         }
109         return null;
110     }
111 
112     public void putAll(Map map)
113     {
114         Iterator it = map.entrySet().iterator();
115 
116         while (it.hasNext())
117         {
118             Entry entry = (Entry) it.next();
119             Object obj = entry.getValue();
120             if( (obj instanceof Boolean) && ((Boolean) obj).booleanValue() )
121             {
122                 performAction((String) entry.getKey());
123             }
124         }
125     }
126 
127     public void clear()
128     {
129         keys.clear();
130     }
131 
132     public Set keySet()
133     {
134         return keys;
135     }
136 
137     public Collection values()
138     {
139         return Collections.nCopies(keys.size(), Boolean.FALSE);
140     }
141 
142     public Set entrySet()
143     {
144         Set set = new HashSet( keys.size() );
145 
146         Iterator it = keys.iterator();
147 
148         while (it.hasNext())
149         {
150             String command = (String) it.next();
151             set.add( new CommandEntry(command) );
152         }
153 
154         return set;
155     }
156 
157     private class CommandEntry implements Entry
158     {
159 
160         private final String command;
161         private boolean commandPerformed = false;
162 
163         public CommandEntry(String command)
164         {
165             this.command = command;
166         }
167 
168         public Object getKey()
169         {
170             return command;
171         }
172 
173         public Object getValue()
174         {
175             return Boolean.valueOf(commandPerformed);
176         }
177 
178         public Object setValue(Object performCommand)
179         {
180             if( (performCommand instanceof Boolean) && ((Boolean)performCommand).booleanValue() )
181             {
182                 performAction( command );
183                 commandPerformed = true;
184             }
185             return Boolean.valueOf(commandPerformed);
186         }
187     }
188 }
189