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 Summary
Fields inherited from class javax.swing.tree.DefaultMutableTreeNode
allowsChildren, children, EMPTY_ENUMERATION, parent, userObject
-
Constructor Summary
ConstructorDescriptionConstructor for a new node.SortableDefaultMutableTreeNode
(GroupOrPicture userObject) Constructor for a new node. -
Method Summary
Modifier and TypeMethodDescriptionvoid
add
(SortableDefaultMutableTreeNode nodeToAdd) This method adds a new node to the data model of the tree.addGroupNode
(String description) Adds a new Group to the current node with the indicated description.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.boolean
addSinglePicture
(File addFile, boolean newOnly, Collection<Integer> selectedCategories) Creates and add a new picture node to the current node from an image file.static Path
commonPath
(Path path0, Path path1) Finds the common path between two input pathsboolean
containsAnAncestor
(Collection<SortableDefaultMutableTreeNode> potentialAncestorNodes) This method reports if one of the supplied nodes is an ancestor of the current node.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 barstatic void
copyPicture
(File sourceFile, File targetFile) Copy any file from sourceFile source File to sourceFile target File location.void
executeDrop
(DropTargetDropEvent dropEvent) This method is called by the drop method of the DragTarget to do the move.static @NotNull List<SortableDefaultMutableTreeNode>
Extract the transferable nodes from the drop eventReturns the first child node under the current node which holds a PictureInfo object.boolean
Informs whether this node allows children.getChildPictureNodes
(boolean recursive) This method collects all pictures under the current node and returns them as an Array List.This method collects all pictures under the current node and returns them as an Array List.getClone()
Returns a new SortableDefaultTreeMode which has the same content as the source nodeReturns the next node with a picture found after current one in the current Group It uses the getNextSibling method of the DefaultMutableTreeNode.Returns the next node with a picture found after current one in the tree.returns the collection associated with this nodeReturns the first node with a picture before the current one in the tree.getRoot()
void
groupInfoChangeEvent
(GroupInfoChangeEvent groupInfoChangeEvent) inform the listener that the GroupInfo has changedboolean
A convenience method to tell if the current node has at least one picture node in the tree of children.void
When this method is invoked on a node it becomes a sub-node of it's preceding group.void
insert
(SortableDefaultMutableTreeNode node, int index) Inserts the node and notifies the tree model of changes if we are sending Model updatesstatic boolean
Checks if the DropTargetDropEvent is suitable.static void
This method memorizes the group associated with the supplied node in the Settings object.boolean
moveBefore
(SortableDefaultMutableTreeNode targetNode) Method that moves the node to the spot before the indicated nodevoid
Method that moves a node down one positionvoid
Method that moves a node to the bottom of the current branchvoid
When this method is invoked on a node it is moved to the first child position of its parent node.void
When this method is invoked on a node it moves itself one position up towards the first child position of its parent node.boolean
moveToIndex
(SortableDefaultMutableTreeNode parentNode, int index) Method that moves the node to the specified indexboolean
moveToLastChild
(SortableDefaultMutableTreeNode targetNode) Method that moves a node to bottom of the specified target group node.void
Method that outdents a node.void
pictureInfoChangeEvent
(PictureInfoChangeEvent pictureInfoChangeEvent) This is where the Nodes in the tree find out about changes in the PictureInfo objectvoid
Removes the node from the parent and sends a nodesWereRemoved notification.void
setPictureCollection
(PictureCollection pictureCollection) Should only be called on the root nodevoid
setUserObject
(Object userObject) This method is being overridden to allow us to capture editing events on the JTree that is rendering this node.void
sortChildren
(Settings.FieldCodes sortCriteria) Call this method to sort the Children of a node by a field.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.Methods inherited from class javax.swing.tree.DefaultMutableTreeNode
add, breadthFirstEnumeration, children, clone, depthFirstEnumeration, getChildAfter, getChildAt, getChildBefore, getChildCount, getDepth, getFirstChild, getFirstLeaf, getIndex, getLastChild, getLastLeaf, getLeafCount, getLevel, getNextLeaf, getNextNode, getNextSibling, getPath, getPathToRoot, getPreviousLeaf, getPreviousNode, getPreviousSibling, getSharedAncestor, getSiblingCount, getUserObject, getUserObjectPath, insert, isLeaf, isNodeAncestor, isNodeChild, isNodeDescendant, isNodeRelated, isNodeSibling, isRoot, pathFromAncestorEnumeration, postorderEnumeration, preorderEnumeration, remove, remove, removeAllChildren, setAllowsChildren, setParent, toString
-
Field Details
-
GENERIC_ERROR
- See Also:
-
-
Constructor Details
-
SortableDefaultMutableTreeNode
public SortableDefaultMutableTreeNode()Constructor for a new node. -
SortableDefaultMutableTreeNode
Constructor for a new node.- Parameters:
userObject
- User Object
-
-
Method Details
-
setPictureCollection
Should only be called on the root node- Parameters:
pictureCollection
- the picture collection that owns this node
-
add
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
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 addednewOnly
- flag whether to check if the picture is in the collection already; if true will only add the picture if it is not yet includedselectedCategories
- selected categories- Returns:
- true if the node was added, false if not.
-
addPicture
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 addedcategoryAssignments
- 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
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 receivedIOException
- when an IO error occurs
-
memorizeGroupOfDropLocation
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
Copy any file from sourceFile source File to sourceFile target File location.- Parameters:
sourceFile
- the source file locationtargetFile
- 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
returns the collection associated with this node- Returns:
- the picture collection.
-
sortChildren
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
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
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
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
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
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
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
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 interfaceMutableTreeNode
- Overrides:
setUserObject
in classDefaultMutableTreeNode
- Parameters:
userObject
- The object to attach to the node
-
executeDrop
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
This is where the Nodes in the tree find out about changes in the PictureInfo object- Specified by:
pictureInfoChangeEvent
in interfacePictureInfoChangeListener
- Parameters:
pictureInfoChangeEvent
- The event
-
groupInfoChangeEvent
Description copied from interface:GroupInfoChangeListener
inform the listener that the GroupInfo has changed- Specified by:
groupInfoChangeEvent
in interfaceGroupInfoChangeListener
- 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 interfaceMutableTreeNode
- Overrides:
removeFromParent
in classDefaultMutableTreeNode
-
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
Inserts the node and notifies the tree model of changes if we are sending Model updates- Parameters:
node
- The nodeindex
- 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
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
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
Method that moves the node to the specified index- Parameters:
parentNode
- The parent node that will get the childindex
- 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 interfaceTreeNode
- Overrides:
getAllowsChildren
in classDefaultMutableTreeNode
- 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 FilestargetDir
- The target directory for the copy operationcopyMode
- 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
- Specified by:
getParent
in interfaceTreeNode
- Overrides:
getParent
in classDefaultMutableTreeNode
-
getRoot
- Overrides:
getRoot
in classDefaultMutableTreeNode
-
getCommonPath
-
commonPath
Finds the common path between two input paths- Parameters:
path0
- The first pathpath1
- The second path- Returns:
- The common path See find-the-longest-path-common-to-two-paths-in-java
-