001/* 002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and others. 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 * 016 * Contributors: 017 * Anahide Tchertchian 018 */ 019package org.nuxeo.ecm.platform.forms.layout.core.registries; 020 021import java.util.ArrayList; 022import java.util.List; 023 024import org.nuxeo.ecm.platform.forms.layout.api.WidgetTypeDefinition; 025import org.nuxeo.runtime.model.SimpleContributionRegistry; 026 027/** 028 * Registry holding widget type definitions for a given category. 029 * 030 * @since 5.5 031 */ 032public class WidgetTypeDefinitionRegistry extends SimpleContributionRegistry<WidgetTypeDefinition> { 033 034 protected final String category; 035 036 public WidgetTypeDefinitionRegistry(String category) { 037 super(); 038 this.category = category; 039 } 040 041 public String getCategory() { 042 return category; 043 } 044 045 @Override 046 public String getContributionId(WidgetTypeDefinition contrib) { 047 return contrib.getName(); 048 } 049 050 public List<WidgetTypeDefinition> getDefinitions() { 051 List<WidgetTypeDefinition> res = new ArrayList<WidgetTypeDefinition>(); 052 for (WidgetTypeDefinition item : currentContribs.values()) { 053 if (item != null) { 054 res.add(item); 055 } 056 } 057 return res; 058 } 059 060 public WidgetTypeDefinition getDefinition(String id) { 061 return getCurrentContribution(id); 062 } 063 064 @Override 065 // overridden to handle aliases 066 public synchronized void addContribution(WidgetTypeDefinition contrib) { 067 super.addContribution(contrib); 068 List<String> aliases = contrib.getAliases(); 069 if (aliases != null) { 070 for (String alias : aliases) { 071 FragmentList<WidgetTypeDefinition> head = addFragment(alias, contrib); 072 contributionUpdated(alias, head.merge(this), contrib); 073 } 074 } 075 } 076 077 /** 078 * Overridden to use equals method when removing elements. 079 * 080 * @since 7.2 081 */ 082 @Override 083 public synchronized void removeContribution(WidgetTypeDefinition contrib) { 084 removeContribution(contrib, true); 085 } 086 087 @Override 088 // overridden to handle aliases 089 public synchronized void removeContribution(WidgetTypeDefinition contrib, boolean useEqualsMethod) { 090 super.removeContribution(contrib, useEqualsMethod); 091 List<String> aliases = contrib.getAliases(); 092 if (aliases != null) { 093 for (String alias : aliases) { 094 FragmentList<WidgetTypeDefinition> head = removeFragment(alias, contrib, useEqualsMethod); 095 if (head != null) { 096 WidgetTypeDefinition result = head.merge(this); 097 if (result != null) { 098 contributionUpdated(alias, result, contrib); 099 } else { 100 contributionRemoved(alias, contrib); 101 } 102 } 103 } 104 } 105 } 106 107}