2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  /*
19   * $Id: AssignValueChangeListenersCommand.java 464373 2006-10-16 04:21:54Z rahul $
20   */
21  package org.apache.shale.clay.component.chain;
22  
23  import java.util.Iterator;
24  
25  import javax.faces.component.EditableValueHolder;
26  import javax.faces.component.UIComponent;
27  
28  import org.apache.commons.chain.Catalog;
29  import org.apache.commons.chain.Command;
30  import org.apache.commons.chain.Context;
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  import org.apache.shale.clay.config.Globals;
34  import org.apache.shale.clay.config.beans.ComponentBean;
35  import org.apache.shale.clay.config.beans.ValueChangeListenerBean;
36  
37  /***
38   * <p>
39   * For each {@link org.apache.shale.clay.config.beans.ValueChangeListenerBean}
40   * in the <code>valueChangeListeners</code> collection, the
41   * {@link org.apache.shale.clay.component.chain.CreateValueChangeListenerCommand}
42   * command will be invoked.
43   * </p>
44   */
45  public class AssignValueChangeListenersCommand extends AbstractCommand {
46  
47      /***
48       * <p>
49       * Common Logging utility.
50       * </p>
51       */
52      private static Log log;
53      static {
54          log = LogFactory.getLog(AssignValueChangeListenersCommand.class);
55      }
56  
57      /***
58       * <p>
59       * Uses the <code>Globals.ADD_VALUE_CHANGE_LISTENER_COMMAND_NAME</code>
60       * command to create a <code>ValueChangeListener</code>.</p>
61       *
62       * @param context common chains
63       * @return <code>true</code> if the chain is complete
64       * @exception Exception propagated up to the top of the chain
65       */
66      public boolean execute(Context context) throws Exception {
67  
68          boolean isFinal = false;
69  
70          ClayContext clayContext = (ClayContext) context;
71          if (clayContext == null) {
72              throw new NullPointerException(getMessages()
73                      .getMessage("clay.null.clayContext"));
74          }
75  
76          UIComponent child = (UIComponent) clayContext.getChild();
77          if (child == null) {
78              throw new NullPointerException(getMessages()
79                      .getMessage("clay.null.childComponent"));
80          }
81  
82          ComponentBean displayElement = clayContext.getDisplayElement();
83          if (displayElement == null) {
84              throw new NullPointerException(getMessages()
85                      .getMessage("clay.null.componentBean"));
86          }
87  
88          if (displayElement.getValueChangeListeners().size() > 0) {
89              if (child instanceof EditableValueHolder) {
90  
91                  Iterator vi = displayElement.getValueChangeListenerIterator();
92                  while (vi.hasNext()) {
93                      ValueChangeListenerBean valueChangeListener = (ValueChangeListenerBean) vi
94                              .next();
95  
96                      ClayContext subContext = (ClayContext) clayContext.clone();
97                      subContext.setDisplayElement(valueChangeListener);
98                      subContext.setParent(child);
99  
100                     Catalog catalog = getCatalog();
101                     Command command = catalog
102                             .getCommand(Globals.ADD_VALUE_CHANGE_LISTENER_COMMAND_NAME);
103                     command.execute(subContext);
104 
105                 }
106             } else {
107                 log.error(getMessages().getMessage(
108                         "assign.valueChangeListener.error",
109                         new Object[] { displayElement }));
110             }
111         }
112 
113         return isFinal;
114     }
115 
116 }