1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.ui.vaadin.editor;
35
36 import info.magnolia.ui.vaadin.editor.CroppableImage.JCropReleaseEvent;
37 import info.magnolia.ui.vaadin.editor.CroppableImage.JCropSelectionEvent;
38 import info.magnolia.ui.vaadin.gwt.client.jcrop.JCropState;
39 import info.magnolia.ui.vaadin.gwt.shared.jcrop.SelectionArea;
40
41 import org.json.JSONArray;
42 import org.json.JSONException;
43 import org.json.JSONObject;
44
45 import com.vaadin.annotations.JavaScript;
46 import com.vaadin.server.AbstractJavaScriptExtension;
47 import com.vaadin.ui.Component;
48 import com.vaadin.ui.Image;
49 import com.vaadin.ui.JavaScriptFunction;
50
51
52
53
54 @JavaScript({ "jquery.color.js", "jquery.Jcrop.min.js", "jcrop_connector.js" })
55 public class JCrop extends AbstractJavaScriptExtension {
56
57 @Override
58 public CroppableImage getParent() {
59 return (CroppableImage)super.getParent();
60 }
61
62 public JCrop(JCropHandler handler) {
63 addFunction("doOnSelect", new JavaScriptFunction() {
64 @Override
65 public void call(JSONArray args) throws JSONException {
66 SelectionArea area = AreaFromJSON(args.getJSONObject(0));
67 getState(false).selection = area;
68 getParent().fireEvent(new JCropSelectionEvent(getParent(), area));
69 }
70 });
71
72 addFunction("doOnRelease", new JavaScriptFunction() {
73 @Override
74 public void call(JSONArray args) throws JSONException {
75 getState().selection = null;
76 getParent().fireEvent(new JCropReleaseEvent(getParent()));
77 }
78 });
79
80 addFunction("onCreated", new JavaScriptFunction() {
81 @Override
82 public void call(JSONArray args) throws JSONException {
83 getState(false).isValid = true;
84 }
85 });
86 }
87
88 protected SelectionArea AreaFromJSON(JSONObject json) {
89 try {
90 return new SelectionArea(json.getInt("x"), json.getInt("y"), json.getInt("w"), json.getInt("h"));
91 } catch (JSONException e) {
92 return new SelectionArea();
93 }
94 }
95
96 @Override
97 public void attach() {
98 super.attach();
99 getParent().addStyleName("croppable" + getConnectorId());
100 if (getState().selectionStatusComponent != null) {
101 ((Component)getState().selectionStatusComponent).addStyleName("crop-status" + getConnectorId());
102 }
103 }
104
105 @Override
106 protected JCropState getState(boolean markAsDirty) {
107 return (JCropState)super.getState(markAsDirty);
108 }
109
110 @Override
111 public JCropState getState() {
112 return (JCropState) super.getState();
113 }
114
115 @Override
116 protected Class<Image> getSupportedParentType() {
117 return Image.class;
118 }
119
120 public void setAspectRatio(double aspectRatio) {
121 getState().aspectRatio = aspectRatio;
122 }
123
124 public void animateTo(SelectionArea area) {
125 callFunction("animateTo", new JSONObject(area));
126 }
127
128 public boolean isCropVisible() {
129 return getState().isVisible;
130 }
131
132 public void setCropVisible(boolean isVisible) {
133 getState().isVisible = isVisible;
134 }
135
136 public void enable() {
137 callFunction("enable");
138 }
139
140 public void disable() {
141 callFunction("disable");
142 }
143
144 public void setSelectionStatusComponent(Component c) {
145 if (getState().selectionStatusComponent != null) {
146 ((Component)getState().selectionStatusComponent).removeStyleName("");
147 }
148 getState().selectionStatusComponent = c;
149 if (getSession() != null) {
150 c.addStyleName("crop-status" + getConnectorId());
151 }
152 }
153
154 public void setBackgroundColor(String color) {
155 getState().backgroundColor = color;
156 }
157
158 public void setBackgroundOpacity(double opacity) {
159 getState().backgroundOpacity = opacity;
160 }
161
162 public void setMinHeight(int height) {
163 getState().minHeight = height;
164 }
165
166 public void setMaxHeight(int height) {
167 getState().maxHeight = height;
168 }
169
170 public void setMaxWidth(int width) {
171 getState().maxWidth = width;
172 }
173
174 public void setMinWidth(int width) {
175 getState().minWidth = width;
176 }
177
178 public void setEnabled(boolean enabled) {
179 getState().enabled = enabled;
180 }
181
182 public void invalidate() {
183 getState().isValid = false;
184 }
185
186 public void setTrueHeight(int height) {
187 getState().trueHeight = height;
188 }
189
190 public void setTrueWidth(int width) {
191 getState().trueWidth = width;
192 }
193
194 public void select(SelectionArea area) {
195 getState().selection = area;
196 }
197 }