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