Clover icon

Magnolia Crop UI for Imaging Module 3.2.7

  1. Project Clover database Fri Sep 9 2016 14:52:54 CEST
  2. Package info.magnolia.module.cropui

File CropperPage.java

 

Coverage histogram

../../../../img/srcFileCovDistChart2.png
0% of files have more coverage

Code metrics

8
44
7
3
199
123
14
0.32
6.29
2.33
2
16.9% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
CropperPage 65 36 19.7% 12 37
0.2448979624.5%
CropperPage.CropperDialog 173 3 0% 1 4
0.00%
CropperPage.CropperControl 182 5 0% 1 6
0.00%
 

Contributing tests

This file is covered by 1 test. .

Source view

1    /**
2    * This file Copyright (c) 2007-2016 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.module.cropui;
35   
36    import info.magnolia.cms.beans.config.ContentRepository;
37    import info.magnolia.cms.core.Content;
38    import info.magnolia.cms.core.HierarchyManager;
39    import info.magnolia.cms.gui.dialog.Dialog;
40    import info.magnolia.cms.gui.dialog.DialogControlImpl;
41    import info.magnolia.cms.gui.dialog.DialogTab;
42    import info.magnolia.content2bean.Content2BeanException;
43    import info.magnolia.content2bean.Content2BeanUtil;
44    import info.magnolia.context.MgnlContext;
45    import info.magnolia.freemarker.FreemarkerUtil;
46    import info.magnolia.module.admininterface.TemplatedMVCHandler;
47   
48    import java.io.IOException;
49    import java.io.Writer;
50    import java.util.ArrayList;
51    import java.util.Collection;
52    import java.util.HashMap;
53    import java.util.Iterator;
54    import java.util.List;
55   
56    import javax.jcr.RepositoryException;
57    import javax.servlet.http.HttpServletRequest;
58    import javax.servlet.http.HttpServletResponse;
59   
60    import org.apache.commons.lang3.StringUtils;
61   
62    /**
63    * The page that provides the image cropping dialog/widget.
64    */
 
65    public class CropperPage extends TemplatedMVCHandler {
66   
67    private String configDialogUUID;
68    private String imagePath;
69   
70    /**
71    * The current params (crop values, chosen config) as a JSON string.
72    */
73    private String currentCrop;
74    private List cropAndResizeConfigs;
75    private Content imageControlConfigNode;
76   
 
77  0 toggle public CropperPage(String name, HttpServletRequest request, HttpServletResponse response) {
78  0 super(name, request, response);
79    }
80   
 
81  0 toggle @Override
82    public void init() {
83  0 super.init();
84   
85  0 if (configDialogUUID == null) {
86  0 throw new IllegalStateException("configDialogUUID should have been set");
87    }
88  0 try {
89  0 final HierarchyManager hm = MgnlContext.getHierarchyManager(ContentRepository.CONFIG);
90  0 imageControlConfigNode = hm.getContentByUUID(configDialogUUID);
91   
92  0 cropAndResizeConfigs = new ArrayList();
93  0 final Collection configNodes = imageControlConfigNode.getChildren();
94  0 final Iterator it = configNodes.iterator();
95  0 while (it.hasNext()) {
96  0 final Content configNode = (Content) it.next();
97  0 cropAndResizeConfigs.add(fromNode(configNode));
98    }
99   
100    } catch (RepositoryException e) {
101  0 throw new RuntimeException(e);
102    } catch (Content2BeanException e) {
103  0 throw new RuntimeException(e); // TODO
104    }
105    }
106   
 
107  8 toggle public static CropAndResizeConfig fromNode(Content node) throws Content2BeanException {
108    // TODO : use content2bean + cleanup
109  8 final CropAndResizeConfig cfg = (CropAndResizeConfig) Content2BeanUtil.setProperties(new CropAndResizeConfig(), node);
110   
111    // if label property wasn't set, we try using the name property, or the node name if neither was set.
112  8 if (StringUtils.isEmpty(cfg.getName())) {
113  2 cfg.setName(node.getName());
114    }
115  8 if (StringUtils.isEmpty(cfg.getLabel())) {
116    // todo: i18n ?
117    // dialog.getMessage(cfg.getName());
118  4 cfg.setLabel(cfg.getName());
119    }
120   
121    // name is always node name, to avoid illegal characters
122  8 cfg.setName(node.getName());
123  8 return cfg;
124   
125    }
126   
 
127  0 toggle @Override
128    public void renderHtml(String view) throws IOException {
129  0 try {
130    // let's fake a dialog for the sake of its layout ... and save button.
131  0 final Dialog dialog = new CropperDialog();
132  0 dialog.init(request, response, null, imageControlConfigNode);
133    // TODO : this is not needed with 3.5
134  0 dialog.setConfig("i18nBasename", "info.magnolia.module.cropui.messages");
135  0 dialog.setConfig("saveLabel", dialog.getMessage("cropper.apply.button"));
136  0 dialog.setConfig("saveOnclick", "cropperSubmit();");
137   
138  0 final String tabLabel = dialog.getMessage("cropper.tab.label");
139  0 final DialogTab tab = dialog.addTab(tabLabel);
140  0 tab.addSub(new CropperControl());
141   
142  0 dialog.drawHtml(getResponse().getWriter());
143   
144    } catch (RepositoryException e) {
145  0 throw new RuntimeException(e); // TODO ?
146    }
147    }
148   
 
149    toggle public void setConfigDialogUUID(String configDialogUUID) {
150    this.configDialogUUID = configDialogUUID;
151    }
152   
 
153    toggle public void setImagePath(String imagePath) {
154    this.imagePath = imagePath;
155    }
156   
 
157    toggle public void setCurrentCrop(String currentCrop) {
158    this.currentCrop = currentCrop;
159    }
160   
 
161    toggle public String getImagePath() {
162    return imagePath;
163    }
164   
 
165    toggle public String getCurrentCrop() {
166    return currentCrop;
167    }
168   
 
169    toggle public List getConfigs() { // <CropAndResizeConfig>
170    return cropAndResizeConfigs;
171    }
172   
 
173    private final class CropperDialog extends Dialog {
 
174  0 toggle @Override
175    protected void drawHtmlPreSubsHead(Writer out) throws IOException {
176  0 super.drawHtmlPreSubsHead(out);
177  0 renderTemplate("/info/magnolia/module/cropui/CropperPage.head.html", out);
178  0 out.flush();
179    }
180    }
181   
 
182    private final class CropperControl extends DialogControlImpl {
 
183  0 toggle @Override
184    public void drawHtml(Writer out) throws IOException {
185  0 out.write("<tbody><tr><td>");
186  0 out.flush();
187  0 renderTemplate("/info/magnolia/module/cropui/CropperPage.html", out);
188  0 out.flush();
189  0 out.write("</td></tr></tbody>");
190    }
191    }
192   
 
193  0 toggle private void renderTemplate(String templateName, Writer out) {
194  0 final HashMap map = new HashMap();
195  0 map.put("this", this);
196  0 FreemarkerUtil.process(templateName, map, out);
197    }
198   
199    }