001: package net.xoetrope.optional.registry;
002:
003: import net.xoetrope.xui.XComponentConstructor;
004: import net.xoetrope.xui.XComponentFactory;
005: import java.awt.Component;
006: import net.xoetrope.xui.XResourceManager;
007: import java.io.Reader;
008: import net.xoetrope.xui.XProjectManager;
009: import net.xoetrope.xml.XmlElement;
010: import net.xoetrope.xml.XmlSource;
011: import net.xoetrope.xui.build.BuildProperties;
012: import net.xoetrope.debug.DebugLogger;
013: import java.util.Vector;
014: import java.util.Hashtable;
015:
016: /**
017: * A component factory that builds components based upon some XML configuration
018: * files.
019: * <p> Copyright (c) Xoetrope Ltd., 2002-2004</p>
020: * <p> $Revision: 1.7 $</p>
021: * <p> License: see License.txt</p>
022: */
023: public class XRegisteredComponentFactory implements
024: XComponentConstructor {
025: protected Hashtable propertiesRegister;
026:
027: public XRegisteredComponentFactory() {
028: propertiesRegister = new Hashtable();
029: read();
030: }
031:
032: /**
033: * A generic factory for adding XComponents. The component is constructed, positioned and
034: * added to the parent panel if one exists. The component is named with a counter value
035: * to uniquely identify the control.
036: * When a ScrollPane is addd it becomes the parent.
037: * @param cf the calling component factory
038: * @param type a constant identifying the type of component to be created
039: * @param content the component text/content
040: */
041: public Component constructComponent(XComponentFactory cf, int type,
042: String content) {
043: throw new UnsupportedOperationException();
044: }
045:
046: /**
047: * A generic factory for adding XComponents. The component is constructed, positioned and
048: * added to the parent panel if one exists. The component is named with a counter value
049: * to uniquely identify the control.
050: * When a ScrollPane is addd it becomes the parent.
051: * @param cf the calling component factory
052: * @param type a name identifying the type of component to be created
053: * @param content the component text/content
054: */
055: public Component constructComponent(XComponentFactory cf,
056: String type, String content) {
057: Component comp = null;
058:
059: ComponentAdapter componentDescription = (ComponentAdapter) propertiesRegister
060: .get(type);
061: try {
062: comp = (Component) componentDescription.clazz.newInstance();
063: componentDescription.setProperty(comp, "content", content,
064: "String");
065: } catch (IllegalAccessException ex) {
066: ex.printStackTrace();
067: } catch (InstantiationException ex) {
068: ex.printStackTrace();
069: }
070:
071: return comp;
072: }
073:
074: /**
075: * A generic factory method for adding non component elements.
076: * @param cf the calling component factory
077: * @param type the object type
078: * @param name a name identifying the element to be created
079: * @param content the component text/content
080: * @param attribs the element attributes if any
081: */
082: public Object addElement(XComponentFactory cf, String type,
083: String name, String content, Hashtable attribs) {
084: throw new UnsupportedOperationException();
085: }
086:
087: /**
088: * Notify the component factories that some of their settings may have changed
089: */
090: public void update() {
091: throw new UnsupportedOperationException();
092: }
093:
094: /**
095: * Set the package name for the factory's widgets. This factory ignores the
096: * default package name as the classes/componentes being instantiated can come
097: * from different packages and their package must be fully specified.
098: */
099: public void setPackageName(String defPackage) {
100: throw new UnsupportedOperationException();
101: }
102:
103: /**
104: * Get the adapter for a particular component type. The adapter can be used
105: * to get and set properties of the component.
106: * @param type the name by which the component is specified and referenced
107: * @return the adapter
108: */
109: public ComponentAdapter getComponentAdapter(String type) {
110: return (ComponentAdapter) propertiesRegister.get(type);
111: }
112:
113: /**
114: * Read the component registry. The format is described in the components.xsd
115: * schema.
116: */
117: protected void read() {
118: String registryFile = XProjectManager.getCurrentProject()
119: .getStartupParam("ComponentRegistry");
120: if ((registryFile == null) || (registryFile.length() == 0))
121: registryFile = "components";
122:
123: try {
124: Reader r = XResourceManager.getBufferedReader(registryFile
125: + ".xml", null);
126: read(r);
127: } catch (Exception ex) {
128: ex.printStackTrace();
129: }
130: }
131:
132: /**
133: * Read the component registry. The format is described in the components.xsd
134: * schema.
135: * @param reader the reader from which to read the file
136: */
137: public void read(Reader reader) {
138: try {
139: XmlElement regRoot = XmlSource.read(reader);
140:
141: Vector componentNodes = regRoot.getChildren();
142: int numComponents = componentNodes.size();
143: for (int i = 0; i < numComponents; i++) {
144: XmlElement componentNode = (XmlElement) componentNodes
145: .elementAt(i);
146: try {
147: String componentName = componentNode
148: .getAttribute("name");
149: ComponentAdapter ca = new ComponentAdapter(
150: componentNode.getAttribute("class"),
151: componentNode.getAttribute("ui"));
152: Vector propertyNodes = componentNode.getChildren();
153: int numProperties = propertyNodes.size();
154: for (int j = 0; j < numProperties; j++) {
155: XmlElement propertyNode = (XmlElement) componentNode
156: .elementAt(j);
157: String type = propertyNode.getAttribute("type");
158: String name = propertyNode.getAttribute("name");
159: String method = propertyNode
160: .getAttribute("method");
161: String attribStr = propertyNode
162: .getAttribute("attrib");
163: boolean attributed = ((attribStr != null) && (attribStr
164: .equals("true")));
165: if ((method == null) || (method.length() == 0))
166: method = type
167: + name.substring(0, 1)
168: .toUpperCase()
169: + name.substring(1);
170: String paramType = "String";
171: if (type.equals("set") || type.equals("both")) {
172: Vector params = propertyNode.getChildren();
173: if (params.size() > 0)
174: paramType = ((XmlElement) params
175: .elementAt(0))
176: .getAttribute("type");
177: }
178: ca.addProperty(type, name, method, paramType,
179: attributed);
180: }
181: propertiesRegister.put(componentName, ca);
182: } catch (Exception ex) {
183: ex.printStackTrace();
184: }
185: }
186: } catch (Exception ex) {
187: if (BuildProperties.DEBUG)
188: DebugLogger
189: .logError("Unable to setup the component registry");
190: }
191: }
192: }
|