001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Anahide Tchertchian
016 */
017package org.nuxeo.ecm.platform.forms.layout.service;
018
019import java.util.Collection;
020import java.util.Collections;
021import java.util.LinkedHashMap;
022import java.util.Map;
023
024import org.nuxeo.runtime.model.ContributionFragmentRegistry;
025
026/**
027 * Registry for {@link DisabledPropertyRefDescriptor} instances
028 * <p>
029 * Only supports merging of contributions on "enabled" information.
030 * <p>
031 * Stores contribution in the registration order, in case some conflicting criteria are found, the first one matching
032 * will apply.
033 *
034 * @since 5.6
035 */
036public class DisabledPropertyRefRegistry extends ContributionFragmentRegistry<DisabledPropertyRefDescriptor> {
037
038    protected final Map<String, DisabledPropertyRefDescriptor> refs;
039
040    public DisabledPropertyRefRegistry() {
041        super();
042        // use linked has map since there is no ordering support, not make sure
043        this.refs = new LinkedHashMap<String, DisabledPropertyRefDescriptor>();
044    }
045
046    @Override
047    public String getContributionId(DisabledPropertyRefDescriptor contrib) {
048        return contrib.getId();
049    }
050
051    @Override
052    public void contributionUpdated(String id, DisabledPropertyRefDescriptor contrib,
053            DisabledPropertyRefDescriptor newOrigContrib) {
054        refs.put(id, contrib);
055    }
056
057    @Override
058    public void contributionRemoved(String id, DisabledPropertyRefDescriptor origContrib) {
059        refs.remove(id);
060    }
061
062    @Override
063    public boolean isSupportingMerge() {
064        return true;
065    }
066
067    @Override
068    public DisabledPropertyRefDescriptor clone(DisabledPropertyRefDescriptor orig) {
069        return orig.clone();
070    }
071
072    @Override
073    public void merge(DisabledPropertyRefDescriptor source, DisabledPropertyRefDescriptor dest) {
074        if (source.getEnabled() != dest.getEnabled()) {
075            dest.setEnabled(source.getEnabled());
076        }
077    }
078
079    public Collection<DisabledPropertyRefDescriptor> getDisabledPropertyRefs() {
080        return Collections.unmodifiableCollection(refs.values());
081    }
082
083}