1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.model.impl;
24  
25  import com.liferay.portal.kernel.util.PropertiesUtil;
26  import com.liferay.portal.kernel.util.SafeProperties;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.model.ColorScheme;
31  import com.liferay.portal.util.PropsValues;
32  
33  import java.io.IOException;
34  
35  import java.util.Properties;
36  
37  import org.apache.commons.logging.Log;
38  import org.apache.commons.logging.LogFactory;
39  
40  /**
41   * <a href="ColorSchemeImpl.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class ColorSchemeImpl implements ColorScheme {
47  
48      public static String getDefaultRegularColorSchemeId() {
49          return PropsValues.DEFAULT_REGULAR_COLOR_SCHEME_ID;
50      }
51  
52      public static String getDefaultWapColorSchemeId() {
53          return PropsValues.DEFAULT_WAP_COLOR_SCHEME_ID;
54      }
55  
56      public static ColorScheme getNullColorScheme() {
57          return new ColorSchemeImpl(
58              getDefaultRegularColorSchemeId(), StringPool.BLANK,
59              StringPool.BLANK);
60      }
61  
62      public ColorSchemeImpl() {
63      }
64  
65      public ColorSchemeImpl(String colorSchemeId) {
66          _colorSchemeId = colorSchemeId;
67      }
68  
69      public ColorSchemeImpl(String colorSchemeId, String name, String cssClass) {
70          _colorSchemeId = colorSchemeId;
71          _name = name;
72          _cssClass = cssClass;
73      }
74  
75      public String getColorSchemeId() {
76          return _colorSchemeId;
77      }
78  
79      public String getName() {
80          if (Validator.isNull(_name)) {
81              return _colorSchemeId;
82          }
83          else {
84              return _name;
85          }
86      }
87  
88      public void setName(String name) {
89          _name = name;
90      }
91  
92      public boolean getDefaultCs() {
93          return _defaultCs;
94      }
95  
96      public boolean isDefaultCs() {
97          return _defaultCs;
98      }
99  
100     public void setDefaultCs(boolean defaultCs) {
101         _defaultCs = defaultCs;
102     }
103 
104     public String getCssClass() {
105         return _cssClass;
106     }
107 
108     public void setCssClass(String cssClass) {
109         _cssClass = cssClass;
110     }
111 
112     public String getColorSchemeImagesPath() {
113         return _colorSchemeImagesPath;
114     }
115 
116     public String getColorSchemeThumbnailPath() {
117 
118         // LEP-5270
119 
120         if (Validator.isNotNull(_cssClass) &&
121             Validator.isNotNull(_colorSchemeImagesPath)) {
122 
123             int pos = _cssClass.indexOf(StringPool.SPACE);
124 
125             if (pos > 0) {
126                 if (_colorSchemeImagesPath.endsWith(
127                         _cssClass.substring(0, pos))) {
128 
129                     String subclassPath = StringUtil.replace(
130                         _cssClass, StringPool.SPACE, StringPool.SLASH);
131 
132                     return _colorSchemeImagesPath + subclassPath.substring(pos);
133                 }
134             }
135         }
136 
137         return _colorSchemeImagesPath;
138     }
139 
140     public void setColorSchemeImagesPath(String colorSchemeImagesPath) {
141         _colorSchemeImagesPath = colorSchemeImagesPath;
142     }
143 
144     public String getSettings() {
145         return PropertiesUtil.toString(_settingsProperties);
146     }
147 
148     public void setSettings(String settings) {
149         _settingsProperties.clear();
150 
151         try {
152             PropertiesUtil.load(_settingsProperties, settings);
153             PropertiesUtil.trimKeys(_settingsProperties);
154         }
155         catch (IOException ioe) {
156             _log.error(ioe);
157         }
158     }
159 
160     public Properties getSettingsProperties() {
161         return _settingsProperties;
162     }
163 
164     public void setSettingsProperties(Properties settingsProperties) {
165         _settingsProperties = settingsProperties;
166     }
167 
168     public String getSetting(String key) {
169         //return _settingsProperties.getProperty(key);
170 
171         // FIX ME
172 
173         if (key.endsWith("-bg")) {
174             return "#FFFFFF";
175         }
176         else {
177             return "#000000";
178         }
179     }
180 
181     public int compareTo(ColorScheme colorScheme) {
182         return getName().compareTo(colorScheme.getName());
183     }
184 
185     public boolean equals(Object obj) {
186         if (obj == null) {
187             return false;
188         }
189 
190         ColorScheme colorScheme = null;
191 
192         try {
193             colorScheme = (ColorScheme)obj;
194         }
195         catch (ClassCastException cce) {
196             return false;
197         }
198 
199         String colorSchemeId = colorScheme.getColorSchemeId();
200 
201         if (getColorSchemeId().equals(colorSchemeId)) {
202             return true;
203         }
204         else {
205             return false;
206         }
207     }
208 
209     private static Log _log = LogFactory.getLog(ColorScheme.class);
210 
211     private String _colorSchemeId;
212     private String _name;
213     private String _cssClass;
214     private String _colorSchemeImagesPath =
215         "${images-path}/color_schemes/${css-class}";
216     private boolean _defaultCs;
217     private Properties _settingsProperties = new SafeProperties();
218 
219 }