Source Code Cross Referenced for WidgetTree.java in  » XML-UI » xmlgui » org » beryl » gui » builder » 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 » xmlgui » org.beryl.gui.builder 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Beryl - A web platform based on XML, XSLT and Java
003:         * This file is part of the Beryl XML GUI
004:         *
005:         * Copyright (C) 2004 Wenzel Jakob <wazlaf@tigris.org>
006:         * 					  Simon Epstein <anyone@simondo.com>
007:         *
008:         * This program is free software; you can redistribute it and/or
009:         * modify it under the terms of the GNU Lesser General Public
010:         * License as published by the Free Software Foundation; either
011:         * version 2.1 of the License, or (at your option) any later version.
012:
013:         * This program is distributed in the hope that it will be useful,
014:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
015:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
016:         * Lesser General Public License for more details.
017:         *
018:         * You should have received a copy of the GNU Lesser General Public
019:         * License along with this program; if not, write to the Free Software
020:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-3107  USA
021:         */
022:
023:        package org.beryl.gui.builder;
024:
025:        import java.lang.reflect.Constructor;
026:        import java.lang.reflect.InvocationTargetException;
027:        import java.util.ArrayList;
028:
029:        import javax.swing.JComponent;
030:
031:        import org.beryl.gui.AnchorFactory;
032:        import org.beryl.gui.Controller;
033:        import org.beryl.gui.GUIEvent;
034:        import org.beryl.gui.GUIEventListener;
035:        import org.beryl.gui.GUIException;
036:        import org.beryl.gui.InternationalizationManager;
037:        import org.beryl.gui.LayoutFactory;
038:        import org.beryl.gui.MessageDialog;
039:        import org.beryl.gui.PropertyFactory;
040:        import org.beryl.gui.Widget;
041:        import org.beryl.gui.WidgetFactory;
042:        import org.beryl.gui.WidgetInfo;
043:        import org.beryl.gui.XMLUtils;
044:        import org.beryl.gui.model.MapChangeEvent;
045:        import org.beryl.gui.model.MapDataModel;
046:        import org.beryl.gui.model.ModelChangeEvent;
047:        import org.beryl.gui.model.ModelChangeListener;
048:        import org.beryl.gui.model.TableDataModel;
049:        import org.beryl.gui.model.TableRow;
050:        import org.beryl.gui.widgets.Dialog;
051:        import org.beryl.gui.widgets.Frame;
052:        import org.beryl.gui.widgets.Panel;
053:        import org.beryl.gui.widgets.PopupMenu;
054:        import org.beryl.gui.widgets.Table;
055:        import org.beryl.gui.widgets.Tree;
056:        import org.beryl.gui.widgets.TreeItem;
057:        import org.w3c.dom.Document;
058:        import org.w3c.dom.Element;
059:        import org.w3c.dom.Node;
060:        import org.w3c.dom.NodeList;
061:
062:        public class WidgetTree extends Controller implements 
063:                ModelChangeListener {
064:            private static PropertyFactory pf = PropertyFactory.getInstance();
065:            private static LayoutFactory lf = LayoutFactory.getInstance();
066:            private static AnchorFactory af = AnchorFactory.getInstance();
067:
068:            /**
069:             * The widget tree frame
070:             */
071:            private Frame frame = null;
072:
073:            /**
074:             * The data model
075:             */
076:            private MapDataModel dataModel = null;
077:
078:            /**
079:             * The tree component
080:             */
081:            private Tree widgetTree = null;
082:
083:            /**
084:             * Root node
085:             */
086:            private TreeItem rootNode = null;
087:
088:            /**
089:             * Tree popup menu
090:             */
091:            private PopupMenu treePopupMenu = null;
092:
093:            /**
094:             * Property popup menu
095:             */
096:            private PopupMenu propertyPopupMenu = null;
097:
098:            /**
099:             * Property table
100:             */
101:            private Table table = null;
102:
103:            /**
104:             * Empty table data model
105:             */
106:            private TableDataModel emptyModel = null;
107:
108:            /* DEBUGGING */
109:            private GUIEventListener debuggingGUIEventListener = null;
110:            private MapDataModel debuggingMapDataModel = null;
111:
112:            private class DebuggingModelChangeListener implements 
113:                    ModelChangeListener {
114:                public void modelChanged(ModelChangeEvent e) {
115:                    if (e instanceof  MapChangeEvent) {
116:                        MapChangeEvent event = (MapChangeEvent) e;
117:                        log.debug("Data model change: '" + event.getKey()
118:                                + "' => '" + event.getNewValue() + "'");
119:                    } else {
120:                        log.debug("Data model change: " + e.toString());
121:                    }
122:                }
123:            };
124:
125:            public WidgetTree(Builder builder) throws GUIException {
126:                dataModel = new MapDataModel();
127:
128:                frame = constructFrame("WidgetTree", dataModel);
129:                treePopupMenu = (PopupMenu) constructWidget("WidgetPopup");
130:                propertyPopupMenu = (PopupMenu) constructWidget("PropertyPopup");
131:                emptyModel = new TableDataModel();
132:
133:                widgetTree = (Tree) frame.getWidget("Tree");
134:                table = (Table) frame.getWidget("Table");
135:                rootNode = (TreeItem) frame.getWidget("RootNode");
136:                table.setTableDataModel(emptyModel);
137:                debuggingMapDataModel = new MapDataModel();
138:                debuggingMapDataModel
139:                        .addModelChangeListener(new DebuggingModelChangeListener());
140:                dataModel.addModelChangeListener(this );
141:            }
142:
143:            public void refresh(Document document) throws GUIException {
144:                for (int i = 0; i < rootNode.getChildCount(); i++) {
145:                    TreeItem item = (TreeItem) rootNode.getChild(i);
146:                    WidgetUserObject object = (WidgetUserObject) item
147:                            .getUserObject();
148:
149:                    if (object.widget instanceof  Frame) {
150:                        ((Frame) object.widget).dispose();
151:                    } else if (object.widget instanceof  Dialog) {
152:                        ((Dialog) object.widget).dispose();
153:                    }
154:                }
155:
156:                rootNode.removeAllChildren();
157:                NodeList children = document.getDocumentElement()
158:                        .getChildNodes();
159:                for (int i = 0; i < children.getLength(); i++) {
160:                    Node node = (Node) children.item(i);
161:
162:                    if (node.getNodeType() == Node.ELEMENT_NODE
163:                            && node.getNodeName().equals("widget")) {
164:                        traverseDocument((Element) node, rootNode);
165:                    }
166:                }
167:                rootNode.setUserObject(new WidgetUserObject(document
168:                        .getDocumentElement(), null, rootNode));
169:                widgetTree.structureChanged(rootNode);
170:                System.out.println(frame.dumpStructure());
171:            }
172:
173:            /**
174:             * Traverses the XML tree, creates the preview
175:             * and builds the widget tree
176:             */
177:            private Widget traverseDocument(Element xmlNode, TreeItem parentNode)
178:                    throws GUIException {
179:                TreeItem newNode = new TreeItem(parentNode, null);
180:                WidgetUserObject parent = (WidgetUserObject) parentNode
181:                        .getUserObject();
182:
183:                String classAttribute = xmlNode.getAttribute("class");
184:                String nameAttribute = xmlNode.getAttribute("name");
185:                String presetAttribute = xmlNode.getAttribute("preset");
186:
187:                if (presetAttribute.equals(""))
188:                    presetAttribute = null;
189:                if (nameAttribute.equals(""))
190:                    nameAttribute = null;
191:                Widget widget = constructWidget(parent.widget, classAttribute,
192:                        nameAttribute, presetAttribute);
193:                WidgetUserObject object = new WidgetUserObject(xmlNode, widget,
194:                        newNode);
195:                newNode.setUserObject(object);
196:                newNode.setText(describeWidget(xmlNode));
197:
198:                NodeList childNodes = xmlNode.getChildNodes();
199:
200:                object.tableModel.addRow(new NameTableRow(object));
201:                Element anchorNode = XMLUtils.getChild(xmlNode, "anchor");
202:
203:                if (anchorNode != null) {
204:                    object.tableModel.addRow(new PropertyTableRow(object, null,
205:                            anchorNode));
206:                }
207:
208:                for (int o = 0; o < childNodes.getLength(); o++) {
209:                    if (childNodes.item(o).getNodeName().equals("property")) {
210:                        Element propertyNode = (Element) childNodes.item(o);
211:                        String propertyName = propertyNode.getAttribute("name");
212:                        Object propertyValue = pf
213:                                .constructProperty(propertyNode);
214:                        object.tableModel.addRow(new PropertyTableRow(object,
215:                                propertyValue, propertyNode));
216:                        widget.setProperty(propertyName, propertyValue);
217:                    } else if (childNodes.item(o).getNodeName()
218:                            .equals("layout")) {
219:                        Element layoutNode = (Element) childNodes.item(o);
220:                        Object propertyValue = lf.constructLayout(widget,
221:                                layoutNode);
222:                        object.tableModel.addRow(new PropertyTableRow(object,
223:                                propertyValue, layoutNode));
224:                        widget.setProperty("layout", propertyValue);
225:                    } else if (childNodes.item(o).getNodeName().equals("emit")) {
226:                        Element emitNode = (Element) childNodes.item(o);
227:                        String name = emitNode.getAttribute("name");
228:                        TableRow row = new TableRow();
229:                        row.setValue("event", emitNode.getAttribute("event"));
230:                        row.setValue("name", name);
231:                        row.setValue("description", widget.getWidgetInfo()
232:                                .getEventEntry(emitNode.getAttribute("event"))
233:                                .getDescription());
234:                        row.setValue("node", emitNode);
235:
236:                        object.eventModel.addRow(row);
237:                    }
238:                }
239:
240:                for (int i = 0; i < childNodes.getLength(); i++) {
241:                    if (childNodes.item(i).getNodeName().equals("widget")) {
242:                        Element childNode = (Element) childNodes.item(i);
243:
244:                        /* Get the child's anchor subnode */
245:                        anchorNode = XMLUtils.getChild(childNode, "anchor");
246:                        Widget childWidget = traverseDocument(childNode,
247:                                newNode);
248:
249:                        widget.addChild(childWidget, createAnchor(childWidget,
250:                                anchorNode));
251:
252:                    }
253:                }
254:
255:                parentNode.addChild(newNode, null);
256:                widget.finalizeConstruction();
257:
258:                if (widget instanceof  Dialog) {
259:                    /* Dialogs get treated a little bit differently */
260:                    Dialog dialog = (Dialog) widget;
261:                    dialog.initDialog(null);
262:                    dialog.show();
263:                }
264:
265:                return widget;
266:            }
267:
268:            public static Object createAnchor(Widget widget, Element anchorNode)
269:                    throws GUIException {
270:                Object anchor = null;
271:
272:                if (anchorNode != null) {
273:                    anchor = af.constructAnchor(anchorNode);
274:                    if (anchor instanceof  AnchorFactory.BoxAnchor) {
275:                        JComponent component = (JComponent) widget.getWidget();
276:
277:                        AnchorFactory.BoxAnchor ba = (AnchorFactory.BoxAnchor) anchor;
278:                        component.setAlignmentX(ba.getAlignmentX());
279:                        component.setAlignmentY(ba.getAlignmentY());
280:                        anchor = null;
281:                    }
282:                } else {
283:                    Object comp = widget.getWidget();
284:
285:                    if (comp != null && comp instanceof  JComponent) {
286:                        JComponent component = (JComponent) comp;
287:
288:                        component.setAlignmentX(0.0f);
289:                        component.setAlignmentY(0.0f);
290:                    }
291:                }
292:
293:                return anchor;
294:            }
295:
296:            /**
297:             * Return a string description of a widget element
298:             */
299:            public static String describeWidget(Element element) {
300:                StringBuffer buf = new StringBuffer();
301:
302:                String classAttribute = element.getAttribute("class");
303:                String nameAttribute = element.getAttribute("name");
304:                String presetAttribute = element.getAttribute("preset");
305:
306:                buf.append(classAttribute);
307:                if (!nameAttribute.equals("")) {
308:                    buf.append(" (").append(nameAttribute).append(")");
309:                }
310:                if (!presetAttribute.equals("")) {
311:                    buf.append(" [").append(presetAttribute).append("]");
312:                }
313:
314:                return buf.toString();
315:            }
316:
317:            public void doInsert(String className) throws GUIException {
318:                TreeItem items[] = (TreeItem[]) dataModel
319:                        .getValue("tree.selected");
320:                if (items.length == 1) {
321:                    WidgetInfo info = constructWidget(null, className, null,
322:                            null).getWidgetInfo();
323:                    Element anchorElement = ((WidgetUserObject) rootNode
324:                            .getUserObject()).element.getOwnerDocument()
325:                            .createElement("anchor");
326:                    new InsertDialog(this , className, info, anchorElement);
327:                }
328:            }
329:
330:            public void doInsert(String className, String preset,
331:                    Element anchorNode) throws GUIException {
332:                TreeItem items[] = (TreeItem[]) dataModel
333:                        .getValue("tree.selected");
334:                TreeItem item = items[0];
335:                WidgetUserObject parentObject = (WidgetUserObject) item
336:                        .getUserObject();
337:                Document document = parentObject.element.getOwnerDocument();
338:
339:                Element childWidget = document.createElement("widget");
340:                childWidget.setAttribute("class", className);
341:                if (preset != null)
342:                    childWidget.setAttribute("preset", preset);
343:
344:                TreeItem newNode = new TreeItem(item, null);
345:                Widget parentWidget = ((WidgetUserObject) item.getUserObject()).widget;
346:                Widget widget = constructWidget(parentWidget, className, null,
347:                        preset);
348:
349:                Object anchor = null;
350:
351:                if (parentWidget != null) {
352:                    anchor = createAnchor(widget, anchorNode);
353:                    parentWidget.addChild(widget, anchor);
354:                }
355:
356:                widget.finalizeConstruction();
357:
358:                parentObject.element.appendChild(childWidget);
359:
360:                if (widget instanceof  Dialog) {
361:                    Dialog dialog = (Dialog) widget;
362:                    dialog.initDialog(null);
363:                    dialog.show();
364:                }
365:
366:                if (parentWidget != null)
367:                    revalidate(parentWidget);
368:
369:                WidgetUserObject userObject = new WidgetUserObject(childWidget,
370:                        widget, newNode);
371:                userObject.tableModel.addRow(new NameTableRow(userObject));
372:
373:                if (anchorNode != null) {
374:                    childWidget.appendChild(anchorNode);
375:                    PropertyTableRow row = new PropertyTableRow(userObject,
376:                            anchor, anchorNode);
377:                    userObject.tableModel.addRow(row);
378:                }
379:
380:                newNode.setUserObject(userObject);
381:                newNode.setText(className);
382:                item.addChild(newNode, null);
383:                widgetTree.structureChanged(item);
384:                dataModel.setValue("tree.selected", new TreeItem[] { newNode });
385:                Builder.markModified();
386:            }
387:
388:            private Widget constructWidget(Widget parentWidget,
389:                    String className, String widgetName, String preset)
390:                    throws GUIException {
391:                try {
392:                    if (className.indexOf('.') == -1)
393:                        className = WidgetFactory.DEFAULT_PREFIX + className;
394:                    Class wClass = Class.forName(className);
395:                    Constructor widgetConstructor = null;
396:                    Widget widget = null;
397:                    if (preset != null)
398:                        widgetConstructor = wClass.getConstructor(new Class[] {
399:                                Widget.class, String.class, String.class });
400:                    else
401:                        widgetConstructor = wClass.getConstructor(new Class[] {
402:                                Widget.class, String.class });
403:                    if (preset != null)
404:                        widget = (Widget) widgetConstructor
405:                                .newInstance(new Object[] { parentWidget,
406:                                        widgetName, preset });
407:                    else
408:                        widget = (Widget) widgetConstructor
409:                                .newInstance(new Object[] { parentWidget,
410:                                        widgetName });
411:
412:                    widget.setDataModel(debuggingMapDataModel);
413:
414:                    return widget;
415:                } catch (ClassNotFoundException e) {
416:                    throw new GUIException("Unknown widget class [" + className
417:                            + "]");
418:                } catch (NoSuchMethodException e) {
419:                    throw new GUIException("Widget constructor not found", e);
420:                } catch (IllegalAccessException e) {
421:                    throw new GUIException(
422:                            "Widget constructor could not be called", e);
423:                } catch (InstantiationException e) {
424:                    throw new GUIException("Widget is abstract", e);
425:                } catch (InvocationTargetException e) {
426:                    throw new GUIException(
427:                            "Widget constructor threw an exception", e);
428:                }
429:            }
430:
431:            private void doDelete(TreeItem item) throws GUIException {
432:                TreeItem parent = (TreeItem) item.getParent();
433:                WidgetUserObject object = (WidgetUserObject) item
434:                        .getUserObject();
435:                object.element.getParentNode().removeChild(object.element);
436:                Widget parentWidget = object.widget.getParentWidget();
437:
438:                if (object.widget.getParentWidget() != null) {
439:                    object.widget.getParentWidget().removeChildWidget(
440:                            object.widget);
441:                    revalidate(parentWidget);
442:                } else {
443:                    if (object.widget instanceof  Frame) {
444:                        ((Frame) object.widget).dispose();
445:                    } else if (object.widget instanceof  Dialog) {
446:                        ((Dialog) object.widget).dispose();
447:                    }
448:                }
449:
450:                parent.remove(item);
451:                widgetTree.structureChanged(parent);
452:                Builder.markModified();
453:            }
454:
455:            public static void revalidate(Widget widget) throws GUIException {
456:                if (widget instanceof  Panel)
457:                    ((Panel) widget).recreateLayout();
458:                else
459:                    widget.revalidate();
460:            }
461:
462:            /**
463:             * Removes and then re-inserts all children of the given widget's parent
464:             */
465:            public static void doReInsert(WidgetUserObject object)
466:                    throws GUIException {
467:                ArrayList temp = new ArrayList(), tempAnchors = new ArrayList();
468:                Widget parentWidget = object.widget.getParentWidget();
469:                TreeItem parentNode = (TreeItem) object.treeNode
470:                        .getParentWidget();
471:
472:                for (int i = 0, count = parentNode.getChildCount(); i < count; i++) {
473:                    TreeItem node = (TreeItem) parentNode.getChild(i);
474:                    WidgetUserObject nodeObj = (WidgetUserObject) node
475:                            .getUserObject();
476:                    temp.add(nodeObj.widget);
477:                    tempAnchors.add(XMLUtils
478:                            .getChild(nodeObj.element, "anchor"));
479:                    parentWidget.removeChildWidget(nodeObj.widget);
480:                }
481:                for (int i = 0; i < temp.size(); i++) {
482:                    Object anchor = createAnchor((Widget) temp.get(i),
483:                            (Element) tempAnchors.get(i));
484:                    parentWidget.addChild((Widget) temp.get(i), anchor);
485:                }
486:                revalidate(parentWidget);
487:            }
488:
489:            public static void doDeleteProperty(PropertyTableRow row)
490:                    throws GUIException {
491:                WidgetUserObject object = row.getUserObject();
492:                String name = (String) row.getValue("name");
493:
494:                if (name.equals("anchor")) {
495:                    row.getPropertyNode().getParentNode().removeChild(
496:                            row.getPropertyNode());
497:                    doReInsert(object);
498:                } else {
499:                    WidgetInfo.PropertyEntry entry = object.widget
500:                            .getWidgetInfo().getPropertyEntry(name);
501:
502:                    MapChangeEvent evt = new MapChangeEvent(null, null,
503:                            "value", null, entry.defaultValue);
504:                    row.modelChanged(evt);
505:
506:                    row.getPropertyNode().getParentNode().removeChild(
507:                            row.getPropertyNode());
508:                }
509:
510:                object.tableModel.removeRow(row);
511:            }
512:
513:            /**
514:             * For simplicity's sake, the XML GUI does not have insertAt style methods
515:             * Therefore we now must remove everything and then add it in a different order
516:             */
517:            private void doMove(TreeItem moveItem, boolean up)
518:                    throws GUIException {
519:                WidgetUserObject object = (WidgetUserObject) moveItem
520:                        .getUserObject();
521:                ArrayList temp = new ArrayList();
522:
523:                /* Update tree */
524:                TreeItem parentTreeItem = (TreeItem) moveItem.getParentWidget();
525:                int treeIndex = parentTreeItem.getChildIndex(moveItem);
526:                if (treeIndex == -1)
527:                    throw new GUIException("Tree node not found");
528:                for (int i = 0, count = parentTreeItem.getChildCount(); i < count; i++) {
529:                    TreeItem item = (TreeItem) parentTreeItem.getChild(0);
530:                    if (item != moveItem)
531:                        temp.add(item);
532:                    parentTreeItem.removeChildWidget(item);
533:                }
534:                temp.add(treeIndex + (up ? -1 : 1), moveItem);
535:                for (int i = 0; i < temp.size(); i++)
536:                    parentTreeItem.addChild((Widget) temp.get(i), null);
537:                temp.clear();
538:
539:                /* Update widget */
540:                Widget parentWidget = object.widget.getParentWidget();
541:                int widgetIndex = parentWidget.getChildIndex(object.widget);
542:                if (widgetIndex == -1)
543:                    throw new GUIException("Widget not found");
544:                for (int i = 0, count = parentWidget.getChildCount(); i < count; i++) {
545:                    Widget widget = (Widget) parentWidget.getChild(0);
546:                    if (widget != object.widget)
547:                        temp.add(widget);
548:                    parentWidget.removeChildWidget(widget);
549:                }
550:                temp.add(widgetIndex + (up ? -1 : 1), object.widget);
551:                for (int i = 0; i < temp.size(); i++) {
552:                    Element anchorNode = XMLUtils.getChild(object.element,
553:                            "anchor");
554:                    Object anchor = null;
555:                    if (anchorNode != null)
556:                        anchor = AnchorFactory.getInstance().constructAnchor(
557:                                anchorNode);
558:                    parentWidget.addChild((Widget) temp.get(i), anchor);
559:                }
560:
561:                /* Update XML - This is horrible */
562:                Element parentElement = (Element) object.element
563:                        .getParentNode();
564:                NodeList nodes = parentElement.getChildNodes();
565:
566:                Element lastWidget = null, beforeWidget = null;
567:
568:                int distance = -1;
569:                for (int i = 0; i < nodes.getLength(); i++) {
570:                    Node node = nodes.item(i);
571:                    if (node == object.element) {
572:                        if (up) {
573:                            beforeWidget = lastWidget;
574:                            break;
575:                        } else {
576:                            distance = 1;
577:                        }
578:                    } else if (node instanceof  Element) {
579:                        Element element = (Element) node;
580:                        if (element.getNodeName().equals("widget")) {
581:                            lastWidget = element;
582:
583:                            if (distance == 0) {
584:                                beforeWidget = lastWidget;
585:                                break;
586:                            }
587:                        }
588:                        if (distance != -1)
589:                            distance--;
590:                    }
591:                }
592:
593:                parentElement.removeChild(object.element);
594:                parentElement.insertBefore(object.element, beforeWidget);
595:
596:                revalidate(parentWidget);
597:                widgetTree.structureChanged(parentTreeItem);
598:                Builder.markModified();
599:            }
600:
601:            public Frame getFrame() {
602:                return frame;
603:            }
604:
605:            public void eventOccured(GUIEvent event) {
606:                String command = event.getName();
607:
608:                try {
609:                    if (command.startsWith("tree.")
610:                            || command.startsWith("move.")) {
611:                        updateEnabledWidgets(event);
612:                    } else if (command.equals("property.popup")) {
613:                        propertyPopupMenu.popup(event);
614:                    } else if (command.equals("delete")
615:                            || command.equals("button_delete")) {
616:                        TreeItem items[] = (TreeItem[]) dataModel
617:                                .getValue("tree.selected");
618:                        for (int i = 0; i < items.length; i++) {
619:                            if (items[i] != rootNode)
620:                                doDelete(items[i]);
621:                        }
622:                    } else if (command.equals("add_property")) {
623:                        TreeItem items[] = (TreeItem[]) dataModel
624:                                .getValue("tree.selected");
625:                        if (items.length == 1 && items[0] != rootNode) {
626:                            new AddPropertyDialog(frame,
627:                                    (WidgetUserObject) items[0].getUserObject());
628:                        }
629:                    } else if (command.equals("delete_property")) {
630:                        TableRow rows[] = (TableRow[]) dataModel
631:                                .getValue("property.value");
632:                        for (int i = 0; i < rows.length; i++) {
633:                            if (rows[i] instanceof  PropertyTableRow) {
634:                                doDeleteProperty((PropertyTableRow) rows[i]);
635:                                Builder.markModified();
636:                            }
637:                        }
638:                    } else if (command.equals("events")) {
639:                        TreeItem items[] = (TreeItem[]) dataModel
640:                                .getValue("tree.selected");
641:
642:                        if (items.length == 1 && items[0] != rootNode) {
643:                            new EventDialog(this , frame,
644:                                    (WidgetUserObject) items[0].getUserObject());
645:                        }
646:                    } else if (command.equals("move_up")
647:                            || command.equals("button_move_up")) {
648:                        TreeItem items[] = (TreeItem[]) dataModel
649:                                .getValue("tree.selected");
650:
651:                        if (items.length == 1 && items[0] != rootNode) {
652:                            doMove(items[0], true);
653:                            dataModel.setValue("tree.selected", items);
654:                            updateEnabledWidgets(event);
655:                        }
656:                    } else if (command.equals("move_down")
657:                            || command.equals("button_move_down")) {
658:                        TreeItem items[] = (TreeItem[]) dataModel
659:                                .getValue("tree.selected");
660:
661:                        if (items.length == 1 && items[0] != rootNode) {
662:                            doMove(items[0], false);
663:                            dataModel.setValue("tree.selected", items);
664:                            updateEnabledWidgets(event);
665:                        }
666:                    }
667:                } catch (Exception e) {
668:                    new MessageDialog(e);
669:                }
670:            }
671:
672:            public void modelChanged(ModelChangeEvent e) throws GUIException {
673:                if (e instanceof  MapChangeEvent) {
674:                    MapChangeEvent event = (MapChangeEvent) e;
675:                    if (event.getKey().equals("tree.selected")) {
676:                        TreeItem items[] = (TreeItem[]) event.getNewValue();
677:                        dataModel.setValue("property.value", new TableRow[] {});
678:                        if (items.length == 1 && items[0] != rootNode) {
679:                            WidgetUserObject object = (WidgetUserObject) items[0]
680:                                    .getUserObject();
681:                            table.setTableDataModel(object.tableModel);
682:                        } else {
683:                            table.setTableDataModel(emptyModel);
684:                        }
685:                        updateEnabledWidgets(null);
686:                    }
687:                }
688:            }
689:
690:            public void refreshInternationalProperties() throws GUIException {
691:                refreshInternationalProperties(rootNode);
692:            }
693:
694:            private void refreshInternationalProperties(TreeItem treeItem)
695:                    throws GUIException {
696:                WidgetUserObject object = (WidgetUserObject) treeItem
697:                        .getUserObject();
698:
699:                if (object.element != null) {
700:                    NodeList nodes = object.element.getChildNodes();
701:                    for (int i = 0; i < nodes.getLength(); i++) {
702:                        Node node = nodes.item(i);
703:                        if (node instanceof  Element
704:                                && node.getNodeName().equals("property")) {
705:                            Element propertyNode = (Element) node;
706:                            String type = propertyNode.getAttribute("type");
707:                            String name = propertyNode.getAttribute("name");
708:                            if (type.equals("") || type.equals("istring")) {
709:                                String text = XMLUtils
710:                                        .extractTextChildren(propertyNode);
711:                                try {
712:                                    object.widget.setProperty(name,
713:                                            InternationalizationManager
714:                                                    .getString(text));
715:                                } catch (Exception e) {
716:                                    log
717:                                            .warn(
718:                                                    "error while refreshing property ["
719:                                                            + name
720:                                                            + "] with internationalization ["
721:                                                            + text + "]", e);
722:                                }
723:                            }
724:                        }
725:                    }
726:                }
727:
728:                for (int i = 0; i < treeItem.getChildCount(); i++)
729:                    refreshInternationalProperties((TreeItem) treeItem
730:                            .getChild(i));
731:            }
732:
733:            private void updateEnabledWidgets(GUIEvent e) throws GUIException {
734:                TreeItem items[] = (TreeItem[]) dataModel
735:                        .getValue("tree.selected");
736:                if (items.length > 0) {
737:                    boolean notRoot = items.length > 1
738:                            || (items.length == 1 && items[0] != rootNode);
739:                    boolean moveUp = items.length == 1
740:                            && items[0] != rootNode
741:                            && (items[0].getParentWidget().getChildIndex(
742:                                    items[0]) > 0);
743:                    boolean moveDown = items.length == 1
744:                            && items[0] != rootNode
745:                            && (items[0].getParentWidget().getChildIndex(
746:                                    items[0]) < items[0].getParentWidget()
747:                                    .getChildCount() - 1);
748:                    frame.getChildWidgetByName("MoveUpButton").setEnabled(
749:                            moveUp);
750:                    frame.getChildWidgetByName("MoveDownButton").setEnabled(
751:                            moveDown);
752:                    frame.getChildWidgetByName("DeleteButton").setEnabled(
753:                            notRoot);
754:                    if (e != null && e.getName().equals("tree.popup")) {
755:                        treePopupMenu.getWidget("Delete").setEnabled(notRoot);
756:                        treePopupMenu.getWidget("MoveUp").setEnabled(moveUp);
757:                        treePopupMenu.getWidget("MoveDown")
758:                                .setEnabled(moveDown);
759:                        treePopupMenu.popup(e);
760:                    }
761:                }
762:            }
763:
764:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.