1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
43
44
45
46
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
65
66
67
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 }