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 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 _galleryItemManager.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 _galleryItemManager.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 // Label 198 DefaultConfiguration labelConf = new DefaultConfiguration("label"); 199 labelConf.setValue(_i18nUtils.translate(model.getLabel())); 200 classConf.addChild(labelConf); 201 202 // Description 203 DefaultConfiguration descriptionConf = new DefaultConfiguration("description"); 204 descriptionConf.setValue(_i18nUtils.translate(model.getDescription())); 205 classConf.addChild(descriptionConf); 206 207 // Icons 208 DefaultConfiguration iconSmallConf = new DefaultConfiguration("icon-small"); 209 iconSmallConf.setValue(model.getSmallImage()); 210 classConf.addChild(iconSmallConf); 211 DefaultConfiguration iconMediumConf = new DefaultConfiguration("icon-medium"); 212 iconMediumConf.setValue(model.getMediumImage()); 213 classConf.addChild(iconMediumConf); 214 DefaultConfiguration iconLargeConf = new DefaultConfiguration("icon-large"); 215 iconLargeConf.setValue(model.getLargeImage()); 216 classConf.addChild(iconLargeConf); 217 218 // Model name 219 DefaultConfiguration modelNameConf = new DefaultConfiguration("modelName"); 220 modelNameConf.setValue(model.getId()); 221 classConf.addChild(modelNameConf); 222 223 // Common configuration 224 @SuppressWarnings("unchecked") 225 Map<String, Object> commonConfig = (Map<String, Object>) this._script.getParameters().get("items-config"); 226 for (String tagName : commonConfig.keySet()) 227 { 228 DefaultConfiguration c = new DefaultConfiguration(tagName); 229 c.setValue(String.valueOf(commonConfig.get(tagName))); 230 classConf.addChild(c); 231 } 232 233 conf.addChild(classConf); 234 return conf; 235 } 236}