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.StringUtils;
27  import org.apache.myfaces.tobago.util.ComponentUtils;
28  
29  import javax.faces.component.UIComponent;
30  import javax.faces.component.ValueHolder;
31  import javax.faces.context.FacesContext;
32  
33  import static org.apache.myfaces.tobago.internal.util.Deprecation.LOG;
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 partially;
47    private String execute;
48    private String render;
49    private String focus;
50    private String confirmation;
51    private Integer delay;
52    private Collapse collapse;
53    private Boolean omit;
54  
55    public Command() {
56    }
57  
58    public Command(
59        final String action, final Boolean transition, final String target, final String execute,
60        final String render, final String focus, final String confirmation, final Integer delay,
61        final Collapse collapse, final Boolean omit) {
62      this.action = action;
63      this.transition = transition;
64      this.target = target;
65      setExecute(execute);
66      setRender(render);
67      this.focus = focus;
68      this.confirmation = confirmation;
69      this.delay = delay;
70      this.collapse = collapse;
71      this.omit = omit;
72    }
73  
74    public Command(final FacesContext facesContext, final AbstractUICommand command) {
75      this(
76          null,
77          command.isTransition(),
78          command.getTarget(),
79          null,
80          null,
81          null,
82          getConfirmation(command),
83          null,
84          AjaxClientBehaviorRenderer.createCollapsible(facesContext, command),
85          command.isOmit());
86    }
87  
88    public Command(final FacesContext facesContext, UIComponent facetComponent, final String focusId) {
89      if (facetComponent instanceof UIForm && facetComponent.getChildCount() == 1) {
90        LOG.warn("Please don't use a form, but a command with immediate=true instead.");
91        facetComponent = facetComponent.getChildren().get(0);
92      }
93      this.action = facetComponent.getClientId(facesContext);
94      // transition == true is the default
95      if (!ComponentUtils.getBooleanAttribute(facetComponent, Attributes.transition)) {
96        this.transition = Boolean.FALSE;
97      }
98      final String target = ComponentUtils.getStringAttribute(facetComponent, Attributes.target);
99      if (target != null) {
100       this.target = target;
101     }
102     if (focusId != null) {
103       this.focus = focusId;
104     }
105 
106     final int delay = ComponentUtils.getIntAttribute(facetComponent, Attributes.delay);
107     if (delay > 0) {
108       this.delay = delay;
109     }
110     // omit == false is the default
111     if (ComponentUtils.getBooleanAttribute(facetComponent, Attributes.omit)) {
112       this.omit = Boolean.TRUE;
113     }
114   }
115 
116   private static String getConfirmation(final AbstractUICommand command) {
117     final String confirmation = command.getConfirmation();
118     if (confirmation != null) {
119       return confirmation;
120     }
121     final UIComponent facet = ComponentUtils.getFacet(command, Facets.confirmation);
122     if (facet instanceof ValueHolder) {
123       final ValueHolder valueHolder = (ValueHolder) facet;
124       return "" + valueHolder.getValue();
125     } else if (facet != null) {
126       LOG.warn("The content of a confirmation facet must be a ValueHolder. Use e. g. <tc:out>.");
127     }
128     return 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   /**
156    * @deprecated use getExecute() getRender() instead
157    */
158   @Deprecated
159   public String getPartially() {
160     return partially;
161   }
162 
163   /**
164    * @deprecated use setExecute() setRender() instead
165    */
166   @Deprecated
167   public void setPartially(final String partially) {
168     if (StringUtils.isNotBlank(partially)) {
169       this.partially = partially;
170     }
171   }
172 
173   public String getExecute() {
174     return execute;
175   }
176 
177   public void setExecute(String execute) {
178     if (StringUtils.isNotBlank(execute)) {
179       this.execute = execute;
180     }
181   }
182 
183   public String getRender() {
184     return render;
185   }
186 
187   public void setRender(String render) {
188     if (StringUtils.isNotBlank(render)) {
189       this.render = render;
190     }
191   }
192 
193   public String getFocus() {
194     return focus;
195   }
196 
197   public void setFocus(final String focus) {
198     this.focus = focus;
199   }
200 
201   public String getConfirmation() {
202     return confirmation;
203   }
204 
205   public void setConfirmation(final String confirmation) {
206     this.confirmation = confirmation;
207   }
208 
209   public Integer getDelay() {
210     return delay;
211   }
212 
213   public void setDelay(final Integer delay) {
214     this.delay = delay;
215   }
216 
217   public Collapse getCollapse() {
218     return collapse;
219   }
220 
221   public void setCollapse(Collapse collapse) {
222     this.collapse = collapse;
223   }
224 
225   public Boolean getOmit() {
226     return omit;
227   }
228 
229   public void setOmit(final Boolean omit) {
230     this.omit = omit;
231   }
232 }