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.imagerotateandresize;
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.BehaviorEvent;
34  import javax.faces.event.FacesEvent;
35  
36  import org.primefaces.component.api.Widget;
37  import org.primefaces.extensions.event.ResizeEvent;
38  import org.primefaces.extensions.event.RotateEvent;
39  import org.primefaces.util.Constants;
40  
41  /**
42   * Component class for the <code>ImageRotateAndResize</code> component.
43   *
44   * @author Thomas Andraschko / last modified by $Author$
45   * @version $Revision$
46   * @since 0.1
47   */
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 = "primefaces-extensions.js")
52  @ResourceDependency(library = "primefaces-extensions", name = "imagerotateandresize/imagerotateandresize.js")
53  public class ImageRotateAndResize extends UIComponentBase implements Widget, ClientBehaviorHolder {
54  
55      public static final String COMPONENT_TYPE = "org.primefaces.extensions.component.ImageRotateAndResize";
56      public static final String COMPONENT_FAMILY = "org.primefaces.extensions.component";
57      public static final String EVENT_ROTATE = "rotate";
58      public static final String EVENT_RESIZE = "resize";
59      private static final String DEFAULT_RENDERER = "org.primefaces.extensions.component.ImageRotateAndResizeRenderer";
60  
61      private static final Collection<String> EVENT_NAMES = Collections.unmodifiableCollection(Arrays.asList(EVENT_ROTATE, EVENT_RESIZE));
62  
63      /**
64       * Properties that are tracked by state saving.
65       *
66       * @author Thomas Andraschko / last modified by $Author$
67       * @version $Revision$
68       */
69      @SuppressWarnings("java:S115")
70      protected enum PropertyKeys {
71  
72          widgetVar, forValue("for");
73  
74          private final String toString;
75  
76          PropertyKeys(String toString) {
77              this.toString = toString;
78          }
79  
80          PropertyKeys() {
81              toString = null;
82          }
83  
84          @Override
85          public String toString() {
86              return ((toString != null) ? toString : super.toString());
87          }
88      }
89  
90      public ImageRotateAndResize() {
91          setRendererType(DEFAULT_RENDERER);
92      }
93  
94      @Override
95      public String getFamily() {
96          return COMPONENT_FAMILY;
97      }
98  
99      @Override
100     public Collection<String> getEventNames() {
101         return EVENT_NAMES;
102     }
103 
104     public String getWidgetVar() {
105         return (String) getStateHelper().eval(PropertyKeys.widgetVar, null);
106     }
107 
108     public void setWidgetVar(final String widgetVar) {
109         getStateHelper().put(PropertyKeys.widgetVar, widgetVar);
110     }
111 
112     public String getFor() {
113         return (String) getStateHelper().eval(PropertyKeys.forValue, null);
114     }
115 
116     public void setFor(final String forValue) {
117         getStateHelper().put(PropertyKeys.forValue, forValue);
118     }
119 
120     @Override
121     public void queueEvent(final FacesEvent event) {
122         final FacesContext context = FacesContext.getCurrentInstance();
123         final Map<String, String> params = context.getExternalContext().getRequestParameterMap();
124         final String clientId = getClientId(context);
125 
126         if (isRequestSource(clientId, params)) {
127             final String eventName = params.get(Constants.RequestParams.PARTIAL_BEHAVIOR_EVENT_PARAM);
128 
129             final BehaviorEvent behaviorEvent = (BehaviorEvent) event;
130 
131             if (eventName.equals(EVENT_RESIZE)) {
132                 final double width = Double.parseDouble(params.get(clientId + "_width"));
133                 final double height = Double.parseDouble(params.get(clientId + "_height"));
134 
135                 final ResizeEvent resizeEvent = new ResizeEvent(this, behaviorEvent.getBehavior(), width, height);
136 
137                 super.queueEvent(resizeEvent);
138             }
139             else if (eventName.equals(EVENT_ROTATE)) {
140                 final int degree = Integer.parseInt(params.get(clientId + "_degree"));
141 
142                 final RotateEvent rotateEvent = new RotateEvent(this, behaviorEvent.getBehavior(), degree);
143 
144                 super.queueEvent(rotateEvent);
145             }
146         }
147         else {
148             super.queueEvent(event);
149         }
150     }
151 
152     private static boolean isRequestSource(final String clientId, final Map<String, String> params) {
153         return clientId.equals(params.get(Constants.RequestParams.PARTIAL_SOURCE_PARAM));
154     }
155 }