001: package net.xoetrope.xui.style;
002:
003: import java.awt.BorderLayout;
004: import java.awt.CardLayout;
005: import java.awt.Component;
006: import java.awt.Container;
007: import java.awt.FlowLayout;
008: import java.awt.GridLayout;
009: import java.awt.LayoutManager;
010:
011: import net.xoetrope.xui.XComponentFactory;
012: import net.xoetrope.xui.XPage;
013: import net.xoetrope.xui.XProjectManager;
014: import java.util.Hashtable;
015: import net.xoetrope.xui.XLayoutHelper;
016:
017: /**
018: * Extends the basic component factory by adding style information.
019: * As the component are added the styles are applied.
020: * <p>Copyright: Copyright (c) Xoetrope Ltd., 1998-2003<br>
021: * License: see license.txt
022: * $Revision: 1.13 $
023: */
024: public class XStyleFactory extends XComponentFactory {
025:
026: protected XStyleManager styleManager;
027:
028: /**
029: * Create the StyleFactory and initialise with an XStyleManager.
030: * @param manager the XStyleManager to initialise with
031: */
032: public XStyleFactory(String packageName) {
033: super (packageName);
034: setup();
035: }
036:
037: /**
038: * moved from the ctor so as to allow subclassed XStyleFactories to do the
039: * initialisation in the getInstance().
040: */
041: protected void setup() {
042: styleManager = XProjectManager.getStyleManager();
043: }
044:
045: /**
046: * Pass the addNamedComponent call to the XStyleFactory base Class
047: * @param type The type of object being constructed
048: * @param constraint the layout constraint
049: * @param txt The content to display
050: * @param style The name of the style to apply
051: * @deprecated since 1.0.4
052: */
053: public Component addComponent(String type, Object constraint,
054: String txt, String style) {
055: Component c = super .addComponent(type, constraint, txt);
056: applyStyle(c, style);
057: return c;
058: }
059:
060: /**
061: * Pass the addNamedComponent call to the XStyleFactory base Class
062: * @param type The type of object being constructed
063: * @param x The x coordinate of the new component
064: * @param y The y coordinate of the new component
065: * @param w The width of the new component
066: * @param h The height of the new component
067: * @param style The name of the style to apply
068: */
069: public Component addComponent(String type, int x, int y, int w,
070: int h, String txt, String style) {
071: Component c = super .addComponent(type, x, y, w, h, txt);
072: applyStyle(c, style);
073: return c;
074: }
075:
076: /**
077: * Overloading method with the same signature in XComponentFactory.
078: * @param type The type of object being constructed
079: * @param x The x coordinate of the new component
080: * @param y The y coordinate of the new component
081: * @param w The width of the new component
082: * @param h The height of the new component
083: * @param txt The text/caption of the component
084: */
085: public Component addComponent(String type, int x, int y, int w,
086: int h, String txt) {
087: return addComponent(type, x, y, w, h, txt, null);
088: }
089:
090: /**
091: * Pass the addComponent call to the XStyleFactory base Class
092: * @param className The name of the class to be constructed
093: * @param x The x coordinate of the new component
094: * @param y The y coordinate of the new component
095: * @param w The width of the new component
096: * @param h The height of the new component
097: * @param style The name of the style to apply
098: */
099: public Component addClass(String className, int x, int y, int w,
100: int h, String style) {
101: Component c = super .addClass(className, x, y, w, h);
102: applyStyle(c, style);
103: return c;
104: }
105:
106: /**
107: * Overloading the method with the same signature in XComponentFactory.
108: * Applies a default style for the component.
109: * @param className The name of the class to be constructed
110: * @param x The x coordinate of the new component
111: * @param y The y coordinate of the new component
112: * @param w The width of the new component
113: * @param h The height of the new component
114: */
115: public Component addClass(String className, int x, int y, int w,
116: int h) {
117: Component c = super .addClass(className, x, y, w, h);
118: applyStyle(c, (String) null);
119: return c;
120: }
121:
122: /**
123: * Pass the addNamedComponent call to the XStyleFactory base Class
124: * @param type The type of object being constructed
125: * @param x The x coordinate of the new component
126: * @param y The y coordinate of the new component
127: * @param w The width of the new component
128: * @param h The height of the new component
129: * @param style The name of the style to apply
130: */
131: public Component addComponent(String type, int x, int y, int w,
132: int h) {
133: Component c = super .addComponent(type, x, y, w, h, null);
134: applyStyle(c, (String) null);
135: return c;
136: }
137:
138: /**
139: * Sets a LayoutManager for the panel
140: * @param cont the container whose layout manager is being set or null to set the parent panel's layout manager
141: * @param type the layout manager as defined in the XLayoutHelper class
142: * @param style the style containing the layout manager attributes
143: */
144: public LayoutManager addLayout(Container cont, int type,
145: Hashtable attribs) {
146: if (cont == null)
147: cont = parentPanel;
148: LayoutManager lm = layoutHelper.addLayout(cont, type, attribs);
149: return lm;
150: }
151:
152: /**
153: * Apply a style to a component, looking up the style name if necessary
154: * @param c the component to style
155: * @param style the style name
156: * @param lookupComp true to lookup the style based on the component class
157: */
158: public void applyStyle(Component c, String style, boolean lookupComp) {
159: boolean createStyle = true;
160: if (style == null) {
161: lookupComp = true;
162: createStyle = false;
163: style = "";
164: }
165: if ((c != null) && (style != null)) {
166: String type = "";
167: if (lookupComp) {
168: type = c.getClass().getName();
169: int pos = type.lastIndexOf(".");
170: type = type.substring(pos + 1, type.length());
171: }
172:
173: XStyle xstyle = styleManager.getStyle(checkStyleName(style,
174: type, lookupComp), createStyle);
175: if (xstyle != null) {
176: c.setFont(styleManager.getFont(xstyle));
177: c.setBackground(xstyle
178: .getStyleAsColor(XStyle.COLOR_BACK));
179: c.setForeground(xstyle
180: .getStyleAsColor(XStyle.COLOR_FORE));
181:
182: if (c instanceof XStyleComponent)
183: ((XStyleComponent) c).setStyle(style);
184: }
185: }
186: }
187:
188: /**
189: * Apply a style to a component. Set the font and foreground/background color.
190: * @param c the component to style
191: * @param style the style name
192: */
193: public void applyStyle(Component c, XStyle xstyle) {
194: if ((c != null) && (xstyle != null)) {
195: c.setFont(styleManager.getFont(xstyle));
196: c.setBackground(xstyle.getStyleAsColor(XStyle.COLOR_BACK));
197: c.setForeground(xstyle.getStyleAsColor(XStyle.COLOR_FORE));
198: }
199: }
200:
201: /**
202: * Called after a new component is created. Append the style to the base and
203: * then append the component classname with '/' delimiters.
204: * @param c Component to apply the style to.
205: * @param style the name of the style to apply.
206: *
207: */
208: public void applyStyle(Component c, String style) {
209: applyStyle(c, style, false);
210: }
211:
212: /**
213: * Check that the style name is fully qualified with the 'base' name and
214: * add the type name if needed.
215: * @param style the style name
216: * @param type the componnet type name
217: * @param lookupComp append the style name to the type if true
218: * @return the fully qualified style name
219: */
220: private String checkStyleName(String style, String type,
221: boolean lookupComp) {
222: String retStyle = "";
223: if (style != null)
224: retStyle = style;
225:
226: if (lookupComp)
227: retStyle += type;
228: return retStyle;
229: }
230: }
|