2017-03-06 11 views
1

내 프로그램에서 운영 체제 디스크 이미지의 파일을 가져 와서 데이터베이스에 세부 정보를 저장하고 다른 기능이 데이터베이스에서 파일을 가져 와서 List (내 자신의 객체 유형)이며 경로를 가져 와서 (데이터베이스에도 저장되어 있음) JTree를 만들고 경로 문자열을 슬래시로 분할하여 다양한 폴더 내부의 파일이 리프 노드이고 폴더가 부모 인 경우 JTree를 생성해야합니다. 파일을 가져 오는 작업은 완벽하지만 JTree에서 일부 폴더가 JTree의 리프 노드로 표시되기 때문에 JTree를 만드는 데 문제가 있다고 생각합니다. 여기에 스크린 샷입니다 :JTree가 리프 노드로 확장 할 수있는 노드를 표시합니다.

you can see the /home and /usr folders are terminal nodes

내 코드가 어떻게 생겼는지 말해 보자 같은 :

public class ActiveCase { 
private void populateList(){ 
    //a function that fetches a list of files from a database and stores it in a list 
List<AbstractFile> l = *a query function that stores the files in the list*; 
listFiles(l); 
} 

public void listFiles(List<AbstractFile> fl){  
    ArrayList<TreePath> te = new ArrayList<TreePath>(); 
    try {   
     for(AbstractFile file : fl){ 
     //don't worry about the isDir() and getName() functions they work        
     if(!file.isDir() && !file.getName().startsWith(".")){           
     te.add(new TreePath(("File list"+formatPath(file.getUniquePath())).split("/"))); 
     } 
    } 
    } catch (TskCoreException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

     TreeModel tmClean = new TreePathsTreeModel("File list", te); 
     cleanPanel.add(new JTree(tmClean)); 
} 

private static String formatPath(String uniquePath){ 
    String[] pathSegments = uniquePath.split("/"); 
    int index = 3; 
    StringBuilder strbuf = new StringBuilder(); 
    for (; index < pathSegments.length; ++index) { 
     if (!pathSegments[index].isEmpty()) { 
      strbuf.append("/").append(pathSegments[index]); 
     } 
    } 
    return strbuf.toString(); 
}   

}

내가 TreeModel를 구현하는 TreePathsTreeModel 클래스있어 :

public class TreePathsTreeModel implements TreeModel { 
     private final ArrayList<TreePath> paths; 
     private final String root; 

     public TreePathsTreeModel(String root, ArrayList<TreePath> te) { 
     this.root = root; 
     this.paths = te; 
     //a little test print statement to see if the TreePath list is in the right format 
     for(TreePath t : paths){ 
      System.out.println(t.toString()); 
     } 
     } 
     @Override 
     public Object getRoot() { 
     return this.root; 
     } 

     @Override 
     public Object getChild(Object parent, int index) { 
     try { 
      return getChildren(parent).get(index); 
     } catch (IndexOutOfBoundsException ex) { 
      return null; 
     } 
     } 

     @Override 
     public int getChildCount(Object parent) { 
     return getChildren(parent).size(); 
     } 

     @Override 
     public boolean isLeaf(Object node) { 
     for (int i = 0; i < paths.size(); i++) { 
      TreePath treePath = paths.get(i); 
      if (treePath.getLastPathComponent().equals(node)) 
      return true; 
     } 
     return false; 
     } 

     // This method is only required for editable trees, so it is not 
     // implemented here. 
     @Override 
     public void valueForPathChanged(TreePath path, Object newValue) { 
     //throw new UnsupportedOperationException("Not supported yet."); 
     } 

     @Override 
     public int getIndexOfChild(Object parent, Object child) { 
     return getChildren(parent).indexOf(child); 
     } 

     // This TreeModel never fires any events (since it is not editable) 
     // so event listener registration methods are left unimplemented 
     @Override 
     public void addTreeModelListener(TreeModelListener l) { 
     //throw new UnsupportedOperationException("Not supported yet."); 
     } 

     @Override 
     public void removeTreeModelListener(TreeModelListener l) { 
     //throw new UnsupportedOperationException("Not supported yet."); 
     } 
       //search all paths in list for given object 
       //return every item one level further than it 
     private ArrayList<String> getChildren(Object parent) { 
     ArrayList<String> children = new ArrayList<String>(); 
     for (int i = 0; i < this.paths.size(); i++) { 
      ArrayList<Object> pathObjects = new ArrayList<Object>(Arrays.asList(this.paths.get(i).getPath())); 
      for (Iterator<Object> it = pathObjects.iterator(); it.hasNext();) { 
      Object parentCandidate = it.next(); 
      if (parentCandidate.equals(parent)) { 
       Iterator<Object> checker = it; 
       try { 
       String child = new DefaultMutableTreeNode(checker.next()).toString(); 
       if (!children.contains(child)) 
        children.add (child); 
       } catch (NoSuchElementException ex) { 

       } 
      } 
      } 
     } 
     return children; 
     } 

    } 

인쇄본에서 알 수있는 한 TreePathsTreeModel 생성자에서 목록의 형식이 올바른지 확인하십시오. 아래 출력 샘플을 붙여 넣습니다.

... 
[File list, dev, tty0] 
[File list, dev, tty1] 
[File list, dev, tty2] 
[File list, dev, tty3] 
[File list, dev, tty4] 
[File list, dev, tty5] 
[File list, dev, tty6] 
[File list, dev, tty7] 
[File list, dev, tty8] 
[File list, dev, tty9] 
[File list, dev, urandom] 
[File list, dev, zero] 
[File list, home, mainvm, examples.desktop] 
[File list, home, mainvm, .cache, wallpaper, 0_5_1920_950_792beab7550410d531e55f95b449f135] 
[File list, home, mainvm, .cache, upstart, unity7.log.2.gz] 
[File list, home, mainvm, .cache, upstart, ssh-agent.log.2.gz] 
[File list, home, mainvm, .cache, upstart, gnome-keyring-ssh.log.1.gz] 
[File list, home, mainvm, .cache, upstart, dbus.log] 
[File list, home, mainvm, .cache, upstart, unity7.log] 
[File list, home, mainvm, .cache, upstart, hud.log] 
[File list, home, mainvm, .cache, upstart, upstart-event-bridge.log.2.gz] 
[File list, home, mainvm, .cache, upstart, unity7.log.1.gz] 
[File list, home, mainvm, .cache, upstart, unity-settings-daemon.log.2.gz] 
[File list, home, mainvm, .cache, upstart, unity-panel-service.log.2.gz] 
[File list, home, mainvm, .cache, upstart, hud.log.1.gz] 
[File list, home, mainvm, .cache, upstart, window-stack-bridge.log.2.gz] 
[File list, home, mainvm, .cache, upstart, gpg-agent.log.2.gz] 
[File list, home, mainvm, .cache, upstart, ssh-agent.log.1.gz] 
[File list, home, mainvm, .cache, upstart, window-stack-bridge.log.1.gz] 
[File list, home, mainvm, .cache, upstart, unity-panel-service.log] 
[File list, home, mainvm, .cache, upstart, unity-settings-daemon.log.1.gz] 
[File list, home, mainvm, .cache, upstart, unity-panel-service.log.1.gz] 
[File list, home, mainvm, .cache, upstart, dbus.log.2.gz] 
[File list, home, mainvm, .cache, upstart, dbus.log.1.gz] 
[File list, home, mainvm, .cache, upstart, gnome-keyring-ssh.log.2.gz] 
[File list, home, mainvm, .cache, upstart, gpg-agent.log.1.gz] 
[File list, home, mainvm, .cache, upstart, indicator-sound.log.2.gz] 
[File list, home, mainvm, .cache, upstart, indicator-sound.log.1.gz] 
[File list, home, mainvm, .cache, upstart, upstart-event-bridge.log.1.gz] 
[File list, home, mainvm, .cache, ibus, bus, registry.F0VAPY] 
... 
[File list, home, mainvm, .local, share, recently-used.xbel.49E0TY] 
[File list, home, mainvm, .local, share, recently-used.xbel] 
[File list, home, mainvm, .gnupg, gnupg_spawn_agent_sentinel.lock] 
[File list, home, mainvm, .gnupg, S.gpg-agent] 
[File list, lib, apparmor, functions] 
[File list, lib, apparmor, profile-load] 
[File list, lib, brltty, brltty.sh] 
[File list, lib, brltty, libbrlttybal.so] 
[File list, lib, brltty, libbrlttybat.so] 
[File list, lib, brltty, libbrlttybba.so] 
[File list, lib, brltty, libbrlttybbc.so] 
[File list, lib, brltty, libbrlttybbd.so] 
[File list, lib, brltty, libbrlttybbg.so] 
[File list, lib, brltty, libbrlttybbl.so] 
[File list, lib, brltty, libbrlttybbm.so] 
[File list, lib, brltty, libbrlttybbn.so] 
[File list, lib, brltty, libbrlttybcb.so] 
[File list, lib, brltty, libbrlttybce.so] 
[File list, lib, brltty, libbrlttybec.so] 
[File list, lib, brltty, libbrlttybeu.so] 
... 

아마도 내가 누락 된 코의 바로 아래에 뭔가가있을 수 있습니다. 그것은 어리석은 실수 일 수도 있습니다. 어떤 아이디어? 이것은 대학에서의 마지막 학년 프로젝트입니다. 제발 도와주세요!

답변

2

버그는 아마도 의 isLeaf(Object node) 방법 일 것입니다. TreeModel.isLeaf(Object node)의 자바 독에서
봐 : node가 잎 true 경우

돌아갑니다. node에 자식이 없더라도이 메서드는 false 을 반환 할 수 있습니다. 파일 시스템의 디렉토리 (예 : )에는 파일이 없을 수 있습니다. 디렉토리를 나타내는 노드는 리프가 아니고 이지만 자식도 없습니다.

그래서, 디렉토리 (심지어 디렉토리)의 경우에 당신은 false 대신 true 반환해야합니다.

+0

감사! 내가 할 일은 isLeaf()에 또 다른 조건을 추가하여 빈 폴더를 확인하는 것뿐이었습니다! 고마워요 !!!! –