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