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