001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.model.impl;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.CharPool;
020    import com.liferay.portal.kernel.util.PropertiesUtil;
021    import com.liferay.portal.kernel.util.SafeProperties;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.model.ColorScheme;
025    
026    import java.io.IOException;
027    
028    import java.util.Properties;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class ColorSchemeImpl implements ColorScheme {
034    
035            public ColorSchemeImpl() {
036                    this(null, null, null);
037            }
038    
039            public ColorSchemeImpl(String colorSchemeId) {
040                    this(colorSchemeId, null, null);
041            }
042    
043            public ColorSchemeImpl(String colorSchemeId, String name, String cssClass) {
044                    _colorSchemeId = colorSchemeId;
045                    _name = name;
046                    _cssClass = cssClass;
047            }
048    
049            @Override
050            public int compareTo(ColorScheme colorScheme) {
051                    return getName().compareTo(colorScheme.getName());
052            }
053    
054            @Override
055            public boolean equals(Object obj) {
056                    if (this == obj) {
057                            return true;
058                    }
059    
060                    if (!(obj instanceof ColorScheme)) {
061                            return false;
062                    }
063    
064                    ColorScheme colorScheme = (ColorScheme)obj;
065    
066                    String colorSchemeId = colorScheme.getColorSchemeId();
067    
068                    if (getColorSchemeId().equals(colorSchemeId)) {
069                            return true;
070                    }
071                    else {
072                            return false;
073                    }
074            }
075    
076            @Override
077            public String getColorSchemeId() {
078                    return _colorSchemeId;
079            }
080    
081            @Override
082            public String getColorSchemeImagesPath() {
083                    return _colorSchemeImagesPath;
084            }
085    
086            @Override
087            public String getColorSchemeThumbnailPath() {
088    
089                    // LEP-5270
090    
091                    if (Validator.isNotNull(_cssClass) &&
092                            Validator.isNotNull(_colorSchemeImagesPath)) {
093    
094                            int pos = _cssClass.indexOf(CharPool.SPACE);
095    
096                            if (pos > 0) {
097                                    if (_colorSchemeImagesPath.endsWith(
098                                                    _cssClass.substring(0, pos))) {
099    
100                                            String subclassPath = StringUtil.replace(
101                                                    _cssClass, CharPool.SPACE, CharPool.SLASH);
102    
103                                            return _colorSchemeImagesPath + subclassPath.substring(pos);
104                                    }
105                            }
106                    }
107    
108                    return _colorSchemeImagesPath;
109            }
110    
111            @Override
112            public String getCssClass() {
113                    return _cssClass;
114            }
115    
116            @Override
117            public boolean getDefaultCs() {
118                    return _defaultCs;
119            }
120    
121            @Override
122            public String getName() {
123                    if (Validator.isNull(_name)) {
124                            return _colorSchemeId;
125                    }
126                    else {
127                            return _name;
128                    }
129            }
130    
131            @Override
132            public String getSetting(String key) {
133                    //return _settingsProperties.getProperty(key);
134    
135                    // FIX ME
136    
137                    if (key.endsWith("-bg")) {
138                            return "#FFFFFF";
139                    }
140                    else {
141                            return "#000000";
142                    }
143            }
144    
145            @Override
146            public String getSettings() {
147                    return PropertiesUtil.toString(_settingsProperties);
148            }
149    
150            @Override
151            public Properties getSettingsProperties() {
152                    return _settingsProperties;
153            }
154    
155            @Override
156            public int hashCode() {
157                    return _colorSchemeId.hashCode();
158            }
159    
160            @Override
161            public boolean isDefaultCs() {
162                    return _defaultCs;
163            }
164    
165            @Override
166            public void setColorSchemeImagesPath(String colorSchemeImagesPath) {
167                    _colorSchemeImagesPath = colorSchemeImagesPath;
168            }
169    
170            @Override
171            public void setCssClass(String cssClass) {
172                    _cssClass = cssClass;
173            }
174    
175            @Override
176            public void setDefaultCs(boolean defaultCs) {
177                    _defaultCs = defaultCs;
178            }
179    
180            @Override
181            public void setName(String name) {
182                    _name = name;
183            }
184    
185            @Override
186            public void setSettings(String settings) {
187                    _settingsProperties.clear();
188    
189                    try {
190                            PropertiesUtil.load(_settingsProperties, settings);
191                            PropertiesUtil.trimKeys(_settingsProperties);
192                    }
193                    catch (IOException ioe) {
194                            _log.error(ioe);
195                    }
196            }
197    
198            @Override
199            public void setSettingsProperties(Properties settingsProperties) {
200                    _settingsProperties = settingsProperties;
201            }
202    
203            private static final Log _log = LogFactoryUtil.getLog(
204                    ColorSchemeImpl.class);
205    
206            private final String _colorSchemeId;
207            private String _colorSchemeImagesPath =
208                    "${images-path}/color_schemes/${css-class}";
209            private String _cssClass;
210            private boolean _defaultCs;
211            private String _name;
212            private Properties _settingsProperties = new SafeProperties();
213    
214    }