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