dimanche 10 mai 2015

Building a file explorer using Jtree

im supposed to create a GenralTree class with a GeneralTreeNode class with certain methods, and implement the GeneralTree in a GUI interface. we studied Trees and their structures and how they work, but we didn't take Jtrees thats why im struggling with it a bit. i found this code while exploring the web but i dont know how to fully manipulate it:

import java.awt.BorderLayout;
import java.io.File;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;

public class GTFrame extends JFrame {

  private JTree fileTree;
  private FileTreeModel FileTreeModel;
  private JTextArea fileDetailsTextArea = new JTextArea();
  public static String targetFile;
 // public JButton button = new JButton("dddd");

  public GTFrame(String directory) {
    super("My File Explorer");
    ////////
    fileDetailsTextArea.setEditable(false);
    FileTreeModel = new FileTreeModel(new File(directory));
    fileTree = new JTree(FileTreeModel);
    fileTree.setEditable(true);

    fileTree.addTreeSelectionListener(new TreeSelectionListener() {
      public void valueChanged(TreeSelectionEvent event) {
        File file = (File) fileTree.getLastSelectedPathComponent();
        fileDetailsTextArea.setText(getFileDetails(file));
      }
    });

    JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, new JScrollPane(
        fileTree), new JScrollPane(fileDetailsTextArea));
    setLayout(new BorderLayout());
    add(splitPane, BorderLayout.CENTER);
    //add(button, BorderLayout.NORTH);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(800, 600);
    setVisible(true);

  }

  private String getFileDetails(File file) {
    if (file == null)
      return "";
    StringBuffer buffer = new StringBuffer();
    buffer.append("Name: " + file.getName() + "\n");
    buffer.append("Path: " + file.getPath() + "\n");
    buffer.append("Size: " + file.length() + "\n");
    return buffer.toString();
  }

  public static void main(String args[]) {
      //System.out.println("enter file: ");
      //targetFile = new Scanner(System.in).nextLine();
    new GTFrame("c://");
  }
}

class FileTreeModel implements TreeModel {
  private File root;

  private Vector listeners = new Vector();

  public FileTreeModel(File rootDirectory) {
    root = rootDirectory;
  }

  public Object getRoot() {
    return root;
  }

  public Object getChild(Object parent, int index) {
    File directory = (File) parent;
    String[] children = directory.list();
    return new TreeFile(directory, children[index]);
  }

  public int getChildCount(Object parent) {
    File file = (File) parent;
    if (file.isDirectory()) {
      String[] fileList = file.list();
      if (fileList != null)
        return file.list().length;
    }
    return 0;
  }

  public boolean isLeaf(Object node) {
    File file = (File) node;
    return file.isFile();
  }

  public int getIndexOfChild(Object parent, Object child) {
    File directory = (File) parent;
    File file = (File) child;
    String[] children = directory.list();
    for (int i = 0; i < children.length; i++) {
      if (file.getName().equals(children[i])) {
        return i;
      }
    }
    return -1;

  }

  public void valueForPathChanged(TreePath path, Object value) {
    File oldFile = (File) path.getLastPathComponent();
    String fileParentPath = oldFile.getParent();
    String newFileName = (String) value;
    File targetFile = new File(fileParentPath, newFileName);
    oldFile.renameTo(targetFile);
    File parent = new File(fileParentPath);
    int[] changedChildrenIndices = { getIndexOfChild(parent, targetFile) };
    Object[] changedChildren = { targetFile };
    fireTreeNodesChanged(path.getParentPath(), changedChildrenIndices, changedChildren);

  }

  private void fireTreeNodesChanged(TreePath parentPath, int[] indices, Object[] children) {
    TreeModelEvent event = new TreeModelEvent(this, parentPath, indices, children);
    Iterator iterator = listeners.iterator();
    TreeModelListener listener = null;
    while (iterator.hasNext()) {
      listener = (TreeModelListener) iterator.next();
      listener.treeNodesChanged(event);
    }
  }

  public void addTreeModelListener(TreeModelListener listener) {
    listeners.add(listener);
  }

  public void removeTreeModelListener(TreeModelListener listener) {
    listeners.remove(listener);
  }

  private class TreeFile extends File {
    public TreeFile(File parent, String child) {
      super(parent, child);
    }

    public String toString() {
      return getName();
    }
  }
}

it displays and explores the files just fine, but i need to be able to do other properties too. it uses File class as the data model for the Jtree and since its the first time i saw this class i can't customize it well.

my question is can i use this class to implement the following methods: - void insert(GeneralTreeNode parentNode, GeneralTreeNode newNode). - String search(GeneralTreeNode newNode). - void delete(GeneralTreeNode existingNode). - sortByLevel(), which sorts each level of the GeneralTree according to Comparable. - traversing methods bfs, preorder, inorder and postorder. also im supposed to set a maximum for files viewed which can be changed by the user.

if File class is the wrong structure to implement the above methods please direct me to the correct method, im not asking for full solutions i just want some hints to go on from.

Aucun commentaire:

Enregistrer un commentaire