001/* 002 * Copyright 2015 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.model; 017 018import java.util.ArrayList; 019import java.util.HashMap; 020import java.util.List; 021import java.util.Map; 022import java.util.Set; 023 024import org.apache.avalon.framework.service.ServiceException; 025import org.apache.avalon.framework.service.ServiceManager; 026 027import org.ametys.core.ui.ClientSideElement; 028import org.ametys.core.ui.SimpleMenu; 029 030/** 031 * This implementation looks at existing {@link ImportModelActionExtensionPoint} to create a button for import skin. 032 */ 033public class ImportModelClientSideElement extends SimpleMenu 034{ 035 private ImportModelActionExtensionPoint _importModelActionEP; 036 037 private boolean _importMenuItemsInitialized; 038 039 @Override 040 public void service(ServiceManager smanager) throws ServiceException 041 { 042 super.service(smanager); 043 _importModelActionEP = (ImportModelActionExtensionPoint) smanager.lookup(ImportModelActionExtensionPoint.ROLE); 044 } 045 046 private synchronized void _resolveImportMenuItems () 047 { 048 if (!_importMenuItemsInitialized) 049 { 050 List<UnresolvedItem> unresolvedImportMenuItems = new ArrayList<>(); 051 052 Set<String> importActionIds = _importModelActionEP.getExtensionsIds(); 053 054 if (importActionIds.size() > 1) 055 { 056 for (String id : importActionIds) 057 { 058 unresolvedImportMenuItems.add(new UnresolvedItem(id, false, false)); 059 } 060 } 061 062 for (UnresolvedItem unresolvedItem : unresolvedImportMenuItems) 063 { 064 String id = unresolvedItem.getId(); 065 ClientSideElement element = _importModelActionEP.getExtension(id); 066 067 if (unresolvedItem.isPrimary()) 068 { 069 _primaryMenuItem = element; 070 } 071 072 _menuItems.add(element); 073 } 074 075 _importMenuItemsInitialized = true; 076 } 077 } 078 079 @Override 080 public List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters) 081 { 082 List<Script> scripts = new ArrayList<>(); 083 084 Set<String> importActionIds = _importModelActionEP.getExtensionsIds(); 085 086 if (importActionIds.size() == 0) 087 { 088 // Non button 089 return scripts; 090 } 091 else if (importActionIds.size() == 1) 092 { 093 for (String id : importActionIds) 094 { 095 ClientSideElement importAction = _importModelActionEP.getExtension(id); 096 return importAction.getScripts(ignoreRights, contextParameters); 097 } 098 } 099 100 _resolveImportMenuItems(); 101 102 List<ScriptFile> scriptFiles = new ArrayList<>(); 103 List<ScriptFile> cssFiles = new ArrayList<>(); 104 Map<String, Object> parameters = new HashMap<>(); 105 106 for (Script script : super.getScripts(ignoreRights, contextParameters)) 107 { 108 scriptFiles.addAll(script.getScriptFiles()); 109 cssFiles.addAll(script.getCSSFiles()); 110 parameters.putAll(script.getParameters()); 111 } 112 113 for (String id : importActionIds) 114 { 115 ClientSideElement importAction = _importModelActionEP.getExtension(id); 116 for (Script script : importAction.getScripts(ignoreRights, contextParameters)) 117 { 118 scriptFiles.addAll(script.getScriptFiles()); 119 cssFiles.addAll(script.getCSSFiles()); 120 parameters.putAll(script.getParameters()); 121 } 122 } 123 124 scripts.add(new Script(this.getId(), _script.getScriptClassname(), scriptFiles, cssFiles, parameters)); 125 return scripts; 126 } 127 128}