import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.tree.*;
import javax.swing.event.*;
import java.io.*;
import java.util.List;
import java.util.Vector;
class MainComponent extends JComponent implements TreeSelectionListener
{
private JTree data;
public final static long serialVersionUID = 2;
public MainComponent()
{
setLayout (new BorderLayout(15, 15));
DefaultMutableTreeNode root = new DefaultMutableTreeNode ("\\");
data = new JTree (root);
data.setCellRenderer(new Renderer());
data.setToggleClickCount(1);
DefaultTreeSelectionModel sm = new DefaultTreeSelectionModel ();
sm.setSelectionMode(DefaultTreeSelectionModel.SINGLE_TREE_SELECTION);
data.setSelectionModel(sm);
JScrollPane datapane = new JScrollPane (data, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
datapane.setPreferredSize(new Dimension (250, 0));
add (datapane, BorderLayout.CENTER);
File[] rootfils = File.listRoots();
for (File fil : rootfils){
DefaultMutableTreeNode node = new DefaultMutableTreeNode (fil);
root.add (node);
}
data.addTreeSelectionListener(this);
}
public void valueChanged (TreeSelectionEvent evt)
{
TreePath path = evt.getPath();
DefaultMutableTreeNode tn = (DefaultMutableTreeNode)path.getLastPathComponent();
if (tn.isRoot()){
return;
}
File fil = (File)tn.getUserObject();
if (!fil.isDirectory()){
// mit fil arbeiten
actualDir = null;
}else{
if (tn.isLeaf()){
File[] fils = fil.listFiles();
for (File f : fils){
if (f.isDirectory() && !f.getName().equals("System Volume Information")){
tn.add (new DefaultMutableTreeNode (f));
}else{
tn.add (new DefaultMutableTreeNode (f));
}
}
}
if (!tn.isLeaf()){
actualDir = tn;
}else{
actualDir = null;
}
}
}
private DefaultMutableTreeNode actualDir = null;
private String lastMessage = "";
public void searchSubNodes()
{
if (actualDir != null){
for (int i = actualDir.getChildCount(); i > 0; --i){
actualDir.remove(0);
}
data.setEnabled(false);
recSearchSubNodes (actualDir);
((DefaultTreeModel)data.getModel()).nodeStructureChanged(actualDir);
}
}
private void recSearchSubNodes(DefaultMutableTreeNode actualDir)
{
Object[] o = actualDir.getPath();
String spath = "";
for (Object x : o){
String news = x.toString();
if (!news.equals("\\")){
spath += news + (news.endsWith("\\") ? "" : "\\");
}
}
File fil = new File (spath);
System.out.println("rekursives Durchsuchen des Verzeichnisses: " + fil.getAbsolutePath());
File[] fils = fil.listFiles();
for (File f : fils){
if (f.isDirectory() && !f.getName().equals("System Volume Information")){
DefaultMutableTreeNode dir = new DefaultMutableTreeNode (f.getName());
actualDir.add (dir);
recSearchSubNodes (dir);
}else{
String s = f.getName();
if (true){ //Abfrage normal unnötig
DefaultMutableTreeNode filtn = new DefaultMutableTreeNode (MusicTitle.titleOf(f));
actualDir.add (filtn);
}
}
}
}
private class Renderer extends DefaultTreeCellRenderer
{
public static final long serialVersionUID = 10;
private Icon i = new ImageIcon ("music.gif");
private Icon i2 = new ImageIcon ("music_notsupported.gif");
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus)
{
super.getTreeCellRendererComponent (tree, value, selected, expanded, leaf, row, hasFocus);
try{
System.out.println(value.getClass().toString() );
File file = (File)((DefaultMutableTreeNode)value).getUserObject();
setIcon( javax.swing.filechooser.FileSystemView.getFileSystemView().getSystemIcon( file ));
setText( javax.swing.filechooser.FileSystemView.getFileSystemView().getSystemDisplayName( file ));
}catch (Exception e){
System.out.println(e );
}
return this;
}
}
public static void main (String[] args)
{
JFrame f = new JFrame();
f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
f.add (new MainComponent());
f.setSize (600, 800);
f.setVisible(true);
}
}