diff --git a/platform/platform-api/src/com/intellij/ui/ToolbarDecorator.java b/platform/platform-api/src/com/intellij/ui/ToolbarDecorator.java index 8a8a53528fa3..87fbe17a5258 100644 --- a/platform/platform-api/src/com/intellij/ui/ToolbarDecorator.java +++ b/platform/platform-api/src/com/intellij/ui/ToolbarDecorator.java @@ -105,7 +105,11 @@ public abstract class ToolbarDecorator implements DataProvider, CommonActionsPan } public static ToolbarDecorator createDecorator(@NotNull JTree tree) { - return new TreeToolbarDecorator(tree).initPosition(); + return createDecorator(tree, null); + } + + private static ToolbarDecorator createDecorator(@Nullable JTree tree, @Nullable ElementProducer producer) { + return new TreeToolbarDecorator(tree, producer).initPosition(); } public static ToolbarDecorator createDecorator(@NotNull JList list) { diff --git a/platform/platform-api/src/com/intellij/ui/TreeToolbarDecorator.java b/platform/platform-api/src/com/intellij/ui/TreeToolbarDecorator.java index 79d1a3af4621..4b10c2e0d0e8 100644 --- a/platform/platform-api/src/com/intellij/ui/TreeToolbarDecorator.java +++ b/platform/platform-api/src/com/intellij/ui/TreeToolbarDecorator.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2011 JetBrains s.r.o. + * Copyright 2000-2012 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,17 +17,90 @@ package com.intellij.ui; import com.intellij.openapi.actionSystem.ActionToolbarPosition; import com.intellij.openapi.util.SystemInfo; +import com.intellij.ui.treeStructure.SimpleNode; +import com.intellij.util.ui.EditableTreeModel; +import com.intellij.util.ui.ElementProducer; +import com.intellij.util.ui.tree.TreeUtil; +import org.jetbrains.annotations.Nullable; import javax.swing.*; +import javax.swing.event.TreeSelectionEvent; +import javax.swing.event.TreeSelectionListener; +import javax.swing.tree.DefaultMutableTreeNode; +import javax.swing.tree.DefaultTreeModel; +import javax.swing.tree.TreePath; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; /** * @author Konstantin Bulenkov */ class TreeToolbarDecorator extends ToolbarDecorator { private final JTree myTree; + @Nullable private final ElementProducer myProducer; - TreeToolbarDecorator(JTree tree) { + 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(); + final TreePath path = myTree.getSelectionPath(); + model.removeNode(path); + } + }; } @Override @@ -42,6 +115,7 @@ class TreeToolbarDecorator extends ToolbarDecorator { @Override protected void updateButtons() { + getPanel().setEnabled(CommonActionsPanel.Buttons.REMOVE, myTree.getSelectionPath() != null); } @Override diff --git a/platform/util/src/com/intellij/util/ui/EditableTreeModel.java b/platform/util/src/com/intellij/util/ui/EditableTreeModel.java new file mode 100644 index 000000000000..c254c4631e38 --- /dev/null +++ b/platform/util/src/com/intellij/util/ui/EditableTreeModel.java @@ -0,0 +1,35 @@ +/* + * Copyright 2000-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.util.ui; + +import javax.swing.tree.TreePath; + +/** + * @author Konstantin Bulenkov + * @since 12.0 + */ +public interface EditableTreeModel { + /** + * Adds a new node into a + * @param parent selected node, maybe used as parent or as a neighbour + * @return path to newly created element + */ + TreePath addNode(TreePath parentOrNeighbour); + + void removeNode(TreePath parent); + + void moveNodeTo(TreePath parentOrNeighbour); +}