View Javadoc
1   /*
2    * Copyright (c) 2011-2024 PrimeFaces Extensions
3    *
4    *  Permission is hereby granted, free of charge, to any person obtaining a copy
5    *  of this software and associated documentation files (the "Software"), to deal
6    *  in the Software without restriction, including without limitation the rights
7    *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    *  copies of the Software, and to permit persons to whom the Software is
9    *  furnished to do so, subject to the following conditions:
10   *
11   *  The above copyright notice and this permission notice shall be included in
12   *  all copies or substantial portions of the Software.
13   *
14   *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20   *  THE SOFTWARE.
21   */
22  package org.primefaces.extensions.component.clipboard;
23  
24  import java.util.Arrays;
25  import java.util.Collection;
26  import java.util.Collections;
27  import java.util.Map;
28  
29  import javax.faces.application.ResourceDependency;
30  import javax.faces.component.UIComponentBase;
31  import javax.faces.component.behavior.ClientBehaviorHolder;
32  import javax.faces.context.FacesContext;
33  import javax.faces.event.AjaxBehaviorEvent;
34  import javax.faces.event.FacesEvent;
35  
36  import org.primefaces.component.api.Widget;
37  import org.primefaces.extensions.event.ClipboardErrorEvent;
38  import org.primefaces.extensions.event.ClipboardSuccessEvent;
39  import org.primefaces.util.Constants;
40  
41  /**
42   * <code>Clipboard</code> component.
43   *
44   * @author Melloware mellowaredev@gmail.com
45   * @since 6.1
46   */
47  @ResourceDependency(library = "primefaces", name = "components.css")
48  @ResourceDependency(library = "primefaces", name = "jquery/jquery.js")
49  @ResourceDependency(library = "primefaces", name = "jquery/jquery-plugins.js")
50  @ResourceDependency(library = "primefaces", name = "core.js")
51  @ResourceDependency(library = "primefaces-extensions", name = "clipboard/clipboard.js")
52  public class Clipboard extends UIComponentBase implements ClientBehaviorHolder, Widget {
53  
54      public static final String COMPONENT_TYPE = "org.primefaces.extensions.component.Clipboard";
55      public static final String COMPONENT_FAMILY = "org.primefaces.extensions.component";
56      private static final String DEFAULT_RENDERER = "org.primefaces.extensions.component.ClipboardRenderer";
57  
58      private static final Collection<String> EVENT_NAMES = Collections
59                  .unmodifiableCollection(Arrays.asList(ClipboardSuccessEvent.NAME, ClipboardErrorEvent.NAME));
60  
61      @SuppressWarnings("java:S115")
62      protected enum PropertyKeys {
63  
64          // @formatter:off
65          widgetVar,
66          action,
67          trigger,
68          target,
69          text,
70          onsuccess,
71          onerror
72          // @formatter:on
73      }
74  
75      /**
76       * Default constructor
77       */
78      public Clipboard() {
79          setRendererType(DEFAULT_RENDERER);
80      }
81  
82      /**
83       * {@inheritDoc}
84       */
85      @Override
86      public String getFamily() {
87          return COMPONENT_FAMILY;
88      }
89  
90      /**
91       * {@inheritDoc}
92       */
93      @Override
94      public Collection<String> getEventNames() {
95          return EVENT_NAMES;
96      }
97  
98      /**
99       * {@inheritDoc}
100      */
101     @Override
102     public String getDefaultEventName() {
103         return ClipboardSuccessEvent.NAME;
104     }
105 
106     /**
107      * {@inheritDoc}
108      */
109     @Override
110     public void processDecodes(final FacesContext fc) {
111         if (isSelfRequest(fc)) {
112             decode(fc);
113         }
114         else {
115             super.processDecodes(fc);
116         }
117     }
118 
119     /**
120      * {@inheritDoc}
121      */
122     @Override
123     public void processValidators(final FacesContext fc) {
124         if (!isSelfRequest(fc)) {
125             super.processValidators(fc);
126         }
127     }
128 
129     /**
130      * {@inheritDoc}
131      */
132     @Override
133     public void processUpdates(final FacesContext fc) {
134         if (!isSelfRequest(fc)) {
135             super.processUpdates(fc);
136         }
137     }
138 
139     /**
140      * {@inheritDoc}
141      */
142     @Override
143     public void queueEvent(final FacesEvent event) {
144         final FacesContext fc = FacesContext.getCurrentInstance();
145 
146         if (isSelfRequest(fc) && event instanceof AjaxBehaviorEvent) {
147             final Map<String, String> params = fc.getExternalContext().getRequestParameterMap();
148             final String eventName = params.get(Constants.RequestParams.PARTIAL_BEHAVIOR_EVENT_PARAM);
149             final String clientId = getClientId(fc);
150             final AjaxBehaviorEvent behaviorEvent = (AjaxBehaviorEvent) event;
151             final String action = params.get(clientId + "_action");
152             final String trigger = params.get(clientId + "_trigger");
153 
154             if (ClipboardSuccessEvent.NAME.equals(eventName)) {
155                 final String text = params.get(clientId + "_text");
156                 final ClipboardSuccessEvent successEvent = new ClipboardSuccessEvent(this, behaviorEvent.getBehavior(),
157                             action, text, trigger);
158                 successEvent.setPhaseId(event.getPhaseId());
159                 super.queueEvent(successEvent);
160 
161                 return;
162             }
163             else if (ClipboardErrorEvent.NAME.equals(eventName)) {
164                 final ClipboardErrorEvent errorEvent = new ClipboardErrorEvent(this, behaviorEvent.getBehavior(), action,
165                             trigger);
166                 errorEvent.setPhaseId(event.getPhaseId());
167                 super.queueEvent(errorEvent);
168 
169                 return;
170             }
171         }
172 
173         super.queueEvent(event);
174     }
175 
176     private boolean isSelfRequest(final FacesContext context) {
177         return getClientId(context)
178                     .equals(context.getExternalContext().getRequestParameterMap().get(
179                                 Constants.RequestParams.PARTIAL_SOURCE_PARAM));
180     }
181 
182     public String getWidgetVar() {
183         return (String) getStateHelper().eval(PropertyKeys.widgetVar, null);
184     }
185 
186     public void setWidgetVar(final String _widgetVar) {
187         getStateHelper().put(PropertyKeys.widgetVar, _widgetVar);
188     }
189 
190     public String getAction() {
191         return (String) getStateHelper().eval(PropertyKeys.action, "copy");
192     }
193 
194     public void setAction(final String _action) {
195         getStateHelper().put(PropertyKeys.action, _action);
196     }
197 
198     public String getTrigger() {
199         return (String) getStateHelper().eval(PropertyKeys.trigger, null);
200     }
201 
202     public void setTrigger(final String _trigger) {
203         getStateHelper().put(PropertyKeys.trigger, _trigger);
204     }
205 
206     public String getTarget() {
207         return (String) getStateHelper().eval(PropertyKeys.target, null);
208     }
209 
210     public void setTarget(final String _target) {
211         getStateHelper().put(PropertyKeys.target, _target);
212     }
213 
214     public String getText() {
215         return (String) getStateHelper().eval(PropertyKeys.text, "PrimeFaces Rocks!");
216     }
217 
218     public void setText(final String _text) {
219         getStateHelper().put(PropertyKeys.text, _text);
220     }
221 
222     public String getOnsuccess() {
223         return (String) getStateHelper().eval(PropertyKeys.onsuccess, null);
224     }
225 
226     public void setOnsuccess(final String _onSuccess) {
227         getStateHelper().put(PropertyKeys.onsuccess, _onSuccess);
228     }
229 
230     public String getOnerror() {
231         return (String) getStateHelper().eval(PropertyKeys.onerror, null);
232     }
233 
234     public void setOnerror(final String _onError) {
235         getStateHelper().put(PropertyKeys.onerror, _onError);
236     }
237 }