View Javadoc

1   /**
2    * This file Copyright (c) 2010-2014 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  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   * An {@link Image} extension that operates JCrop JQuery plugin ({@link http://deepliquid.com/content/Jcrop.html}).
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 }