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