Module org.jpo

Class SortableDefaultMutableTreeNode

java.lang.Object
javax.swing.tree.DefaultMutableTreeNode
org.jpo.datamodel.SortableDefaultMutableTreeNode
All Implemented Interfaces:
Serializable, Cloneable, MutableTreeNode, TreeNode, GroupInfoChangeListener, PictureInfoChangeListener

public class SortableDefaultMutableTreeNode extends DefaultMutableTreeNode implements PictureInfoChangeListener, GroupInfoChangeListener
This is the main data structure object for the JPO Collection. Holds a reference to either a PictureInfo or GroupInfo object in its getUserObject.

It extends the DefaultMutableTreeNode with the Comparable Interface that allows our nodes to be compared.

See Also:
  • Field Details

  • Constructor Details

    • SortableDefaultMutableTreeNode

      public SortableDefaultMutableTreeNode()
      Constructor for a new node.
    • SortableDefaultMutableTreeNode

      public SortableDefaultMutableTreeNode(GroupOrPicture userObject)
      Constructor for a new node.
      Parameters:
      userObject - User Object
  • Method Details

    • setPictureCollection

      public void setPictureCollection(PictureCollection pictureCollection)
      Should only be called on the root node
      Parameters:
      pictureCollection - the picture collection that owns this node
    • add

      public void add(SortableDefaultMutableTreeNode nodeToAdd)
      This method adds a new node to the data model of the tree. The add method which will first do the default behavior and then send a notification to the Tree Model if model updates are being requested. Likewise, the unsaved changes of the collection are only being updated when model updates are not being reported. This allows the loading of collections (which of course massively change the collection in memory) to report nothing changed.
      Parameters:
      nodeToAdd - the new node
    • addGroupNode

      public SortableDefaultMutableTreeNode addGroupNode(String description)
      Adds a new Group to the current node with the indicated description.
      Parameters:
      description - Description for the group
      Returns:
      The new node is returned for convenience.
    • addSinglePicture

      public boolean addSinglePicture(File addFile, boolean newOnly, Collection<Integer> selectedCategories)
      Creates and add a new picture node to the current node from an image file.
      Parameters:
      addFile - the file of the picture that should be added
      newOnly - flag whether to check if the picture is in the collection already; if true will only add the picture if it is not yet included
      selectedCategories - selected categories
      Returns:
      true if the node was added, false if not.
    • addPicture

      public boolean addPicture(File file, Collection<Integer> categoryAssignments)
      this method adds a new Picture to the current node if the JVM has a reader for it.
      Parameters:
      file - the file that should be added
      categoryAssignments - Can be null
      Returns:
      true if the picture was valid, false if not.
    • containsAnAncestor

      public boolean containsAnAncestor(Collection<SortableDefaultMutableTreeNode> potentialAncestorNodes)
      This method reports if one of the supplied nodes is an ancestor of the current node.
      Parameters:
      potentialAncestorNodes - The collection of potential ancestor nodes to check
      Returns:
      true if an ancestor is found
    • isExecuteDropOk

      public static boolean isExecuteDropOk(DropTargetDropEvent event)
      Checks if the DropTargetDropEvent is suitable. Presently we can only deal with local transfers of JpoTransferables
      Parameters:
      event - The drop event
      Returns:
      true if acceptable, false if not
    • extractTransferableNodes

      @NotNull public static @NotNull List<SortableDefaultMutableTreeNode> extractTransferableNodes(DropTargetDropEvent event) throws UnsupportedFlavorException, IOException
      Extract the transferable nodes from the drop event
      Parameters:
      event - the drop event
      Returns:
      a list of the transferable nodes
      Throws:
      UnsupportedFlavorException - when a bad transferable is received
      IOException - when an IO error occurs
    • memorizeGroupOfDropLocation

      public static void memorizeGroupOfDropLocation(SortableDefaultMutableTreeNode node)
      This method memorizes the group associated with the supplied node in the Settings object. If the supplied node is not a group its parent which must be a group is memorized.
      Parameters:
      node - the node to memorize
    • copyPicture

      public static void copyPicture(File sourceFile, File targetFile)
      Copy any file from sourceFile source File to sourceFile target File location.
      Parameters:
      sourceFile - the source file location
      targetFile - the target file location
    • wasNodeDeleted

      public static boolean wasNodeDeleted(SortableDefaultMutableTreeNode potentiallyAffectedNode, TreeModelEvent treeModelEvent)
      This method returns whether the supplied node is a descendant of the deletions that have been detected in the TreeModelListener delivered TreeModelEvent.
      Parameters:
      potentiallyAffectedNode - The node to check whether it is or is a descendant of the deleted node.
      treeModelEvent - The TreeModelEvent that was detected
      Returns:
      true if successful, false if not
    • getPictureCollection

      public PictureCollection getPictureCollection()
      returns the collection associated with this node
      Returns:
      the picture collection.
    • sortChildren

      public void sortChildren(Settings.FieldCodes sortCriteria)
      Call this method to sort the Children of a node by a field.
      Parameters:
      sortCriteria - The criteria by which the pictures should be sorted.
    • getPreviousPicture

      public SortableDefaultMutableTreeNode getPreviousPicture()
      Returns the first node with a picture before the current one in the tree. It uses the getPreviousNode method of DefaultMutableTreeNode.
      Returns:
      the first node with a picture in preorder traversal or null if none found.
    • getNextPicture

      public SortableDefaultMutableTreeNode getNextPicture()
      Returns the next node with a picture found after current one in the tree. This can be in another Group. It uses the getNextNode method of the DefaultMutableTreeNode.
      Returns:
      The SortableDefaultMutableTreeNode that represents the next picture. If no picture can be found it returns null.
    • getNextGroupPicture

      public SortableDefaultMutableTreeNode getNextGroupPicture()
      Returns the next node with a picture found after current one in the current Group It uses the getNextSibling method of the DefaultMutableTreeNode.
      Returns:
      The SortableDefaultMutableTreeNode that represents the next picture. If no picture can be found it returns null.
    • findFirstPicture

      public SortableDefaultMutableTreeNode findFirstPicture()
      Returns the first child node under the current node which holds a PictureInfo object.
      Returns:
      The first child node holding a picture or null if none can be found.
    • getChildPictureNodes

      public List<SortableDefaultMutableTreeNode> getChildPictureNodes(boolean recursive)
      This method collects all pictures under the current node and returns them as an Array List.
      Parameters:
      recursive - Pass true if the method is supposed to recursively search the subgroups, false if not
      Returns:
      A List of child nodes that hold a picture
    • getChildPictureNodesDFS

      public Stream<SortableDefaultMutableTreeNode> getChildPictureNodesDFS()
      This method collects all pictures under the current node and returns them as an Array List.
      Returns:
      A List of child nodes that hold a picture
    • hasChildPictureNodes

      public boolean hasChildPictureNodes()
      A convenience method to tell if the current node has at least one picture node in the tree of children. (Could be one)
      Returns:
      true if at least one picture is found, false if not
    • setUserObject

      public void setUserObject(Object userObject)
      This method is being overridden to allow us to capture editing events on the JTree that is rendering this node. The TreeCellEditor will send the changed label as a String type object to the setUserObject method of this class. My overriding this we can intercept this and update the PictureInfo or GroupInfo accordingly.
      Specified by:
      setUserObject in interface MutableTreeNode
      Overrides:
      setUserObject in class DefaultMutableTreeNode
      Parameters:
      userObject - The object to attach to the node
    • executeDrop

      public void executeDrop(DropTargetDropEvent dropEvent)
      This method is called by the drop method of the DragTarget to do the move. It deals with the intricacies of the drop event and handles all the moving, cloning and positioning that is required.
      Parameters:
      dropEvent - The event the listening object received.
    • pictureInfoChangeEvent

      public void pictureInfoChangeEvent(PictureInfoChangeEvent pictureInfoChangeEvent)
      This is where the Nodes in the tree find out about changes in the PictureInfo object
      Specified by:
      pictureInfoChangeEvent in interface PictureInfoChangeListener
      Parameters:
      pictureInfoChangeEvent - The event
    • groupInfoChangeEvent

      public void groupInfoChangeEvent(GroupInfoChangeEvent groupInfoChangeEvent)
      Description copied from interface: GroupInfoChangeListener
      inform the listener that the GroupInfo has changed
      Specified by:
      groupInfoChangeEvent in interface GroupInfoChangeListener
      Parameters:
      groupInfoChangeEvent - The Change event
    • removeFromParent

      public void removeFromParent()
      Removes the node from the parent and sends a nodesWereRemoved notification. Essentially this is the equivalent of deleting the node from the tree.
      Specified by:
      removeFromParent in interface MutableTreeNode
      Overrides:
      removeFromParent in class DefaultMutableTreeNode
    • getClone

      public SortableDefaultMutableTreeNode getClone()
      Returns a new SortableDefaultTreeMode which has the same content as the source node
      Returns:
      a new node which is a clone of the old one
    • insert

      public void insert(SortableDefaultMutableTreeNode node, int index)
      Inserts the node and notifies the tree model of changes if we are sending Model updates
      Parameters:
      node - The node
      index - the index position
    • moveNodeToTop

      public void moveNodeToTop()
      When this method is invoked on a node it is moved to the first child position of its parent node.
    • moveNodeUp

      public void moveNodeUp()
      When this method is invoked on a node it moves itself one position up towards the first child position of its parent node.
    • moveNodeDown

      public void moveNodeDown()
      Method that moves a node down one position
    • moveNodeToBottom

      public void moveNodeToBottom()
      Method that moves a node to the bottom of the current branch
    • indentNode

      public void indentNode()
      When this method is invoked on a node it becomes a sub-node of it's preceding group.
    • outdentNode

      public void outdentNode()
      Method that outdents a node. This means the node will be placed just after it's parent's node as a child of its grandparent.
    • moveToLastChild

      public boolean moveToLastChild(SortableDefaultMutableTreeNode targetNode)
      Method that moves a node to bottom of the specified target group node. It sets the collection's unsaved updates to true.
      Parameters:
      targetNode - The target node you wish to attach the node to
      Returns:
      true if the move was successful, false if not
    • moveBefore

      public boolean moveBefore(SortableDefaultMutableTreeNode targetNode)
      Method that moves the node to the spot before the indicated node
      Parameters:
      targetNode - The before which you wish to insert the node to
      Returns:
      true if the move was successful, false if not
    • moveToIndex

      public boolean moveToIndex(SortableDefaultMutableTreeNode parentNode, int index)
      Method that moves the node to the specified index
      Parameters:
      parentNode - The parent node that will get the child
      index - the position at which to insert
      Returns:
      true if the move was successful, false if not
    • getAllowsChildren

      public boolean getAllowsChildren()
      Informs whether this node allows children. If the node holds a PictureInfo it does not allow child nodes, if it holds a GroupInfo, it does.
      Specified by:
      getAllowsChildren in interface TreeNode
      Overrides:
      getAllowsChildren in class DefaultMutableTreeNode
      Returns:
      true if child nodes are allowed, false if not
    • copyAddPictures

      public void copyAddPictures(Collection<File> fileCollection, File targetDir, boolean copyMode, JProgressBar progressBar)
      Copies the pictures from the source File collection into the target node while updating a supplied progress bar
      Parameters:
      fileCollection - A Collection framework of the new picture Files
      targetDir - The target directory for the copy operation
      copyMode - Set to true if you want to copy, false if you want to move the pictures.
      progressBar - The optional progressBar that should be incremented.
    • getParent

      public SortableDefaultMutableTreeNode getParent()
      Specified by:
      getParent in interface TreeNode
      Overrides:
      getParent in class DefaultMutableTreeNode
    • getRoot

      Overrides:
      getRoot in class DefaultMutableTreeNode
    • getCommonPath

      public Path getCommonPath()
    • commonPath

      public static Path commonPath(Path path0, Path path1)
      Finds the common path between two input paths
      Parameters:
      path0 - The first path
      path1 - The second path
      Returns:
      The common path See find-the-longest-path-common-to-two-paths-in-java