001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS <http://nuxeo.com> and others
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Jean-Marc Orliaguet, Chalmers
011 *
012 * $Id$
013 */
014
015package org.nuxeo.theme.formats.styles;
016
017import java.util.HashMap;
018import java.util.HashSet;
019import java.util.LinkedHashMap;
020import java.util.Map;
021import java.util.Properties;
022import java.util.Set;
023import java.util.TreeMap;
024
025import org.nuxeo.theme.formats.DefaultFormat;
026import org.nuxeo.theme.formats.Format;
027
028public class StyleFormat extends DefaultFormat implements Style {
029
030    private final Map<String, Map<String, Properties>> styleProperties = new TreeMap<String, Map<String, Properties>>();
031
032    private final Map<String, String> selectorDescriptions = new HashMap<String, String>();
033
034    private String collectionName;
035
036    public Properties getPropertiesFor(String viewName, String path) {
037        Map<String, Properties> propertiesMap = styleProperties.get(viewName);
038        if (propertiesMap != null) {
039            return propertiesMap.get(path);
040        }
041        return null;
042    }
043
044    public void setPropertiesFor(String viewName, String path, Properties properties) {
045        Map<String, Properties> propertiesMap = styleProperties.get(viewName);
046        if (propertiesMap == null) {
047            propertiesMap = new LinkedHashMap<String, Properties>();
048        }
049        Properties updatedProperties = propertiesMap.get(path);
050        if (updatedProperties == null) {
051            updatedProperties = new Properties();
052        }
053        if (properties != null) {
054            for (Object key : properties.keySet()) {
055                String value = properties.getProperty((String) key);
056                if (value.equals("")) {
057                    if (updatedProperties.containsKey(key)) {
058                        updatedProperties.remove(key);
059                    }
060                } else {
061                    updatedProperties.put(key, value);
062                }
063            }
064            propertiesMap.put(path, updatedProperties);
065        } else {
066            propertiesMap.remove(path);
067        }
068        if (propertiesMap.isEmpty()) {
069            styleProperties.remove(viewName);
070        } else {
071            styleProperties.put(viewName, propertiesMap);
072        }
073    }
074
075    public void clearPropertiesFor(String viewName) {
076        styleProperties.remove(viewName);
077    }
078
079    public void clearPropertiesFor(String viewName, String path) {
080        setPropertiesFor(viewName, path, null);
081    }
082
083    public Set<String> getPathsForView(String viewName) {
084        if (styleProperties.containsKey(viewName)) {
085            return styleProperties.get(viewName).keySet();
086        }
087        return new HashSet<String>();
088    }
089
090    public Set<String> getSelectorViewNames() {
091        return styleProperties.keySet();
092    }
093
094    public String getSelectorDescription(String path, String viewName) {
095        if (viewName == null) {
096            viewName = "*";
097        }
098        final String key = String.format("%s/%s", path, viewName);
099        return selectorDescriptions.get(key);
100    }
101
102    public void setSelectorDescription(String path, String viewName, String description) {
103        if (viewName == null) {
104            viewName = "*";
105        }
106        final String key = String.format("%s/%s", path, viewName);
107        selectorDescriptions.put(key, description);
108    }
109
110    public Properties getAllProperties() {
111        Properties properties = new Properties();
112        for (Map<String, Properties> map : styleProperties.values()) {
113            for (Properties s : map.values()) {
114                properties.putAll(s);
115            }
116        }
117        return properties;
118    }
119
120    @Override
121    public void clonePropertiesOf(Format source) {
122        super.clonePropertiesOf(source);
123        // Clone style properties
124        Style sourceStyle = (Style) source;
125        for (String viewName : sourceStyle.getSelectorViewNames()) {
126            for (String path : sourceStyle.getPathsForView(viewName)) {
127                setPropertiesFor(viewName, path, sourceStyle.getPropertiesFor(viewName, path));
128            }
129        }
130    }
131
132    @Override
133    public boolean isNamed() {
134        return getName() != null;
135    }
136
137    @Override
138    public String getCollection() {
139        return collectionName;
140    }
141
142    @Override
143    public void setCollection(String collectionName) {
144        this.collectionName = collectionName;
145    }
146
147}