KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > designer > model > DesignerModel


1 /*
2  * (c) Rob Gordon 2005.
3  */

4 package org.oddjob.designer.model;
5
6 import java.io.ByteArrayOutputStream JavaDoc;
7 import java.util.Observable JavaDoc;
8
9 import org.apache.log4j.Logger;
10 import org.oddjob.arooa.ArooaContext;
11 import org.oddjob.arooa.handlers.MainHandler;
12 import org.oddjob.arooa.handlers.XmlHandler;
13 import org.oddjob.designer.DesignerXMLParser;
14 import org.oddjob.designer.arooa.DesignParser;
15 import org.xml.sax.SAXParseException JavaDoc;
16
17 /**
18  * Model for a designer session.
19  */

20 public class DesignerModel extends Observable JavaDoc {
21     private static final Logger logger = Logger.getLogger(DesignerModel.class);
22     
23     /** The tree model */
24     private final DesignTreeModel treeModel;
25     
26     /** The root component */
27     private final DesignComponent rootComponent;
28     
29     /** The current selected node */
30     private DesignTreeNode currentSelection;
31         
32     /**
33      * Constructor.
34      *
35      * @param start The root component.
36      */

37     public DesignerModel(DesignComponent start) {
38         this.rootComponent = start;
39         treeModel = new DesignTreeModel(start);
40         treeModel.build();
41         
42     }
43     
44     /**
45      * Get the root component.
46      * @return The DesignComponent which is the root.
47      */

48     public DesignComponent getRootComponent() {
49         return rootComponent;
50     }
51
52     /**
53      * Get the tree model.
54      *
55      * @return A TreeModel.
56      */

57     public DesignTreeModel getTreeModel() {
58         return treeModel;
59     }
60     
61     /**
62      * Set the currently selected node.
63      *
64      * @param node The node. May be null.
65      */

66     public void setCurrentSelection(DesignTreeNode node) {
67         this.currentSelection = node;
68         logger.debug("Current selection is [" + currentSelection + "]");
69         setChanged();
70         notifyObservers();
71     }
72     
73     /**
74      * Get the currently selected node.
75      *
76      * @return The currently selected node. Null if none.
77      */

78     public DesignTreeNode getCurrentSelection() {
79         return currentSelection;
80     }
81     
82     public DesignComponent getCurrentComponent() {
83         if (currentSelection == null) {
84             return null;
85         }
86         return currentSelection.getDesignComponent();
87     }
88     
89     public String JavaDoc getCurrentXML() {
90         if (currentSelection == null) {
91             return null;
92         }
93         DesignComponent component = currentSelection.getDesignComponent();
94
95         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
96         XmlHandler handler = new XmlHandler(out);
97         DesignParser dp = new DesignParser(new ArooaContext());
98         try {
99             dp.parse(component.tag(), component, new MainHandler(handler));
100         } catch (SAXParseException JavaDoc ex) {
101             ex.printStackTrace();
102         }
103         return out.toString();
104
105     }
106     
107     public void replaceSelected(String JavaDoc xml) {
108         StructuralDesignComponent parentComponent = (StructuralDesignComponent)
109         ((DesignTreeNode)
110                 currentSelection.getParent()).getDesignComponent();
111
112         DesignComponent dc = DesignerXMLParser.buildComponent(xml);
113         parentComponent.replaceChild(currentSelection.getDesignComponent(), dc);
114         this.treeModel.fireTreeNodesChanged(currentSelection);
115     }
116     
117     public void replaceSelected(DesignComponent dc) {
118         StructuralDesignComponent parentComponent = (StructuralDesignComponent)
119             getParentComponent(currentSelection);
120     
121         parentComponent.replaceChild(currentSelection.getDesignComponent(), dc);
122         this.treeModel.fireTreeNodesChanged(currentSelection);
123         
124     }
125     
126     public void delete(DesignTreeNode child) {
127         if (child == null) {
128             throw new NullPointerException JavaDoc("Child can not be null!");
129         }
130         StructuralDesignComponent parentComponent = (StructuralDesignComponent)
131                 getParentComponent(child);
132         parentComponent.deleteChild(child.getDesignComponent());
133     }
134     
135     public static DesignComponent getParentComponent(DesignTreeNode child) {
136         if (child == null) {
137             return null;
138         }
139         DesignTreeNode parent = (DesignTreeNode) child.getParent();
140         if (parent == null) {
141             return null;
142         }
143         return parent.getDesignComponent();
144         
145     }
146     
147     public static int getIndex(DesignTreeNode child) {
148         if (child == null) {
149             throw new NullPointerException JavaDoc("Child can't be null!");
150         }
151         return child.getParent().getIndex(child);
152     }
153 }
154
Popular Tags