import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JFileChooser;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import java.io.File;
import java.awt.Toolkit;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class DirectoryTreeExample {
    
    public static void main(String[] args) {
        new GUIInteraction();
    } // end main
    
} // end DirectoryTreeExample

class GUIInteraction {
    JFrame theFrame;
    JTextArea outArea;
    JFileChooser chooser;
    final int WIDTH_SCALE = 10;
    final int HEIGHT_SCALE = 25;
    
    GUIInteraction() {
        theFrame = new JFrame();
        Toolkit tk = Toolkit.getDefaultToolkit();
        theFrame.setSize(tk.getScreenSize());
        int width = (int)(tk.getScreenSize().getWidth());
        int height = (int)(tk.getScreenSize().getHeight());
        theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container C = theFrame.getContentPane();
        C.setLayout(new FlowLayout(FlowLayout.CENTER,700,10));
        JButton chooseButton = new JButton("Click to choose a directory");
        chooseButton.addActionListener(new ChooseAction());
        C.add(chooseButton);
        outArea = new JTextArea(height/HEIGHT_SCALE,width/WIDTH_SCALE);
        outArea.setFont(new Font("Courier",Font.BOLD,15));
        JScrollPane outAreaPane = new JScrollPane(outArea);
        C.add(outAreaPane);
        theFrame.setVisible(true);
        chooser = new JFileChooser("C:\\");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    }
    
    class ChooseAction implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            chooser.showOpenDialog(null);
            File directory = chooser.getSelectedFile();
            if( directory == null ) return;
            DirectoryTree directoryTree = new DirectoryTree(directory);
            outArea.setText(directoryTree.toString());
            outArea.setCaretPosition(0);
        }
    } // end ChooseAction
    
} // end GUIInteraction
