コード例 #1
0
ファイル: FileTree.java プロジェクト: kidaak/cnuphys
  /**
   * Obtain a default dir, if there is one, which would have been passed in via a -p argument. Try
   * to expand the dir in the file tree.
   */
  public void expandDefaultDir() {

    // do an overall try catch. Don't want an exception here bringing down
    // the app.

    try {

      String defaultDir = FileUtilities.getDefaultDir();

      if (defaultDir == null) {
        return;
      }

      File file = new File(defaultDir);
      if (!file.exists() || !file.isDirectory()) {
        return;
      }

      String path = file.getCanonicalPath();

      // break the path into components
      String tokens[] = FileUtilities.tokenizePath(path);
      if (tokens == null) {
        return;
      }

      DefaultMutableTreeNode root = (DefaultMutableTreeNode) getModel().getRoot();

      if ((root == null) || (root.getChildCount() < 1)) {
        return;
      }

      int tokenIndex = 0;
      while ((root != null) && (root.getChildCount() > 0) && (tokenIndex < tokens.length)) {

        String token = tokens[tokenIndex];
        tokenIndex++;

        DefaultMutableTreeNode newRoot = getChild(root, token);
        if (newRoot != null) {
          TreeNode pathnodes[] = ((DefaultTreeModel) getModel()).getPathToRoot(newRoot);
          TreePath cpath = new TreePath(pathnodes);
          scrollPathToVisible(cpath);
          expandPath(cpath);
          newRoot = getChild(root, token);
          pathnodes = ((DefaultTreeModel) getModel()).getPathToRoot(newRoot);
        }
        root = newRoot;
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
コード例 #2
0
ファイル: FileTree.java プロジェクト: kidaak/cnuphys
  private DefaultMutableTreeNode getChild(DefaultMutableTreeNode parent, String name) {
    if ((parent == null) || (parent.getChildCount() < 1)) {
      return null;
    }

    try {
      for (Enumeration<?> e = parent.children(); e.hasMoreElements(); ) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) (e.nextElement());
        FileNode fileNode = getFileNode(node);
        if (fileNode != null) {
          File file = fileNode.getFile();
          if ((file != null) && (file.exists() && (file.isDirectory()))) {
            String subTokens[] = FileUtilities.tokenizePath(file.getPath());

            if ((subTokens != null) && (subTokens.length > 0)) {

              String fname = subTokens[subTokens.length - 1];
              if (name.equalsIgnoreCase(fname)) {
                return node;
              }
            } else {
              Log.getInstance().warning("tokenizer in fileTree getChiild not working properly.");
            }
          }
        } // fileNode != null
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

    return null;
  }