All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sourceforge.openutils.mgnlmedia.media.dialog.DialogFileImage Maven / Gradle / Ivy

/**
 *
 * SimpleMedia Module for Magnolia CMS (http://www.openmindlab.com/lab/products/media.html)
 * Copyright(C) 2008-2012, Openmind S.r.l. http://www.openmindonline.it
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see .
 */

package net.sourceforge.openutils.mgnlmedia.media.dialog;

import info.magnolia.cms.beans.config.MIMEMapping;
import info.magnolia.cms.gui.control.File;
import info.magnolia.cms.gui.dialog.DialogFile;
import info.magnolia.cms.gui.misc.CssConstants;
import info.magnolia.cms.gui.misc.Spacer;

import java.io.IOException;
import java.io.Writer;

import javax.jcr.PropertyType;

import net.sourceforge.openutils.mgnlmedia.media.configuration.MediaConfigurationManager;

import org.apache.commons.lang.StringUtils;


/**
 * @author molaschi
 * @version Id:
 */
public class DialogFileImage extends DialogFile
{

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    @Override
    public void initImageExtensions()
    {
        this.getImageExtensions().add("jpg");
        this.getImageExtensions().add("jpeg");
        this.getImageExtensions().add("gif");
        this.getImageExtensions().add("png");
        this.getImageExtensions().add("bmp");
        this.getImageExtensions().add("ico");
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void drawHtml(Writer out) throws IOException
    {
        File control = getFileControl();
        control.setType(this.getConfigValue("type", PropertyType.TYPENAME_STRING));
        control.setSaveInfo(false); // set manualy below
        control.setCssClass(CssConstants.CSSCLASS_FILE);
        control.setCssClassFileName(CssConstants.CSSCLASS_EDIT);
        control.setCssStyles("width", this.getConfigValue("width", "100%")); //$NON-NLS-3$

        this.drawHtmlPre(out);

        String width = this.getConfigValue("width", "100%");

        final boolean preview = Boolean.valueOf(getConfigValue("preview", "true")).booleanValue();
        final boolean extensionIsDisplayableImage = this.getImageExtensions().contains(
            control.getExtension().toLowerCase());
        final boolean showImage = extensionIsDisplayableImage && preview;

        String htmlControlBrowse = control.getHtmlBrowse();
        StringBuffer htmlControlFileName = new StringBuffer();
        htmlControlFileName.append(""
            + getMessage("dialog.file.filename")
            + "");
        htmlControlFileName.append(Spacer.getHtml(1, 1));
        htmlControlFileName.append(control.getHtmlFileName()
            + "."
            + control.getExtension()
            + "");
        String htmlContentEmpty = htmlControlBrowse + Spacer.getHtml(0, 0) + htmlControlFileName;
        out.write("
"); boolean exists = false; if (this.getStorageNode() != null) { exists = this.getStorageNode().getNodeData(this.getName()).isExist(); } if (!exists) { out.write(htmlContentEmpty); out.write("
"); } if (exists && showImage) { out.write("\n
"); } if (exists) { out.write(htmlControlFileName.toString()); if (!showImage) { String iconPath = MIMEMapping.getMIMETypeIcon(control.getExtension()); out.write(Spacer.getHtml(0, 0)); out.write(""); out.write(""); out.write(this.getRequest().getContextPath() + this.getPreviewUrl() + ""); } out.write(Spacer.getHtml(12, 12)); out.write(control.getHtmlRemove("mgnlDialogFileRemove('" + this.getName() + "');")); if (showImage) { out.write("
"); // standard image // todo: image thumbnail template // out.write(""); // tmp workaround: resize in html ... int imgwidth = 150; int imgheight = 150; try { imgwidth = Integer.parseInt(control.getImageWidth()); imgheight = Integer.parseInt(control.getImageHeight()); } catch (NumberFormatException e) { // ignore (is 150) } int bigger = Math.max(imgwidth, imgheight); if (bigger > 150) { imgwidth = 150; } out.write("

"); out.write("\"");

\n"); if (StringUtils.isNotEmpty(control.getImageWidth())) { out.write("

"); out.write(control.getImageWidth()); out.write(" x "); out.write(control.getImageHeight()); out.write("

\n"); } if (StringUtils.isNotEmpty(control.getImageWidth())) { out.write("

"); int w = Integer.parseInt(control.getImageWidth()); int h = Integer.parseInt(control.getImageHeight()); if (w > h) { out.write("landscape"); } else if (h > w) { out.write("portrait"); } else { out.write("square"); } out.write("

\n"); } out.write("
"); } } out.write("
\n"); out.write("
\n\n
\n"); control.setSaveInfo(true); out.write(control.getHtmlSaveInfo()); this.drawHtmlPost(out); } private String getPreviewUrl() { return MediaConfigurationManager.getInstance().getURIMappingPrefix() + this.getStorageNode().getHandle() + "/resolutions/thumbnail/" + this.getStorageNode().getName() + ".jpg"; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy