1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
| import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.Enumeration;
import javax.swing.event.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
public class Tree extends JFrame implements TreeSelectionListener {
private JTree arbre;
private JToolBar barre = new JToolBar();
private JTextField jtf = new JTextField("");
private JButton bouton= new JButton("Search");
public Tree() {
super("DDSM");
bouton.addActionListener(new BoutonListener());
Font police = new Font("Arial", Font.BOLD, 14);
jtf.setFont(police);
jtf.setPreferredSize(new Dimension(150, 30));
jtf.setForeground(Color.BLUE);
jtf.setPreferredSize(new Dimension(150, 30));
jtf.setEditable( true );
barre.add(jtf);
barre.add(bouton);
construireArbre();
add(barre, BorderLayout.SOUTH);
add(new JScrollPane(arbre), BorderLayout.WEST);
setSize(540, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
class BoutonListener implements ActionListener{
String word;
public void actionPerformed(ActionEvent e) {
if(e.getSource() == bouton)
{
word=jtf.getText();
String[] split = word.split("");
for (int i = 0; i < split.length; i++) {
word= split[i];
}
findByName(arbre,split);
}
}
}
public static void main(String[] args) {
new Tree();
}
private void construireArbre() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("DDSM");
String[] volums = DDsm.getVolums();
for (int i = 0; i < volums.length; i++) {
DefaultMutableTreeNode volume = new DefaultMutableTreeNode(volums[i],true);
// creation des noeuds case
String[] cases = DDsm.getCases(volums[i]);
for (int j = 0; j < cases.length; j++) {
DefaultMutableTreeNode ccase = new DefaultMutableTreeNode(cases[j],true);
// creation des noeuds fichier
String[] files = DDsm.getFiles(volums[i], cases[j]);
for (int k = 0; k < files.length; k++) {
ccase.add(new DefaultMutableTreeNode(files[k],false));
}
volume.add(ccase);
}
root.add(volume);
}
DefaultTreeModel treeModel = new DefaultTreeModel(root);
arbre = new JTree(root,true);
arbre.setPreferredSize(new Dimension(300, 1000));
arbre.addTreeSelectionListener(this);
arbre.setEditable(true);
}
public TreePath findByName(JTree tree, String[] names) {
TreeNode root = (TreeNode) tree.getModel().getRoot();
return find(new TreePath(root), names, 0, true);
}
private TreePath find(TreePath parent, Object[] nodes, int depth, boolean byName) {
TreeNode node = (TreeNode) parent.getLastPathComponent();
Object o = node;
if(o == null) return null;
// If by name, convert node to a string
if (byName)
o = o.toString();
// If equal, go down the branch
if (o.equals(nodes[depth])) {
// If at end, return match
if (depth == nodes.length - 1)
return parent;
// Traverse children
if (node.getChildCount() >= 0)
for (Enumeration e = node.children(); e.hasMoreElements(); ) {
TreeNode n = (TreeNode) e.nextElement();
TreePath path = parent.pathByAddingChild(n);
TreePath result = find(path, nodes, depth + 1, byName);
// Found a match
if (result != null)
return result;
}
}
// No match at this branch
return null;
}
@Override
public void valueChanged(TreeSelectionEvent arg0) {
// TODO Auto-generated method stub
}
} |
Partager