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  
20  package org.apache.myfaces.tobago.renderkit.html;
21  
22  import org.apache.myfaces.tobago.component.Attributes;
23  import org.apache.myfaces.tobago.component.Facets;
24  import org.apache.myfaces.tobago.component.UIForm;
25  import org.apache.myfaces.tobago.internal.component.AbstractUICommand;
26  import org.apache.myfaces.tobago.internal.util.Deprecation;
27  import org.apache.myfaces.tobago.internal.util.StringUtils;
28  import org.apache.myfaces.tobago.renderkit.util.RenderUtils;
29  import org.apache.myfaces.tobago.util.ComponentUtils;
30  
31  import javax.faces.component.UIComponent;
32  import javax.faces.component.ValueHolder;
33  import javax.faces.context.FacesContext;
34  
35  /**
36   * @since 2.0.0
37   */
38  public class Command {
39  
40    /**
41     * The action is only needed if the action is not the HTML element itself.
42     */
43    private String action;
44    private Boolean transition;
45    private String target;
46    private String url;
47    private String partially;
48    private String execute;
49    private String render;
50    private String focus;
51    private String confirmation;
52    private Integer delay;
53    private Collapse collapse;
54    private Boolean omit;
55  
56    public Command() {
57    }
58  
59    public Command(
60        final String action, final Boolean transition, final String target, final String url, final String execute,
61        final String render, final String focus, final String confirmation, final Integer delay,
62        final Collapse collapse, final Boolean omit) {
63      this.action = action;
64      this.transition = transition;
65      this.target = target;
66      this.url = url;
67      setExecute(execute);
68      setRender(render);
69      this.focus = focus;
70      this.confirmation = confirmation;
71      this.delay = delay;
72      this.collapse = collapse;
73      this.omit = omit;
74    }
75  
76    public Command(final FacesContext facesContext, final AbstractUICommand command) {
77      this(
78          null,
79          command.isTransition(),
80          command.getTarget(),
81          RenderUtils.generateUrl(facesContext, command),
82          null,
83          null,
84          null,
85          getConfirmation(command),
86          null,
87          AjaxClientBehaviorRenderer.createCollapsible(facesContext, command),
88          command.isOmit());
89    }
90  
91    public Command(final FacesContext facesContext, UIComponent facetComponent, final String focusId) {
92      if (facetComponent instanceof UIForm && facetComponent.getChildCount() == 1) {
93        Deprecation.LOG.warn("Please don't use a form, but a command with immediate=true instead.");
94        facetComponent = facetComponent.getChildren().get(0);
95      }
96      this.action = facetComponent.getClientId(facesContext);
97      // transition == true is the default
98      if (!ComponentUtils.getBooleanAttribute(facetComponent, Attributes.transition)) {
99        this.transition = Boolean.FALSE;
100     }
101     final String target = ComponentUtils.getStringAttribute(facetComponent, Attributes.target);
102     if (target != null) {
103       this.target = target;
104     }
105     // TODO: TOBAGO-1572
106 //    if (facetComponent instanceof AbstractUICommand
107 //        && ((AbstractUICommand) facetComponent).getRenderedPartially().length > 0) {
108 //      setPartially(ComponentUtils.evaluateClientIds(
109 //          facesContext, facetComponent, ((UICommand) facetComponent).getRenderedPartially()));
110 //    } else {
111       if (focusId != null) {
112         this.focus = focusId;
113       }
114 //    }
115 
116     final int delay = ComponentUtils.getIntAttribute(facetComponent, Attributes.delay);
117     if (delay > 0) {
118       this.delay = delay;
119     }
120     // omit == false is the default
121     if (ComponentUtils.getBooleanAttribute(facetComponent, Attributes.omit)) {
122       this.omit = Boolean.TRUE;
123     }
124   }
125 
126   private static String getConfirmation(final AbstractUICommand command) {
127     final ValueHolder facet = (ValueHolder) ComponentUtils.getFacet(command, Facets.confirmation);
128     return facet != null ? "" + facet.getValue() : null;
129   }
130 
131   public String getAction() {
132     return action;
133   }
134 
135   public void setAction(final String action) {
136     this.action = action;
137   }
138 
139   public Boolean getTransition() {
140     return transition;
141   }
142 
143   public void setTransition(final Boolean transition) {
144     this.transition = transition;
145   }
146 
147   public String getTarget() {
148     return target;
149   }
150 
151   public void setTarget(final String target) {
152     this.target = target;
153   }
154 
155   public String getUrl() {
156     return url;
157   }
158 
159   public void setUrl(final String url) {
160     this.url = url;
161   }
162 
163   /**
164    * @deprecated use getExecute() getRender() instead
165    */
166   @Deprecated
167   public String getPartially() {
168     return partially;
169   }
170 
171   /**
172    * @deprecated use setExecute() setRender() instead
173    */
174   @Deprecated
175   public void setPartially(final String partially) {
176     if (StringUtils.isNotBlank(partially)) {
177       this.partially = partially;
178     }
179   }
180 
181   public String getExecute() {
182     return execute;
183   }
184 
185   public void setExecute(String execute) {
186     if (StringUtils.isNotBlank(execute)) {
187       this.execute = execute;
188     }
189   }
190 
191   public String getRender() {
192     return render;
193   }
194 
195   public void setRender(String render) {
196     if (StringUtils.isNotBlank(render)) {
197       this.render = render;
198     }
199   }
200 
201   public String getFocus() {
202     return focus;
203   }
204 
205   public void setFocus(final String focus) {
206     this.focus = focus;
207   }
208 
209   public String getConfirmation() {
210     return confirmation;
211   }
212 
213   public void setConfirmation(final String confirmation) {
214     this.confirmation = confirmation;
215   }
216 
217   public Integer getDelay() {
218     return delay;
219   }
220 
221   public void setDelay(final Integer delay) {
222     this.delay = delay;
223   }
224 
225   public Collapse getCollapse() {
226     return collapse;
227   }
228 
229   public void setCollapse(Collapse collapse) {
230     this.collapse = collapse;
231   }
232 
233   public Boolean getOmit() {
234     return omit;
235   }
236 
237   public void setOmit(final Boolean omit) {
238     this.omit = omit;
239   }
240 }