jdk

Форк
0
/
TreeDemo.java 
137 строк · 4.7 Кб
1
/*
2
 * Copyright (c) 2007, 2021, Oracle and/or its affiliates. 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
 *   - Redistributions in binary form must reproduce the above copyright
12
 *     notice, this list of conditions and the following disclaimer in the
13
 *     documentation and/or other materials provided with the distribution.
14
 *
15
 *   - Neither the name of Oracle nor the names of its
16
 *     contributors may be used to endorse or promote products derived
17
 *     from this software without specific prior written permission.
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
 */
31

32
import javax.swing.JScrollPane;
33
import javax.swing.JTree;
34
import javax.swing.tree.DefaultMutableTreeNode;
35

36
import java.awt.BorderLayout;
37
import java.awt.Insets;
38
import java.io.BufferedReader;
39
import java.io.IOException;
40
import java.io.InputStream;
41
import java.io.InputStreamReader;
42
import java.net.URL;
43

44
import static java.nio.charset.StandardCharsets.UTF_8;
45

46
/**
47
 * JTree Demo
48
 *
49
 * @author Jeff Dinkins
50
 */
51
public class TreeDemo extends DemoModule {
52

53
    JTree tree;
54

55
    /**
56
     * main method allows us to run as a standalone demo.
57
     */
58
    public static void main(String[] args) {
59
        TreeDemo demo = new TreeDemo(null);
60
        demo.mainImpl();
61
    }
62

63
    /**
64
     * TreeDemo Constructor
65
     */
66
    public TreeDemo(SwingSet2 swingset) {
67
        // Set the title for this demo, and an icon used to represent this
68
        // demo inside the SwingSet2 app.
69
        super(swingset, "TreeDemo", "toolbar/JTree.gif");
70

71
        getDemoPanel().add(createTree(), BorderLayout.CENTER);
72
    }
73

74
    public JScrollPane createTree() {
75
        DefaultMutableTreeNode top = new DefaultMutableTreeNode(getString("TreeDemo.music"));
76
        DefaultMutableTreeNode category = null;
77
        DefaultMutableTreeNode artist = null;
78
        DefaultMutableTreeNode record = null;
79

80
        // open tree data
81
        URL url = getClass().getResource("/resources/tree.txt");
82

83
        try {
84
            // convert url to buffered string
85
            InputStream is = url.openStream();
86
            InputStreamReader isr = new InputStreamReader(is, UTF_8);
87
            BufferedReader reader = new BufferedReader(isr);
88

89
            // read one line at a time, put into tree
90
            String line = reader.readLine();
91
            while(line != null) {
92
                // System.out.println("reading in: ->" + line + "<-");
93
                char linetype = line.charAt(0);
94
                switch(linetype) {
95
                   case 'C':
96
                     category = new DefaultMutableTreeNode(line.substring(2));
97
                     top.add(category);
98
                     break;
99
                   case 'A':
100
                     if(category != null) {
101
                         category.add(artist = new DefaultMutableTreeNode(line.substring(2)));
102
                     }
103
                     break;
104
                   case 'R':
105
                     if(artist != null) {
106
                         artist.add(record = new DefaultMutableTreeNode(line.substring(2)));
107
                     }
108
                     break;
109
                   case 'S':
110
                     if(record != null) {
111
                         record.add(new DefaultMutableTreeNode(line.substring(2)));
112
                     }
113
                     break;
114
                   default:
115
                     break;
116
                }
117
                line = reader.readLine();
118
            }
119
        } catch (IOException e) {
120
        }
121

122
        tree = new JTree(top) {
123
            public Insets getInsets() {
124
                return new Insets(5,5,5,5);
125
            }
126
        };
127

128
        tree.setEditable(true);
129

130
        return new JScrollPane(tree);
131
    }
132

133
    void updateDragEnabled(boolean dragEnabled) {
134
        tree.setDragEnabled(dragEnabled);
135
    }
136

137
}
138

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.