001: package fr.aliacom.form.common;
002:
003: import org.apache.log4j.Logger;
004:
005: import fr.aliacom.common.ui.IFrame;
006: import fr.aliacom.common.ui.IInternalFrame;
007: import fr.aliacom.common.ui.ITopLevelFrame;
008:
009: /**
010: * This class is used by client code to create UI from xml files. It is
011: * responsible of parsing the XML files, loading the data in the UI and pushing
012: * the UI to the screen.
013: *
014: * client code gets instance of this class using
015: * <code>FormManager.getInstance()</code>
016: *
017: * @author tom
018: *
019: * (C) 2001, 2003 Thomas Cataldo
020: */
021: public final class FormManager {
022:
023: private Toolkit tk;
024: private IFormParser parser;
025: private IFrame aliaFrame;
026: private int formCount;
027:
028: private static FormManager singleton;
029: private static final Logger log = Logger
030: .getLogger(FormManager.class);
031:
032: static {
033: singleton = new FormManager();
034: }
035:
036: /**
037: * This method is the only safe way to get a reference
038: * on the formManager
039: * @return FormManager
040: */
041: public static FormManager getInstance() {
042: return singleton;
043: }
044:
045: private FormManager() {
046: tk = ToolkitManager.getToolkit();
047: parser = tk.getNewParser();
048: formCount = 0;
049: }
050:
051: public void setMainWindow(IFrame f) {
052: this .aliaFrame = f;
053: }
054:
055: public IFrame getMainWindow() {
056: return aliaFrame;
057: }
058:
059: public synchronized IForm createForm(String formXmlFile,
060: FormContext ctx) {
061: return createForm(null, formXmlFile, ctx);
062: }
063:
064: public synchronized IForm createForm(IForm form,
065: String formXmlFile, FormContext ctx) {
066: try {
067: IForm f = parser.parse(form, formXmlFile, ctx);
068: f.setManager(this );
069: parser.reset();
070:
071: final IInternalFrame jif = tk.newInternalFrame(f);
072: f.setFrame(jif);
073: jif.setContentPane(f);
074: jif.pack();
075: jif.setVisible(true);
076: formCount++;
077: tk.asyncExec(new AsyncLoader(f, tk));
078: return f;
079: } catch (Exception e) {
080: e.printStackTrace();
081: System.exit(1);
082: }
083: return null;
084: }
085:
086: public synchronized IFormComponent createComponent(IForm f,
087: IFormComponent parent, String formXmlFile,
088: FormContext newVars) {
089: IFormComponent ret = parser.parse(f, parent, formXmlFile,
090: newVars);
091: return ret;
092: }
093:
094: public synchronized IForm createTopLevel(String formXmlFile,
095: FormContext ctx) {
096: try {
097: IForm f = parser.parse(null, formXmlFile, ctx);
098: f.setManager(this );
099: parser.reset();
100:
101: ITopLevelFrame jif = tk.newTopLevelFrame(f);
102: f.setFrame(jif);
103: jif.setContentPane(f);
104: jif.pack();
105: jif.setResizable(false);
106: jif.setVisible(true);
107: formCount++;
108: tk.asyncExec(new AsyncLoader(f, tk));
109: return f;
110: } catch (Exception e) {
111: e.printStackTrace();
112: System.exit(1);
113: }
114: return null;
115: }
116:
117: /**
118: * This is called by the close method in <code>IForm</code> implementations.
119: *
120: * @param f the form to close.
121: */
122: public void dispose(IForm f) {
123: log.debug("FormManager::dispose(form)");
124: if (--formCount == 0) {
125: ToolkitManager.getToolkit().stop();
126: }
127: log.debug("FormCount=" + formCount);
128: f.getFrame().dispose();
129: }
130:
131: /**
132: * Asynchronously load values from the FormContext in a form.
133: *
134: * @author tom
135: *
136: * (C) 2001, 2003 Thomas Cataldo
137: */
138: private final class AsyncLoader implements Runnable {
139: private IForm f;
140: private Toolkit tk;
141:
142: public AsyncLoader(IForm f, Toolkit tk) {
143: this .f = f;
144: this .tk = tk;
145: }
146:
147: public void run() {
148: f.getLoader().load();
149: try {
150: f.load();
151: } catch (LoadException le) {
152: tk.handleError(f, le);
153: }
154: }
155:
156: }
157:
158: }
|