TreeToolbarDecorator(JTree tree, @Nullable final ElementProducer<?> producer) { myTree = tree; myProducer = producer; myAddActionEnabled = myRemoveActionEnabled = myUpActionEnabled = myDownActionEnabled = myTree.getModel() instanceof EditableTreeModel; if (myTree.getModel() instanceof EditableTreeModel) { createDefaultTreeActions(); } myTree.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() { @Override public void valueChanged(TreeSelectionEvent e) { updateButtons(); } }); myTree.addPropertyChangeListener("enabled", new PropertyChangeListener() { @Override public void propertyChange(PropertyChangeEvent evt) { updateButtons(); } }); }
private void createDefaultTreeActions() { final EditableTreeModel model = (EditableTreeModel)myTree.getModel(); myAddAction = new AnActionButtonRunnable() { @Override public void run(AnActionButton button) { final TreePath path = myTree.getSelectionPath(); final DefaultMutableTreeNode selected = path == null ? (DefaultMutableTreeNode)myTree.getModel().getRoot() : (DefaultMutableTreeNode)path.getLastPathComponent(); final Object selectedNode = selected.getUserObject(); myTree.stopEditing(); Object element; if (model instanceof DefaultTreeModel && myProducer != null) { element = myProducer.createElement(); if (element == null) return; } else { element = null; } DefaultMutableTreeNode parent = selected; if ((selectedNode instanceof SimpleNode && ((SimpleNode)selectedNode).isAlwaysLeaf()) || !selected.getAllowsChildren()) { parent = (DefaultMutableTreeNode)selected.getParent(); } if (parent != null) { parent.insert(new DefaultMutableTreeNode(element), parent.getChildCount()); } final TreePath createdPath = model.addNode(new TreePath(parent.getPath())); if (path != null) { TreeUtil.selectPath(myTree, createdPath); myTree.requestFocus(); } } }; myRemoveAction = new AnActionButtonRunnable() { @Override public void run(AnActionButton button) { myTree.stopEditing(); if (myTree.getSelectionModel().getSelectionMode() == TreeSelectionModel.SINGLE_TREE_SELECTION) { final TreePath path = myTree.getSelectionPath(); if (path != null) { model.removeNode(path); } } else { final TreePath[] paths = myTree.getSelectionPaths(); if (paths != null && paths.length > 0) { model.removeNodes(Arrays.asList(paths)); } } } }; }
private void createDefaultTreeActions() { final EditableTreeModel model = (EditableTreeModel)myTree.getModel(); myAddAction = new AnActionButtonRunnable() { @Override public void run(AnActionButton button) { final TreePath path = myTree.getSelectionPath(); final DefaultMutableTreeNode selected = path == null ? (DefaultMutableTreeNode)myTree.getModel().getRoot() : (DefaultMutableTreeNode)path.getLastPathComponent(); final Object selectedNode = selected.getUserObject(); myTree.stopEditing(); Object element; if (model instanceof DefaultTreeModel && myProducer != null) { element = myProducer.createElement(); if (element == null) return; } else { element = null; } DefaultMutableTreeNode parent = selected; if ((selectedNode instanceof SimpleNode && ((SimpleNode)selectedNode).isAlwaysLeaf()) || !selected.getAllowsChildren()) { parent = (DefaultMutableTreeNode)selected.getParent(); } if (parent != null) { parent.insert(new DefaultMutableTreeNode(element), parent.getChildCount()); } final TreePath createdPath = model.addNode(new TreePath(parent.getPath())); if (path != null) { TreeUtil.selectPath(myTree, createdPath); myTree.requestFocus(); } } }; myRemoveAction = new AnActionButtonRunnable() { @Override public void run(AnActionButton button) { myTree.stopEditing(); final TreePath path = myTree.getSelectionPath(); model.removeNode(path); } }; }
private void createDefaultTreeActions() { final EditableTreeModel model = (EditableTreeModel)myTree.getModel(); myAddAction = new AnActionButtonRunnable() { @Override public void run(AnActionButton button) { final TreePath path = myTree.getSelectionPath(); final DefaultMutableTreeNode selected = path == null ? (DefaultMutableTreeNode)myTree.getModel().getRoot() : (DefaultMutableTreeNode)path.getLastPathComponent(); final Object selectedNode = selected.getUserObject(); myTree.stopEditing(); Object element; if (model instanceof DefaultTreeModel && myProducer != null) { element = myProducer.createElement(); if (element == null) return; } else { element = null; } DefaultMutableTreeNode parent = selected; if ((selectedNode instanceof SimpleNode && ((SimpleNode)selectedNode).isAlwaysLeaf()) || !selected.getAllowsChildren()) { parent = (DefaultMutableTreeNode)selected.getParent(); } if (parent != null) { parent.insert(new DefaultMutableTreeNode(element), parent.getChildCount()); } final TreePath createdPath = model.addNode(new TreePath(parent.getPath())); if (path != null) { TreeUtil.selectPath(myTree, createdPath); IdeFocusManager.getGlobalInstance().doForceFocusWhenFocusSettlesDown(myTree); } } }; myRemoveAction = new AnActionButtonRunnable() { @Override public void run(AnActionButton button) { myTree.stopEditing(); final TreePath path = myTree.getSelectionPath(); model.removeNode(path); } }; }