001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.providers.context;
006:
007: import com.sun.portal.log.common.PortalLogger;
008:
009: import java.util.Map;
010: import java.util.HashMap;
011: import java.util.Collection;
012: import java.util.ArrayList;
013: import java.util.Iterator;
014: import java.util.logging.Logger;
015: import java.util.logging.Level;
016:
017: /**
018: * A Theme is a collection of visual elements to be used when displaying a
019: * desktop. For example, channel background color, font face etc.
020: * Theme is a global setting (independent of channels) which a user can
021: * select from a preset list or customizing to his/her perference.
022: * The global preset list of themes is configured by the administrator and
023: * it cannot be changed using this API. The custom theme is the theme
024: * that each user can manipulate according to his/her perference. The
025: * constant CUSTOM_THEME defined in this class is to refer to the custom theme.
026: * <p>
027: * Themes are stored as properties accessed through <code>ProviderContext
028: * </code>. This class serves as a wrapper over <code>ProviderContext</code>
029: * to encapsulate the implementation details of theme.
030: * <p>
031: * For details of the theme design and steps to configure the global
032: * preset themes, please refer to the Desktop Customization Guide and the
033: * Administrator's Guide.
034: *
035: @see com.sun.portal.providers.context.ProviderContext
036: **/
037:
038: public class Theme {
039: private static Logger logger = PortalLogger.getLogger(Theme.class);
040:
041: static private final String USER_THEME = "UserTheme";
042: static private final String GLOBAL_THEMES = "GlobalThemes";
043:
044: /**
045: * Constant refering to the custom theme.
046: **/
047: static public final String CUSTOM_THEME = "CustomTheme";
048:
049: /**
050: * Gets the theme name to be used for displaying desktop.
051: *
052: * The theme can be one from the global preset theme list
053: * or CUSTOM_THEME for the custom built theme.
054: *
055: * @param ProviderContext
056: * @return the name of the selected theme name as a String.
057: * @exception ProviderContextException if an error occurs in getting the
058: * selected theme name.
059: **/
060:
061: static public String getSelectedName(String channel,
062: ProviderContext pc) throws ProviderContextException {
063: return pc.getStringProperty(channel, USER_THEME);
064: }
065:
066: /**
067: * Gets the global list of preset theme that is available.
068: *
069: * The list is configured by Portal admin. The list as well as the
070: * attributes in those preset themes cannot be changed using this API.
071: *
072: * @param ProviderContext
073: * @return the list of globally defined preset theme as a collection.
074: * @exception ProviderContextException if an error occurs in getting the
075: * global theme names.
076: **/
077:
078: static public Collection getGlobal(String channel,
079: ProviderContext pc) throws ProviderContextException {
080: return pc.getCollectionProperty(channel, GLOBAL_THEMES, true)
081: .keySet();
082: }
083:
084: /**
085: * Gets the attributes of a theme as a map.
086: *
087: * The returned map has the attribute names as the keys and the theme
088: * values as the values.
089: *
090: * @param ProviderContext
091: * @param theme Theme name, either one from the preset list or <code>
092: * CUSTOM_THEME</code> for the custom theme.
093: * @return The map that maps attribute names to attribute values of the theme.
094: * @exception ProviderContextException if an error occurs in getting the
095: * attributes of the theme.
096: **/
097:
098: static public Map getMap(String channel, ProviderContext pc,
099: String theme) throws ProviderContextException {
100:
101: Map themeMap = null;
102: Map themesMap = pc.getCollectionProperty(channel,
103: GLOBAL_THEMES, true);
104: if (theme.equals(CUSTOM_THEME)) {
105: themeMap = pc.getCollectionProperty(channel, theme);
106: } else {
107: themeMap = (Map) themesMap.get(theme);
108: }
109: if (themeMap == null) {
110: logger.fine("PSDT_CSPPCT0001");
111: for (Iterator i = themesMap.keySet().iterator(); i
112: .hasNext();) {
113: String key = (String) i.next();
114: themeMap = (Map) themesMap.get(key);
115: setSelectedName(channel, pc, key);
116: return themeMap;
117: }
118: }
119: return themeMap;
120: }
121:
122: /**
123: * Gets an attribute value of a theme.
124: *
125: *
126: * @param ProviderContext
127: * @param theme Theme name, either one from the preset list or <code>
128: * CUSTOM_THEME</code> for the custom theme.
129: * @param name Attribute name
130: * @return the attribute value as a String.
131: * @exception ProviderContextException if an error occurs getting the attribute, or
132: * the theme is not <code>CUSTOM_THEME</code> or one in the preset list.
133: **/
134:
135: static public String getAttribute(String channel,
136: ProviderContext pc, String theme, String name)
137: throws ProviderContextException {
138: return (String) getMap(channel, pc, theme).get(name);
139: }
140:
141: /**
142: * Gets an attribute value of a theme, if the attribute is not
143: * defined, and a default value is given, then returns the default value.
144: *
145: *
146: * @param ProviderContext
147: * @param theme Theme name, either one from the preset list or <code>
148: * CUSTOM_THEME</code> for the custom theme.
149: * @param name Attribute name
150: * @param def the default value
151: * @return the attribute value as a String.
152: * @exception ProviderContextException if an error occurs getting the attribute, or
153: * the theme is not <code>CUSTOM_THEME</code> or one in the preset list.
154: **/
155:
156: static public String getAttribute(String channel,
157: ProviderContext pc, String theme, String name, String def)
158: throws ProviderContextException {
159: String val = (String) getMap(channel, pc, theme).get(name);
160: if (val == null && def != null) {
161: val = def;
162: }
163: return val;
164: }
165:
166: /**
167: * Gets an attribute value of the selected theme.
168: *
169: *
170: * @param ProviderContext
171: * @param name Attribute name
172: * @return the attribute value as a String
173: * @exception ProviderContextException if an error occurs getting the attribute.
174: **/
175:
176: static public String getAttribute(String channel,
177: ProviderContext pc, String name)
178: throws ProviderContextException {
179: return (String) getMap(channel, pc,
180: getSelectedName(channel, pc)).get(name);
181: }
182:
183: /**
184: * Sets an attribute value of the custom theme.
185: *
186: * This sets a value for the theme named <code>CUSTOM_THEME</code>
187: *
188: * @param ProviderContext
189: * @param name Attribute name
190: * @param value Attribute value
191: **/
192:
193: static public void setCustomAttribute(String channel,
194: ProviderContext pc, String name, String value) {
195: try {
196: Map customThemeMap = pc.getCollectionProperty(channel,
197: CUSTOM_THEME);
198: customThemeMap.put(name, value);
199: pc.setCollectionProperty(channel, CUSTOM_THEME,
200: customThemeMap);
201: pc.allContentChanged();
202: } catch (ProviderContextException pce) {
203: logger.log(Level.INFO, "PSDT_CSPPCT0002", pce);
204: }
205: }
206:
207: /**
208: * Sets the theme to be used for displaying desktop.
209: *
210: * @param ProviderContext
211: * @param name Theme name, either one from the preset list or <code>
212: * CUSTOM_THEME</code> for the custom theme.
213: * @exception ProviderContextException if an error occurs in setting the
214: * selected theme name.
215: **/
216:
217: static public void setSelectedName(String channel,
218: ProviderContext pc, String name)
219: throws ProviderContextException {
220: pc.setStringProperty(channel, USER_THEME, name);
221: pc.allContentChanged();
222: }
223:
224: }
|