Clover icon

Magnolia Imaging Module 3.2.3

  1. Project Clover database Fri Nov 6 2015 14:54:03 CET
  2. Package info.magnolia.imaging.operations.cropresize

File SelectedCropAndResize.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart3.png
80% of files have more coverage

Code metrics

4
16
3
1
108
53
6
0.38
5.33
3
2
20.7% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
SelectedCropAndResize 61 16 20.7% 6 17
0.2608695626.1%
 

Contributing tests

This file is covered by 1 test. .

Source view

1    /**
2    * This file Copyright (c) 2009-2015 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.imaging.operations.cropresize;
35   
36    import info.magnolia.cms.core.Content;
37    import info.magnolia.cms.core.NodeData;
38    import info.magnolia.imaging.ImagingException;
39    import info.magnolia.imaging.ParameterProvider;
40   
41    import java.awt.image.BufferedImage;
42    import java.util.Collections;
43    import java.util.Map;
44   
45    import javax.jcr.RepositoryException;
46   
47    import org.apache.commons.lang3.StringUtils;
48   
49    import net.sf.json.JSONObject;
50   
51    /**
52    * A CropAndResize implementation which uses coordinates as selected in a UI component. This currently
53    * expects a JSon string representation of CroppingInfo (wrapped in map, which is something that might change...)
54    *
55    * Given the goo that's Magnolia's current "hiding" of binary nodes, disguising them as properties,
56    * it is not possible (?) for a dialog to be configured to store more properties *inside* an image's
57    * node; so we resort to 'cropInfoSiblingPropertySuffix': we will look for a property that has the
58    * same node as the ParameterProvider's node, suffixed, and which is a *sibling* of the image's node,
59    * not a children.
60    */
 
61    public class SelectedCropAndResize extends AbstractCropAndResize<ParameterProvider<Content>> {
62    private int targetWidth;
63    private int targetHeight;
64    private String cropInfoSiblingPropertySuffix;
65   
 
66  0 toggle @Override
67    protected Coords getCroopCoords(BufferedImage source, ParameterProvider<Content> params) throws ImagingException {
68  0 try {
69  0 final Content imageNode = params.getParameter();
70  0 final String cropInfoPropertyName = imageNode.getName() + cropInfoSiblingPropertySuffix;
71  0 final Content parent = imageNode.getParent();
72  0 if (!parent.hasNodeData(cropInfoPropertyName)) {
73  0 throw new ImagingException("There is no '" + cropInfoPropertyName + "' property at " + parent.getHandle());
74    }
75  0 final NodeData cropInfoND = parent.getNodeData(cropInfoPropertyName);
76  0 final CroppingInfo cropInfo = decode(cropInfoND.getString());
77  0 return cropInfo.getCoords();
78    } catch (RepositoryException e) {
79  0 throw new RuntimeException(e); // TODO - MAGNOLIA-2746
80    }
81    }
82   
 
83  0 toggle @Override
84    protected Size getEffectiveTargetSize(BufferedImage source, Coords cropCoords, ParameterProvider<Content> params) {
85  0 return Size.conformToCropRatio(cropCoords, targetWidth, targetHeight);
86    }
87   
 
88    toggle public void setCropInfoSiblingPropertySuffix(String cropInfoSiblingPropertySuffix) {
89    this.cropInfoSiblingPropertySuffix = cropInfoSiblingPropertySuffix;
90    }
91   
 
92    toggle public void setTargetWidth(int targetWidth) {
93    this.targetWidth = targetWidth;
94    }
95   
 
96    toggle public void setTargetHeight(int targetHeight) {
97    this.targetHeight = targetHeight;
98    }
99   
 
100  1 toggle protected CroppingInfo decode(String jsonString) {
101  1 if (StringUtils.isEmpty(jsonString)) {
102  0 return null;
103    }
104  1 final JSONObject json = JSONObject.fromObject(jsonString);
105  1 final Map map = (Map) JSONObject.toBean(json, Map.class, Collections.singletonMap("CropperInfo", CroppingInfo.class));
106  1 return (CroppingInfo) map.get("CropperInfo");
107    }
108    }