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     * @deprecated Script will not work when CSP is activated
56     */
57    @Deprecated
58    private String script;
59  
60    public Command() {
61    }
62  
63    public Command(
64        final String action, final Boolean transition, final String target, final String url, final String[] partially,
65        final String focus, final String confirmation, final Integer delay, final Popup popup, final Boolean omit) {
66      this.action = action;
67      this.transition = transition;
68      this.target = target;
69      this.url = url;
70      this.partially = partially;
71      this.focus = focus;
72      this.confirmation = confirmation;
73      this.delay = delay;
74      this.popup = popup;
75      this.omit = omit;
76    }
77  
78    public Command(final FacesContext facesContext, final AbstractUICommand command) {
79      this(
80          null,
81          command.isTransition(),
82          command.getTarget(),
83          RenderUtils.generateUrl(facesContext, command),
84          ComponentUtils.evaluateClientIds(facesContext, command, command.getRenderedPartially()),
85          null,
86          getConfirmation(command),
87          null,
88          Popup.createPopup(command),
89          command.isOmit());
90      if (command.getOnclick() != null) {
91        script = command.getOnclick();
92      }
93    }
94  
95    public Command(final FacesContext facesContext, UIComponent facetComponent, final String focusId) {
96      if (facetComponent instanceof UIForm && facetComponent.getChildCount() == 1) {
97        Deprecation.LOG.warn("Please don't use a form, but a command with immediate=true instead.");
98        facetComponent = facetComponent.getChildren().get(0);
99      }
100     this.action = facetComponent.getClientId(facesContext);
101     // transition == true is the default
102     if (!ComponentUtils.getBooleanAttribute(facetComponent, Attributes.TRANSITION)) {
103       this.transition = Boolean.FALSE;
104     }
105     final String target = ComponentUtils.getStringAttribute(facetComponent, Attributes.TARGET);
106     if (target != null) {
107       this.target = target;
108     }
109     if (facetComponent instanceof AbstractUICommand
110         && ((AbstractUICommand) facetComponent).getRenderedPartially().length > 0) {
111       this.partially = ComponentUtils.evaluateClientIds(
112           facesContext, facetComponent, ((UICommand) facetComponent).getRenderedPartially());
113     } else {
114       String facetAction = (String) facetComponent.getAttributes().get(Attributes.ONCLICK);
115       if (facetAction != null) {
116         // Replace @autoId
117         facetAction = StringUtils.replace(facetAction, "@autoId", facetComponent.getClientId(facesContext));
118         // XXX this case is deprecated.
119         // not allowed with Content Security Policy (CSP)
120         this.script = facetAction;
121       }
122       if (focusId != null) {
123         this.focus = focusId;
124       }
125     }
126 
127     final int delay = ComponentUtils.getIntAttribute(facetComponent, Attributes.DELAY);
128     if (delay > 0) {
129       this.delay = delay;
130     }
131     // omit == false is the default
132     if (ComponentUtils.getBooleanAttribute(facetComponent, Attributes.OMIT)) {
133       this.omit = Boolean.TRUE;
134     }
135   }
136 
137   private static String getConfirmation(final AbstractUICommand command) {
138     final ValueHolder facet = (ValueHolder) command.getFacet(Facets.CONFIRMATION);
139     return facet != null ? "" + facet.getValue() : null;
140   }
141 
142   public String getAction() {
143     return action;
144   }
145 
146   public void setAction(final String action) {
147     this.action = action;
148   }
149 
150   public Boolean getTransition() {
151     return transition;
152   }
153 
154   public void setTransition(final Boolean transition) {
155     this.transition = transition;
156   }
157 
158   public String getTarget() {
159     return target;
160   }
161 
162   public void setTarget(final String target) {
163     this.target = target;
164   }
165 
166   public String getUrl() {
167     return url;
168   }
169 
170   public void setUrl(final String url) {
171     this.url = url;
172   }
173 
174   public String[] getPartially() {
175     return partially;
176   }
177 
178   public void setPartially(final String[] partially) {
179     this.partially = partially;
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 
222   /**
223    * @deprecated Script will not work when CSP is activated
224    */
225   public String getScript() {
226     return script;
227   }
228 
229   /**
230    * @deprecated Script will not work when CSP is activated
231    */
232   @Deprecated
233   public void setScript(final String script) {
234     this.script = script;
235   }
236 
237 }