36744
|
1 |
/*
|
|
2 |
* Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
package com.sun.swingset3.demos.tree;
|
|
24 |
|
|
25 |
import java.awt.*;
|
|
26 |
import java.io.BufferedReader;
|
|
27 |
import java.io.IOException;
|
|
28 |
import java.io.InputStream;
|
|
29 |
import java.io.InputStreamReader;
|
|
30 |
import java.net.URL;
|
|
31 |
import javax.swing.*;
|
|
32 |
import javax.swing.tree.DefaultMutableTreeNode;
|
|
33 |
|
|
34 |
import com.sun.swingset3.DemoProperties;
|
|
35 |
import com.sun.swingset3.demos.ResourceManager;
|
|
36 |
|
|
37 |
/**
|
|
38 |
* JTree Demo
|
|
39 |
*
|
|
40 |
* @version 1.13 11/17/05
|
|
41 |
* @author Jeff Dinkins
|
|
42 |
*/
|
|
43 |
@DemoProperties(
|
|
44 |
value = "JTree Demo",
|
|
45 |
category = "Data",
|
|
46 |
description = "Demonstrates JTree, a component which supports display/editing of hierarchical data",
|
|
47 |
sourceFiles = {
|
|
48 |
"com/sun/swingset3/demos/tree/TreeDemo.java",
|
|
49 |
"com/sun/swingset3/demos/ResourceManager.java",
|
|
50 |
"com/sun/swingset3/demos/tree/resources/tree.txt",
|
|
51 |
"com/sun/swingset3/demos/tree/resources/TreeDemo.properties",
|
|
52 |
"com/sun/swingset3/demos/tree/resources/images/TreeDemo.gif"
|
|
53 |
}
|
|
54 |
)
|
|
55 |
public class TreeDemo extends JPanel {
|
|
56 |
|
|
57 |
private final ResourceManager resourceManager = new ResourceManager(this.getClass());
|
|
58 |
public static final String DEMO_TITLE = TreeDemo.class.getAnnotation(DemoProperties.class).value();
|
|
59 |
|
|
60 |
/**
|
|
61 |
* main method allows us to run as a standalone demo.
|
|
62 |
*
|
|
63 |
* @param args
|
|
64 |
*/
|
|
65 |
public static void main(String[] args) {
|
|
66 |
JFrame frame = new JFrame(DEMO_TITLE);
|
|
67 |
|
|
68 |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
69 |
frame.getContentPane().add(new TreeDemo());
|
|
70 |
frame.setPreferredSize(new Dimension(800, 600));
|
|
71 |
frame.pack();
|
|
72 |
frame.setLocationRelativeTo(null);
|
|
73 |
frame.setVisible(true);
|
|
74 |
}
|
|
75 |
|
|
76 |
/**
|
|
77 |
* TreeDemo Constructor
|
|
78 |
*/
|
|
79 |
public TreeDemo() {
|
|
80 |
setLayout(new BorderLayout());
|
|
81 |
|
|
82 |
add(new JScrollPane(createTree()), BorderLayout.CENTER);
|
|
83 |
}
|
|
84 |
|
|
85 |
private JTree createTree() {
|
|
86 |
DefaultMutableTreeNode top = new DefaultMutableTreeNode(resourceManager.getString("TreeDemo.music"));
|
|
87 |
DefaultMutableTreeNode catagory = null;
|
|
88 |
DefaultMutableTreeNode artist = null;
|
|
89 |
DefaultMutableTreeNode record = null;
|
|
90 |
|
|
91 |
// open tree data
|
|
92 |
URL url = getClass().getResource("resources/tree.txt");
|
|
93 |
|
|
94 |
try {
|
|
95 |
// convert url to buffered string
|
|
96 |
InputStream is = url.openStream();
|
|
97 |
InputStreamReader isr = new InputStreamReader(is, "UTF-8");
|
|
98 |
BufferedReader reader = new BufferedReader(isr);
|
|
99 |
|
|
100 |
// read one line at a time, put into tree
|
|
101 |
String line = reader.readLine();
|
|
102 |
while (line != null) {
|
|
103 |
// System.out.println("reading in: ->" + line + "<-");
|
|
104 |
char linetype = line.charAt(0);
|
|
105 |
switch (linetype) {
|
|
106 |
case 'C':
|
|
107 |
catagory = new DefaultMutableTreeNode(line.substring(2));
|
|
108 |
top.add(catagory);
|
|
109 |
break;
|
|
110 |
case 'A':
|
|
111 |
if (catagory != null) {
|
|
112 |
catagory.add(artist = new DefaultMutableTreeNode(line.substring(2)));
|
|
113 |
}
|
|
114 |
break;
|
|
115 |
case 'R':
|
|
116 |
if (artist != null) {
|
|
117 |
artist.add(record = new DefaultMutableTreeNode(line.substring(2)));
|
|
118 |
}
|
|
119 |
break;
|
|
120 |
case 'S':
|
|
121 |
if (record != null) {
|
|
122 |
record.add(new DefaultMutableTreeNode(line.substring(2)));
|
|
123 |
}
|
|
124 |
break;
|
|
125 |
default:
|
|
126 |
break;
|
|
127 |
}
|
|
128 |
line = reader.readLine();
|
|
129 |
}
|
|
130 |
} catch (IOException ignored) {
|
|
131 |
}
|
|
132 |
|
|
133 |
JTree tree = new JTree(top) {
|
|
134 |
@Override
|
|
135 |
public Insets getInsets() {
|
|
136 |
return new Insets(5, 5, 5, 5);
|
|
137 |
}
|
|
138 |
};
|
|
139 |
|
|
140 |
tree.setEditable(true);
|
|
141 |
|
|
142 |
return tree;
|
|
143 |
}
|
|
144 |
}
|