| TreeDemo.java |
1 /*
2 * @(#)TreeDemo.java 1.10 04/07/26
3 *
4 * Copyright (c) 2004 Sun Microsystems, Inc. All Rights Reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * -Redistribution of source code must retain the above copyright notice, this
10 * list of conditions and the following disclaimer.
11 *
12 * -Redistribution in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17 * be used to endorse or promote products derived from this software without
18 * specific prior written permission.
19 *
20 * This software is provided "AS IS," without a warranty of any kind. ALL
21 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22 * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23 * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24 * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25 * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27 * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28 * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29 * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31 *
32 * You acknowledge that this software is not designed, licensed or intended
33 * for use in the design, construction, operation or maintenance of any
34 * nuclear facility.
35 */
36
37 /*
38 * @(#)TreeDemo.java 1.10 04/07/26
39 */
40
41
42 import javax.swing.*;
43 import javax.swing.event.*;
44 import javax.swing.tree.*;
45 import javax.accessibility.*;
46
47 import java.awt.*;
48 import java.awt.event.*;
49 import java.beans.*;
50 import java.util.*;
51 import java.io.*;
52 import java.applet.*;
53 import java.net.*;
54
55 /**
56 * JTree Demo
57 *
58 * @version 1.10 07/26/04
59 * @author Jeff Dinkins
60 */
61 public class TreeDemo extends DemoModule {
62
63 JTree tree;
64
65 /**
66 * main method allows us to run as a standalone demo.
67 */
68 public static void main(String[] args) {
69 TreeDemo demo = new TreeDemo(null);
70 demo.mainImpl();
71 }
72
73 /**
74 * TreeDemo Constructor
75 */
76 public TreeDemo(SwingSet2 swingset) {
77 // Set the title for this demo, and an icon used to represent this
78 // demo inside the SwingSet2 app.
79 super(swingset, "TreeDemo", "toolbar/JTree.gif");
80
81 getDemoPanel().add(createTree(), BorderLayout.CENTER);
82 }
83
84 public JScrollPane createTree() {
85 DefaultMutableTreeNode top = new DefaultMutableTreeNode(getString("TreeDemo.music"));
86 DefaultMutableTreeNode catagory = null ;
87 DefaultMutableTreeNode artist = null;
88 DefaultMutableTreeNode record = null;
89
90 // open tree data
91 URL url = getClass().getResource("/resources/tree.txt");
92
93 try {
94 // convert url to buffered string
95 InputStream is = url.openStream();
96 InputStreamReader isr = new InputStreamReader(is);
97 BufferedReader reader = new BufferedReader(isr);
98
99 // read one line at a time, put into tree
100 String line = reader.readLine();
101 while(line != null) {
102 // System.out.println("reading in: ->" + line + "<-");
103 char linetype = line.charAt(0);
104 switch(linetype) {
105 case 'C':
106 catagory = new DefaultMutableTreeNode(line.substring(2));
107 top.add(catagory);
108 break;
109 case 'A':
110 if(catagory != null) {
111 catagory.add(artist = new DefaultMutableTreeNode(line.substring(2)));
112 }
113 break;
114 case 'R':
115 if(artist != null) {
116 artist.add(record = new DefaultMutableTreeNode(line.substring(2)));
117 }
118 break;
119 case 'S':
120 if(record != null) {
121 record.add(new DefaultMutableTreeNode(line.substring(2)));
122 }
123 break;
124 default:
125 break;
126 }
127 line = reader.readLine();
128 }
129 } catch (IOException e) {
130 }
131
132 tree = new JTree(top) {
133 public Insets getInsets() {
134 return new Insets(5,5,5,5);
135 }
136 };
137
138 tree.setEditable(true);
139
140 return new JScrollPane(tree);
141 }
142
143 void updateDragEnabled(boolean dragEnabled) {
144 tree.setDragEnabled(dragEnabled);
145 }
146
147 }
148