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