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