/** * Moves a node and all its children under another node. * * @param dest the destination node * @param node the node to move * @param index the index to move the node to (negative to move to the end of the list) * @throws Exception when the move is not possible */ public void move(Task dest, int index, Task node) throws Exception { /* never move root node or a node unto itself */ if (node.isRoot() || node == dest) return; /* never move a parent down into itself */ for (Task child = dest; child != null; child = (Task) child.getParent()) if (child == node) throw new Exception("Cannot move node under itself!"); // mark the old parent dirty to update the index ((Task) node.getParent()).setDirty(true); /* save the file system path of the old node location */ File oldPath = this.getNodePath(node); /* remove the node and add it under the destination node */ this.treeModel.removeNodeFromParent(node); if (index < 0) index = dest.getChildCount(); this.treeModel.insertNodeInto(node, dest, index); // mark the new parent dirty to update the index dest.setDirty(true); writeOut(dest); /* update the file system: move the task directory */ oldPath.renameTo(this.getNodePath(node)); }
/** * Removes a node and all its children from the tree. * * @param node the node to remove */ public void remove(Task node) { if (node.isRoot()) return; /* never remove the root node */ /* delete the file system path of the node and its children */ File path = this.getNodePath(node); this.deleteDirectory(path); /* remove the node from the tree */ this.treeModel.removeNodeFromParent(node); // mark parent node dirty to update the index if (node.getParent() != null) ((Task) node.getParent()).setDirty(true); }
/** * Writes out a single task. * * @param node the task node * @throws Exception on IO errors */ public void writeOut(Task node) throws Exception { /* get the path of the node and the task */ File path = this.getNodePath(node); if (!node.isRoot()) FileSystemTask.saveTree(node, path); }