001/*
002 * (C) Copyright 2006-2007 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 *     Jean-Marc Orliaguet, Chalmers
018 *
019 * $Id$
020 */
021
022package org.nuxeo.theme.formats.styles;
023
024import java.util.HashMap;
025import java.util.HashSet;
026import java.util.LinkedHashMap;
027import java.util.Map;
028import java.util.Properties;
029import java.util.Set;
030import java.util.TreeMap;
031
032import org.nuxeo.theme.formats.DefaultFormat;
033import org.nuxeo.theme.formats.Format;
034
035public class StyleFormat extends DefaultFormat implements Style {
036
037    private final Map<String, Map<String, Properties>> styleProperties = new TreeMap<String, Map<String, Properties>>();
038
039    private final Map<String, String> selectorDescriptions = new HashMap<String, String>();
040
041    private String collectionName;
042
043    public Properties getPropertiesFor(String viewName, String path) {
044        Map<String, Properties> propertiesMap = styleProperties.get(viewName);
045        if (propertiesMap != null) {
046            return propertiesMap.get(path);
047        }
048        return null;
049    }
050
051    public void setPropertiesFor(String viewName, String path, Properties properties) {
052        Map<String, Properties> propertiesMap = styleProperties.get(viewName);
053        if (propertiesMap == null) {
054            propertiesMap = new LinkedHashMap<String, Properties>();
055        }
056        Properties updatedProperties = propertiesMap.get(path);
057        if (updatedProperties == null) {
058            updatedProperties = new Properties();
059        }
060        if (properties != null) {
061            for (Object key : properties.keySet()) {
062                String value = properties.getProperty((String) key);
063                if (value.equals("")) {
064                    if (updatedProperties.containsKey(key)) {
065                        updatedProperties.remove(key);
066                    }
067                } else {
068                    updatedProperties.put(key, value);
069                }
070            }
071            propertiesMap.put(path, updatedProperties);
072        } else {
073            propertiesMap.remove(path);
074        }
075        if (propertiesMap.isEmpty()) {
076            styleProperties.remove(viewName);
077        } else {
078            styleProperties.put(viewName, propertiesMap);
079        }
080    }
081
082    public void clearPropertiesFor(String viewName) {
083        styleProperties.remove(viewName);
084    }
085
086    public void clearPropertiesFor(String viewName, String path) {
087        setPropertiesFor(viewName, path, null);
088    }
089
090    public Set<String> getPathsForView(String viewName) {
091        if (styleProperties.containsKey(viewName)) {
092            return styleProperties.get(viewName).keySet();
093        }
094        return new HashSet<String>();
095    }
096
097    public Set<String> getSelectorViewNames() {
098        return styleProperties.keySet();
099    }
100
101    public String getSelectorDescription(String path, String viewName) {
102        if (viewName == null) {
103            viewName = "*";
104        }
105        final String key = String.format("%s/%s", path, viewName);
106        return selectorDescriptions.get(key);
107    }
108
109    public void setSelectorDescription(String path, String viewName, String description) {
110        if (viewName == null) {
111            viewName = "*";
112        }
113        final String key = String.format("%s/%s", path, viewName);
114        selectorDescriptions.put(key, description);
115    }
116
117    public Properties getAllProperties() {
118        Properties properties = new Properties();
119        for (Map<String, Properties> map : styleProperties.values()) {
120            for (Properties s : map.values()) {
121                properties.putAll(s);
122            }
123        }
124        return properties;
125    }
126
127    @Override
128    public void clonePropertiesOf(Format source) {
129        super.clonePropertiesOf(source);
130        // Clone style properties
131        Style sourceStyle = (Style) source;
132        for (String viewName : sourceStyle.getSelectorViewNames()) {
133            for (String path : sourceStyle.getPathsForView(viewName)) {
134                setPropertiesFor(viewName, path, sourceStyle.getPropertiesFor(viewName, path));
135            }
136        }
137    }
138
139    @Override
140    public boolean isNamed() {
141        return getName() != null;
142    }
143
144    @Override
145    public String getCollection() {
146        return collectionName;
147    }
148
149    @Override
150    public void setCollection(String collectionName) {
151        this.collectionName = collectionName;
152    }
153
154}