Path: blob/master/src/demo/share/jfc/SwingSet2/TreeDemo.java
41149 views
/*1*2* Copyright (c) 2007, 2019, Oracle and/or its affiliates. All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7*8* - Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10*11* - Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* - Neither the name of Oracle nor the names of its16* contributors may be used to endorse or promote products derived17* from this software without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS20* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,21* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR22* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR23* 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, OR26* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF27* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING28* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS29* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.30*/313233import javax.swing.*;34import javax.swing.event.*;35import javax.swing.tree.*;36import javax.accessibility.*;3738import java.awt.*;39import java.awt.event.*;40import java.beans.*;41import java.util.*;42import java.io.*;43import java.applet.*;44import java.net.*;4546/**47* JTree Demo48*49* @author Jeff Dinkins50*/51public class TreeDemo extends DemoModule {5253JTree tree;5455/**56* main method allows us to run as a standalone demo.57*/58public static void main(String[] args) {59TreeDemo demo = new TreeDemo(null);60demo.mainImpl();61}6263/**64* TreeDemo Constructor65*/66public TreeDemo(SwingSet2 swingset) {67// Set the title for this demo, and an icon used to represent this68// demo inside the SwingSet2 app.69super(swingset, "TreeDemo", "toolbar/JTree.gif");7071getDemoPanel().add(createTree(), BorderLayout.CENTER);72}7374public JScrollPane createTree() {75DefaultMutableTreeNode top = new DefaultMutableTreeNode(getString("TreeDemo.music"));76DefaultMutableTreeNode category = null;77DefaultMutableTreeNode artist = null;78DefaultMutableTreeNode record = null;7980// open tree data81URL url = getClass().getResource("/resources/tree.txt");8283try {84// convert url to buffered string85InputStream is = url.openStream();86InputStreamReader isr = new InputStreamReader(is, "UTF-8");87BufferedReader reader = new BufferedReader(isr);8889// read one line at a time, put into tree90String line = reader.readLine();91while(line != null) {92// System.out.println("reading in: ->" + line + "<-");93char linetype = line.charAt(0);94switch(linetype) {95case 'C':96category = new DefaultMutableTreeNode(line.substring(2));97top.add(category);98break;99case 'A':100if(category != null) {101category.add(artist = new DefaultMutableTreeNode(line.substring(2)));102}103break;104case 'R':105if(artist != null) {106artist.add(record = new DefaultMutableTreeNode(line.substring(2)));107}108break;109case 'S':110if(record != null) {111record.add(new DefaultMutableTreeNode(line.substring(2)));112}113break;114default:115break;116}117line = reader.readLine();118}119} catch (IOException e) {120}121122tree = new JTree(top) {123public Insets getInsets() {124return new Insets(5,5,5,5);125}126};127128tree.setEditable(true);129130return new JScrollPane(tree);131}132133void updateDragEnabled(boolean dragEnabled) {134tree.setDragEnabled(dragEnabled);135}136137}138139140