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.LinkedHashMap; 021import java.util.List; 022import java.util.Map; 023import java.util.Set; 024 025import org.apache.avalon.framework.configuration.Configuration; 026import org.apache.avalon.framework.configuration.ConfigurationException; 027import org.apache.avalon.framework.configuration.DefaultConfiguration; 028import org.apache.avalon.framework.service.ServiceException; 029import org.apache.avalon.framework.service.ServiceManager; 030import org.apache.cocoon.ProcessingException; 031 032import org.ametys.core.ui.ClientSideElement; 033import org.ametys.core.ui.SimpleMenu; 034import org.ametys.core.ui.StaticClientSideElement; 035import org.ametys.core.util.I18nUtils; 036import org.ametys.runtime.i18n.I18nizableText; 037import org.ametys.web.skin.SkinModel; 038import org.ametys.web.skin.SkinModelsManager; 039 040/** 041 * {@link StaticClientSideElement} for models menu 042 */ 043public class ModelsMenu extends SimpleMenu 044{ 045 private SkinModelsManager _modelsManager; 046 private boolean _modelsInitialized; 047 private I18nUtils _i18nUtils; 048 049 @Override 050 public void service(ServiceManager smanager) throws ServiceException 051 { 052 super.service(smanager); 053 _modelsManager = (SkinModelsManager) smanager.lookup(SkinModelsManager.ROLE); 054 _i18nUtils = (I18nUtils) smanager.lookup(I18nUtils.ROLE); 055 } 056 057 @Override 058 public List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters) 059 { 060 Map<String, String> rights = getRights(contextParameters); 061 062 if (!ignoreRights && !hasRight(rights)) 063 { 064 return Collections.EMPTY_LIST; 065 } 066 067 try 068 { 069 Set<String> models = _modelsManager.getModels(); 070 if (models.size() > 0) 071 { 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 @Override 086 protected void _getGalleryItems(Map<String, Object> parameters, Map<String, Object> contextualParameters) 087 { 088 try 089 { 090 _lazyInitializeModelsGallery(); 091 } 092 catch (Exception e) 093 { 094 throw new IllegalStateException("Unable to lookup client side element local components", e); 095 } 096 097 if (_galleryItems.size() > 0) 098 { 099 parameters.put("gallery-item", new LinkedHashMap<String, Object>()); 100 101 for (GalleryItem galleryItem : _galleryItems) 102 { 103 @SuppressWarnings("unchecked") 104 Map<String, Object> galleryItems = (Map<String, Object>) parameters.get("gallery-item"); 105 galleryItems.put("gallery-groups", new ArrayList<>()); 106 107 for (GalleryGroup galleryGp : galleryItem.getGroups()) 108 { 109 @SuppressWarnings("unchecked") 110 List<Object> galleryGroups = (List<Object>) galleryItems.get("gallery-groups"); 111 112 Map<String, Object> groupParams = new LinkedHashMap<>(); 113 114 I18nizableText label = galleryGp.getLabel(); 115 groupParams.put("label", label); 116 117 // Group items 118 List<String> gpItems = new ArrayList<>(); 119 for (ClientSideElement element : galleryGp.getItems()) 120 { 121 gpItems.add(element.getId()); 122 } 123 124 if (gpItems.size() > 0) 125 { 126 groupParams.put("items", gpItems); 127 galleryGroups.add(groupParams); 128 } 129 } 130 } 131 } 132 } 133 134 135 /** 136 * Lazy initialization of the language gallery 137 * @throws ConfigurationException if the models' configuration is wrong 138 * @throws ProcessingException if an error occurs when retrieving the models 139 */ 140 private synchronized void _lazyInitializeModelsGallery() throws ConfigurationException, ProcessingException 141 { 142 if (!_modelsInitialized) 143 { 144 Set<String> models = _modelsManager.getModels(); 145 146 if (models.size() > 0) 147 { 148 GalleryItem galleryItem = new GalleryItem(); 149 150 GalleryGroup galleryGroup = new GalleryGroup(new I18nizableText("plugin." + _pluginName, "PLUGINS_SKINFACTORY_MODELSMENU_GROUP_LABEL")); 151 galleryItem.addGroup(galleryGroup); 152 153 for (String id : models) 154 { 155 SkinModel model = _modelsManager.getModel(id); 156 157 String itemId = this.getId() + "-" + id; 158 159 Configuration conf = _getModelConfiguration(itemId, model); 160 _getGalleryItemManager().addComponent(_pluginName, null, itemId, StaticClientSideElement.class, conf); 161 galleryGroup.addItem(new UnresolvedItem(itemId, true)); 162 } 163 164 _galleryItems.add(galleryItem); 165 } 166 167 if (_galleryItems.size() > 0) 168 { 169 try 170 { 171 _getGalleryItemManager().initialize(); 172 } 173 catch (Exception e) 174 { 175 throw new ConfigurationException("Unable to lookup parameter local components", e); 176 } 177 } 178 } 179 180 _modelsInitialized = true; 181 } 182 183 /** 184 * Get the configuration of the model item 185 * @param id The id of item 186 * @param model The model 187 * @return The configuration 188 */ 189 protected Configuration _getModelConfiguration(String id, SkinModel model) 190 { 191 DefaultConfiguration conf = new DefaultConfiguration("extension"); 192 conf.setAttribute("id", id); 193 194 DefaultConfiguration classConf = new DefaultConfiguration("class"); 195 classConf.setAttribute("name", "Ametys.ribbon.element.ui.ButtonController"); 196 197 // enable toggle 198 DefaultConfiguration enableToggleConf = new DefaultConfiguration("toggle-enabled"); 199 enableToggleConf.setValue("true"); 200 classConf.addChild(enableToggleConf); 201 202 203 // Label 204 DefaultConfiguration labelConf = new DefaultConfiguration("label"); 205 labelConf.setValue(_i18nUtils.translate(model.getLabel())); 206 classConf.addChild(labelConf); 207 208 // Description 209 DefaultConfiguration descriptionConf = new DefaultConfiguration("description"); 210 descriptionConf.setValue(_i18nUtils.translate(model.getDescription())); 211 classConf.addChild(descriptionConf); 212 213 // Icons 214 DefaultConfiguration iconSmallConf = new DefaultConfiguration("icon-small"); 215 iconSmallConf.setValue(model.getSmallImage()); 216 classConf.addChild(iconSmallConf); 217 DefaultConfiguration iconMediumConf = new DefaultConfiguration("icon-medium"); 218 iconMediumConf.setValue(model.getMediumImage()); 219 classConf.addChild(iconMediumConf); 220 DefaultConfiguration iconLargeConf = new DefaultConfiguration("icon-large"); 221 iconLargeConf.setValue(model.getLargeImage()); 222 classConf.addChild(iconLargeConf); 223 224 // Model name 225 DefaultConfiguration modelNameConf = new DefaultConfiguration("modelName"); 226 modelNameConf.setValue(model.getId()); 227 classConf.addChild(modelNameConf); 228 229 // Common configuration 230 @SuppressWarnings("unchecked") 231 Map<String, Object> commonConfig = (Map<String, Object>) this._script.getParameters().get("items-config"); 232 for (String tagName : commonConfig.keySet()) 233 { 234 DefaultConfiguration c = new DefaultConfiguration(tagName); 235 c.setValue(String.valueOf(commonConfig.get(tagName))); 236 classConf.addChild(c); 237 } 238 239 conf.addChild(classConf); 240 return conf; 241 } 242}