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.theme.styling.service.registries;
020
021import java.util.HashMap;
022import java.util.Map;
023
024import org.nuxeo.runtime.model.ContributionFragmentRegistry;
025import org.nuxeo.theme.styling.service.descriptors.SimpleStyle;
026
027/**
028 * Registry for theme style resources, handling merge of registered {@link SimpleStyle} elements.
029 *
030 * @since 5.5
031 */
032public class StyleRegistry extends ContributionFragmentRegistry<SimpleStyle> {
033
034    protected Map<String, SimpleStyle> themePageStyles = new HashMap<String, SimpleStyle>();
035
036    @Override
037    public String getContributionId(SimpleStyle contrib) {
038        return contrib.getName();
039    }
040
041    @Override
042    public void contributionUpdated(String id, SimpleStyle contrib, SimpleStyle newOrigContrib) {
043        themePageStyles.put(id, contrib);
044    }
045
046    @Override
047    public void contributionRemoved(String id, SimpleStyle origContrib) {
048        themePageStyles.remove(id);
049    }
050
051    @Override
052    public SimpleStyle clone(SimpleStyle orig) {
053        SimpleStyle clone = new SimpleStyle();
054        clone.setName(orig.getName());
055        clone.setSrc(orig.getSrc());
056        clone.setContent(orig.getContent());
057        return clone;
058    }
059
060    @Override
061    public void merge(SimpleStyle src, SimpleStyle dst) {
062        // no merge => replace content
063        dst.setSrc(src.getSrc());
064        dst.setContent(src.getContent());
065    }
066
067    public SimpleStyle getStyle(String id) {
068        return themePageStyles.get(id);
069    }
070}