001/* 002 * Copyright 2011 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.skinfactory.parameters; 017 018import java.nio.file.Path; 019import java.util.regex.Pattern; 020 021import org.ametys.runtime.i18n.I18nizableText; 022import org.ametys.skinfactory.SkinFactoryComponent; 023import org.ametys.web.skin.SkinModel; 024import org.ametys.web.skin.SkinModel.Theme; 025 026/** 027 * Implementation of {@link AbstractSkinParameter} for a CSS property of type color (color, background-color, border-color) 028 */ 029public class CSSColorParameter extends CSSParameter 030{ 031 private static final Pattern __HEX_HASH_LESS_PATTERN = Pattern.compile("^([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$"); 032 private static final Pattern __HEX_PATTERN = Pattern.compile("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$"); 033 034 private SkinModel _model; 035 private SkinFactoryComponent _skinFactory; 036 037 /** 038 * Constructor 039 * @param id the unique id 040 * @param label the label 041 * @param description the description 042 * @param cssFile the css file 043 * @param cssProperty the css property 044 * @param defaultValue the default value 045 * @param model the model 046 * @param skinFactory the skin factory manager 047 */ 048 public CSSColorParameter(String id, I18nizableText label, I18nizableText description, Path cssFile, String cssProperty, String defaultValue, SkinModel model, SkinFactoryComponent skinFactory) 049 { 050 super(id, label, description, cssFile, cssProperty, defaultValue); 051 _model = model; 052 _skinFactory = skinFactory; 053 } 054 055 @Override 056 public void apply(Path tempDir, Path modelDir, Object value, String lang) throws SkinParameterException 057 { 058 String cValue = (String) value; 059 060 if (cValue.startsWith("color-theme-")) 061 { 062 String themeId = _skinFactory.getColorTheme(tempDir); 063 Theme theme = _model.getTheme(themeId); 064 065 if (theme != null) 066 { 067 int colorIndex = Integer.parseInt(cValue.substring("color-theme-".length())); 068 069 String color = theme.getColor(colorIndex); 070 if (__HEX_HASH_LESS_PATTERN.matcher(color).matches()) 071 { 072 cValue = "#" + color.toUpperCase(); 073 } 074 else if (__HEX_PATTERN.matcher(color).matches()) 075 { 076 cValue = color.toUpperCase(); 077 } 078 else 079 { 080 cValue = color; // rgb(a) 081 } 082 } 083 } 084 085 super.apply(tempDir, modelDir, cValue.equals("transparent") ? cValue : cValue, lang); 086 } 087}