Package de.chris_soft.nanodoa.gui.tree

Source Code of de.chris_soft.nanodoa.gui.tree.ArchiveTree

/**
* NanoDoA - File based document archive
*
* Copyright (C) 2011-2012 Christian Packenius, christian.packenius@googlemail.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package de.chris_soft.nanodoa.gui.tree;

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.List;

import javax.swing.JTree;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeExpansionListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

import de.chris_soft.nanodoa.God;
import de.chris_soft.nanodoa.gui.tree.path.DocumentPath;
import de.chris_soft.nanodoa.gui.tree.path.SubPath;
import de.chris_soft.utilities.LogUtils;
import de.chris_soft.utilities.Pair;
import de.chris_soft.utilities.swing.SwingUtils;

/**
* Special tree (new documents, unlabeled documents, etc.).
* @author Christian Packenius.
*/
public class ArchiveTree extends DocumentTree implements TreeExpansionListener, TreeSelectionListener, FocusListener {
  /**
   * serialVersionUID.
   */
  private static final long serialVersionUID = 6218420096988709658L;

  /**
   * Constructor.
   */
  public ArchiveTree() {
    super("Archive");
    initializeArchiveTree();
  }

  private void initializeArchiveTree() {
    getTree().addFocusListener(this);
    addTreeSelectionListener(this);
    addTreeExpansionListener(this);
    fillArchiveTree();
  }

  private void fillArchiveTree() {
    Runnable runnable = new Runnable() {
      @Override
      public void run() {
        try {
          List<Pair<Long, String>> list = God.archive.getDirectories(0);
          for (Pair<Long, String> pair : list) {
            SubPath subpath = new SubPath(pair.obj1, pair.obj2);
            if (!hasNode(subpath)) {
              addNode(null, subpath);
              List<Pair<Long, String>> list2 = God.archive.getDirectories(pair.obj1);
              for (Pair<Long, String> pair2 : list2) {
                SubPath subpath2 = new SubPath(pair2.obj1, pair2.obj2);
                if (!hasNode(subpath2)) {
                  addNode(subpath, subpath2);
                }
              }
            }
          }
        }
        catch (Exception exception) {
          LogUtils.log(exception);
          SwingUtils.showError(null, "Couldn't load archives first level sub paths!");
        }
      }
    };
    Thread thread = new Thread(runnable, "Archive tree filling thread");
    thread.start();
  }

  /**
   * @see java.awt.event.FocusListener#focusGained(java.awt.event.FocusEvent)
   */
  @Override
  public void focusGained(FocusEvent e) {
    if (e.getSource() instanceof JTree) {
      JTree tree = (JTree) e.getSource();
      TreePath selectionPath = tree.getSelectionPath();
      if (selectionPath != null) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) selectionPath.getLastPathComponent();
        if (node.getUserObject() instanceof DocumentPath) {
          showSelectedDocumentInTreeNode(tree);
        }
      }
    }
  }

  /**
   * @see java.awt.event.FocusListener#focusLost(java.awt.event.FocusEvent)
   */
  @Override
  public void focusLost(FocusEvent e) {
    // Ignore.
  }

  private void showSelectedDocumentInTreeNode(JTree tree) {
    TreePath selectionPath = tree.getSelectionPath();
    if (selectionPath != null) {
      DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) selectionPath.getLastPathComponent();
      Object userObject = treeNode.getUserObject();
      if (userObject instanceof DocumentPath) {
        DocumentPath docPath = (DocumentPath) userObject;
        God.appWindow.setCurrentDocument(docPath.docID);
        // selectShownDocumentInArchiveTree(treeNode, docPath);
      }
    }
  }

  /**
   * Show given document.
   * @param docID Document ID.
   */
  public void selectDocumentInArchiveTree(long docID) {
    boolean selected = false;
    JTree tree = getTree();
    TreePath selectionPath = tree.getSelectionPath();
    if (selectionPath != null) {
      DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) selectionPath.getLastPathComponent();
      Object userObject = selectedNode.getUserObject();
      if (userObject instanceof DocumentPath) {
        DocumentPath docPath = (DocumentPath) userObject;
        if (docPath.docID == docID) {
          selected = true;
        }
      }
    }

    if (!selected) {
      DocumentPath docPath = new DocumentPath(docID);
      if (!hasNode(docPath)) {
        try {
          fillArchiveTreeToDocumentRecursive(docPath);
        }
        catch (Exception exception) {
          // Ignore.
        }
      }
      setCurrentNode(docPath);
    }
  }

  private void fillArchiveTreeToDocumentRecursive(Object userObject) throws Exception {
    long pathID = 0;
    if (userObject instanceof DocumentPath) {
      pathID = God.archive.db.getPathFromDocument(((DocumentPath) userObject).docID);
    }
    else {
      pathID = God.archive.db.getParentPath((Long) userObject);
    }
    if (pathID != 0) {
      fillArchiveTreeToDocumentRecursive(pathID);
    }
    SubPath parent = new SubPath(pathID, null);
    if (userObject instanceof DocumentPath) {
      if (!hasNode(userObject)) {
        addNode(parent, userObject);
      }
      setCurrentNode(userObject);
    }
    else {
      SubPath subpath = new SubPath((Long) userObject, God.archive.getPathName((Long) userObject));
      if (!hasNode(subpath)) {
        addNode(parent, subpath);
      }
      setCurrentNode(new SubPath((Long) userObject, null));
    }
  }

  /**
   * @see javax.swing.event.TreeSelectionListener#valueChanged(javax.swing.event.TreeSelectionEvent)
   */
  @Override
  public void valueChanged(TreeSelectionEvent e) {
    JTree tree = (JTree) e.getSource();
    showSelectedDocumentInTreeNode(tree);
    collapseUnusedTreeNodes(e, tree);
  }

  private void collapseUnusedTreeNodes(TreeSelectionEvent e, JTree tree) {
    TreePath newPath = e.getNewLeadSelectionPath();
    TreePath oldPath = e.getOldLeadSelectionPath();
    if (oldPath == null || newPath == null) {
      return;
    }
    for (Object oldPathObject : oldPath.getPath()) {
      boolean found = false;
      for (Object newPathObject : newPath.getPath()) {
        if (oldPathObject == newPathObject) {
          found = true;
          break;
        }
      }
      if (!found) {
        while (oldPath.getLastPathComponent() != oldPathObject) {
          oldPath = oldPath.getParentPath();
        }
        tree.collapsePath(oldPath);
        break;
      }
    }
  }

  /**
   * @see javax.swing.event.TreeExpansionListener#treeExpanded(javax.swing.event.TreeExpansionEvent)
   */
  @Override
  public void treeExpanded(TreeExpansionEvent event) {
    TreePath treePath = event.getPath();
    DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) treePath.getLastPathComponent();
    for (int i = 0; i < treeNode.getChildCount(); i++) {
      DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) treeNode.getChildAt(i);
      fillTreeNode(childNode);
    }
  }

  /**
   * @see javax.swing.event.TreeExpansionListener#treeCollapsed(javax.swing.event.TreeExpansionEvent)
   */
  @Override
  public void treeCollapsed(TreeExpansionEvent event) {
    // Ignore.
  }

  private void fillTreeNode(DefaultMutableTreeNode parentNode) {
    Object userObject = parentNode.getUserObject();
    if (userObject instanceof SubPath) {
      try {
        SubPath subPath = (SubPath) userObject;
        addAllSubPathsToTreeNode(subPath);
        addAllDocumentsToTreeNode(subPath);
      }
      catch (Exception exception) {
        LogUtils.log(exception);
      }
    }
  }

  private void addAllSubPathsToTreeNode(SubPath subPath) throws Exception {
    List<Pair<Long, String>> list = God.archive.getDirectories(subPath.id);
    for (Pair<Long, String> pair : list) {
      SubPath subpath = new SubPath(pair.obj1, pair.obj2);
      if (!hasNode(subpath)) {
        addNode(subPath, subpath);
      }
    }
  }

  private void addAllDocumentsToTreeNode(SubPath subPath) throws Exception {
    List<Long> docs = God.archive.getFilesFromSubDirectory(subPath.id);
    for (long docID : docs) {
      DocumentPath docPath = new DocumentPath(docID);
      if (!hasNode(docPath)) {
        addNode(subPath, docPath);
      }
    }
  }
}
TOP

Related Classes of de.chris_soft.nanodoa.gui.tree.ArchiveTree

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.