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.clientsidelement; 017 018import java.util.ArrayList; 019import java.util.Collections; 020import java.util.List; 021import java.util.Map; 022import java.util.Set; 023 024import org.apache.avalon.framework.configuration.Configuration; 025import org.apache.avalon.framework.configuration.ConfigurationException; 026import org.apache.avalon.framework.configuration.DefaultConfiguration; 027import org.apache.avalon.framework.service.ServiceException; 028import org.apache.avalon.framework.service.ServiceManager; 029import org.apache.cocoon.ProcessingException; 030 031import org.ametys.core.ui.SimpleMenu; 032import org.ametys.core.ui.StaticClientSideElement; 033import org.ametys.core.util.I18nUtils; 034import org.ametys.web.skin.SkinModel; 035import org.ametys.web.skin.SkinModel.Theme; 036import org.ametys.web.skin.SkinModelsManager; 037 038/** 039 * {@link StaticClientSideElement} for thematic of colors 040 * 041 */ 042public class ThemeColorsMenu extends SimpleMenu 043{ 044 private SkinModelsManager _modelsManager; 045 private I18nUtils _i18nUtils; 046 private boolean _themeColorsInitialized; 047 048 @Override 049 public void service(ServiceManager smanager) throws ServiceException 050 { 051 super.service(smanager); 052 _modelsManager = (SkinModelsManager) smanager.lookup(SkinModelsManager.ROLE); 053 _i18nUtils = (I18nUtils) smanager.lookup(I18nUtils.ROLE); 054 } 055 056 @Override 057 public List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters) 058 { 059 Map<String, String> rights = getRights(contextParameters); 060 061 if (!ignoreRights && !hasRight(rights)) 062 { 063 return Collections.EMPTY_LIST; 064 } 065 066 try 067 { 068 Set<String> models = _modelsManager.getModels(); 069 if (models.size() > 0) 070 { 071 _lazyInitializeThemeColorsMenu(contextParameters); 072 return super.getScripts(ignoreRights, contextParameters); 073 } 074 else 075 { 076 return Collections.EMPTY_LIST; 077 } 078 } 079 catch (Exception e) 080 { 081 throw new IllegalStateException("Unable to lookup client side element local components", e); 082 } 083 } 084 085 /** 086 * Lazy initialization of the theme colors menu 087 * @param contextualParameters The contextual parameters 088 * @throws ConfigurationException if the theme colors' configuration is wrong 089 * @throws ProcessingException if an error occurs when retrieving the models 090 */ 091 private synchronized void _lazyInitializeThemeColorsMenu(Map<String, Object> contextualParameters) throws ConfigurationException, ProcessingException 092 { 093 if (!_themeColorsInitialized) 094 { 095 Set<String> models = _modelsManager.getModels(); 096 097 if (models.size() > 0) 098 { 099 for (String modelName : models) 100 { 101 SkinModel model = _modelsManager.getModel(modelName); 102 Map<String, Theme> themes = model.getThemes(); 103 104 if (themes.size() > 0) 105 { 106 for (Theme theme : themes.values()) 107 { 108 String itemId = this.getId() + "-" + modelName + "-" + theme.getId(); 109 110 Configuration conf = _getThemeColorsConfiguration(itemId, theme, modelName, contextualParameters); 111 _menuItemManager.addComponent(_pluginName, null, itemId, StaticClientSideElement.class, conf); 112 113 _unresolvedMenuItems.add(new UnresolvedItem(itemId, true)); 114 } 115 } 116 } 117 } 118 119 _themeColorsInitialized = true; 120 } 121 } 122 123 /** 124 * Get the configuration of the model item 125 * @param id The id of item 126 * @param theme The design 127 * @param modelName The model name 128 * @param contextualParameters The contextual parameters 129 * @return The configuration 130 */ 131 protected Configuration _getThemeColorsConfiguration(String id, Theme theme, String modelName, Map<String, Object> contextualParameters) 132 { 133 DefaultConfiguration conf = new DefaultConfiguration("extension"); 134 conf.setAttribute("id", id); 135 136 DefaultConfiguration classConf = new DefaultConfiguration("class"); 137 classConf.setAttribute("name", "Ametys.plugins.skinfactory.ribbon.menu.ThemeColorsMenuItem"); 138 139 // Label 140 DefaultConfiguration labelConf = new DefaultConfiguration("label"); 141 labelConf.setValue(_i18nUtils.translate(theme.getLabel())); 142 classConf.addChild(labelConf); 143 144 // Colors 145 List<String> colors = theme.getColors(); 146 DefaultConfiguration colorSizeConf = new DefaultConfiguration("color-size"); 147 colorSizeConf.setValue(Integer.toString(colors.size())); 148 classConf.addChild(colorSizeConf); 149 150 int j = 0; 151 for (String color : colors) 152 { 153 DefaultConfiguration colorConf = new DefaultConfiguration("color-" + j); 154 colorConf.setValue(color); 155 classConf.addChild(colorConf); 156 j++; 157 } 158 159 // Model name 160 DefaultConfiguration modelNameConf = new DefaultConfiguration("modelName"); 161 modelNameConf.setValue(modelName); 162 classConf.addChild(modelNameConf); 163 164 // Theme 165 DefaultConfiguration themeConf = new DefaultConfiguration("theme"); 166 themeConf.setValue(theme.getId()); 167 classConf.addChild(themeConf); 168 169 // Common configuration 170 @SuppressWarnings("unchecked") 171 Map<String, Object> commonConfig = (Map<String, Object>) this._script.getParameters().get("items-config"); 172 for (String tagName : commonConfig.keySet()) 173 { 174 DefaultConfiguration c = new DefaultConfiguration(tagName); 175 c.setValue(String.valueOf(commonConfig.get(tagName))); 176 classConf.addChild(c); 177 } 178 179 conf.addChild(classConf); 180 181 Map<String, String> rights = getRights(contextualParameters); 182 List<DefaultConfiguration> rightsList = new ArrayList<> (); 183 for (Map.Entry<String, String> rightAndPrefix : rights.entrySet()) 184 { 185 DefaultConfiguration rightConf = new DefaultConfiguration("right"); 186 rightConf.setValue(rightAndPrefix.getKey()); 187 if (rightAndPrefix.getValue() != null) 188 { 189 rightConf.setAttribute("context-prefix", rightAndPrefix.getValue()); 190 } 191 rightsList.add(rightConf); 192 } 193 194 if (rightsList.size() == 1) 195 { 196 // right 197 conf.addChild(rightsList.get(0)); 198 } 199 else if (rightsList.size() > 1) 200 { 201 // rights 202 DefaultConfiguration rightsConf = new DefaultConfiguration("rights"); 203 for (DefaultConfiguration rightConf : rightsList) 204 { 205 rightsConf.addChild(rightConf); 206 } 207 conf.addChild(rightsConf); 208 } 209 210 return conf; 211 } 212 213 214}