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.ade.galleries.client.preview; 029 030import org.opencms.gwt.client.util.CmsClientStringUtil; 031 032/** 033 * Predefined image format restriction. To be used within the image format tab of the image preview.<p> 034 * 035 * @since 8.0.0 036 */ 037public class CmsImageFormatRestriction implements I_CmsFormatRestriction { 038 039 /** The height. */ 040 private int m_height; 041 042 /** The label. */ 043 private String m_label; 044 045 /** The format name. */ 046 private String m_name; 047 048 /** The width. */ 049 private int m_width; 050 051 /** 052 * Constructor.<p> 053 * 054 * @param name the format name 055 * @param label the label 056 * @param config the configuration 057 */ 058 public CmsImageFormatRestriction(String name, String label, String config) { 059 060 m_name = name; 061 m_label = label; 062 parseConfig(config); 063 } 064 065 /** 066 * Returns if given configuration string is valid.<p> 067 * 068 * @param config configuration string 069 * 070 * @return <code>true</code> if given configuration string is valid 071 */ 072 public static native boolean isValidConfig(String config)/*-{ 073 var regex = /^(\?|\d+)x(\?|\d+)$/; 074 return regex.test(config); 075 }-*/; 076 077 /** 078 * @see org.opencms.ade.galleries.client.preview.I_CmsFormatRestriction#adjustCroppingParam(org.opencms.ade.galleries.client.preview.CmsCroppingParamBean) 079 */ 080 public void adjustCroppingParam(CmsCroppingParamBean croppingParam) { 081 082 if (!matchesCroppingParam(croppingParam)) { 083 croppingParam.reset(); 084 } 085 if (!isHeightEditable()) { 086 croppingParam.setTargetHeight(m_height); 087 } 088 if (!isWidthEditable()) { 089 croppingParam.setTargetWidth(m_width); 090 } 091 croppingParam.setFormatName(getName()); 092 } 093 094 /** 095 * @see org.opencms.ade.galleries.client.preview.I_CmsFormatRestriction#getHeight(int, int) 096 */ 097 public int getHeight(int orgHeight, int orgWidth) { 098 099 if ((m_height == I_CmsFormatRestriction.DIMENSION_NOT_SET) 100 && (m_width == I_CmsFormatRestriction.DIMENSION_NOT_SET)) { 101 return orgHeight; 102 } 103 104 return (m_height == I_CmsFormatRestriction.DIMENSION_NOT_SET) ? ((orgHeight * m_width) / orgWidth) : m_height; 105 } 106 107 /** 108 * @see org.opencms.ade.galleries.client.preview.I_CmsFormatRestriction#getLabel() 109 */ 110 public String getLabel() { 111 112 return m_label; 113 } 114 115 /** 116 * @see org.opencms.ade.galleries.client.preview.I_CmsFormatRestriction#getName() 117 */ 118 public String getName() { 119 120 return m_name; 121 } 122 123 /** 124 * @see org.opencms.ade.galleries.client.preview.I_CmsFormatRestriction#getWidth(int, int) 125 */ 126 public int getWidth(int orgHeight, int orgWidth) { 127 128 if ((m_height == I_CmsFormatRestriction.DIMENSION_NOT_SET) 129 && (m_width == I_CmsFormatRestriction.DIMENSION_NOT_SET)) { 130 return orgWidth; 131 } 132 133 return (m_width == I_CmsFormatRestriction.DIMENSION_NOT_SET) ? ((orgWidth * m_height) / orgHeight) : m_width; 134 } 135 136 /** 137 * @see org.opencms.ade.galleries.client.preview.I_CmsFormatRestriction#isCroppingEnabled() 138 */ 139 public boolean isCroppingEnabled() { 140 141 return true; 142 } 143 144 /** 145 * @see org.opencms.ade.galleries.client.preview.I_CmsFormatRestriction#isFixedRatio() 146 */ 147 public boolean isFixedRatio() { 148 149 return !isHeightEditable() && !isWidthEditable(); 150 } 151 152 /** 153 * @see org.opencms.ade.galleries.client.preview.I_CmsFormatRestriction#isHeightEditable() 154 */ 155 public boolean isHeightEditable() { 156 157 return m_height == I_CmsFormatRestriction.DIMENSION_NOT_SET; 158 } 159 160 /** 161 * @see org.opencms.ade.galleries.client.preview.I_CmsFormatRestriction#isWidthEditable() 162 */ 163 public boolean isWidthEditable() { 164 165 return m_width == I_CmsFormatRestriction.DIMENSION_NOT_SET; 166 } 167 168 /** 169 * @see org.opencms.ade.galleries.client.preview.I_CmsFormatRestriction#matchesCroppingParam(org.opencms.ade.galleries.client.preview.CmsCroppingParamBean) 170 */ 171 public boolean matchesCroppingParam(CmsCroppingParamBean croppingParam) { 172 173 if (!isHeightEditable() && (m_height != croppingParam.getTargetHeight())) { 174 return false; 175 } 176 177 if (!isWidthEditable() && (m_width != croppingParam.getTargetWidth())) { 178 return false; 179 } 180 return true; 181 } 182 183 /** 184 * Parses the the given configuration string.<p> 185 * 186 * @param config the configuration 187 */ 188 private void parseConfig(String config) { 189 190 if (isValidConfig(config)) { 191 String[] conf = config.split("x"); 192 if (conf[0].trim().equals("?")) { 193 m_width = I_CmsFormatRestriction.DIMENSION_NOT_SET; 194 } else { 195 m_width = CmsClientStringUtil.parseInt(conf[0]); 196 m_width = (m_width == 0) ? I_CmsFormatRestriction.DIMENSION_NOT_SET : m_width; 197 } 198 if (conf[1].trim().equals("?")) { 199 m_height = I_CmsFormatRestriction.DIMENSION_NOT_SET; 200 } else { 201 m_height = CmsClientStringUtil.parseInt(conf[1]); 202 m_height = (m_height == 0) ? I_CmsFormatRestriction.DIMENSION_NOT_SET : m_height; 203 } 204 } 205 } 206}