/**
* 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.io.File;
import java.util.List;
import java.util.Properties;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import de.chris_soft.nanoarchive.DocumentFoundListener;
import de.chris_soft.nanodoa.God;
import de.chris_soft.nanodoa.gui.tree.path.DocumentPath;
import de.chris_soft.nanodoa.gui.tree.path.SpecialPath;
/**
* Special tree (new documents, unlabeled documents, etc.).
* @author Christian Packenius.
*/
public class SpecialTree extends DocumentTree implements DocumentFoundListener, TreeSelectionListener, FocusListener {
/**
* serialVersionUID.
*/
private static final long serialVersionUID = -8717813515400245844L;
/**
* Constructor.
*/
public SpecialTree() {
super("Specials");
initializeSpecialTree();
}
private void initializeSpecialTree() {
getTree().addFocusListener(this);
addTreeSelectionListener(this);
}
/**
* @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);
}
}
}
/**
* @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;
}
}
}
/**
* Removes the document from the list of unlabeled documents in the
* corresponding tree branch.
* @param documentID Document ID.
*/
public void removeFromUnlabeledDocuments(Long documentID) {
final Object specialPath = new SpecialPath("Unlabeled documents");
if (hasNode(specialPath)) {
List<Object> children = getChildUserObjects(specialPath);
DocumentPath docPath = new DocumentPath(documentID);
if (children.contains(docPath)) {
removeUserObjectPath(new Object[] { rootObject, specialPath, docPath });
}
}
}
/**
* A new document has been added to the archive. Add it to the tree view here.
* @param documentID
*/
public void addNewArchiveDocument(long documentID) {
addDocumentToSpecialPath(new SpecialPath("New documents"), documentID);
addToUnlabeledDocuments(documentID);
}
/**
* @see de.chris_soft.nanoarchive.DocumentFoundListener#foundDocument(String,
* java.io.File, long, java.util.Properties)
*/
@Override
public void foundDocument(String searchTerm, File document, long documentID, Properties metadata) {
addDocumentToSpecialPath(new SpecialPath(searchTerm), documentID);
}
private void addDocumentToSpecialPath(final Object searchPath, long documentID) {
if (!hasNode(searchPath)) {
addNode(null, searchPath);
}
List<Object> children = getChildUserObjects(searchPath);
DocumentPath docPath = new DocumentPath(documentID);
if (!children.contains(docPath)) {
addNode(searchPath, docPath);
}
}
/**
* @see de.chris_soft.nanoarchive.DocumentFoundListener#noElseDocumentFound()
*/
@Override
public void noElseDocumentFound() {
// Ignore.
}
/**
* Adds a document to the list of the unlabeled documents.
* @param documentID ID of the document.
*/
public void addToUnlabeledDocuments(long documentID) {
Object specialPath = new SpecialPath("Unlabeled documents");
addDocumentToSpecialPath(specialPath, documentID);
}
/**
* Selects the last node of the unlabeled documents path.
*/
public void selectLastUnlabeledDocument() {
Object specialPath = new SpecialPath("Unlabeled documents");
List<Object> childUserObjects = getChildUserObjects(specialPath);
if (!childUserObjects.isEmpty()) {
setCurrentNode(specialPath);
setCurrentChild(childUserObjects.get(childUserObjects.size() - 1));
}
}
}