KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > finalist > jag > template > parser > JagBlockViewer


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.parser;
19
20 import javax.swing.*;
21 import javax.swing.event.*;
22 import javax.swing.tree.*;
23 import javax.accessibility.*;
24 import java.awt.*;
25 import java.awt.event.*;
26 import java.beans.*;
27 import java.util.*;
28 import java.io.*;
29 import java.applet.*;
30 import java.net.*;
31
32 /**
33  * Class JagBlockViewer
34  *
35  * Test class for viewing the JagBlockTree structure in a Swing component.
36  *
37  * @author Wendel D. de Witte
38  * @version %I%, %G%
39  */

40 public class JagBlockViewer extends javax.swing.JFrame JavaDoc {
41    /** Field rootBlock */
42    JagBlock rootBlock = null;
43
44    /** Field buffer */
45    StringBuffer JavaDoc buffer;
46
47
48    /**
49     * Constructor JagBlockTree
50     *
51     *
52     * @param rootBlock
53     *
54     */

55    public JagBlockViewer(JagBlock rootBlock) {
56       buffer = new StringBuffer JavaDoc();
57       this.rootBlock = rootBlock;
58
59       getContentPane().add(createTree(), BorderLayout.CENTER);
60       setSize(400, 400);
61       setVisible(true);
62    }
63
64
65    /**
66     * Method getTextString
67     *
68     *
69     * @return
70     *
71     */

72    public final String JavaDoc getTextString() {
73       return buffer.toString();
74    }
75
76
77    /**
78     * Method createTree
79     *
80     *
81     * @return
82     *
83     */

84    public JScrollPane createTree() {
85       DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
86       createTree(root, rootBlock);
87
88       JTree tree = new JTree(root) {
89          public Insets getInsets() {
90             return new Insets(5, 5, 5, 5);
91          }
92       };
93
94       return new JScrollPane(tree);
95    }
96
97
98    /**
99     * Method createTree
100     *
101     *
102     * @param parent
103     * @param block
104     *
105     */

106    protected void createTree(DefaultMutableTreeNode parent, JagBlock block) {
107       JagBlock blockChild = block.getFirstChild();
108
109       while (blockChild != null) {
110          DefaultMutableTreeNode child =
111             new DefaultMutableTreeNode(blockChild.getText());
112
113          if (blockChild.getType() == JagParserConstants.TAGSTART) {
114             buffer.append("TAG");
115          }
116
117          if (blockChild.getType() == JagParserConstants.TEXT) {
118             buffer.append(blockChild.getText());
119          }
120
121          createTree(child, blockChild);
122          parent.add(child);
123
124          blockChild = blockChild.getNextSibling();
125       }
126    }
127 }
Popular Tags