Example usage for javax.swing.tree DefaultMutableTreeNode getChildCount

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

Introduction

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

Prototype

public int getChildCount() 

Source Link

Document

Returns the number of children of this node.

Usage

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

public void addIntellectualEntities(List<FileGroupCollection> entities) {
    updateWorkerProgress(50);/*from w w w .  j  a  v a  2 s  . co m*/
    DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode();
    DepositTreeModel model;
    if (theEntityTree.getModel() instanceof DepositTreeModel) {
        model = (DepositTreeModel) theEntityTree.getModel();
        model.setRoot(rootNode);
    } else {
        model = new DepositTreeModel(rootNode, ETreeType.EntityTree);
        theEntityTree.setModel(model);
    }
    if (entities == null) {
        rootNode.setUserObject("Intellectual Entity");
    } else {
        // Set folder as Root
        if (entities.size() == 1) {
            FileGroupCollection collection = entities.get(0);
            rootNode.setUserObject(collection);
            for (int i = 0; collection.getFileGroupList() != null
                    && i < collection.getFileGroupList().size(); i++) {
                addIntellectualEntity(rootNode, collection.getFileGroupList().get(i), false);
            }
        }
        // Set each file as IE
        else {
            rootNode.setUserObject(theFsoRoot.getDescription());
            for (FileGroupCollection entity : entities) {
                DefaultMutableTreeNode childNode = new DefaultMutableTreeNode();
                childNode.setUserObject(entity);
                model.insertNodeInto(childNode, rootNode, rootNode.getChildCount());
                for (int i = 0; entity.getFileGroupList() != null
                        && i < entity.getFileGroupList().size(); i++) {
                    addIntellectualEntity(childNode, entity.getFileGroupList().get(i), false);
                }
            }
        }
    }
    // When the Swing Worker is used, expanding the leaf nodes has been moved to the end of the IE building process. Now located in the Swing Worker's Done() method.
    if (buildIE_Worker == null) {
        expandNode(theEntityTree, rootNode, true);
    }
    updateWorkerProgress(60);
    theEntityTree.repaint();
}

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

public void addStructMapItem(StructMap structMapItem, DefaultMutableTreeNode rootNode, boolean editEntry) {
    DefaultMutableTreeNode newNode = new DefaultMutableTreeNode();
    newNode.setUserObject(structMapItem);
    DepositTreeModel model = (DepositTreeModel) theStructMapTree.getModel();
    model.insertNodeInto(newNode, rootNode, rootNode.getChildCount());
    for (int i = 0; structMapItem.getChildren() != null && i < structMapItem.getChildren().size(); i++) {
        addStructMapItem(structMapItem.getChildren().get(i), newNode, false);
    }// w w w. ja  v  a2  s.co  m
    for (int i = 0; structMapItem.getFiles() != null && i < structMapItem.getFiles().size(); i++) {
        DefaultMutableTreeNode newFileNode = new DefaultMutableTreeNode();
        newFileNode.setUserObject(structMapItem.getFiles().get(i));
        model.insertNodeInto(newFileNode, newNode, newNode.getChildCount());
    }
    TreePath newPath = new TreePath(newNode.getPath());
    theStructMapTree.scrollPathToVisible(newPath);
    theStructMapTree.setSelectionPath(newPath);
    if (editEntry) {
        editStructMap();
    }
}

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

public StructMapCollection getStructures() {
    StructMapCollection retVal = new StructMapCollection();
    DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode) theStructMapTree.getModel().getRoot();
    for (int i = 0; rootNode != null && i < rootNode.getChildCount(); i++) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) rootNode.getChildAt(i);
        if (node.getUserObject() instanceof StructMap) {
            StructMap entity = (StructMap) node.getUserObject();
            retVal.add(entity);//from  ww w.  j a v a2 s  . c  o  m
        }
    }
    return retVal;
}

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

public ArrayList<FileGroupCollection> getEntities() {
    ArrayList<FileGroupCollection> retVal = new ArrayList<FileGroupCollection>();
    DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode) theEntityTree.getModel().getRoot();
    if (rootNode.getUserObject() instanceof FileGroupCollection) {
        FileGroupCollection collection = (FileGroupCollection) rootNode.getUserObject();
        retVal.add(collection);//from  www. j a  va 2s .c om
    } else {
        for (int i = 0; rootNode != null && i < rootNode.getChildCount(); i++) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) rootNode.getChildAt(i);
            if (node.getUserObject() instanceof FileGroupCollection) {
                FileGroupCollection collection = (FileGroupCollection) node.getUserObject();
                retVal.add(collection);
            }
        }
    }
    return retVal;
}

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

/**
 * Refreshes Entity tree to expand any labels that may not have rendered fully. Eg. "Som..." instead of "Some Folder" 
 *///from   w w w  .  ja  va2s  .c  om
private void refreshEntityTree() {
    DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode) theEntityTree.getModel().getRoot();
    DefaultTreeModel model = (DefaultTreeModel) theEntityTree.getModel();
    if (rootNode.getChildCount() > 0) {
        for (int i = 0; i < rootNode.getChildCount(); i++) {
            DefaultMutableTreeNode child = (DefaultMutableTreeNode) rootNode.getChildAt(i);
            model.nodeStructureChanged(child);
            model.nodeChanged(child);
        }
        expandNode(theEntityTree, rootNode, true);
    }
}

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

private void deleteMapItemIfNotInIE(DefaultMutableTreeNode node) {
    for (int i = 0; i < node.getChildCount(); i++) {
        DefaultMutableTreeNode nodeChild = (DefaultMutableTreeNode) node.getChildAt(i);
        deleteMapItemIfNotInIE(nodeChild);
    }//  w  ww .ja  va 2  s  .c  o  m
    if (node.getUserObject() instanceof FileSystemObject) {
        FileSystemObject fso = (FileSystemObject) node.getUserObject();
        if (!this.fileIsInEntity(fso)) {
            this.deleteStructMapNode(node);
        }
    }
}

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

private boolean droppingDoesntMakeSense(List<DefaultMutableTreeNode> sourceNode,
        DefaultMutableTreeNode destinationNode) {
    /**/*w  w  w.j ava  2s. c  om*/
     * Doesn't make sense if: The destination node is the source node The
     * destination node is the immediate parent of the source node The
     * destination node is a child of the source node
     */
    boolean doesntMakeSense = false;
    for (DefaultMutableTreeNode node : sourceNode) {
        doesntMakeSense = ((node.equals(destinationNode))
                || ((((DefaultMutableTreeNode) node).getParent() != null)
                        && (((DefaultMutableTreeNode) node).getParent().equals(destinationNode))));
        for (int i = 0; !doesntMakeSense && i < node.getChildCount(); i++) {
            DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) node.getChildAt(i);
            if (destinationNode.equals(childNode)) {
                doesntMakeSense = true;
            } else {
                ArrayList<DefaultMutableTreeNode> nodeList = new ArrayList<DefaultMutableTreeNode>();
                nodeList.add(childNode);
                doesntMakeSense = droppingDoesntMakeSense(nodeList, destinationNode);
            }
        }
        if (doesntMakeSense) {
            break;
        }
    }
    return doesntMakeSense;
}

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();//w  ww . j  a v a2s.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//from   w w w  . j  a v  a  2 s.co  m
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//from   w ww . j a  v  a 2  s.  co  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);
}