001: package com.xoetrope.carousel.builder;
002:
003: import java.util.HashSet;
004: import java.util.Enumeration;
005:
006: import java.awt.Component;
007: import java.awt.Container;
008:
009: import net.xoetrope.editor.XEditorUtilities;
010: import net.xoetrope.editor.project.pages.components.proxy.XComponentProxy;
011: import net.xoetrope.editor.project.pages.components.proxy.XMenuBarProxy;
012: import net.xoetrope.editor.project.pages.components.proxy.XPanelProxy;
013: import net.xoetrope.xui.XComponentConstructor;
014: import net.xoetrope.xui.XProject;
015: import net.xoetrope.xui.style.XStyleFactory;
016:
017: /**
018: * A special component factory to build customized objects for use in the editor
019: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
020: * the GNU Public License (GPL), please see license.txt for more details. If
021: * you make commercial use of this software you must purchase a commercial
022: * license from Xoetrope.</p>
023: * <p> $Revision: 1.2 $</p>
024: */
025: public class XEditorHtmlComponentFactory extends XStyleFactory {
026: private static HashSet proxiedComponents;
027:
028: /**
029: * Create the StyleFactory and initialise with an XStyleManager.
030: * @param manager the XStyleManager to initialise with
031: */
032: public XEditorHtmlComponentFactory(XProject project,
033: String packageName) {
034: super (project, packageName);
035: if (proxiedComponents == null)
036: proxiedComponents = new HashSet();
037: }
038:
039: /**
040: * A generic factory for constructing XComponents.
041: * @param type a constant identifying the type of component to be created
042: * @param content the component text/content
043: */
044: public Object constructComponent(String type, String content) {
045: int id = getTypeCode(type);
046: Component c = null;
047: // if (( id == XLABEL ) || ( id == XEDIT ) /*|| ( type == XPage.XBUTTON )*/) {
048: // XComponentProxy xcp = new XComponentProxy();
049: // xcp.setProxiedComponent( (Component)super.constructComponent( type, content ));
050: // xcp.setText( content );
051: // return xcp;
052: // }
053: if (id == XPANEL) {
054: XPanelProxy comp = new XPanelProxy();
055: if (parentPanel == null)
056: parentPanel = (Container) comp;
057: return comp;
058: } else if (id == XMENUBAR) {
059: currentMenuBar = new XMenuBarProxy();
060: return (Component) currentMenuBar;
061: }
062: // else {
063: // c = (Component)super.constructComponent( type, content );
064: // if (( c == null ) && ( id == XUNKNOWN ))
065: // c = new XUnknown();
066: // else if ( !(( id == XIMAGE ) || ( id == XIMAGEMAP ) || ( id == XCOMBO ) || ( id == XHOTSPOTIMAGE )))
067: // return c;
068: //
069: // XComponentProxy xcp = new XComponentProxy();
070: // xcp.setProxiedComponent( c );
071: // return xcp;
072: // }
073: return super .constructComponent(type, content);
074: }
075:
076: /**
077: * Apply a style to a component, looking up the style name if necessary
078: * @param c the component to style
079: * @param style the style name
080: * @param lookupComp true to lookup the style based on the component class
081: */
082: public void applyStyle(Component c, String style, boolean lookupComp) {
083: if (c instanceof XComponentProxy)
084: c = ((XComponentProxy) c).getProxiedComponent();
085:
086: super .applyStyle(c, style, lookupComp);
087:
088: if ((style == null || style.length() == 0) && (c != null)) {
089: String compName = XEditorUtilities.getComponentClassName(c);
090: super .applyStyle(c, compName);
091: }
092: }
093:
094: /**
095: * Called after a new component is created. Append the style to the base and
096: * then append the component classname with '/' delimiters.
097: * @param c Component to apply the style to.
098: * @param style the name of the style to apply.
099: *
100: */
101: public void applyStyle(Object c, String style) {
102: if (c instanceof XComponentProxy)
103: c = ((XComponentProxy) c).getProxiedComponent();
104: super .applyStyle(c, style);
105: }
106:
107: /**
108: * A generic factory for creating registered components via the
109: * XComponentConstructor interface or component factories.
110: * @param type a name identifying the type of component to be created
111: * @param x the left coordinate
112: * @param y the top coordinate
113: * @param w the width
114: * @param h the height
115: * @param content the component text/content
116: */
117: protected Object buildRegisteredComponent(String type,
118: String content) {
119: Enumeration enumeration = componentFactories.keys();
120: while (enumeration.hasMoreElements()) {
121: XComponentConstructor factory = (XComponentConstructor) componentFactories
122: .get(enumeration.nextElement());
123: Object comp = factory.constructComponent(this , type,
124: content);
125: if (comp != null) {
126: if (isComponentProxied(comp)) {
127: XComponentProxy xcp = new XComponentProxy();
128: xcp.setProxiedComponent((Component) comp);
129:
130: return xcp;
131: }
132: return comp;
133: }
134: }
135: return null;
136: }
137:
138: /**
139: * Check to see if this component should be proxied for use in the editor
140: * @param c the component
141: * @return true if it is to be proxied
142: */
143: protected boolean isComponentProxied(Object c) {
144: return proxiedComponents.contains(c.getClass());
145: }
146:
147: /**
148: * Adds a component to the list of proxied components
149: * @param c the component.
150: */
151: public static void proxyComponent(Class c) {
152: if (proxiedComponents == null)
153: proxiedComponents = new HashSet();
154: if (!proxiedComponents.contains(c))
155: proxiedComponents.add(c);
156: }
157: }
|