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.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   * Field that wraps A {@link CroppableImage}, manages the {@link SelectionArea} as a value.
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 }