1 /*
   2  *
   3  * Copyright (c) 2007, 2019, Oracle and/or its affiliates. All rights reserved.
   4  *
   5  * Redistribution and use in source and binary forms, with or without
   6  * modification, are permitted provided that the following conditions
   7  * are met:
   8  *
   9  *   - Redistributions of source code must retain the above copyright
  10  *     notice, this list of conditions and the following disclaimer.
  11  *
  12  *   - Redistributions in binary form must reproduce the above copyright
  13  *     notice, this list of conditions and the following disclaimer in the
  14  *     documentation and/or other materials provided with the distribution.
  15  *
  16  *   - Neither the name of Oracle nor the names of its
  17  *     contributors may be used to endorse or promote products derived
  18  *     from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  21  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31  */
  32 
  33 
  34 import javax.swing.*;
  35 import javax.swing.event.*;
  36 import javax.swing.tree.*;
  37 import javax.accessibility.*;
  38 
  39 import java.awt.*;
  40 import java.awt.event.*;
  41 import java.beans.*;
  42 import java.util.*;
  43 import java.io.*;
  44 import java.applet.*;
  45 import java.net.*;
  46 
  47 /**
  48  * JTree Demo
  49  *
  50  * @author Jeff Dinkins
  51  */
  52 public class TreeDemo extends DemoModule {
  53 
  54     JTree tree;
  55 
  56     /**
  57      * main method allows us to run as a standalone demo.
  58      */
  59     public static void main(String[] args) {
  60         TreeDemo demo = new TreeDemo(null);
  61         demo.mainImpl();
  62     }
  63 
  64     /**
  65      * TreeDemo Constructor
  66      */
  67     public TreeDemo(SwingSet2 swingset) {
  68         // Set the title for this demo, and an icon used to represent this
  69         // demo inside the SwingSet2 app.
  70         super(swingset, "TreeDemo", "toolbar/JTree.gif");
  71 
  72         getDemoPanel().add(createTree(), BorderLayout.CENTER);
  73     }
  74 
  75     public JScrollPane createTree() {
  76         DefaultMutableTreeNode top = new DefaultMutableTreeNode(getString("TreeDemo.music"));
  77         DefaultMutableTreeNode category = null;
  78         DefaultMutableTreeNode artist = null;
  79         DefaultMutableTreeNode record = null;
  80 
  81         // open tree data
  82         URL url = getClass().getResource("/resources/tree.txt");
  83 
  84         try {
  85             // convert url to buffered string
  86             InputStream is = url.openStream();
  87             InputStreamReader isr = new InputStreamReader(is, "UTF-8");
  88             BufferedReader reader = new BufferedReader(isr);
  89 
  90             // read one line at a time, put into tree
  91             String line = reader.readLine();
  92             while(line != null) {
  93                 // System.out.println("reading in: ->" + line + "<-");
  94                 char linetype = line.charAt(0);
  95                 switch(linetype) {
  96                    case 'C':
  97                      category = new DefaultMutableTreeNode(line.substring(2));
  98                      top.add(category);
  99                      break;
 100                    case 'A':
 101                      if(category != null) {
 102                          category.add(artist = new DefaultMutableTreeNode(line.substring(2)));
 103                      }
 104                      break;
 105                    case 'R':
 106                      if(artist != null) {
 107                          artist.add(record = new DefaultMutableTreeNode(line.substring(2)));
 108                      }
 109                      break;
 110                    case 'S':
 111                      if(record != null) {
 112                          record.add(new DefaultMutableTreeNode(line.substring(2)));
 113                      }
 114                      break;
 115                    default:
 116                      break;
 117                 }
 118                 line = reader.readLine();
 119             }
 120         } catch (IOException e) {
 121         }
 122 
 123         tree = new JTree(top) {
 124             public Insets getInsets() {
 125                 return new Insets(5,5,5,5);
 126             }
 127         };
 128 
 129         tree.setEditable(true);
 130 
 131         return new JScrollPane(tree);
 132     }
 133 
 134     void updateDragEnabled(boolean dragEnabled) {
 135         tree.setDragEnabled(dragEnabled);
 136     }
 137 
 138 }