Source Code Cross Referenced for XulBuilder.java in  » XML-UI » xui32 » com » xoetrope » builder » mozilla » xul » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » XML UI » xui32 » com.xoetrope.builder.mozilla.xul 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.xoetrope.builder.mozilla.xul;
002:
003:        import java.io.Reader;
004:        import java.util.Enumeration;
005:        import java.util.Hashtable;
006:        import java.util.Vector;
007:
008:        import java.awt.Component;
009:        import java.awt.Container;
010:
011:        import net.xoetrope.builder.XuiBuilder;
012:        import net.xoetrope.debug.DebugLogger;
013:        import net.xoetrope.xml.XmlElement;
014:        import net.xoetrope.xml.XmlSource;
015:        import net.xoetrope.xui.XPage;
016:        import net.xoetrope.xui.PageSupport;
017:        import net.xoetrope.xui.XProject;
018:        import net.xoetrope.xui.XRadioButtonGroup;
019:        import net.xoetrope.xui.build.BuildProperties;
020:        import net.xoetrope.xui.data.XModel;
021:        import net.xoetrope.swing.XPanel;
022:        import net.xoetrope.swing.XEdit;
023:        import net.xoetrope.swing.XPassword;
024:        import net.xoetrope.swing.XTextArea;
025:        import java.awt.GridLayout;
026:        import com.xoetrope.swing.XSlider;
027:        import net.xoetrope.swing.XButton;
028:        import javax.swing.BorderFactory;
029:        import java.awt.Color;
030:        import javax.swing.JComponent;
031:        import net.xoetrope.xui.style.XStyle;
032:
033:        /**
034:         * An experimental build for Mozilla's XUL format
035:         * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
036:         * the GNU Public License (GPL), please see license.txt for more details. If
037:         * you make commercial use of this software you must purchase a commercial
038:         * license from Xoetrope.</p>
039:         * <p> $Revision: 1.7 $</p>
040:         */
041:        public class XulBuilder extends XuiBuilder {
042:            public static final String XFORMS_XMLNS = "http://www.w3.org/2002/xforms";
043:            public static final String XHTML_XMLNS = "http://www.w3.org/1999/xhtml";
044:            public static final String XEVENTS_XMLNS = "http://www.w3.org/2001/xml-events";
045:
046:            public static final int BOX = 0;
047:            public static final int DESCRIPTION = 1;
048:            public static final int INPUT = 2;
049:            public static final int SECRET = 3;
050:            public static final int TEXTAREA = 4;
051:            public static final int MODEL = 5;
052:            public static final int GROUP = 6;
053:            public static final int RANGE = 7;
054:            public static final int UPLOAD = 8;
055:            public static final int TRIGGER = 9;
056:            public static final int SUBMIT = 10;
057:
058:            private static Hashtable formTags = null;
059:
060:            protected String packageName;
061:
062:            protected String selectStyle = XPage.RADIO;
063:
064:            public XulBuilder(XProject project) {
065:                super (project);
066:                setupFormTags();
067:            }
068:
069:            /**
070:             * Read an XML description of the page and construct a new XPage. An instance
071:             * of the class specified by the class attribute is constructed or else an
072:             * instance of XPage if no class attribute is specified. The new page is
073:             * populated but is not yet added to its parent.
074:             * <br>
075:             * The startup file parameter 'DefaultClass' is used to obtain a default for
076:             * each page's class if a class parameter is not specified in the page's XML
077:             * <br>
078:             * The startup file parameter 'Validations' is used to obtain a default for
079:             * each page's set of validation rules
080:             *
081:             * @param reader a input stream from which to read the page
082:             * @param pageName the name of the page
083:             * @param include the page to be loaded is being included in another page
084:             * @return the page
085:             */
086:            public PageSupport readPage(Reader reader, String pageName,
087:                    boolean include) {
088:                XmlElement model = XmlSource.read(reader);
089:                setupPage(model, pageName, include);
090:                setupModel(pageName);
091:
092:                String namespace = model.getNamespace();
093:
094:                if ((namespace == null)
095:                        || namespace.toLowerCase().equals(XFORMS_XMLNS))
096:                    readForm(model);
097:                else if (namespace.toLowerCase().equals(XHTML_XMLNS))
098:                    readHtml(model);
099:                else if (namespace.toLowerCase().equals(XEVENTS_XMLNS))
100:                    readEvents(model);
101:
102:                page.doLayout();
103:                return page;
104:            }
105:
106:            /**
107:             * Read a form element
108:             * @param model the XML
109:             * @return true if the children require further processing
110:             */
111:            protected void readForm(XmlElement model) {
112:                Component parent = (Component) componentFactory
113:                        .getParentComponent();
114:                try {
115:                    String typeName = model.getName();
116:
117:                    Object obj = formTags.get(typeName);
118:                    if (obj != null) {
119:                        int tagId = ((Integer) obj).intValue();
120:
121:                        switch (tagId) {
122:                        case BOX:
123:                            readBox(model);
124:                            break;
125:                        case DESCRIPTION:
126:                            readDescription(model);
127:                            break;
128:                        case INPUT:
129:                            readInput(model);
130:                            break;
131:                        case SECRET:
132:                            readSecret(model);
133:                            break;
134:                        case TEXTAREA:
135:                            readTextArea(model);
136:                            break;
137:                        case MODEL:
138:                            readModel(model);
139:                            break;
140:                        case GROUP:
141:                            readGroup(model);
142:                            break;
143:                        case RANGE:
144:                            readRange(model);
145:                            break;
146:                        case UPLOAD:
147:                            readUpload(model);
148:                            break;
149:                        case TRIGGER:
150:                            readTrigger(model);
151:                            break;
152:                        case SUBMIT:
153:                            readSubmit(model);
154:                            break;
155:                        }
156:                    } else
157:                        addComponent(typeName, model);
158:                } catch (Exception ex) {
159:                }
160:
161:                // Reset the parent component to its state prior to this call
162:                componentFactory.setParentComponent(parent);
163:            }
164:
165:            /**
166:             * Read html elements
167:             * @param model the XHTML
168:             * @return true if the children require further processing
169:             */
170:            protected boolean readHtml(XmlElement model) {
171:                Vector componentNodes = model.getChildren();
172:                int numNodes = componentNodes.size();
173:                for (int i = 0; i < numNodes; i++) {
174:                    XmlElement childNode = (XmlElement) componentNodes
175:                            .elementAt(i);
176:                    if ((childNode != null)
177:                            && (childNode.getNamespace().equals(XFORMS_XMLNS) || childNode
178:                                    .getNamespace().equals(XEVENTS_XMLNS)))
179:                        readForm(childNode);
180:                    else
181:                        readHtml(childNode);
182:                }
183:
184:                return true;
185:            }
186:
187:            /**
188:             * Read html elements
189:             * @param model the XHTML
190:             * @return true if the children require further processing
191:             */
192:            protected boolean readEvents(XmlElement model) {
193:                Vector componentNodes = model.getChildren();
194:                int numNodes = componentNodes.size();
195:                for (int i = 0; i < numNodes; i++) {
196:                    XmlElement childNode = (XmlElement) componentNodes
197:                            .elementAt(i);
198:                    if ((childNode != null)
199:                            && (childNode.getNamespace().equals(XFORMS_XMLNS)))
200:                        readForm(childNode);
201:                    else
202:                        readHtml(childNode);
203:                }
204:
205:                return true;
206:            }
207:
208:            /**
209:             * Read a select node
210:             * @param selectNode the xml representing the select item
211:             */
212:            protected void readBox(XmlElement boxNode) {
213:                XPanel panel = (XPanel) addComponent("panel", boxNode);
214:                componentFactory.setParentComponent(panel);
215:                String orientAttrib = boxNode.getAttribute("orient");
216:                if (orientAttrib.equals("horizontal"))
217:                    panel.setLayout(new GridLayout(1, 0));
218:                else
219:                    panel.setLayout(new GridLayout(0, 1));
220:                panel
221:                        .setBorder(BorderFactory
222:                                .createLineBorder(Color.lightGray));
223:
224:                Vector componentNodes = boxNode.getChildren();
225:                int numNodes = componentNodes.size();
226:                for (int i = 0; i < numNodes; i++) {
227:                    XmlElement childNode = (XmlElement) componentNodes
228:                            .elementAt(i);
229:                    if (childNode != null)
230:                        readForm(childNode);
231:                }
232:            }
233:
234:            protected XModel readItemset(XmlElement itemsetNode) {
235:                String model = "XForms/" + itemsetNode.getAttribute("model");
236:                String nodeSet = itemsetNode.getAttribute("nodeset");
237:                if (nodeSet.charAt(0) == '/')
238:                    nodeSet = nodeSet.substring(1);
239:
240:                XModel modelNode = (XModel) currentProject.getModel()
241:                        .get(model);
242:                return (XModel) modelNode.get(nodeSet);
243:            }
244:
245:            protected XModel readItem(XmlElement itemsetNode) {
246:                String model = "XForms/" + itemsetNode.getAttribute("model");
247:                String nodeSet = itemsetNode.getAttribute("ref");
248:                if (nodeSet.charAt(0) == '/')
249:                    nodeSet = nodeSet.substring(1);
250:
251:                XModel modelNode = (XModel) currentProject.getModel()
252:                        .get(model);
253:                return (XModel) modelNode.get(nodeSet);
254:            }
255:
256:            /**
257:             * Read a select1 node
258:             * @param selectNode the xml representing the select item
259:             */
260:            protected void readDescription(XmlElement selectNode) {
261:                addComponent("label", selectNode);
262:            }
263:
264:            protected void readModel(XmlElement modelNode) {
265:                String modelId = modelNode.getAttribute("id");
266:                XModel xformsModel = (XModel) currentProject.getModel().get(
267:                        modelId == null ? "XForms" : "XForms" + "/" + modelId);
268:
269:                XmlElement instanceNode = modelNode
270:                        .getFirstChildNamed("xf:instance");
271:                if (instanceNode == null)
272:                    instanceNode = modelNode.getFirstChildNamed("instance");
273:                if (instanceNode == null)
274:                    instanceNode = modelNode;
275:                if (instanceNode != null) {
276:                    Vector itemNodes = instanceNode.getChildren();
277:                    int numItems = itemNodes.size();
278:                    for (int i = 0; i < numItems; i++) {
279:                        XmlElement childNode = (XmlElement) itemNodes
280:                                .elementAt(i);
281:                        XModel model = (XModel) xformsModel.get(childNode
282:                                .getName());
283:                        model.setTagName(childNode.getName());
284:                        Enumeration enumeration = childNode
285:                                .enumerateAttributeNames();
286:                        model.setNumAttributes(2 + childNode
287:                                .getAttributeCount());
288:                        int j = 2;
289:                        while (enumeration.hasMoreElements()) {
290:                            model.setAttribValue(j++, childNode
291:                                    .getAttribute((String) enumeration
292:                                            .nextElement()));
293:                        }
294:
295:                        readModelChildren(model, childNode);
296:                    }
297:                }
298:            }
299:
300:            protected void readModelChildren(XModel parentModel,
301:                    XmlElement parentNode) {
302:                Vector itemNodes = parentNode.getChildren();
303:                int numItems = itemNodes.size();
304:                for (int i = 0; i < numItems; i++) {
305:                    XmlElement childNode = (XmlElement) itemNodes.elementAt(i);
306:                    XModel childModel = (XModel) parentModel.get(childNode
307:                            .getName());
308:                    parentModel
309:                            .set(childNode.getName(), childNode.getContent());
310:                    readModelChildren(childModel, childNode);
311:                }
312:            }
313:
314:            protected void readGroup(XmlElement modelNode) {
315:                Component parent = (Component) componentFactory
316:                        .getParentComponent();
317:                try {
318:                    XPanel panel = (XPanel) addComponent("panel", modelNode);
319:                    componentFactory.setParentComponent(panel);
320:                    panel.setLayout(new GridLayout(0, 1));
321:
322:                    Vector componentNodes = modelNode.getChildren();
323:                    int numNodes = componentNodes.size();
324:                    for (int i = 0; i < numNodes; i++) {
325:                        XmlElement childNode = (XmlElement) componentNodes
326:                                .elementAt(i);
327:                        if (childNode != null)
328:                            readForm(childNode);
329:                    }
330:
331:                } catch (Exception ex) {
332:                }
333:
334:                // Reset the parent component to its state prior to this call
335:                componentFactory.setParentComponent(parent);
336:            }
337:
338:            protected void readInput(XmlElement inputNode) {
339:                Component parent = (Component) componentFactory
340:                        .getParentComponent();
341:                try {
342:                    XPanel panel = (XPanel) addComponent("panel", inputNode);
343:                    componentFactory.setParentComponent(panel);
344:
345:                    XmlElement childNode = inputNode
346:                            .getFirstChildNamed("label");
347:                    if (childNode != null)
348:                        addComponent("label", childNode);
349:
350:                    XEdit edit = (XEdit) addComponent("edit", inputNode);
351:
352:                    childNode = inputNode.getFirstChildNamed("help");
353:                    if (childNode != null)
354:                        edit.setToolTipText(childNode.getContent());
355:                } catch (Exception ex) {
356:                }
357:
358:                // Reset the parent component to its state prior to this call
359:                componentFactory.setParentComponent(parent);
360:            }
361:
362:            protected void readRange(XmlElement rangeNode) {
363:                Component parent = (Component) componentFactory
364:                        .getParentComponent();
365:                try {
366:                    XPanel panel = (XPanel) addComponent("panel", rangeNode);
367:                    componentFactory.setParentComponent(panel);
368:
369:                    XmlElement childNode = rangeNode
370:                            .getFirstChildNamed("label");
371:                    if (childNode != null)
372:                        addComponent("label", childNode);
373:
374:                    XSlider slider = (XSlider) addComponent("slider", rangeNode);
375:                    if (rangeNode.getAttribute("model") != null) {
376:                        XModel item = readItem(rangeNode);
377:                    }
378:
379:                    String attrib = rangeNode.getAttribute("step");
380:                    if (attrib != null)
381:                        slider.setIncrement(Integer.parseInt(attrib));
382:
383:                    attrib = rangeNode.getAttribute("start");
384:                    if (attrib != null)
385:                        slider.setMinimum(Integer.parseInt(attrib));
386:
387:                    attrib = rangeNode.getAttribute("end");
388:                    if (attrib != null)
389:                        slider.setMaximum(Integer.parseInt(attrib));
390:
391:                    childNode = rangeNode.getFirstChildNamed("help");
392:                    if (childNode != null)
393:                        slider.setToolTipText(childNode.getContent());
394:                } catch (Exception ex) {
395:                    ex.printStackTrace();
396:                }
397:
398:                // Reset the parent component to its state prior to this call
399:                componentFactory.setParentComponent(parent);
400:            }
401:
402:            protected void readUpload(XmlElement uploadNode) {
403:                Component parent = (Component) componentFactory
404:                        .getParentComponent();
405:                try {
406:                    XPanel panel = (XPanel) addComponent("panel", uploadNode);
407:                    componentFactory.setParentComponent(panel);
408:
409:                    XmlElement childNode = uploadNode
410:                            .getFirstChildNamed("label");
411:                    if (childNode != null)
412:                        addComponent("label", childNode);
413:
414:                    XEdit edit = (XEdit) addComponent("edit", uploadNode);
415:                    XButton button = (XButton) addComponent("button",
416:                            uploadNode);
417:                    button.setText("...");
418:
419:                    childNode = uploadNode.getFirstChildNamed("help");
420:                    if (childNode != null)
421:                        edit.setToolTipText(childNode.getContent());
422:                } catch (Exception ex) {
423:                    ex.printStackTrace();
424:                }
425:
426:                // Reset the parent component to its state prior to this call
427:                componentFactory.setParentComponent(parent);
428:            }
429:
430:            protected void readSecret(XmlElement inputNode) {
431:                Component parent = (Component) componentFactory
432:                        .getParentComponent();
433:                try {
434:                    XPanel panel = (XPanel) addComponent("panel", inputNode);
435:                    componentFactory.setParentComponent(panel);
436:
437:                    XmlElement childNode = inputNode
438:                            .getFirstChildNamed("label");
439:                    if (childNode != null)
440:                        addComponent("label", childNode);
441:
442:                    XPassword edit = (XPassword) addComponent("password",
443:                            inputNode);
444:
445:                    childNode = inputNode.getFirstChildNamed("help");
446:                    if (childNode != null)
447:                        edit.setToolTipText(childNode.getContent());
448:                } catch (Exception ex) {
449:                }
450:
451:                // Reset the parent component to its state prior to this call
452:                componentFactory.setParentComponent(parent);
453:            }
454:
455:            protected void readTextArea(XmlElement inputNode) {
456:                Component parent = (Component) componentFactory
457:                        .getParentComponent();
458:                try {
459:                    XPanel panel = (XPanel) addComponent("panel", inputNode);
460:                    componentFactory.setParentComponent(panel);
461:
462:                    XmlElement childNode = inputNode
463:                            .getFirstChildNamed("label");
464:                    if (childNode != null)
465:                        addComponent("label", childNode);
466:
467:                    XTextArea edit = (XTextArea) addComponent("textarea",
468:                            inputNode);
469:
470:                    childNode = inputNode.getFirstChildNamed("help");
471:                    if (childNode != null)
472:                        edit.setToolTipText(childNode.getContent());
473:                } catch (Exception ex) {
474:                }
475:
476:                // Reset the parent component to its state prior to this call
477:                componentFactory.setParentComponent(parent);
478:            }
479:
480:            protected void readTrigger(XmlElement triggerNode) {
481:                Vector actionNodes = triggerNode.getChildren("action");
482:                int numActions = actionNodes.size();
483:                if (numActions > 0) {
484:                    for (int i = 0; i < numActions; i++) {
485:                        XmlElement actionNode = (XmlElement) actionNodes
486:                                .elementAt(i);
487:                        String eventStr = actionNode.getAttribute("ev:event");
488:                        String handlerStr = actionNode
489:                                .getAttribute("ev:handler");
490:                        addActions(actionNode, eventStr, handlerStr);
491:                    }
492:                } else {
493:                    String eventStr = triggerNode.getAttribute("ev:event");
494:                    String handlerStr = triggerNode.getAttribute("ev:handler");
495:                    addActions(triggerNode, eventStr, handlerStr);
496:                }
497:            }
498:
499:            protected void readSubmit(XmlElement submitNode) {
500:                Component parent = (Component) componentFactory
501:                        .getParentComponent();
502:                try {
503:                    XPanel panel = (XPanel) addComponent("panel", submitNode);
504:                    componentFactory.setParentComponent(panel);
505:
506:                    XmlElement childNode = submitNode
507:                            .getFirstChildNamed("label");
508:                    if (childNode != null)
509:                        addComponent("label", childNode);
510:
511:                    XButton button = (XButton) addComponent("button",
512:                            submitNode);
513:                    button.setText("Submit");
514:                    String submissionStr = submitNode
515:                            .getAttribute("submission");
516:
517:                    String model = "XForms/submission/@id=" + submissionStr;
518:                    XModel submissionNode = (XModel) currentProject.getModel()
519:                            .get(model);
520:                    String action = submissionNode
521:                            .getAttribValueAsString(submissionNode
522:                                    .getAttribute("action"));
523:                    String ref = submissionNode
524:                            .getAttribValueAsString(submissionNode
525:                                    .getAttribute("ref"));
526:                    String method = submissionNode
527:                            .getAttribValueAsString(submissionNode
528:                                    .getAttribute("method"));
529:                    String separator = submissionNode
530:                            .getAttribValueAsString(submissionNode
531:                                    .getAttribute("separator"));
532:                    String encoding = submissionNode
533:                            .getAttribValueAsString(submissionNode
534:                                    .getAttribute("encoding"));
535:                    String replace = submissionNode
536:                            .getAttribValueAsString(submissionNode
537:                                    .getAttribute("replace"));
538:                    String indent = submissionNode
539:                            .getAttribValueAsString(submissionNode
540:                                    .getAttribute("indent"));
541:                    String version = submissionNode
542:                            .getAttribValueAsString(submissionNode
543:                                    .getAttribute("version"));
544:                    String omit_xml_declaration = submissionNode
545:                            .getAttribValueAsString(submissionNode
546:                                    .getAttribute("omit-xml-declaration"));
547:                    String standalone = submissionNode
548:                            .getAttribValueAsString(submissionNode
549:                                    .getAttribute("standalone"));
550:                    String cdata_section_elements = submissionNode
551:                            .getAttribValueAsString(submissionNode
552:                                    .getAttribute("cdata-section-elements"));
553:
554:                    childNode = submitNode.getFirstChildNamed("help");
555:                    if (childNode != null)
556:                        button.setToolTipText(childNode.getContent());
557:                } catch (Exception ex) {
558:                }
559:
560:                // Reset the parent component to its state prior to this call
561:                componentFactory.setParentComponent(parent);
562:            }
563:
564:            protected void addActions(XmlElement actionNode, String event,
565:                    String handler) {
566:                Vector actionNodes = actionNode.getChildren();
567:                int numActions = actionNodes.size();
568:                for (int i = 0; i < numActions; i++) {
569:                    XmlElement methodNode = (XmlElement) actionNodes
570:                            .elementAt(i);
571:                    String method = methodNode.getName();
572:                    String controlStr = methodNode.getAttribute("control");
573:                    Component targetComp = (Component) page;
574:                    if (controlStr != null)
575:                        targetComp = (Component) page.findComponent(controlStr);
576:
577:                    String eventType = getEventType(event);
578:                    if (eventType != null)
579:                        addHandler(page, targetComp, eventType, method);
580:
581:                    String eventStr = methodNode.getAttribute("ev:event");
582:                    if (eventStr != null) {
583:                        eventType = getEventType(eventStr);
584:                        if (eventType != null)
585:                            addHandler(page, targetComp, eventType, method);
586:                    }
587:                }
588:            }
589:
590:            /**
591:             * Adds an individual component element to the page (this method may be called
592:             * recursively for nested elements). Several methods will be attempted until a
593:             * component is successfully created. Firstly the built-in component types are
594:             * checked, then any additional registered component constructors. The types
595:             * can be specified by type ID, type name or class name.
596:             * @param childName the name of the child element
597:             * @param childNode the XML element containing the component specification.
598:             * @return the new component
599:             */
600:            protected Component addComponent(String childName,
601:                    XmlElement childNode) {
602:                String nameStr = null, contentStr, styleStr;
603:
604:                Component comp = null;
605:                try {
606:                    nameStr = contentStr = styleStr = null;
607:                    Hashtable componentAttributes = new Hashtable(); // Local copy of the component attributes
608:                    Enumeration attribNamesEnum = childNode
609:                            .enumerateAttributeNames();
610:                    while (attribNamesEnum.hasMoreElements()) {
611:                        String attribName = (String) attribNamesEnum
612:                                .nextElement();
613:                        String attribValue = (String) childNode
614:                                .getAttribute(attribName);
615:                        if (attribName.compareTo("ref") == 0)
616:                            nameStr = attribValue;
617:                        else if (attribName.compareTo("content") == 0) {
618:                            contentStr = (String) rootPage
619:                                    .evaluateAttribute(attribValue);
620:                            componentAttributes.put(attribName, contentStr);
621:                        } else if (attribName.compareTo("class") == 0)
622:                            styleStr = childNode.getName() + "/" + attribValue;
623:                        else {
624:                            // Save a copy of the attributes in the page for post creation usage
625:                            ((XPage) rootPage).setAttribute(attribName,
626:                                    nameStr, attribValue);
627:
628:                            // Save a local copy for use during the construction process
629:                            componentAttributes.put(attribName, attribValue);
630:                        }
631:                    }
632:
633:                    contentStr = childNode.getContent();
634:
635:                    // Load with a name like 'Button'
636:                    try {
637:                        comp = (Component) componentFactory.addComponent(
638:                                getComponentRenderType(childName), 0, 0, -1,
639:                                -1, contentStr, styleStr);
640:                    } catch (Exception e) {
641:                        comp = null;
642:                    }
643:
644:                    if (comp == null)
645:                        return null;
646:
647:                    applyAttributes((JComponent) comp, componentAttributes);
648:
649:                    // Set the component name
650:                    comp.setName(nameStr);
651:
652:                    // Special handling for radio buttons
653:                    // For the first rb create a group
654:                    // For subsequent rbs add them to the group
655:                    // If another type of component is found reset the groups
656:                    if (comp instanceof  XRadioButtonGroup) {
657:                        XRadioButtonGroup rb = ((XRadioButtonGroup) comp);
658:                        if (checkBoxGroup == null) {
659:                            // The radio button may already have a group
660:                            if (rb.getRadioButtonGroup() == null)
661:                                checkBoxGroup = rb.createGroup();
662:                            else
663:                                checkBoxGroup = rb.getRadioButtonGroup();
664:                        } else
665:                            rb.setRadioButtonGroup(checkBoxGroup);
666:                    } else
667:                        checkBoxGroup = null;
668:
669:                    // Setup any extra attributes specified in the XML
670:                    setComponentAttributes(childName, comp, componentAttributes);
671:                } catch (Exception e) {
672:                    if (BuildProperties.DEBUG)
673:                        DebugLogger
674:                                .logError("While adding the component element: "
675:                                        + nameStr);
676:                    e.printStackTrace();
677:                }
678:
679:                return comp;
680:            }
681:
682:            protected void setupPage(XmlElement model, String pageName,
683:                    boolean include) {
684:                String className = model.getAttribute("class");
685:                if (className == null) {
686:                    // Try to get a default startup class name if none has been specified in the XML
687:                    try {
688:                        className = currentProject
689:                                .getStartupParam("DefaultClass");
690:                    } catch (Exception ex) {
691:                    }
692:                    if (className == null)
693:                        className = "net.xoetrope.xui.XPage";
694:                }
695:
696:                if (!include) {
697:                    if ((className.indexOf('.') <= 0)
698:                            && (packageName.length() > 1))
699:                        className = packageName + className;
700:                    try {
701:                        page = loadClass(className);
702:                    } catch (Exception e) {
703:                        if (BuildProperties.DEBUG)
704:                            DebugLogger
705:                                    .trace("Unable to load the named class: "
706:                                            + className);
707:                        page = new XPage();
708:                    }
709:                    setPageName(pageName);
710:
711:                    componentFactory.setParentComponent((Container) page);
712:                    String styleName = model.getAttribute("style");
713:                    if (styleName != null)
714:                        componentFactory.applyStyle(page, styleName);
715:                }
716:                String layoutStyle = model.getAttribute("layoutStyle");
717:                if (layoutStyle == null)
718:                    layoutStyle = "BoxLayout";
719:                Hashtable ht = new Hashtable();
720:                ht.put("isHorz", "1");
721:                componentFactory.addLayout(null, layoutHelper
722:                        .getLayoutType("box"), ht);
723:
724:                int width = 0, height = 0;
725:                Enumeration attribNamesEnum = model.enumerateAttributeNames();
726:                while (attribNamesEnum.hasMoreElements()) {
727:                    String attribName = (String) attribNamesEnum.nextElement();
728:                    String attribValue = (String) model
729:                            .getAttribute(attribName);
730:                    page.setAttribute(attribName, null, page
731:                            .evaluateAttribute(attribValue));
732:                    if (attribName.compareTo("resource") == 0) {
733:                        String resName;
734:                        if ((attribValue != null) && (attribValue.length() > 0))
735:                            resName = page.evaluatePath(attribValue);
736:                        else
737:                            resName = currentProject
738:                                    .getStartupParam("Language");
739:                        componentFactory.setResourceBundle(resName);
740:                        page.getComponentFactory().setResourceBundle(resName);
741:                    }
742:                }
743:
744:                rootPage = (XPage) page;
745:            }
746:
747:            protected void setupModel(String pageName) {
748:                try {
749:                    int pos = pageName.lastIndexOf('-');
750:                    if (pos > 0) {
751:                        String modelName = pageName.substring(0, pos + 1)
752:                                + "model.xml";
753:                        Reader reader = currentProject
754:                                .getBufferedReader(modelName);
755:                        XmlElement model = XmlSource.read(reader);
756:                        if (model.getName().indexOf("model") >= 0)
757:                            readModel(model);
758:                        else {
759:                            Vector children = model.getChildren();
760:                            int numChildren = children.size();
761:                            for (int i = 0; i < numChildren; i++)
762:                                readModel((XmlElement) children.elementAt(i));
763:                        }
764:                    }
765:                } catch (Exception ex) {
766:                    if (BuildProperties.DEBUG)
767:                        DebugLogger
768:                                .logWarning("Unable to read the XForms model file for page:"
769:                                        + pageName);
770:                }
771:            }
772:
773:            protected String getComponentRenderType(String childType) {
774:                if (childType.equals("choices")) {
775:                    if (selectStyle.equals(XPage.COMBO))
776:                        return "combo";
777:                }
778:                return childType;
779:            }
780:
781:            protected String getEventType(String event) {
782:                if (event == null)
783:                    return null;
784:
785:                if (event.equals("xforms-focus"))
786:                    return "FocusHandler";
787:                else if (event.equals("submit"))
788:                    return "ActionHandler";
789:
790:                return null;
791:            }
792:
793:            protected void setupFormTags() {
794:                if (formTags == null) {
795:                    formTags = new Hashtable();
796:                    formTags.put("box", new Integer(BOX));
797:                    formTags.put("description", new Integer(DESCRIPTION));
798:                    formTags.put("input", new Integer(INPUT));
799:                    formTags.put("secret", new Integer(SECRET));
800:                    formTags.put("textarea", new Integer(TEXTAREA));
801:                    formTags.put("model", new Integer(MODEL));
802:                    formTags.put("group", new Integer(GROUP));
803:                    formTags.put("range", new Integer(RANGE));
804:                    formTags.put("upload", new Integer(UPLOAD));
805:                    formTags.put("trigger", new Integer(TRIGGER));
806:                    formTags.put("submit", new Integer(SUBMIT));
807:                }
808:            }
809:
810:            protected void applyAttributes(JComponent comp, Hashtable attribs) {
811:                Enumeration enumeration = attribs.keys();
812:                while (enumeration.hasMoreElements()) {
813:                    String attribName = (String) enumeration.nextElement();
814:                    String attribValue = (String) attribs.get(attribName);
815:                    attribName = attribName.toLowerCase();
816:
817:                    try {
818:                        if (attribName.equals("height")) {
819:                            comp.setSize(Math.max(comp.getSize().width, 1),
820:                                    Integer.parseInt(attribValue));
821:                            //          comp.setPreferredSize( new Dimension( /*comp.getSize().width*/20, Integer.parseInt( attribValue ) ));
822:                        } else if (attribName.equals("width")) {
823:                            comp.setSize(Integer.parseInt(attribValue), Math
824:                                    .max(comp.getSize().height, 1));
825:                            //          comp.setPreferredSize( new Dimension( Integer.parseInt( attribValue ), 20 ));//comp.getSize().height ));
826:                        } else if (attribName.equals("foreground"))
827:                            comp
828:                                    .setForeground(parseColorAttribute(attribValue));
829:                        else if (attribName.equals("background"))
830:                            comp
831:                                    .setBackground(parseColorAttribute(attribValue));
832:                    } catch (Exception ex) {
833:                        ex.printStackTrace();
834:                    }
835:                }
836:            }
837:
838:            /**
839:             * Gets a color value from a name or an RGB value
840:             * @param colorName
841:             * @return the color or null if no color can be matched
842:             */
843:            protected Color parseColorAttribute(String colorName) {
844:                if (colorName == null)
845:                    return null;
846:                String name = colorName.toLowerCase();
847:                if (name.equals("red"))
848:                    return Color.red;
849:                else if (name.equals("green"))
850:                    return Color.green;
851:                else if (name.equals("blue"))
852:                    return Color.blue;
853:                else if (name.equals("black"))
854:                    return Color.black;
855:                else if (name.equals("cyan"))
856:                    return Color.cyan;
857:                else if (name.equals("darkGray"))
858:                    return Color.darkGray;
859:                else if (name.equals("gray"))
860:                    return Color.gray;
861:                else if (name.equals("lightGray"))
862:                    return Color.lightGray;
863:                else if (name.equals("magenta"))
864:                    return Color.magenta;
865:                else if (name.equals("orange"))
866:                    return Color.orange;
867:                else if (name.equals("pink"))
868:                    return Color.pink;
869:                else if (name.equals("white"))
870:                    return Color.white;
871:                else if (name.equals("yellow"))
872:                    return Color.yellow;
873:                else if (name.charAt(0) == '#') {
874:                    return new Color(Integer.parseInt(name.substring(1), 16));
875:                } else if (name.charAt(0) == '{') {
876:                    String styleName = name.substring(1, name.indexOf('}'));
877:                    return currentProject.getStyleManager().getStyle(styleName)
878:                            .getStyleAsColor(XStyle.COLOR_FORE);
879:                }
880:
881:                return null;
882:            }
883:
884:            /**
885:             * Get the page loader type - a unique name identifying the loader
886:             * @return "xul"
887:             */
888:            public String getType() {
889:                return "xul";
890:            }
891:
892:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.