Search in sources :

Example 31 with AbstractTreeNode

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));
    }
}
Also used : HashMap(com.intellij.util.containers.hash.HashMap) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) Project(com.intellij.openapi.project.Project) DataContext(com.intellij.openapi.actionSystem.DataContext) ArrayList(java.util.ArrayList) List(java.util.List) DnDAwareTree(com.intellij.ide.dnd.aware.DnDAwareTree)

Example 32 with AbstractTreeNode

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;
}
Also used : ArrayList(java.util.ArrayList) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode)

Example 33 with AbstractTreeNode

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;
}
Also used : AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) Convertor(com.intellij.util.containers.Convertor)

Example 34 with AbstractTreeNode

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();
}
Also used : AllIcons(com.intellij.icons.AllIcons) TreePath(javax.swing.tree.TreePath) VirtualFile(com.intellij.openapi.vfs.VirtualFile) AbstractTreeBuilder(com.intellij.ide.util.treeView.AbstractTreeBuilder) DnDAwareTree(com.intellij.ide.dnd.aware.DnDAwareTree) LocalFileSystem(com.intellij.openapi.vfs.LocalFileSystem) File(java.io.File) PsiManager(com.intellij.psi.PsiManager) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) ArrayList(java.util.ArrayList) java.awt(java.awt) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) RelativeRectangle(com.intellij.ui.awt.RelativeRectangle) SmartPsiElementPointer(com.intellij.psi.SmartPsiElementPointer) Disposer(com.intellij.openapi.util.Disposer) Project(com.intellij.openapi.project.Project) com.intellij.ide.dnd(com.intellij.ide.dnd) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem) Collections(java.util.Collections) IconUtil(com.intellij.util.IconUtil) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) RelativeRectangle(com.intellij.ui.awt.RelativeRectangle) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) TreePath(javax.swing.tree.TreePath) SmartPsiElementPointer(com.intellij.psi.SmartPsiElementPointer)

Example 35 with AbstractTreeNode

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;
}
Also used : ArrayList(java.util.ArrayList) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) ViewSettings(com.intellij.ide.projectView.ViewSettings) SettingsProvider(com.intellij.ide.projectView.SettingsProvider) SmartPsiElementPointer(com.intellij.psi.SmartPsiElementPointer) PsiElement(com.intellij.psi.PsiElement)

Aggregations

AbstractTreeNode (com.intellij.ide.util.treeView.AbstractTreeNode)154 NotNull (org.jetbrains.annotations.NotNull)69 ArrayList (java.util.ArrayList)52 VirtualFile (com.intellij.openapi.vfs.VirtualFile)43 Project (com.intellij.openapi.project.Project)36 PsiFile (com.intellij.psi.PsiFile)31 Module (com.intellij.openapi.module.Module)29 PsiDirectory (com.intellij.psi.PsiDirectory)27 PsiElement (com.intellij.psi.PsiElement)17 Nullable (org.jetbrains.annotations.Nullable)15 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)14 PsiManager (com.intellij.psi.PsiManager)11 ProjectViewNode (com.intellij.ide.projectView.ProjectViewNode)10 PsiDirectoryNode (com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode)10 List (java.util.List)10 PresentationData (com.intellij.ide.projectView.PresentationData)6 ViewSettings (com.intellij.ide.projectView.ViewSettings)6 PsiFileNode (com.intellij.ide.projectView.impl.nodes.PsiFileNode)6 NamedLibraryElement (com.intellij.ide.projectView.impl.nodes.NamedLibraryElement)5 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)5