mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
better support for trees
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user