/* * The MIT License * * Copyright 2015 Jaafar EL Bakkali. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package ersn.openmc; /** * * @author Jaafar EL Bakkali */ import java.awt.BorderLayout; import java.awt.Dimension; import java.io.File; import java.util.Collections; import java.util.Vector; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTree; import javax.swing.event.TreeSelectionEvent; import javax.swing.event.TreeSelectionListener; import javax.swing.tree.DefaultMutableTreeNode; public class ERSNOpenMC_FileTree extends JPanel { /** Construct a FileTree * @param dir */ public ERSNOpenMC_FileTree(File dir) { setLayout(new BorderLayout()); JTree tree = new JTree(addNodes(null, dir)); tree.addTreeSelectionListener(new TreeSelectionListener() { @Override public void valueChanged(TreeSelectionEvent e) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) e .getPath().getLastPathComponent(); System.out.println("You selected " + node); } }); JScrollPane scrollpane = new JScrollPane(); scrollpane.getViewport().add(tree); add(BorderLayout.CENTER, scrollpane); } final DefaultMutableTreeNode addNodes(DefaultMutableTreeNode curTop, File dir) { String curPath = dir.getPath(); DefaultMutableTreeNode curDir = new DefaultMutableTreeNode(curPath); if (curTop != null) { curTop.add(curDir); } Vector ol = new Vector(); String[] tmp = dir.list(); for (int i = 0; i < tmp.length; i++) ol.addElement(tmp[i]); Collections.sort(ol, String.CASE_INSENSITIVE_ORDER); File f; Vector files = new Vector(); for (int i = 0; i < ol.size(); i++) { String thisObject = (String) ol.elementAt(i); String newPath; if (curPath.equals(".")) newPath = thisObject; else newPath = curPath + File.separator + thisObject; if ((f = new File(newPath)).isDirectory()) addNodes(curDir, f); else files.addElement(thisObject); } // Pass two: for files. for (int fnum = 0; fnum < files.size(); fnum++) curDir.add(new DefaultMutableTreeNode(files.elementAt(fnum))); return curDir; } public Dimension getMinimumSize() { return new Dimension(300, 350); } public Dimension getPreferredSize() { return new Dimension(300, 350); } /** Main: make a Frame, add a FileTree */ }