001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package org.wings.plaf;
014:
015: import org.apache.commons.logging.Log;
016: import org.apache.commons.logging.LogFactory;
017: import org.wings.SComponent;
018: import org.wings.SIcon;
019: import org.wings.SLayoutManager;
020: import org.wings.session.Session;
021: import org.wings.session.SessionManager;
022: import org.wings.style.Style;
023: import org.wings.style.StyleSheet;
024:
025: import java.io.Serializable;
026:
027: /**
028: * The CGManager holds a reference to the current laf.
029: * It delegates to a session related CGDefaults table, that is backed by
030: * the laf's defaults.
031: */
032: public class CGManager implements Serializable {
033: private final transient static Log log = LogFactory
034: .getLog(CGManager.class);
035:
036: private LookAndFeel lookAndFeel;
037: private ResourceDefaults defaults = null;
038:
039: /**
040: * Get an Object from the defaults table.
041: * If the there's no value associated to the <code>key</code>, the request
042: * is delegated to the laf's defaults table.
043: *
044: * @param key the lookup key
045: * @param clazz the class of the object in question
046: */
047: public Object getObject(String key, Class clazz) {
048: return getDefaults().get(key, clazz);
049: }
050:
051: /**
052: * Get a ComponentCG from the defaults table.
053: * If the there's no value associated to the <code>target</code>'s cgClassID, the request
054: * is delegated to the laf's defaults table.
055: *
056: * @param target the SComponent
057: */
058: public ComponentCG getCG(SComponent target) {
059: return (ComponentCG) getDefaults().get(target.getClass(),
060: ComponentCG.class);
061: }
062:
063: /**
064: * Get a ComponentCG from the defaults table.
065: * If the there's no value associated to the <code>target</code>'s cgClassID, the request
066: * is delegated to the laf's defaults table.
067: *
068: * @param targetClass the SComponent class
069: */
070: public ComponentCG getCG(Class targetClass) {
071: return (ComponentCG) getDefaults().get(targetClass,
072: ComponentCG.class);
073: }
074:
075: /**
076: * Get a ComponentCG from the defaults table.
077: * If the there's no value associated to the <code>target</code>'s cgClassID, the request
078: * is delegated to the laf's defaults table.
079: *
080: * @param cgClassID the cg class id
081: */
082: public ComponentCG getCG(String cgClassID) {
083: return (ComponentCG) getDefaults().get(cgClassID,
084: ComponentCG.class);
085: }
086:
087: /**
088: * Get a LayoutManagerCG from the defaults table.
089: * If the there's no value associated to the <code>target</code>'s cgClassID, the request
090: * is delegated to the laf's defaults table.
091: *
092: * @param target the SLayoutManager
093: */
094: public LayoutCG getCG(SLayoutManager target) {
095: return (LayoutCG) getDefaults().get(target.getClass(),
096: LayoutCG.class);
097: }
098:
099: /**
100: * Get a StyleSheet from the defaults table.
101: * If the there's no value associated to the <code>key</code>, the request
102: * is delegated to the laf's defaults table.
103: *
104: * @param key the lookup key
105: */
106: public StyleSheet getStyleSheet(String key) {
107: return (StyleSheet) getDefaults().get(key, StyleSheet.class);
108: }
109:
110: /**
111: * Get a Style from the defaults table.
112: * If the there's no value associated to the <code>key</code>, the request
113: * is delegated to the laf's defaults table.
114: *
115: * @param key the lookup key
116: */
117: public Style getStyle(String key) {
118: return (Style) getDefaults().get(key, Style.class);
119: }
120:
121: /**
122: * Get a Icon from the defaults table.
123: * If the there's no value associated to the <code>key</code>, the request
124: * is delegated to the laf's defaults table.
125: *
126: * @param key the lookup key
127: */
128: public SIcon getIcon(String key) {
129: return (SIcon) getDefaults().get(key, SIcon.class);
130: }
131:
132: /**
133: * Set the defaults table.
134: *
135: * @param defaults the defaults table
136: */
137: public void setDefaults(ResourceDefaults defaults) {
138: this .defaults = defaults;
139: }
140:
141: /**
142: * Return the defaults table.
143: *
144: * @return the defaults table
145: */
146: public ResourceDefaults getDefaults() {
147: if (defaults == null) {
148: log.warn("defaults == null");
149: }
150: return defaults;
151: }
152:
153: /**
154: * Returns the current default look and feel.
155: *
156: * @return the current default look and feel
157: * @see #setLookAndFeel
158: */
159: public LookAndFeel getLookAndFeel() {
160: return lookAndFeel;
161: }
162:
163: /**
164: * Set the current default look and feel using a LookAndFeel object.
165: *
166: * @param newLookAndFeel the LookAndFeel object
167: * @see #getLookAndFeel
168: */
169: public void setLookAndFeel(LookAndFeel newLookAndFeel) {
170: lookAndFeel = newLookAndFeel;
171:
172: if (newLookAndFeel != null) {
173: setDefaults(newLookAndFeel.createDefaults());
174: // have the session fire a propertyChangeEvent regarding the new lookAndFeel
175: Session session = SessionManager.getSession();
176: if (session != null) {
177: session.setProperty("lookAndFeel", ""
178: + newLookAndFeel.hashCode());
179: }
180: } else {
181: log.warn("lookandfeel == null");
182: setDefaults(null);
183: }
184: }
185: }
|