KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > finalist > jag > template > TemplateStructure


1 /* Copyright (C) 2003 Finalist IT Group
2  *
3  * This file is part of JAG - the Java J2EE Application Generator
4  *
5  * JAG is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * JAG is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with JAG; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */

17
18 package com.finalist.jag.template;
19
20
21 import java.util.*;
22
23 import com.finalist.jag.util.*;
24
25
26 /**
27  * Class TemplateStructure
28  *
29  *
30  * @author Wendel D. de Witte
31  * @version %I%, %G%
32  */

33 public class TemplateStructure implements Cloneable JavaDoc {
34
35    /** Field rootNode */
36    private TemplateTreeItem rootNode = null;
37
38
39    /**
40     * Constructor TemplateStructure
41     *
42     *
43     */

44    public TemplateStructure() {
45    }
46
47
48    /**
49     * Constructor TemplateStructure
50     *
51     *
52     * @param n
53     *
54     */

55    public TemplateStructure(TemplateStructure n) {
56       rootNode = new TemplateTreeItem(n.getRoot());
57    }
58
59
60    /**
61     * Method setRoot
62     *
63     *
64     * @param rootNode
65     *
66     */

67    public void setRoot(TemplateTreeItem rootNode) {
68       this.rootNode = rootNode;
69    }
70
71
72    /**
73     * Method getRoot
74     *
75     *
76     * @return
77     *
78     */

79    public final TemplateTreeItem getRoot() {
80
81       if (rootNode == null) {
82          rootNode = new TemplateTreeItem();
83       }
84
85       return rootNode;
86    }
87
88
89    /**
90     * Method getTextBlockList
91     *
92     *
93     * @return
94     *
95     */

96    public final TemplateTextBlockList getTextBlockList() {
97
98       TemplateTextBlockList list = new TemplateTextBlockList();
99
100       getTextBlockList(list, getRoot());
101
102       return list;
103    }
104
105
106    /**
107     * Method cut
108     *
109     * Requirement : both nodes must be in the same branche.
110     *
111     * @param first
112     * @param last
113     *
114     * @return
115     *
116     */

117    public TemplateStructure cut(TemplateTreeItem first,
118       TemplateTreeItem last) {
119
120       TemplateStructure returnValue = new TemplateStructure();
121       TemplateTreeItem tmp = first;
122
123       if (((first == null) || (last == null))
124          || (first.getParent() != last.getParent())) {
125          return returnValue;
126       }
127
128       // Disconnect first and last from the hive in order to
129
// create their own collective.
130
if (first.getPrevSibling() != null) {
131          TreeItem prevItem = first.getPrevSibling();
132
133          prevItem.setNextSibling(last.getNextSibling());
134       }
135       else if (first.getParent() != null) {
136          TreeItem parentItem = first.getParent();
137
138          parentItem.setFirstChild(last.getNextSibling());
139       }
140
141       last.disconnectSiblings();
142
143       // return the new hive
144
returnValue.getRoot().setFirstChild(first);
145
146       return returnValue;
147    }
148
149
150    /**
151     * Method getTextBlockList
152     *
153     *
154     * @param list
155     * @param item
156     *
157     */

158    private void getTextBlockList(TemplateTextBlockList list,
159       TemplateTreeItem item) {
160
161       if (item.getTextBlock() != null) {
162          list.add(item.getTextBlock());
163       }
164
165       TreeItem childItem = item.getFirstChild();
166
167       while (childItem != null) {
168          getTextBlockList(list, (TemplateTreeItem) childItem);
169
170          childItem = childItem.getNextSibling();
171       }
172    }
173
174
175    /**
176     * Method getTemplateTreeItem
177     *
178     *
179     * @param textBlock
180     *
181     * @return
182     *
183     */

184    public TemplateTreeItem getTemplateTreeItem(TemplateTextBlock textBlock) {
185
186       Iterator iterator = getCollection().iterator();
187
188       while (iterator.hasNext()) {
189          TemplateTreeItem item = (TemplateTreeItem) iterator.next();
190
191          if (item.getTextBlock() == textBlock) {
192             return item;
193          }
194       }
195
196       return null;
197    }
198
199
200    /**
201     * Method getCollection
202     *
203     *
204     * @return
205     *
206     */

207    public Collection getCollection() {
208
209       LinkedList list = new LinkedList();
210
211       getCollection(list, getRoot());
212
213       return list;
214    }
215
216
217    /**
218     * Method getCollection
219     *
220     *
221     * @param list
222     * @param item
223     *
224     */

225    private void getCollection(LinkedList list, TemplateTreeItem item) {
226
227       list.add(item);
228
229       TreeItem childItem = item.getFirstChild();
230
231       while (childItem != null) {
232          getCollection(list, (TemplateTreeItem) childItem);
233
234          childItem = childItem.getNextSibling();
235       }
236    }
237
238
239    /**
240     * Method clearBodyText
241     *
242     *
243     *
244     */

245    public void clearBodyText() {
246       clearBodyText(rootNode);
247    }
248
249
250    /**
251     * Method clearBodyText
252     *
253     *
254     *
255     */

256    private void clearBodyText(TemplateTreeItem parentItem) {
257
258       if (parentItem == null) {
259          return;
260       }
261       parentItem.getTextBlock().set();
262
263       TemplateTreeItem templateItem =
264          (TemplateTreeItem) parentItem.getFirstChild();
265
266       while (templateItem != null) {
267          clearBodyText(templateItem);
268          templateItem = (TemplateTreeItem) templateItem.getNextSibling();
269       }
270    }
271 }
272
Popular Tags