View Javadoc
1   /**
2    * This file Copyright (c) 2007-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.module.cropui;
35  
36  import info.magnolia.cms.core.Content;
37  import info.magnolia.cms.core.NodeData;
38  import info.magnolia.cms.gui.control.Button;
39  import info.magnolia.cms.gui.control.File;
40  import info.magnolia.cms.gui.control.Hidden;
41  import info.magnolia.cms.gui.dialog.DialogBox;
42  import info.magnolia.cms.gui.misc.CssConstants;
43  import org.apache.commons.lang3.StringUtils;
44  
45  import javax.jcr.RepositoryException;
46  import javax.servlet.http.HttpServletRequest;
47  import javax.servlet.http.HttpServletResponse;
48  import java.io.IOException;
49  import java.io.Writer;
50  
51  /**
52   * A control that's related to another control (named with fileControlName) and opens an edit window.
53   *
54   * @author gjoseph
55   * @version $Revision: $ ($Author: $)
56   */
57  public class CropAndResizeControl extends DialogBox {
58      private Content configNode;
59  
60      @Override
61      public void init(HttpServletRequest request, HttpServletResponse response, Content websiteNode, Content configNode) throws RepositoryException {
62          super.init(request, response, websiteNode, configNode);
63          this.configNode = configNode;
64      }
65  
66      public Content getConfigNode() {
67          return configNode;
68      }
69  
70      @Override
71      public void drawHtml(Writer out) throws IOException {
72          drawHtmlPre(out);
73  
74          // TODO : for now we can't handle a paragraph where the image hasn't been uploaded yet. (MGNLIMG-7)
75          if (getWebsiteNode() != null) {
76              final String fileControlName = getConfigValue("fileControlName", null);
77              if (fileControlName == null) {
78                  throw new IllegalStateException("Need a fileControlName config parameter to know which file control to use.");
79              }
80              final File fileControl = new File(fileControlName, getWebsiteNode());
81              final String imagePath = fileControl.getHandle() + "." + fileControl.getExtension();
82              final String cropperInfoControlName = getCropperInfoPropertyName(fileControlName);
83              final String previewId = cropperInfoControlName + "_previewZone";
84  
85              out.write("\n<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">");
86              //width=\"" + width + "\">");
87              out.write("<tr><td class=\"" + CssConstants.CSSCLASS_FILEIMAGE + "\">");
88  
89              // preview
90              final NodeData bin = getWebsiteNode().getNodeData(getTargetBinaryPropertyName(fileControlName));
91              out.write("<div id=\"");
92              out.write(previewId);
93              out.write("\">");
94              if (bin.isExist()) {
95                  final File preview = new File(getTargetBinaryPropertyName(fileControlName), getWebsiteNode());
96                  int previewHeight = -1;
97                  try {
98                      previewHeight = Integer.parseInt(preview.getImageHeight());
99                      if (previewHeight > 100) {
100                         previewHeight = 100;
101                     }
102                 } catch (NumberFormatException e) {
103                     // if the height wasn't stored .. should really not happen except if you used an old snapshot of this module ...
104                 }
105 
106                 out.write("<img src=\"");
107                 out.write(getRequest().getContextPath());
108                 out.write(preview.getPath());
109                 if (previewHeight > 0) {
110                     out.write("\" height=\"");
111                     out.write(String.valueOf(previewHeight));
112                 }
113                 out.write("\" />");
114                 if (StringUtils.isNotEmpty(preview.getImageWidth())) {
115                     out.write("<p><em style='white-space:nowrap'>");
116                     out.write(getMessage("cropper.preview.widthheight", new String[]{preview.getImageWidth(), preview.getImageHeight()}));
117                     out.write("</em></p>\n");
118                 }
119             }
120             out.write("<p style=\"display: none;\" class=\"warning\">");
121             out.write(getMessage("cropper.preview.tobeprocessed"));
122             out.write("</p>");
123             out.write("</div>");
124 
125             out.write("</td><td>");
126 
127             final Hidden cropperInfo = new Hidden(cropperInfoControlName, getWebsiteNode());
128             cropperInfo.setId(cropperInfoControlName);
129             cropperInfo.setSaveInfo(true);
130 
131             final String controlUUID = configNode.getUUID();
132             final Button button = new Button();
133             button.setLabel(getMessage("cropper.edit.button"));
134             button.setOnclick("new mgnl.cropui.ImageCropper.openCropper('" + cropperInfo.getId() + "', '" + controlUUID + "', '" + imagePath + "');");
135 
136             out.write("<div class=\""); // just to give a little padding
137             out.write(CssConstants.CSSCLASS_DESCRIPTION);
138             out.write("\">");
139             out.write(button.getHtml());
140             out.write(cropperInfo.getHtml());
141             out.write("</div>");
142 
143             out.write("</td></tr></table>");
144         }
145 
146         drawHtmlPost(out);
147     }
148 
149     public static String getCropperInfoPropertyName(String fileControlName) {
150         return fileControlName + "_cropperInfo";
151     }
152 
153     public static String getTargetBinaryPropertyName(String fileControlName) {
154         return fileControlName + "_resized";
155     }
156 }