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.List;
022
023import org.nuxeo.ecm.platform.forms.layout.api.WidgetType;
024import org.nuxeo.runtime.model.SimpleContributionRegistry;
025
026/**
027 * Registry holding widget type instances for a given category.
028 *
029 * @since 5.5
030 */
031public class WidgetTypeRegistry extends SimpleContributionRegistry<WidgetType> {
032
033    protected final String category;
034
035    public WidgetTypeRegistry(String category) {
036        super();
037        this.category = category;
038    }
039
040    public String getCategory() {
041        return category;
042    }
043
044    @Override
045    public String getContributionId(WidgetType contrib) {
046        return contrib.getName();
047    }
048
049    public WidgetType getWidgetType(String id) {
050        return getCurrentContribution(id);
051    }
052
053    @Override
054    // overridden to handle aliases
055    public synchronized void addContribution(WidgetType contrib) {
056        super.addContribution(contrib);
057        List<String> aliases = contrib.getAliases();
058        if (aliases != null) {
059            for (String alias : aliases) {
060                FragmentList<WidgetType> head = addFragment(alias, contrib);
061                contributionUpdated(alias, head.merge(this), contrib);
062            }
063        }
064    }
065
066    /**
067     * Overridden to use equals method when removing elements.
068     *
069     * @since 7.2
070     */
071    @Override
072    public synchronized void removeContribution(WidgetType contrib) {
073        removeContribution(contrib, true);
074    }
075
076    @Override
077    // overridden to handle aliases
078    public synchronized void removeContribution(WidgetType contrib, boolean useEqualsMethod) {
079        super.removeContribution(contrib, useEqualsMethod);
080        List<String> aliases = contrib.getAliases();
081        if (aliases != null) {
082            for (String alias : aliases) {
083                FragmentList<WidgetType> head = removeFragment(alias, contrib, useEqualsMethod);
084                if (head != null) {
085                    WidgetType result = head.merge(this);
086                    if (result != null) {
087                        contributionUpdated(alias, result, contrib);
088                    } else {
089                        contributionRemoved(alias, contrib);
090                    }
091                }
092            }
093        }
094    }
095
096}