001    /**
002     * Copyright (c) 2000-2013 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            }
037    
038            public ColorSchemeImpl(String colorSchemeId) {
039                    _colorSchemeId = colorSchemeId;
040            }
041    
042            public ColorSchemeImpl(String colorSchemeId, String name, String cssClass) {
043                    _colorSchemeId = colorSchemeId;
044                    _name = name;
045                    _cssClass = cssClass;
046            }
047    
048            public int compareTo(ColorScheme colorScheme) {
049                    return getName().compareTo(colorScheme.getName());
050            }
051    
052            @Override
053            public boolean equals(Object obj) {
054                    if (obj == null) {
055                            return false;
056                    }
057    
058                    ColorScheme colorScheme = null;
059    
060                    try {
061                            colorScheme = (ColorScheme)obj;
062                    }
063                    catch (ClassCastException cce) {
064                            return false;
065                    }
066    
067                    String colorSchemeId = colorScheme.getColorSchemeId();
068    
069                    if (getColorSchemeId().equals(colorSchemeId)) {
070                            return true;
071                    }
072                    else {
073                            return false;
074                    }
075            }
076    
077            public String getColorSchemeId() {
078                    return _colorSchemeId;
079            }
080    
081            public String getColorSchemeImagesPath() {
082                    return _colorSchemeImagesPath;
083            }
084    
085            public String getColorSchemeThumbnailPath() {
086    
087                    // LEP-5270
088    
089                    if (Validator.isNotNull(_cssClass) &&
090                            Validator.isNotNull(_colorSchemeImagesPath)) {
091    
092                            int pos = _cssClass.indexOf(CharPool.SPACE);
093    
094                            if (pos > 0) {
095                                    if (_colorSchemeImagesPath.endsWith(
096                                                    _cssClass.substring(0, pos))) {
097    
098                                            String subclassPath = StringUtil.replace(
099                                                    _cssClass, CharPool.SPACE, CharPool.SLASH);
100    
101                                            return _colorSchemeImagesPath + subclassPath.substring(pos);
102                                    }
103                            }
104                    }
105    
106                    return _colorSchemeImagesPath;
107            }
108    
109            public String getCssClass() {
110                    return _cssClass;
111            }
112    
113            public boolean getDefaultCs() {
114                    return _defaultCs;
115            }
116    
117            public String getName() {
118                    if (Validator.isNull(_name)) {
119                            return _colorSchemeId;
120                    }
121                    else {
122                            return _name;
123                    }
124            }
125    
126            public String getSetting(String key) {
127                    //return _settingsProperties.getProperty(key);
128    
129                    // FIX ME
130    
131                    if (key.endsWith("-bg")) {
132                            return "#FFFFFF";
133                    }
134                    else {
135                            return "#000000";
136                    }
137            }
138    
139            public String getSettings() {
140                    return PropertiesUtil.toString(_settingsProperties);
141            }
142    
143            public Properties getSettingsProperties() {
144                    return _settingsProperties;
145            }
146    
147            @Override
148            public int hashCode() {
149                    return _colorSchemeId.hashCode();
150            }
151    
152            public boolean isDefaultCs() {
153                    return _defaultCs;
154            }
155    
156            public void setColorSchemeImagesPath(String colorSchemeImagesPath) {
157                    _colorSchemeImagesPath = colorSchemeImagesPath;
158            }
159    
160            public void setCssClass(String cssClass) {
161                    _cssClass = cssClass;
162            }
163    
164            public void setDefaultCs(boolean defaultCs) {
165                    _defaultCs = defaultCs;
166            }
167    
168            public void setName(String name) {
169                    _name = name;
170            }
171    
172            public void setSettings(String settings) {
173                    _settingsProperties.clear();
174    
175                    try {
176                            PropertiesUtil.load(_settingsProperties, settings);
177                            PropertiesUtil.trimKeys(_settingsProperties);
178                    }
179                    catch (IOException ioe) {
180                            _log.error(ioe);
181                    }
182            }
183    
184            public void setSettingsProperties(Properties settingsProperties) {
185                    _settingsProperties = settingsProperties;
186            }
187    
188            private static Log _log = LogFactoryUtil.getLog(ColorScheme.class);
189    
190            private String _colorSchemeId;
191            private String _colorSchemeImagesPath =
192                    "${images-path}/color_schemes/${css-class}";
193            private String _cssClass;
194            private boolean _defaultCs;
195            private String _name;
196            private Properties _settingsProperties = new SafeProperties();
197    
198    }