/** * Set the currently selected Variable. * * @param v select this Variable, must be already in the tree. */ public void setSelected(VariableIF v) { if (v == null) return; // construct chain of variables List<VariableIF> vchain = new ArrayList<>(); vchain.add(v); VariableIF vp = v; while (vp.isMemberOfStructure()) { vp = vp.getParentStructure(); vchain.add(0, vp); // reverse } // construct chain of groups List<Group> gchain = new ArrayList<>(); Group gp = vp.getParentGroup(); gchain.add(gp); while (gp.getParentGroup() != null) { gp = gp.getParentGroup(); gchain.add(0, gp); // reverse } List<Object> pathList = new ArrayList<>(); // start at root, work down through the nested groups, if any GroupNode gnode = (GroupNode) model.getRoot(); pathList.add(gnode); Group parentGroup = gchain.get(0); // always the root group for (int i = 1; i < gchain.size(); i++) { parentGroup = gchain.get(i); gnode = gnode.findNestedGroup(parentGroup); assert gnode != null; pathList.add(gnode); } vp = vchain.get(0); VariableNode vnode = gnode.findNestedVariable(vp); if (vnode == null) return; // not found pathList.add(vnode); // now work down through the structure members, if any for (int i = 1; i < vchain.size(); i++) { vp = vchain.get(i); vnode = vnode.findNestedVariable(vp); if (vnode == null) return; // not found pathList.add(vnode); } // convert to TreePath, and select it Object[] paths = pathList.toArray(); TreePath treePath = new TreePath(paths); tree.setSelectionPath(treePath); tree.scrollPathToVisible(treePath); }
public void setFile(NetcdfFile ds) { if (ds != currentDataset) { currentDataset = ds; model = new DatasetTreeModel(ds); tree.setModel(model); } }
/** Constructor. */ public DatasetTreeView() { // the catalog tree tree = new JTree() { public JToolTip createToolTip() { return new MultilineTooltip(); } }; tree.setModel(new DefaultTreeModel(new DefaultMutableTreeNode(null, false))); tree.setCellRenderer(new MyTreeCellRenderer()); tree.addMouseListener( new MouseAdapter() { public void mousePressed(MouseEvent e) { int selRow = tree.getRowForLocation(e.getX(), e.getY()); if (selRow != -1) { TreeNode node = (TreeNode) tree.getLastSelectedPathComponent(); if (node instanceof VariableNode) { VariableIF v = ((VariableNode) node).var; firePropertyChangeEvent(new PropertyChangeEvent(this, "Selection", null, v)); } } if ((selRow != -1) && (e.getClickCount() == 2)) { // acceptSelected(); } } }); tree.putClientProperty("JTree.lineStyle", "Angled"); tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); tree.setToggleClickCount(1); ToolTipManager.sharedInstance().registerComponent(tree); // layout setLayout(new BorderLayout()); add(new JScrollPane(tree), BorderLayout.CENTER); }
public void clear() { currentDataset = null; model = null; tree.setModel(null); }