001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package nextapp.echo2.app;
031:
032: import java.util.HashMap;
033: import java.util.Map;
034:
035: //TODO: Potential add removeStyle(), iterator() methods.
036:
037: /**
038: * A mutable implementation of a <code>StyleSheet</code>.
039: */
040: public class MutableStyleSheet implements StyleSheet {
041:
042: private Map namedStyleMap = new HashMap();
043: private Map defaultStyleMap = new HashMap();
044:
045: /**
046: * Adds a <code>Style</code> to the <code>StyleSheet</code>.
047: *
048: * @param componentClass the <code>Class</code> of the
049: * <code>Component</code> for which the style is to be used
050: * @param styleName the name of the style
051: * @param style the <code>Style</code> to be added
052: */
053: public void addStyle(Class componentClass, String styleName,
054: Style style) {
055: if (styleName == null) {
056: defaultStyleMap.put(componentClass, style);
057: } else {
058: Map styleMap = (Map) namedStyleMap.get(styleName);
059: if (styleMap == null) {
060: styleMap = new HashMap();
061: namedStyleMap.put(styleName, styleMap);
062: }
063: styleMap.put(componentClass, style);
064: }
065: }
066:
067: /**
068: * Adds the contents of another <code>StyleSheet</code> to this
069: * <code>StyleSheet</code>. Future changes to the added
070: * <code>StyleSheet</code> will not be reflected, unless it is re-added.
071: *
072: * @param styleSheet the <code>StyleSheet</code> to add
073: */
074: public void addStyleSheet(MutableStyleSheet styleSheet) {
075: namedStyleMap.putAll(styleSheet.namedStyleMap);
076: defaultStyleMap.putAll(styleSheet.defaultStyleMap);
077: }
078:
079: /**
080: * @see nextapp.echo2.app.StyleSheet#getStyle(java.lang.Class, java.lang.String)
081: */
082: public Style getStyle(Class componentClass, String styleName) {
083: if (styleName == null) {
084: // Retrieve generic style.
085: while (componentClass != Object.class) {
086: Style style = (Style) defaultStyleMap
087: .get(componentClass);
088: if (style != null) {
089: return style;
090: }
091: componentClass = componentClass.getSuperclass();
092: }
093: return null;
094: } else {
095: // Retrieve named style.
096: Map styleMap = (Map) namedStyleMap.get(styleName);
097: if (styleMap == null) {
098: return null;
099: }
100: while (componentClass != Object.class) {
101: Style style = (Style) styleMap.get(componentClass);
102: if (style != null) {
103: return style;
104: }
105: componentClass = componentClass.getSuperclass();
106: }
107: return null;
108: }
109: }
110: }
|