Package de.chris_soft.nanodoa.gui.tree

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

/**
* 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.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.LabelPath;

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

  /**
   * Constructor.
   */
  public LabelTree() {
    super("Labels");
    initializeDocumentTree();
  }

  private void initializeDocumentTree() {
    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;
      }
    }
  }

  /**
   * Renames an existing label.
   * @param labelID
   * @param newLabelName
   */
  public void labelRenamed(long labelID, String newLabelName) {
    List<Object> children = getChildUserObjects(rootObject);
    for (Object userObject : children) {
      LabelPath labelPath = (LabelPath) userObject;
      if (labelPath.getID() == labelID) {
        labelPath.rename(newLabelName);
        refreshNode(userObject);
      }
    }
  }
}
TOP

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

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.