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.custom.effect;
20  
21  import javax.faces.FacesException;
22  import javax.faces.application.ResourceDependencies;
23  import javax.faces.application.ResourceDependency;
24  import javax.faces.component.UIComponent;
25  import javax.faces.component.behavior.ClientBehavior;
26  import javax.faces.component.behavior.ClientBehaviorContext;
27  import javax.faces.render.ClientBehaviorRenderer;
28  
29  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFClientBehaviorRenderer;
30  
31  @JSFClientBehaviorRenderer(
32          renderKitId = "HTML_BASIC",
33          type = "org.apache.myfaces.custom.effect.EffectSquishBehavior")
34  @ResourceDependencies({
35      @ResourceDependency(library="oam.custom.prototype", name="prototype.js"),
36      @ResourceDependency(library="oam.custom.prototype", name="effects.js")
37  })
38  public class EffectSquishClientBehaviorRenderer extends ClientBehaviorRenderer
39  {
40  
41      @Override
42      public String getScript(ClientBehaviorContext behaviorContext,
43              ClientBehavior behavior)
44      {
45          AbstractEffectSquishClientBehavior effectBehavior = (AbstractEffectSquishClientBehavior) behavior;
46          
47          UIComponent target = ( effectBehavior.getForId() != null ) ? 
48                  behaviorContext.getComponent().findComponent(effectBehavior.getForId()) : 
49                      behaviorContext.getComponent();
50          if (target == null)
51          {
52              throw new FacesException("Cannot find component set with forId: "+effectBehavior.getForId());
53          }
54          String clientId = target.getClientId();
55          
56          StringBuilder sb = new StringBuilder();
57          
58          sb.append("Effect.Squish('");
59          sb.append(clientId);
60          sb.append("')");
61          
62          if (effectBehavior.getAppendJs() != null && effectBehavior.getAppendJs().length() > 0)
63          {
64              sb.append(';');
65              sb.append(effectBehavior.getAppendJs());
66          }
67  
68          return sb.toString();
69      }
70  }