KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > swingset > TreeDemo


1 /*
2  * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * -Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * -Redistribution in binary form must reproduct the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the distribution.
14  *
15  * Neither the name of Sun Microsystems, Inc. or the names of contributors
16  * may be used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * This software is provided "AS IS," without a warranty of any kind. ALL
20  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
21  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
22  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
23  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
24  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
25  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
26  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
27  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
28  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
29  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
30  *
31  * You acknowledge that Software is not designed, licensed or intended for
32  * use in the design, construction, operation or maintenance of any nuclear
33  * facility.
34  */

35
36 /*
37  * @(#)TreeDemo.java 1.7 03/01/23
38  */

39 package demo.swingset;
40
41 import swingwtx.swing.*;
42 import swingwtx.swing.event.*;
43 import swingwtx.swing.tree.*;
44 import javax.accessibility.*;
45
46 import swingwt.awt.*;
47 import swingwt.awt.event.*;
48 import java.beans.*;
49 import java.util.*;
50 import java.io.*;
51 import java.applet.*;
52 import java.net.*;
53
54 /**
55  * JTree Demo
56  *
57  * @version 1.7 01/23/03
58  * @author Jeff Dinkins
59  */

60 public class TreeDemo extends DemoModule {
61
62     /**
63      * main method allows us to run as a standalone demo.
64      */

65     public static void main(String JavaDoc[] args) {
66     TreeDemo demo = new TreeDemo(null);
67     demo.mainImpl();
68     }
69
70     /**
71      * TreeDemo Constructor
72      */

73     public TreeDemo(SwingSet2 swingset) {
74     // Set the title for this demo, and an icon used to represent this
75
// demo inside the SwingSet2 app.
76
super(swingset, "TreeDemo", "toolbar/JTree.gif");
77
78     getDemoPanel().add(createTree(), BorderLayout.CENTER);
79     }
80  
81     public JScrollPane createTree() {
82         DefaultMutableTreeNode top = new DefaultMutableTreeNode(getString("TreeDemo.music"));
83         DefaultMutableTreeNode catagory = null ;
84     DefaultMutableTreeNode artist = null;
85     DefaultMutableTreeNode record = null;
86
87     // open tree data
88
URL url = getClass().getResource("/resources/tree.txt");
89
90     try {
91         // convert url to buffered string
92
InputStream is = url.openStream();
93         InputStreamReader isr = new InputStreamReader(is);
94         BufferedReader reader = new BufferedReader(isr);
95
96         // read one line at a time, put into tree
97
String JavaDoc line = reader.readLine();
98         while(line != null) {
99         // System.out.println("reading in: ->" + line + "<-");
100
char linetype = line.charAt(0);
101         switch(linetype) {
102            case 'C':
103              catagory = new DefaultMutableTreeNode(line.substring(2));
104              top.add(catagory);
105              break;
106            case 'A':
107              if(catagory != null) {
108                  catagory.add(artist = new DefaultMutableTreeNode(line.substring(2)));
109              }
110              break;
111            case 'R':
112              if(artist != null) {
113                  artist.add(record = new DefaultMutableTreeNode(line.substring(2)));
114              }
115              break;
116            case 'S':
117              if(record != null) {
118                  record.add(new DefaultMutableTreeNode(line.substring(2)));
119              }
120              break;
121            default:
122              break;
123         }
124         line = reader.readLine();
125         }
126     } catch (IOException e) {
127     }
128
129     JTree tree = new JTree(top) {
130         public Insets getInsets() {
131         return new Insets(5,5,5,5);
132         }
133     };
134     return new JScrollPane(tree);
135     }
136
137 }
138
Popular Tags