View Javadoc
1   /**
2    * This file Copyright (c) 2010-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * Field that wraps A {@link CroppableImage}, manages the {@link SelectionArea} as a value.
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 }