001: package com.xoetrope.awt;
002:
003: import com.xoetrope.awt.survey.XKioskQuestion;
004: import com.xoetrope.awt.survey.XQuestion;
005: import com.xoetrope.awt.survey.XWrapQuestion;
006: import java.util.Hashtable;
007:
008: import java.awt.Component;
009:
010: import net.xoetrope.xui.XComponentConstructor;
011: import net.xoetrope.xui.XComponentFactory;
012: import net.xoetrope.xui.XImageHolder;
013: import net.xoetrope.xui.XProjectManager;
014: import net.xoetrope.xui.XTextHolder;
015: import net.xoetrope.registry.ComponentAdapter;
016: import net.xoetrope.xui.XProject;
017:
018: /**
019: * @todo get rid of this class, all components should be added through the registry
020: * A factory class used to add the components available as part of the XForms
021: * package. These are: Question, WrapQuestion, KioskQuestion, ProgressBar, Date,
022: * DateChooser, Keypad, Money and EasterEgg.
023: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
024: * the GNU Public License (GPL), please see license.txt for more details. If
025: * you make commercial use of this software you must purchase a commercial
026: * license from Xoetrope.</p>
027: * <p>$Revision: 1.6 $</p>
028: */
029: public class XuiProComponentFactory implements XComponentConstructor {
030: private XQuestion lastQuestion;
031: private int nextValue = 0;
032: private String defaultQuestionFormat;
033: private String packageName = "net.xoetrope.awt";
034:
035: /**
036: * The owner project and the context in which this object operates.
037: */
038: protected XProject currentProject;
039:
040: /**
041: * @param proj The project to which the factory belongs
042: */
043: public XuiProComponentFactory(XProject project) {
044: currentProject = project;
045: update();
046: }
047:
048: public void update() {
049: try {
050: defaultQuestionFormat = currentProject
051: .getStartupParam("DefaultQuestionFormat");
052: } catch (Exception ex) {
053: }
054: if (defaultQuestionFormat == null)
055: defaultQuestionFormat = "Question";
056: }
057:
058: /**
059: * Set the package name for the factory's widgets.
060: * @param defPackage the package in which the widgets are defined
061: */
062: public void setPackageName(String defPackage) {
063: packageName = defPackage;
064: }
065:
066: /**
067: * Constructs XForms components on behalf of the component factory. At present
068: * the following types are supported
069: * @param cf the calling component factory
070: * @param type
071: * @param content
072: * @return The new Component
073: */
074: public Component constructComponent(XComponentFactory cf, int type,
075: String content) {
076: return null;
077: }
078:
079: /**
080: * Constructs XForms components on behalf of the component factory. At present
081: * the following types are supported
082: * <UL>
083: * <LI>ProgressBar</LI>
084: * </UL>
085: * @param cf the calling component factory
086: * @param type the component type name
087: * @param content the contents if applicable
088: * @return the new component
089: */
090: public Object constructComponent(XComponentFactory cf, String type,
091: String content) {
092: Component comp = null;
093: if (type.compareToIgnoreCase("Question") == 0) {
094: if (defaultQuestionFormat.compareTo("WrapQuestion") == 0)
095: comp = lastQuestion = new XWrapQuestion();
096: else if (defaultQuestionFormat.compareTo("KioskQuestion") == 0)
097: comp = lastQuestion = new XKioskQuestion();
098: if (comp == null)
099: comp = lastQuestion = new XQuestion();
100: ((XTextHolder) comp).setText(content);
101: nextValue = 0;
102: }
103: // else if ( type.compareToIgnoreCase( "ProgressBar" ) == 0 )
104: // comp = new XProgressBar();
105: // else if ( type.compareToIgnoreCase( "Date" ) == 0 )
106: // comp = new XDateEdit();
107: // else if ( type.compareToIgnoreCase( "DateChooser" ) == 0 )
108: // comp = new XDateChooser();
109: // else if ( type.compareToIgnoreCase( "Keypad" ) == 0 )
110: // comp = new XKeypad();
111: // else if ( type.compareToIgnoreCase( "Money" ) == 0 )
112: // comp = new XMoneyEdit();
113: // else if ( type.compareToIgnoreCase( "ImageButton" ) == 0 )
114: // comp = new XImageButton();
115: // else if ( type.compareToIgnoreCase( "Shape" ) == 0 )
116: // comp = new XShape();
117: // else if ( type.compareToIgnoreCase( "AWTSpinner" ) == 0 )
118: // comp = new XSpinner();
119: else if (type.compareToIgnoreCase("EasterEgg") == 0) {
120: comp = new XEasterEgg();
121: ((XImageHolder) comp).setImage(currentProject
122: .getImage(content));
123: }
124: return comp;
125: }
126:
127: /**
128: * A factory method for adding non component elements. This includes question
129: * response elements
130: * @param cf the calling component factory
131: * @param type the object type
132: * @param name a name identifying the element to be created
133: * @param content the component text/content
134: * @param attribs the element attributes if any
135: */
136: public Object addElement(XComponentFactory cf, String type,
137: String name, String content, Hashtable attribs) {
138: if (type.compareToIgnoreCase("Response") == 0)
139: lastQuestion.addResponse(new Integer((String) attribs
140: .get("id")).intValue(), content);
141: else if (type.compareToIgnoreCase("QuestionManager") == 0) {
142: int slots = new Integer((String) attribs.get("slots"))
143: .intValue();
144: int width = new Integer((String) attribs.get("w"))
145: .intValue();
146: int height = new Integer((String) attribs.get("h"))
147: .intValue();
148: for (int i = 0; i < slots; i++) {
149: Component comp = lastQuestion = (XQuestion) cf
150: .addComponent("Question", 0, 0, width, height,
151: "");
152: comp.setName(name);
153: ((XTextHolder) comp).setText(content);
154: lastQuestion.setAttribute("ref", "next");
155: }
156: lastQuestion.setLastQuestion(true);
157: nextValue = 0;
158: }
159:
160: return lastQuestion;
161: }
162:
163: /**
164: * Get the component adapter for this type
165: * @param type the name identifying the type
166: */
167: public ComponentAdapter getComponentAdapter(String type) {
168: return null;
169: }
170: }
|