001: package org.contineo.web;
002:
003: import net.sf.opentranquera.xkins.core.Path;
004: import net.sf.opentranquera.xkins.core.Skin;
005: import net.sf.opentranquera.xkins.core.Xkins;
006:
007: import org.contineo.web.util.Constants;
008:
009: import java.util.ArrayList;
010: import java.util.HashMap;
011: import java.util.Iterator;
012: import java.util.List;
013: import java.util.Map;
014:
015: import javax.faces.context.FacesContext;
016: import javax.faces.model.SelectItem;
017:
018: /**
019: * <p>
020: * The StyleBean class is the backing bean which manages the demonstrations'
021: * active theme. There are currently two themes supported by the bean; XP and
022: * Royale.
023: * </p>
024: * <p/>
025: * <p>
026: * The webpages' style attributes are modified by changing link in the header of
027: * the HTML document. The selectInputDate and tree components' styles are
028: * changed by changing the location of their image src directories.
029: * </p>
030: *
031: * @since 0.3.0
032: */
033: public class StyleBean {
034: // folder icons for the respective themes
035: public static final String XP_BRANCH_EXPANDED_ICON = "xmlhttp/css/xp/css-images/tree_folder_open.gif";
036: public static final String XP_BRANCH_CONTRACTED_ICON = "xmlhttp/css/xp/css-images/tree_folder_close.gif";
037: public static final String XP_SPACER_ICON = "xmlhttp/css/xp/css-images/spacer.gif";
038:
039: // possible theme choices
040: private final String XP = "xp";
041: private final String ROYALE = "royale";
042:
043: // default theme
044: private String currentStyle = XP;
045: private String tempStyle = XP;
046:
047: // available style list
048: private ArrayList styleList;
049:
050: // default theme image directory for selectinputdate and theme.
051: private String imageDirectory = "./xmlhttp/css/xp/css-images/";
052:
053: /**
054: * Creates a new instance of the StyleBean.
055: */
056: public StyleBean() {
057: // initialize the style list
058: styleList = new ArrayList();
059: styleList.add(new SelectItem(XP, XP));
060: styleList.add(new SelectItem(ROYALE, ROYALE));
061: }
062:
063: /**
064: * Gets the current style.
065: *
066: * @return current style
067: */
068: public String getCurrentStyle() {
069: return currentStyle;
070: }
071:
072: /**
073: * Sets the current style of the application to one of the predetermined
074: * themes.
075: *
076: * @param currentStyle
077: */
078: public void setCurrentStyle(String currentStyle) {
079: this .tempStyle = currentStyle;
080: }
081:
082: /**
083: * Gets the html needed to insert a valid css link tag.
084: *
085: * @return the tag information needed for a valid css link tag
086: */
087: public String getStyle() {
088: String contextPath = FacesContext.getCurrentInstance()
089: .getExternalContext().getRequestContextPath();
090:
091: return "<link rel=\"stylesheet\" type=\"text/css\" href=\""
092: + contextPath + "/xmlhttp/css/" + currentStyle + "/"
093: + currentStyle + ".css" + "\"></link>";
094: }
095:
096: /**
097: * Gets the image directory to use for the selectinputdate and tree theming.
098: *
099: * @return image directory used for theming
100: */
101: public String getImageDirectory() {
102: return imageDirectory;
103: }
104:
105: /**
106: * Applies temp style to to the current style and image directory and
107: * manually refreshes the icons in the navigation tree. The page will reload
108: * based on navigation rules to ensure the theme is applied; this is
109: * necessary because of difficulties encountered by updating the stylesheet
110: * reference within the <HEAD> of the document.
111: *
112: * @return the reload navigation attribute
113: */
114: public String changeStyle() {
115: currentStyle = tempStyle;
116: imageDirectory = "./xmlhttp/css/" + currentStyle
117: + "/css-images/";
118:
119: return "reload";
120: }
121:
122: /**
123: * Gets a list of available theme names that can be applied.
124: *
125: * @return available theme list
126: */
127: public List getStyleList() {
128: return styleList;
129: }
130:
131: public static String getPath(String name) {
132: FacesContext facesContext = FacesContext.getCurrentInstance();
133: Map application = facesContext.getExternalContext()
134: .getApplicationMap();
135: Xkins xs = (Xkins) application.get(Xkins.ATTR_SKINS);
136: Skin skin = xs.getSkin(facesContext.getExternalContext()
137: .getInitParameter(Constants.SKIN));
138:
139: return skin.getUrl() + skin.getPath(name).getUrl();
140: }
141:
142: public Map getPaths() {
143: FacesContext facesContext = FacesContext.getCurrentInstance();
144: Map application = facesContext.getExternalContext()
145: .getApplicationMap();
146: Xkins xs = (Xkins) application.get(Xkins.ATTR_SKINS);
147: Skin skin = xs.getSkin(facesContext.getExternalContext()
148: .getInitParameter(Constants.SKIN));
149: Map<String, Path> paths = (Map<String, Path>) skin.getPaths();
150: Map<String, String> fullPaths = new HashMap<String, String>();
151:
152: for (Iterator iter = paths.keySet().iterator(); iter.hasNext();) {
153: String name = (String) iter.next();
154: fullPaths.put(name, skin.getUrl()
155: + paths.get(name).getUrl());
156: }
157:
158: return fullPaths;
159: }
160:
161: public String getImagesPath() {
162: return StyleBean.getPath("images");
163: }
164:
165: public String getCssPath() {
166: return StyleBean.getPath("css");
167: }
168:
169: public static String getImagePath(String imageName) {
170: return StyleBean.getPath("images").substring(1) + "/"
171: + imageName;
172: }
173: }
|