001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.ui.dialogs.history.diff;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.file.history.CmsHistoryResourceHandler;
033import org.opencms.file.types.CmsResourceTypeImage;
034import org.opencms.gwt.shared.CmsHistoryResourceBean;
035import org.opencms.main.CmsException;
036import org.opencms.main.OpenCms;
037import org.opencms.util.CmsRequestUtil;
038import org.opencms.workplace.comparison.CmsHistoryListUtil;
039
040import com.google.common.base.Optional;
041import com.vaadin.server.ExternalResource;
042import com.vaadin.ui.Component;
043import com.vaadin.v7.ui.HorizontalLayout;
044import com.vaadin.ui.Image;
045import com.vaadin.ui.Panel;
046
047/**
048 * Displays two image versions side by side, scaled.<p>
049 */
050public class CmsImageDiff implements I_CmsDiffProvider {
051
052    /**
053     * @see org.opencms.ui.dialogs.history.diff.I_CmsDiffProvider#diff(org.opencms.file.CmsObject, org.opencms.gwt.shared.CmsHistoryResourceBean, org.opencms.gwt.shared.CmsHistoryResourceBean)
054     */
055    public Optional<Component> diff(CmsObject cms, CmsHistoryResourceBean v1, CmsHistoryResourceBean v2)
056    throws CmsException {
057
058        CmsResource r1 = A_CmsAttributeDiff.readResource(cms, v1);
059        if (OpenCms.getResourceManager().matchResourceType(CmsResourceTypeImage.getStaticTypeName(), r1.getTypeId())) {
060            HorizontalLayout hl = new HorizontalLayout();
061            hl.setSpacing(true);
062            String v1Param = v1.getVersion().getVersionNumber() != null
063            ? "" + v1.getVersion().getVersionNumber()
064            : "" + CmsHistoryResourceHandler.PROJECT_OFFLINE_VERSION;
065            String v2Param = v2.getVersion().getVersionNumber() != null
066            ? "" + v2.getVersion().getVersionNumber()
067            : "" + CmsHistoryResourceHandler.PROJECT_OFFLINE_VERSION;
068
069            String link1 = OpenCms.getLinkManager().substituteLinkForUnknownTarget(
070                cms,
071                CmsHistoryListUtil.getHistoryLink(cms, v1.getStructureId(), v1Param));
072            String link2 = OpenCms.getLinkManager().substituteLinkForUnknownTarget(
073                cms,
074                CmsHistoryListUtil.getHistoryLink(cms, v2.getStructureId(), v2Param));
075            int scaleWidth = 400;
076            int scaleHeight = (2 * scaleWidth) / 3;
077            final String scaleParams = "w:" + scaleWidth + ",h:" + scaleHeight + ",t:1"; // scale type 1 for thumbnails (no enlargement)
078            link1 = CmsRequestUtil.appendParameter(link1, "__scale", scaleParams);
079            link2 = CmsRequestUtil.appendParameter(link2, "__scale", scaleParams);
080            Image img1 = new Image("", new ExternalResource(link1));
081            Image img2 = new Image("", new ExternalResource(link2));
082            for (Image img : new Image[] {img1, img2}) {
083                img.setWidth("" + scaleWidth + "px");
084            }
085            img1.setCaption("V1");
086            img2.setCaption("V2");
087            hl.addComponent(img1);
088            hl.addComponent(img2);
089            Panel result = new Panel("Image comparison");
090            hl.setMargin(true);
091            result.setContent(hl);
092            return Optional.fromNullable((Component)result);
093        } else {
094            return Optional.absent();
095        }
096
097    }
098
099}