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.internal.renderkit;
21  
22  import org.apache.myfaces.tobago.component.Attributes;
23  import org.apache.myfaces.tobago.internal.component.AbstractUICommand;
24  import org.apache.myfaces.tobago.internal.component.AbstractUIForm;
25  import org.apache.myfaces.tobago.internal.renderkit.renderer.TobagoClientBehaviorRenderer;
26  import org.apache.myfaces.tobago.internal.util.StringUtils;
27  import org.apache.myfaces.tobago.util.ComponentUtils;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  import javax.faces.component.UIComponent;
32  import javax.faces.context.FacesContext;
33  import java.lang.invoke.MethodHandles;
34  
35  /**
36   * @since 2.0.0
37   */
38  public class Command {
39  
40    private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
41  
42    /**
43     * The action is only needed if the action is not the HTML element itself.
44     */
45    private String action;
46    private Boolean transition;
47    private String target;
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 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      setExecute(execute);
67      setRender(render);
68      this.focus = focus;
69      this.confirmation = confirmation;
70      this.delay = delay;
71      this.collapse = collapse;
72      this.omit = omit;
73    }
74  
75    public Command(final FacesContext facesContext, final AbstractUICommand command) {
76      this(
77          null,
78          command.isTransition(),
79          command.getTarget(),
80          null,
81          null,
82          null,
83          ComponentUtils.getConfirmation(command),
84          null,
85          TobagoClientBehaviorRenderer.createCollapsible(facesContext, command),
86          command.isOmit());
87    }
88  
89    public Command(final FacesContext facesContext, final UIComponent facetComponent, final String focusId) {
90      final UIComponent component;
91      if (facetComponent instanceof AbstractUIForm && facetComponent.getChildCount() == 1) {
92        LOG.warn("Please don't use a form, but a command with immediate=true instead.");
93        component = facetComponent.getChildren().get(0);
94      } else {
95        component = facetComponent;
96      }
97      this.action = component.getClientId(facesContext);
98      // transition == true is the default
99      if (!ComponentUtils.getBooleanAttribute(component, Attributes.transition)) {
100       this.transition = Boolean.FALSE;
101     }
102     final String targetAttribute = ComponentUtils.getStringAttribute(component, Attributes.target);
103     if (targetAttribute != null) {
104       this.target = targetAttribute;
105     }
106     if (focusId != null) {
107       this.focus = focusId;
108     }
109 
110     final int delayAttribute = ComponentUtils.getIntAttribute(component, Attributes.delay);
111     if (delayAttribute > 0) {
112       this.delay = delayAttribute;
113     }
114     // omit == false is the default
115     if (ComponentUtils.getBooleanAttribute(component, Attributes.omit)) {
116       this.omit = Boolean.TRUE;
117     }
118   }
119 
120   public String getAction() {
121     return action;
122   }
123 
124   public void setAction(final String action) {
125     this.action = action;
126   }
127 
128   public Boolean getTransition() {
129     return transition;
130   }
131 
132   public void setTransition(final Boolean transition) {
133     this.transition = transition;
134   }
135 
136   public String getTarget() {
137     return target;
138   }
139 
140   public void setTarget(final String target) {
141     this.target = target;
142   }
143 
144   public String getExecute() {
145     return execute;
146   }
147 
148   public void setExecute(final String execute) {
149     if (StringUtils.isNotBlank(execute)) {
150       this.execute = execute;
151     }
152   }
153 
154   public String getRender() {
155     return render;
156   }
157 
158   public void setRender(final String render) {
159     if (StringUtils.isNotBlank(render)) {
160       this.render = render;
161     }
162   }
163 
164   public String getFocus() {
165     return focus;
166   }
167 
168   public void setFocus(final String focus) {
169     this.focus = focus;
170   }
171 
172   public String getConfirmation() {
173     return confirmation;
174   }
175 
176   public void setConfirmation(final String confirmation) {
177     this.confirmation = confirmation;
178   }
179 
180   public Integer getDelay() {
181     return delay;
182   }
183 
184   public void setDelay(final Integer delay) {
185     this.delay = delay;
186   }
187 
188   public Collapse getCollapse() {
189     return collapse;
190   }
191 
192   public void setCollapse(final Collapse collapse) {
193     this.collapse = collapse;
194   }
195 
196   public Boolean getOmit() {
197     return omit;
198   }
199 
200   public void setOmit(final Boolean omit) {
201     this.omit = omit;
202   }
203 
204   public void merge(final Command c) {
205 
206     //XXX TBD: check if this is okay.
207     // we need at least this for "execute" and "render" in the moment.
208 
209     if (action == null) {
210       action = c.action;
211     }
212     if (transition == null) {
213       transition = c.transition;
214     }
215     if (target == null) {
216       target = c.target;
217     }
218     if (execute != null) {
219       if (c.execute != null) {
220         execute += " " + c.execute;
221       }
222     } else {
223       execute = c.execute;
224     }
225     if (render != null) {
226       if (c.render != null) {
227         render += " " + c.render;
228       }
229     } else {
230       render = c.render;
231     }
232     if (focus == null) {
233       focus = c.focus;
234     }
235     if (confirmation == null) {
236       confirmation = c.confirmation;
237     }
238     if (delay == null) {
239       delay = c.delay;
240     }
241     if (collapse == null) {
242       collapse = c.collapse;
243     }
244     if (omit == null) {
245       omit = c.omit;
246     }
247   }
248 }