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