use of com.intellij.ide.util.treeView.AbstractTreeNode in project intellij-community by JetBrains.
the class DeleteFromFavoritesAction method actionPerformed.
@Override
public void actionPerformed(AnActionEvent e) {
final DataContext dataContext = e.getDataContext();
Project project = e.getProject();
FavoritesViewTreeBuilder builder = FavoritesTreeViewPanel.FAVORITES_TREE_BUILDER_KEY.getData(dataContext);
if (project == null || builder == null) {
return;
}
Set<Object> selection = builder.getSelectedElements();
if (selection.isEmpty()) {
return;
}
FavoritesManager favoritesManager = FavoritesManager.getInstance(project);
String listName = FavoritesTreeViewPanel.FAVORITES_LIST_NAME_DATA_KEY.getData(dataContext);
FavoritesListProvider provider = favoritesManager.getListProvider(listName);
if (provider != null && provider.willHandle(CommonActionsPanel.Buttons.REMOVE, project, selection)) {
provider.handle(CommonActionsPanel.Buttons.REMOVE, project, selection, builder.getTree());
return;
}
FavoritesTreeNodeDescriptor[] roots = FavoritesTreeViewPanel.CONTEXT_FAVORITES_ROOTS_DATA_KEY.getData(dataContext);
final DnDAwareTree tree = FavoritesTreeViewPanel.FAVORITES_TREE_KEY.getData(dataContext);
assert roots != null && tree != null;
Map<String, List<AbstractTreeNode>> toRemove = new HashMap<>();
for (FavoritesTreeNodeDescriptor root : roots) {
final AbstractTreeNode node = root.getElement();
if (node instanceof FavoritesListNode) {
favoritesManager.removeFavoritesList((String) node.getValue());
} else {
final FavoritesListNode listNode = FavoritesTreeUtil.extractParentList(root);
LOG.assertTrue(listNode != null);
final String name = listNode.getName();
if (!toRemove.containsKey(name)) {
toRemove.put(name, new ArrayList<>());
}
toRemove.get(name).add(node);
}
}
for (String name : toRemove.keySet()) {
favoritesManager.removeRoot(name, toRemove.get(name));
}
}
use of com.intellij.ide.util.treeView.AbstractTreeNode in project intellij-community by JetBrains.
the class TaskDefaultFavoriteListProvider method getPathToUsualNode.
//@Override
//public Comparator<FavoritesTreeNodeDescriptor> getNodeDescriptorComparator() {
// return new Comparator<FavoritesTreeNodeDescriptor>() {
// @Override
// public int compare(FavoritesTreeNodeDescriptor o1, FavoritesTreeNodeDescriptor o2) {
// return o1.getIndex() - o2.getIndex();
// }
// };
//}
//@Override
//public Operation createCustomOperation(OperationType operationType) {
// switch (operationType) {
// case ADD:return getCustomAddOperation();
// case EDIT: return getCustomEditOperation();
// default:return null;
// }
//}
//
// private Operation getCustomAddOperation() {
// return new Operation() {
// @Override
// public boolean willHandle(final Project project, @NotNull Set<Object> selectedObjects) {//todo
// //final int count = tree.getSelectionCount();
// //if (count != 1) {
// // return false;
// //}
// //final TreePath path = tree.getSelectionPath();
// //if (path.getPathCount() > 2) return true;
// return false;
// }
//
// @Override
// public String getCustomName() {
// return "New Note";
// }
//
// @Override
// public void handle(final Project project, @NotNull Set<Object> selectedObjects) {//todo
//final Object component = tree.getSelectionPath().getLastPathComponent();
//if (component instanceof DefaultMutableTreeNode) {
// final Object uo = ((DefaultMutableTreeNode)component).getUserObject();
// if (uo instanceof FavoritesTreeNodeDescriptor) {
// final FavoritesManager favoritesManager = FavoritesManager.getInstance(project);
//
// final AbstractTreeNode treeNode = ((FavoritesTreeNodeDescriptor)uo).getElement();
// final NoteNode node = new NoteNode("Test text", false);
// final NoteProjectNode noteNode = new NoteProjectNode(project, node, favoritesManager.getViewSettings());
// final Consumer<String> after = new Consumer<String>() {
// @Override
// public void consume(String text) {
// node.setText(text);
// // above it
// final AbstractTreeNode parent = treeNode.getParent();
// noteNode.setParent(parent);
// if (parent instanceof ProjectViewNodeWithChildrenList) {
// // add through manager
// //((ProjectViewNodeWithChildrenList)parent).addChildBefore(noteNode, treeNode);
// final List<AbstractTreeNode> pathToSelected = FavoritesTreeUtil.getLogicalPathToSelected(tree);
// final List<AbstractTreeNode> elements;
// AbstractTreeNode sibling;
// if (pathToSelected.isEmpty()) {
// elements = pathToSelected;
// sibling = null;
// }
// else {
// elements = pathToSelected.subList(0, pathToSelected.size() - 1);
// sibling = pathToSelected.get(pathToSelected.size() - 1);
// }
// favoritesManager.addRoot(CURRENT_TASK, elements, noteNode, sibling);
// } else if (parent instanceof FavoritesListNode) {
// favoritesManager.addRoot(CURRENT_TASK, Collections.<AbstractTreeNode>emptyList(), noteNode, treeNode);
// }
// }
// };
// showNotePopup(project, tree, after, "");
// }
//}
//}
//};
//}
// ! containing self
public static List<AbstractTreeNode> getPathToUsualNode(final AbstractTreeNode treeNode) {
final List<AbstractTreeNode> result = new ArrayList<>();
AbstractTreeNode current = treeNode;
while (current != null && (!(current instanceof FavoritesRootNode))) {
result.add(current);
current = current.getParent();
}
Collections.reverse(result);
return result;
}
use of com.intellij.ide.util.treeView.AbstractTreeNode in project intellij-community by JetBrains.
the class FavoritesManager method removeRoot.
public synchronized boolean removeRoot(@NotNull String name, @NotNull List<AbstractTreeNode> elements) {
final Convertor<AbstractTreeNode, AbstractUrl> convertor = new Convertor<AbstractTreeNode, AbstractUrl>() {
@Override
public AbstractUrl convert(AbstractTreeNode obj) {
return createUrlByElement(obj.getValue(), myProject);
}
};
boolean result = true;
for (AbstractTreeNode element : elements) {
final List<AbstractTreeNode> path = TaskDefaultFavoriteListProvider.getPathToUsualNode(element);
result &= findListToRemoveFrom(name, path.subList(1, path.size()), convertor);
}
return result;
}
use of com.intellij.ide.util.treeView.AbstractTreeNode in project intellij-community by JetBrains.
the class FavoritesPanel method setupDnD.
private void setupDnD() {
DnDSupport.createBuilder(myTree).setBeanProvider(info -> {
final TreePath path = myTree.getPathForLocation(info.getPoint().x, info.getPoint().y);
if (path != null && path.getPathCount() == 3) {
Object o = path.getLastPathComponent();
if (o instanceof DefaultMutableTreeNode) {
o = ((DefaultMutableTreeNode) o).getUserObject();
if (o instanceof FavoritesTreeNodeDescriptor) {
FavoritesTreeNodeDescriptor root = ((FavoritesTreeNodeDescriptor) o).getFavoritesRoot();
if (root != null && root != o) {
o = root.getElement();
if (o instanceof FavoritesListNode && ((FavoritesListNode) o).getProvider() == null) {
return new DnDDragStartBean(path);
}
}
}
}
}
if (path != null && path.getPathCount() == 2) {
Object o = path.getLastPathComponent();
if (o instanceof DefaultMutableTreeNode) {
o = ((DefaultMutableTreeNode) o).getUserObject();
if (o instanceof FavoritesTreeNodeDescriptor) {
FavoritesTreeNodeDescriptor root = ((FavoritesTreeNodeDescriptor) o).getFavoritesRoot();
if (root == o) {
return new DnDDragStartBean(path);
}
}
}
}
return new DnDDragStartBean("") {
@Override
public boolean isEmpty() {
return true;
}
};
}).setTargetChecker(new DnDTargetChecker() {
@Override
public boolean update(DnDEvent event) {
final Object obj = event.getAttachedObject();
if ("".equals(obj)) {
event.setDropPossible(false);
return false;
}
int pathCount = 0;
if (obj instanceof TreePath) {
pathCount = ((TreePath) obj).getPathCount();
if (pathCount < 2) {
event.setDropPossible(false);
return true;
}
}
FavoritesListNode node = myViewPanel.findFavoritesListNode(event.getPoint());
if ((obj instanceof TreePath && pathCount == 3 && myViewPanel.myTree.getPath(node).isDescendant((TreePath) obj)) || (node != null && node.getProvider() != null)) {
event.setDropPossible(false);
return false;
}
highlight(node, event);
if (obj instanceof TreePath && pathCount == 2 && node != null && node.getProvider() == null) {
event.setDropPossible(true);
return true;
}
if (node != null) {
event.setDropPossible(true);
return true;
}
event.setDropPossible(false);
return false;
}
}).setDropHandler(new DnDDropHandler() {
@Override
public void drop(DnDEvent event) {
final FavoritesListNode node = myViewPanel.findFavoritesListNode(event.getPoint());
final FavoritesManager mgr = FavoritesManager.getInstance(myProject);
if (node == null)
return;
final String listTo = node.getValue();
final Object obj = event.getAttachedObject();
if (obj instanceof TreePath) {
final TreePath path = (TreePath) obj;
final String listFrom = FavoritesTreeViewPanel.getListNodeFromPath(path).getValue();
if (listTo.equals(listFrom))
return;
if (path.getPathCount() == 3) {
final AbstractTreeNode abstractTreeNode = ((FavoritesTreeNodeDescriptor) ((DefaultMutableTreeNode) path.getLastPathComponent()).getUserObject()).getElement();
Object element = abstractTreeNode.getValue();
mgr.removeRoot(listFrom, Collections.singletonList(abstractTreeNode));
if (element instanceof SmartPsiElementPointer) {
element = ((SmartPsiElementPointer) element).getElement();
}
mgr.addRoots(listTo, null, element);
}
if (path.getPathCount() == 2) {
//favorites lists manual sorting
Rectangle bounds = myTree.getPathBounds(myTree.getPath(node));
if (bounds != null) {
mgr.setOrder(listFrom, listTo, event.getPoint().y < bounds.y + bounds.height / 2);
}
}
} else if (obj instanceof TransferableWrapper) {
myViewPanel.dropPsiElements(mgr, node, ((TransferableWrapper) obj).getPsiElements());
} else if (obj instanceof DnDNativeTarget.EventInfo) {
myViewPanel.dropPsiElements(mgr, node, getPsiFiles(FileCopyPasteUtil.getFileList(((DnDNativeTarget.EventInfo) obj).getTransferable())));
}
}
}).setImageProvider(info -> new DnDImage(myFavoritesImage, new Point(-myFavoritesImage.getWidth(null) / 2, -myFavoritesImage.getHeight(null) / 2))).enableAsNativeTarget().setDisposableParent(myProject).install();
}
use of com.intellij.ide.util.treeView.AbstractTreeNode in project intellij-community by JetBrains.
the class FavoritesTreeStructure method getChildElements.
@Override
public Object[] getChildElements(Object element) {
if (!(element instanceof AbstractTreeNode)) {
return ArrayUtil.EMPTY_OBJECT_ARRAY;
}
final AbstractTreeNode favTreeElement = (AbstractTreeNode) element;
try {
if (!(element instanceof FavoritesListNode)) {
Object[] elements = super.getChildElements(favTreeElement);
if (elements.length > 0)
return elements;
ViewSettings settings = favTreeElement instanceof SettingsProvider ? ((SettingsProvider) favTreeElement).getSettings() : ViewSettings.DEFAULT;
return ArrayUtil.toObjectArray(myNonProjectProvider.modify(favTreeElement, new ArrayList<>(), settings));
}
final List<AbstractTreeNode> result = new ArrayList<>();
final FavoritesListNode listNode = (FavoritesListNode) element;
if (listNode.getProvider() != null) {
return ArrayUtil.toObjectArray(listNode.getChildren());
}
final Collection<AbstractTreeNode> roots = FavoritesListNode.getFavoritesRoots(myProject, listNode.getName(), listNode);
for (AbstractTreeNode<?> abstractTreeNode : roots) {
final Object value = abstractTreeNode.getValue();
if (value == null)
continue;
if (value instanceof PsiElement && !((PsiElement) value).isValid())
continue;
if (value instanceof SmartPsiElementPointer && ((SmartPsiElementPointer) value).getElement() == null)
continue;
boolean invalid = false;
for (FavoriteNodeProvider nodeProvider : Extensions.getExtensions(FavoriteNodeProvider.EP_NAME, myProject)) {
if (nodeProvider.isInvalidElement(value)) {
invalid = true;
break;
}
}
if (invalid)
continue;
result.add(abstractTreeNode);
}
//}
return ArrayUtil.toObjectArray(result);
} catch (Exception e) {
LOGGER.error(e);
}
return ArrayUtil.EMPTY_OBJECT_ARRAY;
}
Aggregations