public CatalogTree(final LeafSelectionListener leafSelectionListener, AppContext appContext, UIContext uiContext) { this.appContext = appContext; this.uiContext = uiContext; jTree = new JTree(); jTree.setRootVisible(false); ((DefaultTreeModel) jTree.getModel()).setRoot(CatalogTreeUtils.createRootNode()); CatalogTreeUtils.addCellRenderer(jTree); addWillExpandListener(); CatalogTreeUtils.addTreeSelectionListener(jTree, leafSelectionListener); }
private void appendToNode(JTree jTree, InvDataset dataset, MutableTreeNode parentNode) { final DefaultTreeModel treeModel = (DefaultTreeModel) jTree.getModel(); if (dataset instanceof InvCatalogRef) { CatalogTreeUtils.appendCatalogNode(parentNode, treeModel, (InvCatalogRef) dataset); } else { appendLeafNode(parentNode, treeModel, dataset); } }
private void getLeaves(Object node, TreeModel model, Set<OpendapLeaf> result) { for (int i = 0; i < model.getChildCount(node); i++) { if (model.isLeaf(model.getChild(node, i))) { final DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) (model.getChild(node, i)); if (CatalogTreeUtils.isDapNode(childNode) || CatalogTreeUtils.isFileNode(childNode)) { result.add((OpendapLeaf) childNode.getUserObject()); } } else { getLeaves(model.getChild(node, i), model, result); } } }
@Override public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) value; if (CatalogTreeUtils.isDapNode(value)) { setLeafIcon(dapIcon); setToolTip(node, tree); } else if (CatalogTreeUtils.isFileNode(value)) { setLeafIcon(fileIcon); setToolTip(node, tree); } else try { if (leaf && isOpendapLeaf(node) && CatalogTreeUtils.getCatalogDatasets(node).isEmpty()) { setLeafIcon(errorIcon); tree.setToolTipText(null); } else { setLeafIcon(getClosedIcon()); } } catch (IOException e) { setLeafIcon(errorIcon); tree.setToolTipText(null); } super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus); return this; }
@Test public void testThatUserObjectWhichIsNoOpendapLeafIsResolvedToFalse() { final Integer userObject = 4; final DefaultMutableTreeNode noDapNode = new DefaultMutableTreeNode(userObject); assertEquals(false, CatalogTreeUtils.isDapNode(noDapNode)); }
@Test public void testAppendingVariousDatasets() { //preparation datasets.add(createDataset(catalog, "dapName", "OPENDAP")); datasets.add(createDataset(catalog, "fileName", "FILE")); datasets.add(createCatalogRefDataset()); //execution new CatalogTree(null, new DefaultAppContext(""), null).appendToNode(new JTree(), datasets, parentNode, true); //verification assertEquals(3, parentNode.getChildCount()); assertEquals(true, CatalogTreeUtils.isDapNode(parentNode.getChildAt(0))); assertEquals(false, CatalogTreeUtils.isDapNode(parentNode.getChildAt(1))); assertEquals(false, CatalogTreeUtils.isCatalogReferenceNode(parentNode.getChildAt(1))); assertEquals(true, CatalogTreeUtils.isCatalogReferenceNode(parentNode.getChildAt(2).getChildAt(0))); }
public void setNewRootDatasets(List<InvDataset> rootDatasets) { final DefaultTreeModel model = (DefaultTreeModel) jTree.getModel(); final DefaultMutableTreeNode rootNode = CatalogTreeUtils.createRootNode(); model.setRoot(rootNode); appendToNode(jTree, rootDatasets, rootNode, true); fireCatalogElementsInsertionFinished(); expandPath(rootNode); leafToParentNode.clear(); }
private void testThatChildIsDapNodeWhichHasFileAccessToo(DefaultMutableTreeNode parentNode) { testThatChildIsDapNode(parentNode); assertEquals(true, CatalogTreeUtils.isFileNode(parentNode.getChildAt(0))); }
@Test public void testThatCellRendererIsSet() { final JTree jTree = new JTree(); final TreeCellRenderer renderer1 = jTree.getCellRenderer(); assertNotNull(renderer1); assertEquals(true, renderer1 instanceof DefaultTreeCellRenderer); CatalogTreeUtils.addCellRenderer(jTree); final TreeCellRenderer renderer2 = jTree.getCellRenderer(); assertNotNull(renderer2); assertEquals(true, renderer2 instanceof DefaultTreeCellRenderer); assertNotSame(renderer1, renderer2); }
@Test public void testThatUserObjectWhichIsNoStringIsResolvedToFalse() { final Integer userObject = 4; final DefaultMutableTreeNode notADapNode = new DefaultMutableTreeNode(userObject); assertEquals(false, CatalogTreeUtils.isCatalogReferenceNode(notADapNode)); }
void insertCatalogElements(InputStream catalogIS, URI catalogBaseUri, DefaultMutableTreeNode parent, boolean expandPath) { final List<InvDataset> catalogDatasets = CatalogTreeUtils.getCatalogDatasets(catalogIS, catalogBaseUri); insertCatalogElements(catalogDatasets, parent, expandPath); }
void appendToNode(final JTree jTree, List<InvDataset> datasets, MutableTreeNode parentNode, boolean goDeeper) { for (InvDataset dataset : datasets) { final MutableTreeNode deeperParent; if (!goDeeper || !CatalogTreeUtils.isHyraxId(dataset.getID())) { appendToNode(jTree, dataset, parentNode); if (parentNode.getChildCount() == 0) { continue; } deeperParent = (MutableTreeNode) parentNode.getChildAt(parentNode.getChildCount() - 1); } else { deeperParent = parentNode; } if (goDeeper && !(dataset instanceof InvCatalogRef)) { appendToNode(jTree, dataset.getDatasets(), deeperParent, false); } } }
@Test public void testThatNullIsResolvedToFalse() { final Object noDapNode = null; assertEquals(false, CatalogTreeUtils.isDapNode(noDapNode)); }
@Test public void testAppendCatalogNode() throws URISyntaxException { //preparation datasets.add(createCatalogRefDataset()); //execution new CatalogTree(null, new DefaultAppContext(""), null).appendToNode(new JTree(), datasets, parentNode, true); //verification assertEquals(1, parentNode.getChildCount()); assertEquals(1, parentNode.getChildAt(0).getChildCount()); assertEquals(true, parentNode.getChildAt(0).getChildAt(0).isLeaf()); assertEquals(false, CatalogTreeUtils.isDapNode(parentNode.getChildAt(0))); assertEquals(false, CatalogTreeUtils.isCatalogReferenceNode(parentNode.getChildAt(0))); assertEquals(false, CatalogTreeUtils.isDapNode(parentNode.getChildAt(0).getChildAt(0))); assertEquals(true, CatalogTreeUtils.isCatalogReferenceNode(parentNode.getChildAt(0).getChildAt(0))); }
@Test public void testThatAWellDefinedRootNodeIsCreated() { final DefaultMutableTreeNode rootNode = CatalogTreeUtils.createRootNode(); assertNotNull(rootNode); final Object userObject = rootNode.getUserObject(); assertNotNull(userObject); assertTrue(userObject instanceof String); assertEquals("root", userObject.toString()); }
private void testThatChildIsOnlyDapNodeWithoutFileAccess(DefaultMutableTreeNode parentNode) { testThatChildIsDapNode(parentNode); assertEquals(false, CatalogTreeUtils.isFileNode(parentNode.getChildAt(0))); }
@Test public void testThatRendererRendersDifferentTypes() { final JTree jTree = new JTree(); CatalogTreeUtils.addCellRenderer(jTree); final TreeCellRenderer dapCellRenderer = jTree.getCellRenderer();
@Test public void testThatNullIsResolvedToFalse() { final Object notADapNode = null; assertEquals(false, CatalogTreeUtils.isCatalogReferenceNode(notADapNode)); }
public static List<InvDataset> getCatalogDatasets(DefaultMutableTreeNode node) throws IOException { Object userObject = node.getUserObject(); if (!(userObject instanceof CatalogNode)) { return Collections.emptyList(); } final CatalogNode catalogNode = (CatalogNode) userObject; final URL catalogUrl; InputStream inputStream = null; try { catalogUrl = new URL(catalogNode.getCatalogUri()); URLConnection urlConnection = catalogUrl.openConnection(); inputStream = urlConnection.getInputStream(); return getCatalogDatasets(inputStream, catalogUrl.toURI()); } catch (URISyntaxException e) { throw new IOException(e); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException ignore) { // ok } } } } }
private void collectURIs() { final TreePath[] selectionPaths = ((JTree) catalogTree.getComponent()).getSelectionModel().getSelectionPaths(); if (selectionPaths == null || selectionPaths.length <= 0) { return; } for (TreePath selectionPath : selectionPaths) { final DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) selectionPath.getLastPathComponent(); if (CatalogTreeUtils.isDapNode(treeNode) || CatalogTreeUtils.isFileNode(treeNode)) { final OpendapLeaf leaf = (OpendapLeaf) treeNode.getUserObject(); if (leaf.isDapAccess()) { dapURIs.put(leaf.getDapUri(), leaf.getFileSize() >= 2 * 1024 * 1024); } else if (leaf.isFileAccess()) { fileURIs.add(leaf.getFileUri()); } } } }