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  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFProperty;
31  
32  @JSFClientBehaviorRenderer(
33          renderKitId = "HTML_BASIC",
34          type = "org.apache.myfaces.custom.effect.EffectBlindDownBehavior")
35  @ResourceDependencies({
36      @ResourceDependency(library="oam.custom.prototype", name="prototype.js"),
37      @ResourceDependency(library="oam.custom.prototype", name="effects.js")
38  })
39  public class EffectBlindDownClientBehaviorRenderer extends ClientBehaviorRenderer
40  {
41      /**
42       * Effect.Appear('id_of_element', { duration: 3.0 });
43       */
44      @Override
45      public String getScript(ClientBehaviorContext behaviorContext,
46              ClientBehavior behavior)
47      {
48          AbstractEffectBlindDownClientBehavior effectBehavior = (AbstractEffectBlindDownClientBehavior) behavior;
49          
50          UIComponent target = ( effectBehavior.getForId() != null ) ? 
51                  behaviorContext.getComponent().findComponent(effectBehavior.getForId()) : 
52                      behaviorContext.getComponent();
53          if (target == null)
54          {
55              throw new FacesException("Cannot find component set with forId: "+effectBehavior.getForId());
56          }
57          String clientId = target.getClientId();
58          
59          StringBuilder sb = new StringBuilder();
60          
61          sb.append("new Effect.BlindDown('");
62          sb.append(clientId);
63          sb.append("'");
64          if (effectBehavior.getScaleX() != null || effectBehavior.getScaleY() != null ||
65              effectBehavior.getScaleMode() != null || effectBehavior.getScaleFromCenter() != null || effectBehavior.getScaleContent() != null ||
66              effectBehavior.getScaleFrom() != null || effectBehavior.getScaleTo() != null || effectBehavior.getDuration() != null)
67          {
68              sb.append(",{");
69              boolean addComma = false;
70              addComma = EffectUtils.addProperty(sb, "scaleX", effectBehavior.getScaleX(), addComma);
71              addComma = EffectUtils.addProperty(sb, "scaleY", effectBehavior.getScaleY(), addComma);
72              addComma = EffectUtils.addStringProperty(sb, "scaleMode", effectBehavior.getScaleMode(), addComma);
73              addComma = EffectUtils.addProperty(sb, "scaleFromCenter", effectBehavior.getScaleFromCenter(), addComma);
74              addComma = EffectUtils.addProperty(sb, "scaleContent", effectBehavior.getScaleContent(), addComma);
75              addComma = EffectUtils.addProperty(sb, "scaleFrom", effectBehavior.getScaleFrom(), addComma);
76              addComma = EffectUtils.addProperty(sb, "scaleTo", effectBehavior.getScaleTo(), addComma);
77              addComma = EffectUtils.addProperty(sb, "duration", effectBehavior.getDuration(), addComma);
78              sb.append('}');
79          }
80          sb.append(')');
81          
82          if (effectBehavior.getAppendJs() != null && effectBehavior.getAppendJs().length() > 0)
83          {
84              sb.append(';');
85              sb.append(effectBehavior.getAppendJs());
86          }
87  
88          return sb.toString();
89      }
90  }