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
37 import info.magnolia.ui.vaadin.editor.CroppableImage.ReleaseListener;
38 import info.magnolia.ui.vaadin.editor.CroppableImage.SelectionListener;
39 import info.magnolia.ui.vaadin.gwt.shared.jcrop.SelectionArea;
40
41 import com.vaadin.server.Resource;
42 import com.vaadin.ui.Component;
43 import com.vaadin.ui.CustomField;
44
45
46
47
48 public class JCropField extends CustomField<SelectionArea> {
49
50 private final JCrop jcrop;
51
52 private CroppableImage image;
53
54 private SelectionArea value;
55
56 public JCropField() {
57 super();
58 this.jcrop = getContent().getJcrop();
59 setWidth("");
60 setCropVisible(true);
61 setEnabled(true);
62 image.addSelectionListener(e -> setValue(e.getArea()));
63 image.addReleaseListener(e -> setValue(null));
64 }
65
66 @Override
67 protected CroppableImage getContent() {
68 return (CroppableImage)super.getContent();
69 }
70
71 public void setStatusComponent(Component c) {
72 jcrop.setSelectionStatusComponent(c);
73 }
74
75 public Resource getImageSource() {
76 return image.getSource();
77 }
78
79 public void setAspectRatio(double aspectRatio) {
80 jcrop.setAspectRatio(aspectRatio);
81 }
82
83 public void setCropVisible(boolean isVisible) {
84 jcrop.setCropVisible(isVisible);
85 }
86
87 public void addSelectionListener(SelectionListener listener) {
88 image.addSelectionListener(listener);
89 }
90
91 public void addReleaseListener(ReleaseListener listener) {
92 image.addReleaseListener(listener);
93 }
94
95 public void setImageSource(Resource source) {
96 jcrop.invalidate();
97 image.setSource(source);
98 }
99
100 public void setBackgroundColor(String color) {
101 jcrop.setBackgroundColor(color);
102 }
103
104 public void setBackgroundOpacity(double opacity) {
105 jcrop.setBackgroundOpacity(opacity);
106 }
107
108 public void setMinHeight(int height) {
109 jcrop.setMinHeight(height);
110 }
111
112 public void setMaxHeight(int height) {
113 jcrop.setMaxHeight(height);
114 }
115
116 public void setMinWidth(int width) {
117 jcrop.setMinWidth(width);
118 }
119
120 public void setMaxWidth(int width) {
121 jcrop.setMaxWidth(width);
122 }
123
124 public void setRatio(double ratio) {
125 jcrop.setAspectRatio(ratio);
126 }
127
128 public void select(SelectionArea area) {
129 jcrop.select(area);
130 }
131
132 @Override
133 public void setWidth(float width, Unit unit) {
134 super.setWidth(width, unit);
135 getContent().setWidth(width, unit);
136 }
137
138 @Override
139 public void setHeight(float height, Unit unit) {
140 super.setHeight(height, unit);
141 getContent().setHeight(height, unit);
142 }
143
144 @Override
145 public void setValue(SelectionArea newFieldValue) {
146 if ((newFieldValue == null && getValue() != null) || (newFieldValue != null && !newFieldValue.equals(getValue()))) {
147 super.setValue(newFieldValue);
148 }
149 }
150
151 @Override
152 public SelectionArea getValue() {
153 return value;
154 }
155
156 @Override
157 protected void doSetValue(SelectionArea value) {
158 this.value = value;
159 }
160
161 @Override
162 protected Component initContent() {
163 if (image == null) {
164 image = new CroppableImage();
165 }
166 return image;
167 }
168
169 @Override
170 public void setEnabled(boolean enabled) {
171 super.setEnabled(enabled);
172 jcrop.setEnabled(enabled);
173 }
174
175 public void animateSelection(SelectionArea selectionArea) {
176 jcrop.animateTo(selectionArea);
177 }
178
179 public void setTrueHeight(int height) {
180 jcrop.setTrueHeight(height);
181 }
182
183 public void setTrueWidth(int width) {
184 jcrop.setTrueWidth(width);
185 }
186 }