001: package net.xoetrope.builder.editor;
002:
003: import java.awt.Component;
004:
005: import net.xoetrope.xui.XPage;
006: import net.xoetrope.xui.style.XStyleFactory;
007: import net.xoetrope.builder.editor.components.swing.XMenuBarProxy;
008: import net.xoetrope.builder.editor.components.swing.XPanelProxy;
009: import java.awt.Container;
010: import net.xoetrope.xui.style.XStyle;
011: import net.xoetrope.builder.editor.components.swing.XComponentProxy;
012: import java.util.HashSet;
013: import net.xoetrope.xui.XComponentConstructor;
014: import javax.swing.JComponent;
015:
016: /**
017: * A special component factory to build customized objects for use in the editor
018: * <p>Copyright: Copyright (c) Xoetrope Ltd., 1998-2003</p>
019: * <p>Company: Xoetrope Ltd.</p>
020: */
021: public class XEditorComponentFactory extends XStyleFactory {
022: private static HashSet proxiedComponents;
023:
024: /**
025: * Create the StyleFactory and initialise with an XStyleManager.
026: * @param manager the XStyleManager to initialise with
027: */
028: public XEditorComponentFactory(String packageName) {
029: super (packageName);
030: if (proxiedComponents == null)
031: proxiedComponents = new HashSet();
032: }
033:
034: /**
035: * A generic factory for constructing XComponents.
036: * @param type a constant identifying the type of component to be created
037: * @param content the component text/content
038: */
039: public Component constructComponent(int type, String content) {
040: Component c = null;
041: if (type == XPage.XCOMBO) {
042: XComponentProxy xcp = new XComponentProxy();
043: xcp.setProxiedComponent(super .constructComponent(type,
044: content));
045: return xcp;
046: } else if (type == XPage.XLABEL) {
047: XComponentProxy xcp = new XComponentProxy();
048: xcp.setProxiedComponent(super .constructComponent(type,
049: content));
050: xcp.setText(content);
051: ((JComponent) xcp.getProxiedComponent()).setOpaque(true);
052: return xcp;
053: } else if (type == XPage.XPANEL) {
054: XPanelProxy comp = new XPanelProxy();
055: if (parentPanel == null)
056: parentPanel = (Container) comp;
057: return comp;
058: } else if (type == XPage.XMENUBAR) {
059: currentMenuBar = new XMenuBarProxy();
060: return (Component) currentMenuBar;
061: } else
062: return super .constructComponent(type, content);
063: }
064:
065: /**
066: * Apply a style to a component, looking up the style name if necessary
067: * @param c the component to style
068: * @param style the style name
069: * @param lookupComp true to lookup the style based on the component class
070: */
071: public void applyStyle(Component c, String style, boolean lookupComp) {
072: super .applyStyle(c, style, lookupComp);
073:
074: if ((style == null || style.length() == 0) && (c != null)) {
075: String compName = XEditorUtilities.getComponentClassName(c);
076: super .applyStyle(c, compName);
077: }
078: }
079:
080: /**
081: * A generic factory for adding registered components via the
082: * XComponentConstructor interface or component factories.
083: * @param type a name identifying the type of component to be created
084: * @param x the left coordinate
085: * @param y the top coordinate
086: * @param w the width
087: * @param h the height
088: * @param content the component text/content
089: */
090: protected Component buildRegisteredComponent(String type,
091: String content) {
092: int numFactories = componentFactories.size();
093: for (int i = 0; i < numFactories; i++) {
094: XComponentConstructor factory = (XComponentConstructor) componentFactories
095: .elementAt(i);
096: Component comp = factory.constructComponent(this , type,
097: content);
098: if (comp != null) {
099: if (isComponentProxied(comp)) {
100: XComponentProxy xcp = new XComponentProxy();
101: xcp.setProxiedComponent(comp);
102:
103: addComponent(xcp);
104: return xcp;
105: } else
106: addComponent(comp);
107: return comp;
108: }
109: }
110: return null;
111: }
112:
113: /**
114: * Check to see if this component should be proxied for use in the editor
115: * @param c the component
116: * @return true if it is to be proxied
117: */
118: protected boolean isComponentProxied(Component c) {
119: return proxiedComponents.contains(c.getClass());
120: }
121:
122: /**
123: * Adds a component to the list of proxied components
124: * @param c the component.
125: */
126: public static void proxyComponent(Class c) {
127: if (proxiedComponents == null)
128: proxiedComponents = new HashSet();
129: proxiedComponents.add(c);
130: }
131: }
|