Example usage for javax.swing.tree DefaultMutableTreeNode getUserObject

List of usage examples for javax.swing.tree DefaultMutableTreeNode getUserObject

Introduction

In this page you can find the example usage for javax.swing.tree DefaultMutableTreeNode getUserObject.

Prototype

public Object getUserObject() 

Source Link

Document

Returns this node's user object.

Usage

From source file:nz.govt.natlib.ndha.manualdeposit.ManualDepositPresenter.java

private void itmAddStructure(java.awt.event.ActionEvent evt) {
    DefaultMutableTreeNode node = theNodeInClipboard.get(0);
    if ((node.getUserObject() instanceof StructMap) || (node.getUserObject() instanceof String)) {
        StructMap newStruct = StructMap.create("New Structure", null, null, 0);
        if (node.getUserObject() instanceof StructMap) {
            StructMap parent = (StructMap) node.getUserObject();
            if (parent.getChildren() == null) {
                parent.setChildren(new StructMapCollection());
            }//w w  w.  ja  v  a  2 s .  c  o  m
            parent.getChildren().add(newStruct);
        }
        addStructMapItem(newStruct, node, true);
    }
    manualDepositFrame.checkButtons();
}

From source file:nz.govt.natlib.ndha.manualdeposit.ManualDepositPresenter.java

private FileSystemObject getSelectedFSO() {
    FileSystemObject fso = null;/*  www.j  a  va2 s. c o m*/
    JTree tree = getTree(ETreeType.FileSystemTree);
    if (tree.getSelectionPath() != null) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getSelectionPath().getLastPathComponent();
        if (node != null && node.getUserObject() instanceof FileSystemObject) {
            fso = (FileSystemObject) node.getUserObject();
        }
    }
    return fso;
}

From source file:nz.govt.natlib.ndha.manualdeposit.ManualDepositPresenter.java

private void treeValueChanged(javax.swing.event.TreeSelectionEvent evt) {
    JTree sourceTree = (JTree) evt.getSource();
    TreePath[] paths = sourceTree.getSelectionPaths();
    if (paths != null) {
        theNodeInClipboard.clear();//from   ww  w  .ja  va  2s. c om
        for (TreePath path : paths) {
            theNodeInClipboard.add((DefaultMutableTreeNode) path.getLastPathComponent());
        }
    }

    DefaultMutableTreeNode node = (DefaultMutableTreeNode) evt.getPath().getLastPathComponent();
    JTree tree = (JTree) evt.getSource();
    if (getTreeType(tree).equals(ETreeType.FileSystemTree)) {
        if (node.getUserObject() instanceof FileSystemObject) {
            tree.setEditable(false);
        }
    }
    DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node.getParent();
    while (parent != null && parent.getUserObject() instanceof FileSystemObject) {
        node = parent;
        parent = (DefaultMutableTreeNode) node.getParent();
    }
    tree.setEditable(true);
    if ((parent != null) && (parent.getUserObject() instanceof StructMap)) {
        if (node.getUserObject() instanceof FileSystemObject) {
            tree.setEditable(false);
        }
    }
}

From source file:nz.govt.natlib.ndha.manualdeposit.ManualDepositPresenter.java

private boolean dropOK(DefaultMutableTreeNode destinationNode, JTree destinationTree) {
    boolean canDrop = false;
    if (droppingDoesntMakeSense(theNodeInClipboard, destinationNode)) {
        return false;
    }/* w w w .j av a  2s.  c om*/

    if (dragFromTreeType.equals(ETreeType.FileSystemTree)) {
        for (DefaultMutableTreeNode node : theNodeInClipboard) {
            if (node.getUserObject() instanceof FileSystemObject) {
                if (entityRootSet) {
                    if (destinationNode.getUserObject() instanceof FileGroup) {
                        canDrop = true;
                    } else if (destinationNode.getUserObject() instanceof FileSystemObject) {
                        canDrop = true;
                    }
                } else {
                    canDrop = true;
                }
            }
            if (!canDrop) {
                break;
            }
        }
    } else if (dragFromTreeType.equals(ETreeType.EntityTree)) {
        // If we have a multiple entity structure, can't drop at all
        if (getEntities().size() == 1) {
            // Can drop onto the Entity Tree or the StructMap tree
            DepositTreeModel model = (DepositTreeModel) destinationTree.getModel();
            if (model.getTreeType().equals(ETreeType.EntityTree)) {
                // Dragging to itself
                // Can drop a FileSystemObject or a StructMap onto a
                // StructMap
                // Can only drop a FileSystemObject onto a FileSystemObject
                for (DefaultMutableTreeNode node : theNodeInClipboard) {
                    if ((node.getUserObject() instanceof FileSystemObject)
                            && ((destinationNode.getUserObject() instanceof FileSystemObject)
                                    || (destinationNode.getUserObject() instanceof FileGroup))) {
                        canDrop = true;
                        break;
                    }
                }
            } else { // Dragging to StructMap
                // Can only drop a FileSystemObject
                // Can drop onto a FileSystemObject or a StructMap
                for (DefaultMutableTreeNode node : theNodeInClipboard) {
                    if ((node.getUserObject() instanceof FileSystemObject)
                            && ((destinationNode.getUserObject() instanceof StructMap)
                                    || (destinationNode.getUserObject() instanceof FileSystemObject))) {
                        FileSystemObject fso = (FileSystemObject) node.getUserObject();
                        canDrop = !fileIsInStructMap(fso) || isCopying;
                        if (canDrop) {
                            break;
                        }
                    }
                }
            }
        }
    } else { // Dragging from StructMap
        // Can only drop onto the StructMap tree
        // Can drop a FileSystemObject or a StructMap onto a StructMap
        // Can only drop a FileSystemObject onto a FileSystemObject
        DepositTreeModel model = (DepositTreeModel) destinationTree.getModel();
        if (model.getTreeType().equals(ETreeType.StructMapTree)) {
            for (DefaultMutableTreeNode node : theNodeInClipboard) {
                if ((node.getUserObject() instanceof FileSystemObject)
                        || (node.getUserObject() instanceof StructMap)) {
                    if (destinationNode.getUserObject() instanceof StructMap) {
                        canDrop = true;
                    } else if (node.getUserObject() instanceof FileSystemObject) {
                        canDrop = true;
                    }
                }
                if (canDrop) {
                    break;
                }
            }
        }
    }
    return canDrop;
}

From source file:nz.govt.natlib.ndha.manualdeposit.ManualDepositPresenter.java

private ArrayList<DefaultMutableTreeNode> cleanDuplicateFSOs(ArrayList<DefaultMutableTreeNode> nodes) {
    ArrayList<DefaultMutableTreeNode> retVal = new ArrayList<DefaultMutableTreeNode>();
    ArrayList<FileSystemObject> fsoList = new ArrayList<FileSystemObject>();
    for (DefaultMutableTreeNode nodeFromClipboard : nodes) {
        if (nodeFromClipboard.getUserObject() instanceof FileSystemObject) {
            FileSystemObject fso = (FileSystemObject) nodeFromClipboard.getUserObject();
            if (!fsoList.contains(fso)) {
                fsoList.add(fso);/*from  w w w.  j  a  va  2s  .  c o  m*/
                for (FileSystemObject fsoChild : fso.getAllChildren(false)) {
                    fsoList.add(fsoChild);
                }
                retVal.add(nodeFromClipboard);
            }
        }
    }
    return retVal;
}

From source file:nz.govt.natlib.ndha.manualdeposit.ManualDepositPresenter.java

private boolean dropNodes(DefaultMutableTreeNode node, DepositTreeModel model) {
    boolean succeeded = false;
    try {//  www  .  j a v  a 2 s.c  o m
        ArrayList<DefaultMutableTreeNode> nodeInClipboard = getClipboardCopy();
        if (!entityRootSet) { // Has to be a drag from the file system to
            // the entity
            succeeded = true;
            for (DefaultMutableTreeNode nodeFromClipboard : nodeInClipboard) {
                if (nodeFromClipboard.getUserObject() instanceof FileSystemObject) {
                    FileSystemObject fso = (FileSystemObject) nodeFromClipboard.getUserObject();
                    if (fso.getIsFile()) {
                        DefaultMutableTreeNode nodeParent = (DefaultMutableTreeNode) nodeFromClipboard
                                .getParent();
                        theFsoRoot = (FileSystemObject) nodeParent.getUserObject();
                        theFsoRootFile = fso;
                    } else {
                        theFsoRoot = fso;
                        theFsoRootFile = null;
                    }
                    setRoot();
                }
            }
        } else {
            if (dragFromTreeType.equals(ETreeType.FileSystemTree)) {
                // Must have a FileSystemObject and dragging to entity tree
                nodeInClipboard = cleanDuplicateFSOs(nodeInClipboard);
                for (DefaultMutableTreeNode nodeFromClipboard : nodeInClipboard) {
                    FileSystemObject fso = (FileSystemObject) nodeFromClipboard.getUserObject();
                    fso.setSortBy(SortBy.UserArranged);
                    if (node.getUserObject() instanceof FileSystemObject) {
                        DefaultMutableTreeNode nodeParent = (DefaultMutableTreeNode) node.getParent();
                        FileSystemObject fsoDestination = (FileSystemObject) node.getUserObject();
                        if (nodeParent.getUserObject() instanceof FileSystemObject) {
                            FileSystemObject fsoParent = (FileSystemObject) nodeParent.getUserObject();
                            fsoParent.getChildren().add(fso, fsoDestination.getSortOrder());
                        } else {
                            FileGroup entity = (FileGroup) nodeParent.getUserObject();
                            entity.getChildren().add(fso, fsoDestination.getSortOrder());
                        }
                    } else {
                        fso.setSortOrder(0);
                        addFileToEntity(fso, (FileGroup) node.getUserObject());
                    }
                }
                // Added 23/09/2013 by Ben. Ref: #996 - Jumbling issue
                sortFilesInEntity((FileGroup) node.getUserObject(), manualDepositFrame.getCurrentSortBy());
                dragFromFileSystemTree_startThread(getEntities(), theFsoRoot, true, false);

            } else if (dragFromTreeType.equals(ETreeType.EntityTree)) {
                if (model.getTreeType().equals(ETreeType.EntityTree)) {
                    // Must have a FileSystemObject
                    for (DefaultMutableTreeNode nodeFromClipboard : nodeInClipboard) {
                        FileSystemObject fso = (FileSystemObject) nodeFromClipboard.getUserObject();
                        DefaultMutableTreeNode nodeParent = (DefaultMutableTreeNode) nodeFromClipboard
                                .getParent();
                        deleteEntityNode(nodeFromClipboard);
                        if (node.getUserObject() instanceof FileSystemObject) {
                            FileSystemObject fsoDestination = (FileSystemObject) node.getUserObject();
                            if (fsoDestination.getIsFile()) {
                                nodeParent = (DefaultMutableTreeNode) node.getParent();
                                if (nodeParent.getUserObject() instanceof FileSystemObject) {
                                    FileSystemObject fsoParent = (FileSystemObject) nodeParent.getUserObject();
                                    fsoParent.getChildren().add(fso, fsoDestination.getSortOrder());
                                } else {
                                    FileGroup entity = (FileGroup) nodeParent.getUserObject();
                                    entity.getChildren().add(fso, fsoDestination.getSortOrder());
                                }
                            } else {
                                addFileToEntityFile(fso, (FileSystemObject) node.getUserObject());
                            }
                        } else {
                            addFileToEntity(fso, (FileGroup) node.getUserObject());
                        }
                    }
                    addIntellectualEntities(getEntities());
                    updateWorkerProgress(100);
                } else {
                    // Must have a FileSystemObject
                    for (DefaultMutableTreeNode nodeFromClipboard : nodeInClipboard) {
                        FileSystemObject fso = (FileSystemObject) nodeFromClipboard.getUserObject();
                        // Must have a StructMap or a FileSystemObject parent
                        if (node.getUserObject() instanceof StructMap) {
                            StructMap map = (StructMap) node.getUserObject();
                            if (map.getFiles() == null) {
                                map.setFiles(new FSOCollection());
                            }
                            map.getFiles().add(fso);
                            map.getFiles().setSortBy(manualDepositFrame.getCurrentSortBy());
                            map.getFiles().reSortList();

                            int nodeIndex = 0;
                            for (FileSystemObject fso1 : map.getFiles()) {
                                if (fso.equals(fso1)) {
                                    break; // Exit the FOR loop
                                }
                                nodeIndex++;
                            }
                            addFileToStructMap(fso, node, nodeIndex, false);
                        } else {
                            DefaultMutableTreeNode nodeParent = (DefaultMutableTreeNode) node.getParent();
                            StructMap mapParent = (StructMap) nodeParent.getUserObject();
                            FileSystemObject fsoDestination = (FileSystemObject) node.getUserObject();
                            mapParent.getFiles().add(fso, fsoDestination.getSortOrder());
                            addStructMap(getStructures());
                        }
                    }
                    // Reset the Struct Sort Order
                    if (node.getUserObject() instanceof StructMap) {
                        StructMap updatedStructMap = (StructMap) node.getUserObject();
                        int structSortOrder = 0;
                        for (FileSystemObject fsoUpdated : updatedStructMap.getFiles()) {
                            fsoUpdated.setStructSortOrder(structSortOrder);
                            structSortOrder++;
                        }
                    } else {
                        DefaultMutableTreeNode nodeParent = (DefaultMutableTreeNode) node.getParent();
                        StructMap mapParent = (StructMap) nodeParent.getUserObject();
                        int structSortOrder = 0;
                        for (FileSystemObject fsoUpdated : mapParent.getFiles()) {
                            fsoUpdated.setStructSortOrder(structSortOrder);
                            structSortOrder++;
                        }
                    }
                }
            } else {
                // Can drag a FileSystemObject or a StructMap
                // drag source parent can only be a StructMap
                // drag target can be a StructMap or a FileSystemObject
                // if it's a FileSystemObject, we are re-ordering
                for (DefaultMutableTreeNode nodeFromClipboard : nodeInClipboard) {
                    StructMap mapSourceParent = (StructMap) ((DefaultMutableTreeNode) nodeFromClipboard
                            .getParent()).getUserObject();
                    StructMap mapDestination;
                    int position = 0;
                    if (node.getUserObject() instanceof StructMap) {
                        mapDestination = (StructMap) node.getUserObject();
                    } else {
                        FileSystemObject fsoChild = (FileSystemObject) node.getUserObject();
                        position = fsoChild.getSortOrder();
                        DefaultMutableTreeNode nodeParent = (DefaultMutableTreeNode) node.getParent();
                        mapDestination = (StructMap) nodeParent.getUserObject();
                    }
                    deleteStructMapNode(nodeFromClipboard);
                    if (nodeFromClipboard.getUserObject() instanceof FileSystemObject) {
                        FileSystemObject fso = (FileSystemObject) nodeFromClipboard.getUserObject();
                        if (mapDestination.getFiles() == null) {
                            mapDestination.setFiles(new FSOCollection());
                        }
                        mapDestination.getFiles().add(fso, position);
                    } else {
                        StructMap mapFrom = (StructMap) nodeFromClipboard.getUserObject();
                        mapSourceParent.getChildren().remove(mapFrom);
                        if (mapDestination.getChildren() == null) {
                            mapDestination.setChildren(new StructMapCollection());
                        }
                        mapDestination.getChildren().add(mapFrom);
                    }
                }
                addStructMap(getStructures());
            }
        }
    } catch (Exception ex) {
        reportException(ex);
    }
    return succeeded;
}

From source file:nz.govt.natlib.ndha.manualdeposit.ManualDepositPresenterTest.java

private TreePath[] loadEntity(String entityPath) {
    DepositTreeModel model = (DepositTreeModel) theFrame.treeFileSystem.getModel();
    DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode) model.getRoot();
    File theFile = new File(entityPath);
    LOG.debug("testProcessFileTreeKeyPress, root full path " + theFile.getAbsolutePath());
    try {//from   w  w  w .jav  a  2 s.  com
        LOG.debug("testProcessFileTreeKeyPress, root canonical path " + theFile.getCanonicalPath());
        theFrame.storeCurrentPath(theFile.getCanonicalPath());
    } catch (Exception ex) {
        fail();
    }
    FileSystemObject fso = (FileSystemObject) rootNode.getUserObject();
    fso.ensureChildPathLoaded(theFrame.getCurrentPath());
    FSOCollection coll = FSOCollection.create();
    coll.add(fso);
    FileSystemObject child = coll.getFSOByFullPath(theFrame.getCurrentPath(), true);
    depositPresenter.addFileSystemRoot(fso, false, false, theFrame.getCurrentPath());
    depositPresenter.selectNode(child, ETreeType.FileSystemTree);
    TreePath currentTreePath = theFrame.treeFileSystem.getSelectionPath();
    DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode) currentTreePath.getLastPathComponent();
    assertTrue(currentNode.getUserObject() instanceof FileSystemObject);
    fso = (FileSystemObject) currentNode.getUserObject();
    assertFalse(fso.getIsPlaceholder());
    assertTrue(fso.getFullPath().equals(theFrame.getCurrentPath()));
    return theFrame.treeFileSystem.getSelectionPaths();
}

From source file:nz.govt.natlib.ndha.manualdeposit.ManualDepositPresenterTest.java

private void addFilesToEntityAndCheckResults(DepositTreeModel fileSystemModel, DepositTreeModel entityModel,
        String[] fileNames // Make sure you send 3 file names
        , RepresentationTypes theType) {
    DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode) fileSystemModel.getRoot();
    FSOCollection coll = FSOCollection.create();
    coll = FSOCollection.create();//ww  w . j a  va 2  s.  c o  m
    coll.add((FileSystemObject) rootNode.getUserObject());
    FileSystemObject child = coll.getFSOByFullFileName(fileNames[0], true);
    depositPresenter.selectNode(child, ETreeType.FileSystemTree);
    TreePath firstPath = theFrame.treeFileSystem.getSelectionPath();
    child = coll.getFSOByFullFileName(fileNames[1], true);
    depositPresenter.selectNode(child, ETreeType.FileSystemTree);
    TreePath secondPath = theFrame.treeFileSystem.getSelectionPath();
    child = coll.getFSOByFullFileName(fileNames[2], true);
    depositPresenter.selectNode(child, ETreeType.FileSystemTree);
    TreePath thirdPath = theFrame.treeFileSystem.getSelectionPath();
    TreePath[] selectionPaths = new TreePath[] { firstPath, secondPath, thirdPath };
    theFrame.treeFileSystem.setSelectionPaths(selectionPaths);
    depositPresenter.processFileTreeKeyPress(theType.hotKeyValue(), selectionPaths);
    // Check that is has been created
    child = coll.getFSOByFullFileName(fileNames[0], true);
    depositPresenter.selectNode(child, ETreeType.FileSystemTree);
    assertTrue(theFrame.treeFileSystem.getSelectionPath() == null);
    rootNode = (DefaultMutableTreeNode) entityModel.getRoot();
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) rootNode.getChildAt(rootNode.getChildCount() - 1);
    assertTrue(node.getUserObject() instanceof FileGroup);
    FileGroup group = (FileGroup) node.getUserObject();
    assertTrue(group.getEntityType().equals(theType));
    assertTrue(node.getChildCount() == 3);
}

From source file:nz.govt.natlib.ndha.manualdeposit.ManualDepositPresenterTest.java

@Test
//Temporarily adding ignore as this testcase has some defect and fails
// all the time. This has to be fixed in the next release.
@Ignore//  w  ww  . j  a v a2s  .  c  om
public final void testHeapsOfStuff() {
    /**
     * Test a lot of the entity creation / deletion Tests hot keys also
     */
    depositPresenter.refreshFileList();
    DepositTreeModel fileSystemModel = (DepositTreeModel) theFrame.treeFileSystem.getModel();
    DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode) fileSystemModel.getRoot();
    assertTrue(rootNode.getChildCount() > 0);
    DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) rootNode.getChildAt(0);
    assertTrue(childNode.getChildCount() == 1); // Should be a placeholder
    DefaultMutableTreeNode grandChildNode = (DefaultMutableTreeNode) childNode.getChildAt(0);
    assertTrue(grandChildNode.getUserObject() instanceof FileSystemObject);
    FileSystemObject fso = (FileSystemObject) grandChildNode.getUserObject();
    assertTrue(fso.getIsPlaceholder());
    TreePath currentTreePath = new TreePath(childNode.getPath());
    depositPresenter.expandFileSystemTree(currentTreePath);
    if (childNode.getChildCount() > 0) {
        grandChildNode = (DefaultMutableTreeNode) childNode.getChildAt(0);
        assertTrue(grandChildNode.getUserObject() instanceof FileSystemObject);
        fso = (FileSystemObject) grandChildNode.getUserObject();
        assertFalse(fso.getIsPlaceholder());
    }
    try {
        setUp();
    } catch (Exception ex) {
        fail();
    }
    // After setup the model will have changed - but it shouldn't change
    // again after this
    fileSystemModel = (DepositTreeModel) theFrame.treeFileSystem.getModel();
    rootNode = (DefaultMutableTreeNode) fileSystemModel.getRoot();

    TreePath[] selectionPaths = loadEntity(RESOURCES_INPUT_PATH_NAMED);
    // Entity root not set yet - should be able to get a menu
    JPopupMenu menu = depositPresenter.processFileTreeKeyPress('m', selectionPaths);
    assertTrue(menu != null);
    // Set the root
    menu = depositPresenter.processFileTreeKeyPress('s', selectionPaths);
    DepositTreeModel entityModel = (DepositTreeModel) theFrame.treeEntities.getModel();
    DepositTreeModel structMapModel = (DepositTreeModel) theFrame.treeStructMap.getModel();
    assertTrue(menu == null);
    // The digital original should already have been added automatically
    // Only one allowed, so this should fail.
    assertFalse(depositPresenter.canAddRepresentationType('d'));
    // Only one allowed, but none added so far, so this should succeed.
    assertTrue(depositPresenter.canAddRepresentationType('p'));
    // multiple allowed, but none added so far, so this should succeed.
    assertTrue(depositPresenter.canAddRepresentationType('a'));
    // multiple allowed, two added so far, so this should succeed.
    assertTrue(depositPresenter.canAddRepresentationType('m'));

    // While we've got the named stuff loaded, might as well check the move
    // up/down
    DefaultMutableTreeNode ieRootNode = (DefaultMutableTreeNode) entityModel.getRoot();
    // Right - we should have one DO & 2 AC
    assertTrue(ieRootNode.getChildCount() == 3);
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) ieRootNode.getChildAt(0);
    assertTrue(node.getUserObject() instanceof FileGroup);
    FileGroup group = (FileGroup) node.getUserObject();
    assertTrue(group.getEntityType().equals(RepresentationTypes.DigitalOriginal));
    int childNo = 0;
    childNode = (DefaultMutableTreeNode) node.getChildAt(childNo);
    assertTrue(childNode.getUserObject() instanceof FileSystemObject);
    fso = (FileSystemObject) childNode.getUserObject();
    // while (fso.getFileName().equalsIgnoreCase(".svn")) {
    // childNo++;
    // childNode = (DefaultMutableTreeNode)node.getChildAt(childNo);
    // assertTrue(childNode.getUserObject() instanceof FileSystemObject);
    // fso = (FileSystemObject)childNode.getUserObject();
    // }
    // childNo = 0;
    fso.setSortBy(SortBy.UserArranged);
    while (!fso.getFileName().equalsIgnoreCase("Chapter1")) {
        childNo++;
        childNode = (DefaultMutableTreeNode) node.getChildAt(childNo);
        assertTrue(childNode.getUserObject() instanceof FileSystemObject);
        fso = (FileSystemObject) childNode.getUserObject();
    }
    doIEMoveTest(childNo, node.getChildCount(), fso, group);

    childNo = 0;
    DefaultMutableTreeNode subChildNode = (DefaultMutableTreeNode) childNode.getChildAt(childNo);
    assertTrue(subChildNode.getUserObject() instanceof FileSystemObject);
    FileSystemObject fsoChild = (FileSystemObject) subChildNode.getUserObject();
    fsoChild.setSortBy(SortBy.UserArranged);
    while (!fsoChild.getFileName().equalsIgnoreCase("test_87_archive_1_P_1.jpg")) {
        childNo++;
        subChildNode = (DefaultMutableTreeNode) childNode.getChildAt(childNo);
        assertTrue(subChildNode.getUserObject() instanceof FileSystemObject);
        fsoChild = (FileSystemObject) subChildNode.getUserObject();
    }
    fso.setSortBy(SortBy.UserArranged);
    fsoChild.setSortBy(SortBy.UserArranged);
    // doIEMoveTest(childNo, childNode.getChildCount(), fsoChild, fso);

    node = (DefaultMutableTreeNode) ieRootNode.getChildAt(1);
    assertTrue(node.getUserObject() instanceof FileGroup);
    group = (FileGroup) node.getUserObject();
    assertTrue(group.getEntityType().equals(RepresentationTypes.AccessCopy));
    node = (DefaultMutableTreeNode) ieRootNode.getChildAt(2);
    assertTrue(node.getUserObject() instanceof FileGroup);
    group = (FileGroup) node.getUserObject();
    assertTrue(group.getEntityType().equals(RepresentationTypes.AccessCopy));

    DefaultMutableTreeNode structRootNode = (DefaultMutableTreeNode) structMapModel.getRoot();
    childNo = 0;
    childNode = (DefaultMutableTreeNode) structRootNode.getChildAt(childNo);
    assertTrue(childNode.getUserObject() instanceof StructMap);
    StructMap map = (StructMap) childNode.getUserObject();
    while (!map.getStructureName().equalsIgnoreCase("Chapter1")) {
        childNo++;
        childNode = (DefaultMutableTreeNode) structRootNode.getChildAt(childNo);
        assertTrue(childNode.getUserObject() instanceof StructMap);
        map = (StructMap) childNode.getUserObject();
    }
    doStructMoveTest(childNo, structRootNode.getChildCount(), map, structRootNode.getUserObject());
    childNo = 0;
    subChildNode = (DefaultMutableTreeNode) childNode.getChildAt(childNo);
    assertTrue(subChildNode.getUserObject() instanceof StructMap);
    map = (StructMap) subChildNode.getUserObject();
    while (!map.getStructureName().equalsIgnoreCase("Page 1")) {
        childNo++;
        subChildNode = (DefaultMutableTreeNode) childNode.getChildAt(childNo);
        assertTrue(subChildNode.getUserObject() instanceof StructMap);
        map = (StructMap) subChildNode.getUserObject();
    }
    doStructMoveTest(childNo, structRootNode.getChildCount(), map, childNode.getUserObject());
    childNo = 0;
    DefaultMutableTreeNode subSubChildNode = (DefaultMutableTreeNode) subChildNode.getChildAt(childNo);
    assertTrue(subSubChildNode.getUserObject() instanceof FileSystemObject);
    fso = (FileSystemObject) subSubChildNode.getUserObject();
    while (!fso.getFileName().equalsIgnoreCase("test_87_view_1_P_1.jpg")) {
        childNo++;
        subSubChildNode = (DefaultMutableTreeNode) subChildNode.getChildAt(childNo);
        assertTrue(subSubChildNode.getUserObject() instanceof FileSystemObject);
        fso = (FileSystemObject) subSubChildNode.getUserObject();
    }
    doStructMoveTest(childNo, subChildNode.getChildCount(), fso, subChildNode.getUserObject());

    rootNode = (DefaultMutableTreeNode) fileSystemModel.getRoot();
    FSOCollection coll = FSOCollection.create();
    coll.add((FileSystemObject) rootNode.getUserObject());
    FileSystemObject child = coll.getFSOByFullFileName("test_87_view_1_P_1.jpg", true);
    depositPresenter.selectNode(child, ETreeType.EntityTree);
    assertTrue(depositPresenter.canCreateAutoStructItem());

    rootNode = (DefaultMutableTreeNode) entityModel.getRoot();
    depositPresenter.selectNode(rootNode.getUserObject(), ETreeType.EntityTree);
    assertFalse(depositPresenter.canCreateAutoStructItem());
    node = (DefaultMutableTreeNode) rootNode.getChildAt(0);
    depositPresenter.selectNode(node.getUserObject(), ETreeType.EntityTree);
    assertFalse(depositPresenter.canCreateAutoStructItem());

    // Delete all the struct map & entity nodes so we can re-add them using
    // hot keys
    rootNode = (DefaultMutableTreeNode) structMapModel.getRoot();
    int nodeNo = 0;
    node = (DefaultMutableTreeNode) rootNode.getChildAt(nodeNo);
    map = (StructMap) node.getUserObject();
    while (!map.getStructureName().equals("Chapter1")) {
        nodeNo++;
        node = (DefaultMutableTreeNode) rootNode.getChildAt(nodeNo);
        map = (StructMap) node.getUserObject();
    }
    nodeNo = 0;
    childNode = (DefaultMutableTreeNode) node.getChildAt(nodeNo);
    map = (StructMap) childNode.getUserObject();
    while (!map.getStructureName().equals("Page 1")) {
        nodeNo++;
        childNode = (DefaultMutableTreeNode) node.getChildAt(nodeNo);
        map = (StructMap) childNode.getUserObject();
    }
    assertTrue(childNode.getChildCount() == 3);
    grandChildNode = (DefaultMutableTreeNode) childNode.getChildAt(0);
    TreePath path = new TreePath(grandChildNode.getPath());
    theFrame.treeStructMap.setSelectionPath(path);
    Object nodeObject = grandChildNode.getUserObject();
    // _presenter.selectNode(nodeObject, ETreeType.StructMapTree);
    assertTrue(depositPresenter.canDeleteStructItem());
    depositPresenter.deleteStructMapItem();
    assertTrue(childNode.getChildCount() == 2);
    int noOfNodes = node.getChildCount();
    path = new TreePath(childNode.getPath());
    theFrame.treeStructMap.setSelectionPath(path);
    nodeObject = childNode.getUserObject();
    // _presenter.selectNode(nodeObject, ETreeType.StructMapTree);
    assertTrue(depositPresenter.canDeleteStructItem());
    depositPresenter.deleteStructMapItem();
    assertTrue(node.getChildCount() == noOfNodes - 1);

    noOfNodes = rootNode.getChildCount();
    for (int i = 0; i < noOfNodes; i++) {
        node = (DefaultMutableTreeNode) rootNode.getChildAt(0);
        path = new TreePath(node.getPath());
        theFrame.treeStructMap.setSelectionPath(path);
        nodeObject = node.getUserObject();
        // _presenter.selectNode(nodeObject, ETreeType.StructMapTree);
        assertTrue(depositPresenter.canDeleteStructItem());
        depositPresenter.deleteStructMapItem();
        assertTrue(rootNode.getChildCount() == noOfNodes - i - 1);
        if (rootNode.getChildCount() > 0) {
            node = (DefaultMutableTreeNode) rootNode.getChildAt(0);
            assertFalse(node.getUserObject().equals(nodeObject));
        }
    }
    assertTrue(rootNode.getChildCount() == 0);

    // Struct map should be clear now - clear IE
    rootNode = (DefaultMutableTreeNode) entityModel.getRoot();
    node = (DefaultMutableTreeNode) rootNode.getChildAt(0);
    nodeNo = 0;
    childNode = (DefaultMutableTreeNode) node.getChildAt(nodeNo);
    fso = (FileSystemObject) childNode.getUserObject();
    while (!fso.getDescription().equals("Chapter1")) {
        nodeNo++;
        childNode = (DefaultMutableTreeNode) rootNode.getChildAt(nodeNo);
        fso = (FileSystemObject) childNode.getUserObject();
    }
    nodeNo = 0;
    grandChildNode = (DefaultMutableTreeNode) childNode.getChildAt(nodeNo);
    fso = (FileSystemObject) grandChildNode.getUserObject();
    while (!fso.getFileName().equals("test_87_archive_1_P_1.jpg")) {
        nodeNo++;
        grandChildNode = (DefaultMutableTreeNode) childNode.getChildAt(nodeNo);
        fso = (FileSystemObject) grandChildNode.getUserObject();
    }
    noOfNodes = childNode.getChildCount();
    path = new TreePath(grandChildNode.getPath());
    JPopupMenu pop = depositPresenter.getEntityMenu(grandChildNode);
    assertTrue(pop.getSubElements().length == 3);
    pop = depositPresenter.getEntityMenu(childNode);
    assertTrue(pop.getSubElements().length == 2);
    pop = depositPresenter.getEntityMenu(node);
    assertTrue(pop.getSubElements().length == 1);
    pop = depositPresenter.getEntityMenu(rootNode);
    assertTrue(pop.getSubElements().length == 3);

    theFrame.treeEntities.setSelectionPath(path);
    // _presenter.selectNode(nodeObject, ETreeType.EntityTree);
    assertTrue(depositPresenter.canDeleteEntityItem());
    depositPresenter.deleteEntity();
    assertTrue(childNode.getChildCount() == noOfNodes - 1);
    noOfNodes = node.getChildCount();
    path = new TreePath(childNode.getPath());
    theFrame.treeEntities.setSelectionPath(path);
    // _presenter.selectNode(nodeObject, ETreeType.EntityTree);
    assertTrue(depositPresenter.canDeleteEntityItem());
    depositPresenter.deleteEntity();
    assertTrue(node.getChildCount() == noOfNodes - 1);

    rootNode = (DefaultMutableTreeNode) entityModel.getRoot();
    noOfNodes = rootNode.getChildCount();
    for (int i = 0; i < noOfNodes; i++) {
        node = (DefaultMutableTreeNode) rootNode.getChildAt(0);
        path = new TreePath(node.getPath());
        theFrame.treeEntities.setSelectionPath(path);
        nodeObject = node.getUserObject();
        // _presenter.selectNode(nodeObject, ETreeType.EntityTree);
        assertTrue(depositPresenter.canDeleteEntityItem());
        depositPresenter.deleteEntity();
        assertTrue(rootNode.getChildCount() == noOfNodes - i - 1);
        if (rootNode.getChildCount() > 0) {
            node = (DefaultMutableTreeNode) rootNode.getChildAt(0);
            assertFalse(node.getUserObject().equals(nodeObject));
        }
        rootNode = (DefaultMutableTreeNode) entityModel.getRoot(); // Make
        // sure
        // we
        // have
        // the
        // right
        // root
        // node
        // - it
        // changes
    }
    assertTrue(rootNode.getChildCount() == 0);
    depositPresenter.resetScreen();

    // Adding an auto structure item from a single file
    // should result in all three representation types being there
    selectionPaths = loadEntity(RESOURCES_INPUT_MANUAL);
    // Entity root not set yet - should be able to get a menu
    menu = depositPresenter.processFileTreeKeyPress('m', selectionPaths);
    assertTrue(menu != null);
    // Set the root
    menu = depositPresenter.processFileTreeKeyPress('s', selectionPaths);
    assertTrue(menu == null);
    // Only one allowed, but none added so far, so this should succeed.
    assertTrue(depositPresenter.canAddRepresentationType('d'));
    // Only one allowed, but none added so far, so this should succeed.
    assertTrue(depositPresenter.canAddRepresentationType('p'));
    // multiple allowed, but none added so far, so this should succeed.
    assertTrue(depositPresenter.canAddRepresentationType('a'));
    // multiple allowed, two added so far, so this should succeed.
    assertTrue(depositPresenter.canAddRepresentationType('m'));

    // Create DO
    rootNode = (DefaultMutableTreeNode) fileSystemModel.getRoot();
    String[] fileNames = { "File1.jpg", "File1.tif", "File1.tn" };
    addFilesToEntityAndCheckResults(fileSystemModel, entityModel, fileNames,
            RepresentationTypes.DigitalOriginal);

    // Create AC 1
    rootNode = (DefaultMutableTreeNode) fileSystemModel.getRoot();
    fileNames = new String[] { "File2.jpg", "File2.tif", "File2.tn" };
    addFilesToEntityAndCheckResults(fileSystemModel, entityModel, fileNames, RepresentationTypes.AccessCopy);

    // Create AC 2
    rootNode = (DefaultMutableTreeNode) fileSystemModel.getRoot();
    fileNames = new String[] { "File3.jpg", "File3.tif", "File3.tn" };
    addFilesToEntityAndCheckResults(fileSystemModel, entityModel, fileNames, RepresentationTypes.AccessCopy);

    // Should now have a completely populated IE - test Struct map
    // Adding an auto structure item from a single file
    rootNode = (DefaultMutableTreeNode) entityModel.getRoot();
    coll = FSOCollection.create();
    node = (DefaultMutableTreeNode) rootNode.getChildAt(0);
    group = (FileGroup) node.getUserObject();
    coll = group.getChildren();
    child = coll.getFSOByFullFileName("File1.jpg", true);
    depositPresenter.selectNode(child, ETreeType.EntityTree);
    assertTrue(depositPresenter.canCreateAutoStructItem());
    theFrame.setInputResult("First Test");
    depositPresenter.createAutoStructItem(true);
    rootNode = (DefaultMutableTreeNode) structMapModel.getRoot();
    assertTrue(rootNode.getChildCount() == 1);
    node = (DefaultMutableTreeNode) rootNode.getChildAt(0);
    assertTrue(node.getChildCount() == 3);
    assertTrue(node.getUserObject() instanceof StructMap);
    map = (StructMap) node.getUserObject();
    assertTrue(map.getStructureName().equalsIgnoreCase("First Test"));
    node = (DefaultMutableTreeNode) node.getChildAt(0);
    ;
    assertTrue(node.getUserObject().equals(child));

    // Adding an auto structure item NOT from a single file
    // should result in only one representation type being there - the one
    // selected
    rootNode = (DefaultMutableTreeNode) entityModel.getRoot();
    coll = FSOCollection.create();
    node = (DefaultMutableTreeNode) rootNode.getChildAt(1);
    group = (FileGroup) node.getUserObject();
    coll = group.getChildren();
    child = coll.getFSOByFullFileName("File2.jpg", true);
    depositPresenter.selectNode(child, ETreeType.EntityTree);
    assertTrue(depositPresenter.canCreateAutoStructItem());
    theFrame.setInputResult("Second Test");
    depositPresenter.createAutoStructItem(false);
    rootNode = (DefaultMutableTreeNode) structMapModel.getRoot();
    assertTrue(rootNode.getChildCount() == 2);
    node = (DefaultMutableTreeNode) rootNode.getChildAt(1);
    assertTrue(node.getChildCount() == 1);
    assertTrue(node.getUserObject() instanceof StructMap);
    map = (StructMap) node.getUserObject();
    assertTrue(map.getStructureName().equalsIgnoreCase("Second Test"));
    node = (DefaultMutableTreeNode) node.getChildAt(0);
    ;
    assertTrue(node.getUserObject().equals(child));

    depositPresenter.resetScreen();
    selectionPaths = loadEntity(RESOURCES_INPUT_MULTIPLE);
    // Entity root should now be unset - should be able to get a menu
    menu = depositPresenter.processFileTreeKeyPress('m', selectionPaths);
    assertTrue(menu != null);
    // Set the multiple root
    menu = depositPresenter.processFileTreeKeyPress('e', selectionPaths);
    assertTrue(menu == null);
    rootNode = (DefaultMutableTreeNode) entityModel.getRoot();
    LOG.debug("ChildCount: " + rootNode.getChildCount());
    assertTrue(rootNode.getChildCount() >= 3); // 3 on local PC, 4 on
    // server. Gah!
    for (int i = 0; i < 3; i++) {
        node = (DefaultMutableTreeNode) rootNode.getChildAt(i);
        assertTrue(node.getUserObject() instanceof FileGroupCollection);
    }
}

From source file:nz.govt.natlib.ndha.manualdeposit.ManualDepositPresenterTest.java

@Test
public final void testFavourites() {
    LOG.debug("*************************************************************************");
    LOG.debug("testFavourites");
    LOG.debug("*************************************************************************");
    // Set it up// w  w  w  . j a v  a2  s .  c  o  m
    FileUtils.deleteFileOrDirectoryRecursive(FAVOURITES_PATH);
    try {
        setUp();
    } catch (Exception ex) {
        fail();
    }
    DepositTreeModel fileSystemModel = (DepositTreeModel) theFrame.treeFileSystem.getModel();
    DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode) fileSystemModel.getRoot();
    FSOCollection coll = FSOCollection.create();
    FileSystemObject fso = (FileSystemObject) rootNode.getUserObject();
    coll.add(fso);
    if (fso.getFile() == null) {
        LOG.debug("Root Object (file null): " + fso.getDescription());
    } else {
        try {
            LOG.debug("Root Object (file not null): " + fso.getFile().getCanonicalPath());
        } catch (Exception ex) {
        }
    }
    for (int i = 0; i < rootNode.getChildCount(); i++) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) rootNode.getChildAt(i);
        fso = (FileSystemObject) node.getUserObject();
        if (fso.getFile() == null) {
            LOG.debug("Child " + i + " (file null): " + fso.getDescription());
        } else {
            try {
                LOG.debug("Child " + i + " (file not null): " + fso.getFile().getCanonicalPath());
            } catch (Exception ex) {
            }
        }
    }
    File settingsPathFile = new File(RESOURCES_SETTINGS_PATH);
    File favouritesPathFile = new File(FAVOURITES_PATH);
    assertTrue(favouritesPathFile.exists());
    FileSystemObject child = null;
    // Make sure the tree includes the path
    String fullPath = "";
    try {
        fullPath = settingsPathFile.getCanonicalPath();
    } catch (Exception ex) {
        fail();
    }
    String[] paths;
    String fileSeparator = System.getProperty("file.separator");
    if (fileSeparator.equals("\\")) {
        fullPath = fullPath.replaceAll("/", fileSeparator);
        paths = fullPath.split("[\\\\]");
    } else {
        fullPath = fullPath.replaceAll("[\\\\]", fileSeparator);
        paths = fullPath.split(fileSeparator);
    }
    String currentPath = "";
    String rootPath = paths[0] + fileSeparator;
    LOG.debug("Full Path: " + fullPath);
    LOG.debug("Root Path: " + rootPath);
    for (int i = 0; i < paths.length; i++) {
        LOG.debug("Path " + i + ": " + paths[i]);
    }
    for (int i = 0; i < paths.length; i++) {
        currentPath += paths[i] + fileSeparator;
        LOG.debug("Current Path: " + currentPath);
        child = coll.getFSOByFullPath(currentPath, true);
        if (child == null) {
            LOG.debug("Child is null (not found)");
        } else {
            if (child.getFile() == null) {
                LOG.debug("Child found, file is null");
            } else {
                LOG.debug("Child found, path " + child.getFullPath());
            }
        }
        depositPresenter.selectNode(child, ETreeType.FileSystemTree);
        TreePath currentTreePath = theFrame.treeFileSystem.getSelectionPath();
        LOG.debug("currentTreePath: " + currentTreePath.toString());
        depositPresenter.expandFileSystemTree(currentTreePath);
    }

    // Test the favourites
    // No favourites loaded - check that the menu is empty
    TreePath[] treePaths = null;
    depositPresenter.clearFavourites();
    assertFalse(depositPresenter.canStoreFavourites(treePaths));
    treePaths = theFrame.treeFileSystem.getSelectionPaths();
    assertTrue(depositPresenter.canStoreFavourites(treePaths));
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) theFrame.treeFileSystem
            .getLastSelectedPathComponent();
    fso = (FileSystemObject) node.getUserObject();
    JPopupMenu menu = (JPopupMenu) theFrame.mnuFileFavourites.getSubElements()[0];
    assertTrue(menu.getSubElements().length == 1);
    JMenuItem item = (JMenuItem) menu.getSubElements()[0];
    String noFavouritesText = item.getText();
    assertFalse(noFavouritesText.equals(fso.getFullPath()));

    // Store a favourite & check that it is included in the menu
    depositPresenter.storeAsFavourite(node);
    assertTrue(menu.getSubElements().length == 2);
    item = (JMenuItem) menu.getSubElements()[0];
    assertTrue(item.getText().equals(fso.getFullPath()));
    // Reset the screen & then check that the favourite is included in the
    // menu
    try {
        setUp();
    } catch (Exception ex) {
        fail();
    }
    menu = (JPopupMenu) theFrame.mnuFileFavourites.getSubElements()[0];
    assertTrue(menu.getSubElements().length == 2);
    item = (JMenuItem) menu.getSubElements()[0];
    assertTrue(item.getText().equals(fso.getFullPath()));
    // Delete the storage file & make sure that there are no menu items
    FileUtils.deleteFileOrDirectoryRecursive(FAVOURITES_PATH);
    try {
        setUp();
    } catch (Exception ex) {
        fail();
    }
    menu = (JPopupMenu) theFrame.mnuFileFavourites.getSubElements()[0];
    assertTrue(menu.getSubElements().length == 1);
    item = (JMenuItem) menu.getSubElements()[0];
    assertTrue(item.getText().equals(noFavouritesText));

    // Check clearing
    depositPresenter.storeAsFavourite(node);
    assertTrue(menu.getSubElements().length == 2);
    item = (JMenuItem) menu.getSubElements()[0];
    assertTrue(item.getText().equals(fso.getFullPath()));
    depositPresenter.clearFavourites();
    assertTrue(theFrame.mnuFileFavourites.getSubElements().length == 1);
    item = (JMenuItem) menu.getSubElements()[0];
    assertTrue(item.getText().equals(noFavouritesText));

    // Check loading a directory
    fileSystemModel = (DepositTreeModel) theFrame.treeFileSystem.getModel();
    rootNode = (DefaultMutableTreeNode) fileSystemModel.getRoot();
    coll = FSOCollection.create();
    coll.add((FileSystemObject) rootNode.getUserObject());
    currentPath = rootPath;
    depositPresenter.loadPath(currentPath);
    DefaultMutableTreeNode newNode = (DefaultMutableTreeNode) theFrame.treeFileSystem
            .getLastSelectedPathComponent();
    FileSystemObject fsoNew = (FileSystemObject) newNode.getUserObject();
    assertTrue(fsoNew.getFullPath().equals(currentPath));
    currentPath = fso.getFullPath();
    depositPresenter.loadPath(currentPath);
    newNode = (DefaultMutableTreeNode) theFrame.treeFileSystem.getLastSelectedPathComponent();
    fsoNew = (FileSystemObject) newNode.getUserObject();
    assertTrue(fsoNew.getFullPath().equals(fso.getFullPath()));
    FileUtils.deleteFileOrDirectoryRecursive(FAVOURITES_PATH);
}