Search in sources :

Example 6 with TreeItem

use of com.intellij.util.TreeItem in project intellij-community by JetBrains.

the class FavoritesManager method addRoot.

public synchronized boolean addRoot(@NotNull String name, @NotNull List<AbstractTreeNode> parentElements, final AbstractTreeNode newElement, @Nullable AbstractTreeNode sibling) {
    final List<TreeItem<Pair<AbstractUrl, String>>> items = myName2FavoritesRoots.get(name);
    if (items == null)
        return false;
    AbstractUrl url = createUrlByElement(newElement.getValue(), myProject);
    if (url == null)
        return false;
    final TreeItem<Pair<AbstractUrl, String>> newItem = new TreeItem<>(Pair.create(url, newElement.getClass().getName()));
    if (parentElements.isEmpty()) {
        // directly to list
        if (sibling != null) {
            TreeItem<Pair<AbstractUrl, String>> after = null;
            AbstractUrl siblingUrl = createUrlByElement(sibling.getValue(), myProject);
            int idx = -1;
            for (int i = 0; i < items.size(); i++) {
                TreeItem<Pair<AbstractUrl, String>> item = items.get(i);
                if (item.getData().getFirst().equals(siblingUrl)) {
                    idx = i;
                    break;
                }
            }
            if (idx != -1) {
                items.add(idx, newItem);
            } else {
                items.add(newItem);
            }
        } else {
            items.add(newItem);
        }
        rootsChanged();
        return true;
    }
    Collection<TreeItem<Pair<AbstractUrl, String>>> list = items;
    TreeItem<Pair<AbstractUrl, String>> item = null;
    for (AbstractTreeNode obj : parentElements) {
        AbstractUrl objUrl = createUrlByElement(obj.getValue(), myProject);
        item = findNextItem(objUrl, list);
        if (item == null)
            return false;
        list = item.getChildren();
    }
    if (sibling != null) {
        TreeItem<Pair<AbstractUrl, String>> after = null;
        AbstractUrl siblingUrl = createUrlByElement(sibling.getValue(), myProject);
        for (TreeItem<Pair<AbstractUrl, String>> treeItem : list) {
            if (treeItem.getData().getFirst().equals(siblingUrl)) {
                after = treeItem;
                break;
            }
        }
        if (after == null) {
            item.addChild(newItem);
        } else {
            item.addChildAfter(newItem, after);
        }
    } else {
        item.addChild(newItem);
    }
    rootsChanged();
    return true;
}
Also used : TreeItem(com.intellij.util.TreeItem) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode)

Example 7 with TreeItem

use of com.intellij.util.TreeItem in project intellij-community by JetBrains.

the class FavoritesManager method contains.

// currently only one level here..
public boolean contains(@NotNull String name, @NotNull final VirtualFile vFile) {
    final ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(myProject).getFileIndex();
    final Set<Boolean> find = new HashSet<>();
    final ContentIterator contentIterator = new ContentIterator() {

        @Override
        public boolean processFile(VirtualFile fileOrDir) {
            if (fileOrDir != null && fileOrDir.getPath().equals(vFile.getPath())) {
                find.add(Boolean.TRUE);
            }
            return true;
        }
    };
    Collection<TreeItem<Pair<AbstractUrl, String>>> urls = getFavoritesListRootUrls(name);
    for (TreeItem<Pair<AbstractUrl, String>> pair : urls) {
        AbstractUrl abstractUrl = pair.getData().getFirst();
        if (abstractUrl == null) {
            continue;
        }
        final Object[] path = abstractUrl.createPath(myProject);
        if (path == null || path.length < 1 || path[0] == null) {
            continue;
        }
        Object element = path[path.length - 1];
        if (element instanceof SmartPsiElementPointer) {
            final VirtualFile virtualFile = PsiUtilBase.getVirtualFile(((SmartPsiElementPointer) element).getElement());
            if (virtualFile == null)
                continue;
            if (vFile.getPath().equals(virtualFile.getPath())) {
                return true;
            }
            if (!virtualFile.isDirectory()) {
                continue;
            }
            projectFileIndex.iterateContentUnderDirectory(virtualFile, contentIterator);
        }
        if (element instanceof PsiElement) {
            final VirtualFile virtualFile = PsiUtilBase.getVirtualFile((PsiElement) element);
            if (virtualFile == null)
                continue;
            if (vFile.getPath().equals(virtualFile.getPath())) {
                return true;
            }
            if (!virtualFile.isDirectory()) {
                continue;
            }
            projectFileIndex.iterateContentUnderDirectory(virtualFile, contentIterator);
        }
        if (element instanceof Module) {
            ModuleRootManager.getInstance((Module) element).getFileIndex().iterateContent(contentIterator);
        }
        if (element instanceof LibraryGroupElement) {
            final boolean inLibrary = ModuleRootManager.getInstance(((LibraryGroupElement) element).getModule()).getFileIndex().isInContent(vFile) && projectFileIndex.isInLibraryClasses(vFile);
            if (inLibrary) {
                return true;
            }
        }
        if (element instanceof NamedLibraryElement) {
            NamedLibraryElement namedLibraryElement = (NamedLibraryElement) element;
            final VirtualFile[] files = namedLibraryElement.getOrderEntry().getRootFiles(OrderRootType.CLASSES);
            if (files != null && ArrayUtil.find(files, vFile) > -1) {
                return true;
            }
        }
        if (element instanceof ModuleGroup) {
            ModuleGroup group = (ModuleGroup) element;
            final Collection<Module> modules = group.modulesInGroup(myProject, true);
            for (Module module : modules) {
                ModuleRootManager.getInstance(module).getFileIndex().iterateContent(contentIterator);
            }
        }
        for (FavoriteNodeProvider provider : Extensions.getExtensions(FavoriteNodeProvider.EP_NAME, myProject)) {
            if (provider.elementContainsFile(element, vFile)) {
                return true;
            }
        }
        if (!find.isEmpty()) {
            return true;
        }
    }
    return false;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) TreeItem(com.intellij.util.TreeItem) LibraryGroupElement(com.intellij.ide.projectView.impl.nodes.LibraryGroupElement) NamedLibraryElement(com.intellij.ide.projectView.impl.nodes.NamedLibraryElement) Module(com.intellij.openapi.module.Module)

Example 8 with TreeItem

use of com.intellij.util.TreeItem in project intellij-community by JetBrains.

the class FavoritesManager method readFavoritesOneLevel.

private static void readFavoritesOneLevel(Element list, Project project, Collection<TreeItem<Pair<AbstractUrl, String>>> result) {
    final List listChildren = list.getChildren(FAVORITES_ROOT);
    if (listChildren == null || listChildren.isEmpty())
        return;
    for (Object favorite : listChildren) {
        final Element favoriteElement = (Element) favorite;
        final String className = favoriteElement.getAttributeValue(CLASS_NAME);
        final AbstractUrl abstractUrl = readUrlFromElement(favoriteElement, project);
        if (abstractUrl != null) {
            final TreeItem<Pair<AbstractUrl, String>> treeItem = new TreeItem<>(Pair.create(abstractUrl, className));
            result.add(treeItem);
            readFavoritesOneLevel(favoriteElement, project, treeItem.getChildren());
        }
    }
}
Also used : TreeItem(com.intellij.util.TreeItem) NamedLibraryElement(com.intellij.ide.projectView.impl.nodes.NamedLibraryElement) LibraryGroupElement(com.intellij.ide.projectView.impl.nodes.LibraryGroupElement) Element(org.jdom.Element)

Example 9 with TreeItem

use of com.intellij.util.TreeItem in project intellij-community by JetBrains.

the class FavoritesListNode method processUrls.

private static void processUrls(Project project, Collection<TreeItem<Pair<AbstractUrl, String>>> urls, Collection<AbstractTreeNode> result, final AbstractTreeNode me) {
    for (TreeItem<Pair<AbstractUrl, String>> pair : urls) {
        AbstractUrl abstractUrl = pair.getData().getFirst();
        final Object[] path = abstractUrl.createPath(project);
        if (path == null || path.length < 1 || path[0] == null) {
            continue;
        }
        try {
            final String className = pair.getData().getSecond();
            @SuppressWarnings("unchecked") final Class<? extends AbstractTreeNode> nodeClass = (Class<? extends AbstractTreeNode>) Class.forName(className);
            final AbstractTreeNode node = ProjectViewNode.createTreeNode(nodeClass, project, path[path.length - 1], FavoritesManager.getInstance(project).getViewSettings());
            node.setParent(me);
            node.setIndex(result.size());
            result.add(node);
            if (node instanceof ProjectViewNodeWithChildrenList) {
                final List<TreeItem<Pair<AbstractUrl, String>>> children = pair.getChildren();
                if (children != null && !children.isEmpty()) {
                    Collection<AbstractTreeNode> childList = new ArrayList<>();
                    processUrls(project, children, childList, node);
                    for (AbstractTreeNode treeNode : childList) {
                        ((ProjectViewNodeWithChildrenList) node).addChild(treeNode);
                    }
                }
            }
        } catch (Exception e) {
            LOGGER.error(e);
        }
    }
}
Also used : TreeItem(com.intellij.util.TreeItem) AbstractUrl(com.intellij.ide.projectView.impl.AbstractUrl) ArrayList(java.util.ArrayList) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) Pair(com.intellij.openapi.util.Pair)

Aggregations

TreeItem (com.intellij.util.TreeItem)9 LibraryGroupElement (com.intellij.ide.projectView.impl.nodes.LibraryGroupElement)5 NamedLibraryElement (com.intellij.ide.projectView.impl.nodes.NamedLibraryElement)5 AbstractTreeNode (com.intellij.ide.util.treeView.AbstractTreeNode)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 Element (org.jdom.Element)4 Module (com.intellij.openapi.module.Module)3 NotNull (org.jetbrains.annotations.NotNull)3 IdeBundle (com.intellij.ide.IdeBundle)2 EP_NAME (com.intellij.ide.favoritesTreeView.FavoritesListProvider.EP_NAME)2 AddToFavoritesAction (com.intellij.ide.favoritesTreeView.actions.AddToFavoritesAction)2 com.intellij.ide.projectView.impl (com.intellij.ide.projectView.impl)2 AbstractUrl (com.intellij.ide.projectView.impl.AbstractUrl)2 Disposable (com.intellij.openapi.Disposable)2 ApplicationManager (com.intellij.openapi.application.ApplicationManager)2 ProjectComponent (com.intellij.openapi.components.ProjectComponent)2 Extensions (com.intellij.openapi.extensions.Extensions)2 ModuleUtil (com.intellij.openapi.module.ModuleUtil)2 Project (com.intellij.openapi.project.Project)2 com.intellij.openapi.roots (com.intellij.openapi.roots)2