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