Search in sources :

Example 21 with NodeDescriptor

use of com.intellij.ide.util.treeView.NodeDescriptor in project intellij-community by JetBrains.

the class TodoPanel method updatePreviewPanel.

private void updatePreviewPanel() {
    if (myProject == null || myProject.isDisposed())
        return;
    List<UsageInfo> infos = new ArrayList<>();
    final TreePath path = myTree.getSelectionPath();
    if (path != null) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
        Object userObject = node.getUserObject();
        if (userObject instanceof NodeDescriptor) {
            Object element = ((NodeDescriptor) userObject).getElement();
            TodoItemNode pointer = myTodoTreeBuilder.getFirstPointerForElement(element);
            if (pointer != null) {
                final SmartTodoItemPointer value = pointer.getValue();
                final Document document = value.getDocument();
                final PsiFile psiFile = PsiDocumentManager.getInstance(myProject).getPsiFile(document);
                final RangeMarker rangeMarker = value.getRangeMarker();
                if (psiFile != null) {
                    infos.add(new UsageInfo(psiFile, rangeMarker.getStartOffset(), rangeMarker.getEndOffset()));
                }
            }
        }
    }
    myUsagePreviewPanel.updateLayout(infos.isEmpty() ? null : infos);
}
Also used : TreePath(javax.swing.tree.TreePath) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) ArrayList(java.util.ArrayList) NodeDescriptor(com.intellij.ide.util.treeView.NodeDescriptor) PsiFile(com.intellij.psi.PsiFile) RangeMarker(com.intellij.openapi.editor.RangeMarker) TodoItemNode(com.intellij.ide.todo.nodes.TodoItemNode) Document(com.intellij.openapi.editor.Document) UsageInfo(com.intellij.usageView.UsageInfo)

Example 22 with NodeDescriptor

use of com.intellij.ide.util.treeView.NodeDescriptor in project intellij-community by JetBrains.

the class TodoPanel method getData.

@Override
public Object getData(String dataId) {
    if (CommonDataKeys.NAVIGATABLE.is(dataId)) {
        TreePath path = myTree.getSelectionPath();
        if (path == null) {
            return null;
        }
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
        Object userObject = node.getUserObject();
        if (!(userObject instanceof NodeDescriptor)) {
            return null;
        }
        Object element = ((NodeDescriptor) userObject).getElement();
        if (!(element instanceof TodoFileNode || element instanceof TodoItemNode)) {
            // allow user to use F4 only on files an TODOs
            return null;
        }
        TodoItemNode pointer = myTodoTreeBuilder.getFirstPointerForElement(element);
        if (pointer != null) {
            return new OpenFileDescriptor(myProject, pointer.getValue().getTodoItem().getFile().getVirtualFile(), pointer.getValue().getRangeMarker().getStartOffset());
        } else {
            return null;
        }
    } else if (CommonDataKeys.VIRTUAL_FILE.is(dataId)) {
        final PsiFile file = getSelectedFile();
        return file != null ? file.getVirtualFile() : null;
    } else if (CommonDataKeys.PSI_ELEMENT.is(dataId)) {
        return getSelectedElement();
    } else if (CommonDataKeys.VIRTUAL_FILE_ARRAY.is(dataId)) {
        PsiFile file = getSelectedFile();
        if (file != null) {
            return new VirtualFile[] { file.getVirtualFile() };
        } else {
            return VirtualFile.EMPTY_ARRAY;
        }
    } else if (PlatformDataKeys.HELP_ID.is(dataId)) {
        //noinspection HardCodedStringLiteral
        return "find.todoList";
    }
    return super.getData(dataId);
}
Also used : TreePath(javax.swing.tree.TreePath) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) TodoFileNode(com.intellij.ide.todo.nodes.TodoFileNode) NodeDescriptor(com.intellij.ide.util.treeView.NodeDescriptor) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) PsiFile(com.intellij.psi.PsiFile) TodoItemNode(com.intellij.ide.todo.nodes.TodoItemNode)

Example 23 with NodeDescriptor

use of com.intellij.ide.util.treeView.NodeDescriptor in project intellij-community by JetBrains.

the class ComponentTree method getSelectedElements.

public <T> List<T> getSelectedElements(Class<? extends T> elementClass) {
    final TreePath[] paths = getSelectionPaths();
    if (paths == null) {
        return Collections.emptyList();
    }
    final ArrayList<T> result = new ArrayList<>(paths.length);
    for (TreePath path : paths) {
        final DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
        Object userObject = node.getUserObject();
        if (userObject instanceof NodeDescriptor && elementClass.isInstance(((NodeDescriptor) userObject).getElement())) {
            //noinspection unchecked
            result.add((T) ((NodeDescriptor) node.getUserObject()).getElement());
        }
    }
    return result;
}
Also used : TreePath(javax.swing.tree.TreePath) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) NodeDescriptor(com.intellij.ide.util.treeView.NodeDescriptor)

Example 24 with NodeDescriptor

use of com.intellij.ide.util.treeView.NodeDescriptor in project android by JetBrains.

the class AndroidProjectViewPane method getData.

@Override
public Object getData(String dataId) {
    if (CommonDataKeys.PROJECT.is(dataId)) {
        return myProject;
    }
    if (PlatformDataKeys.DELETE_ELEMENT_PROVIDER.is(dataId)) {
        Object o = getSelectedElement();
        if (o instanceof PsiDirectory) {
            VirtualFile directory = ((PsiDirectory) o).getVirtualFile();
            // See https://code.google.com/p/android/issues/detail?id=212522
            if (isTopModuleDirectoryOrParent(directory)) {
                return new NoOpDeleteProvider();
            }
        }
    }
    if (LangDataKeys.MODULE.is(dataId)) {
        Object o = getSelectedElement();
        if (o instanceof PackageElement) {
            PackageElement packageElement = (PackageElement) o;
            return packageElement.getModule();
        } else if (o instanceof AndroidFacet) {
            return ((AndroidFacet) o).getModule();
        }
    }
    if (CommonDataKeys.VIRTUAL_FILE.is(dataId)) {
        Object o = getSelectedElement();
        if (o instanceof PackageElement) {
            PackageElement packageElement = (PackageElement) o;
            Module m = packageElement.getModule();
            if (m != null) {
                PsiDirectory[] folders = packageElement.getPackage().getDirectories(GlobalSearchScope.moduleScope(m));
                if (folders.length > 0) {
                    return folders[0].getVirtualFile();
                } else {
                    return null;
                }
            }
        }
    }
    if (CommonDataKeys.VIRTUAL_FILE_ARRAY.is(dataId)) {
        NodeDescriptor selectedDescriptor = getSelectedDescriptor();
        if (selectedDescriptor instanceof FileGroupNode) {
            PsiFile[] files = ((FileGroupNode) selectedDescriptor).getFiles();
            if (files.length > 0) {
                List<VirtualFile> virtualFiles = Lists.newArrayListWithExpectedSize(files.length);
                for (PsiFile file : files) {
                    if (file.isValid()) {
                        virtualFiles.add(file.getVirtualFile());
                    }
                }
                return virtualFiles.toArray(new VirtualFile[virtualFiles.size()]);
            }
        }
        if (selectedDescriptor instanceof DirectoryGroupNode) {
            PsiDirectory[] directories = ((DirectoryGroupNode) selectedDescriptor).getDirectories();
            if (directories.length > 0) {
                List<VirtualFile> virtualFiles = Lists.newArrayListWithExpectedSize(directories.length);
                for (PsiDirectory directory : directories) {
                    if (directory.isValid()) {
                        virtualFiles.add(directory.getVirtualFile());
                    }
                }
                return virtualFiles.toArray(new VirtualFile[virtualFiles.size()]);
            }
        }
    }
    if (CommonDataKeys.PSI_ELEMENT.is(dataId)) {
        Object o = getSelectedElement();
        if (o instanceof PsiElement) {
            return o;
        } else if (o instanceof List<?>) {
            List<?> l = (List<?>) o;
            if (!l.isEmpty() && l.get(0) instanceof PsiElement) {
                return l.get(0);
            }
        }
        NodeDescriptor selectedDescriptor = getSelectedDescriptor();
        if (selectedDescriptor instanceof FileGroupNode) {
            PsiFile[] files = ((FileGroupNode) selectedDescriptor).getFiles();
            if (files.length > 0) {
                return files[0];
            }
        }
        if (selectedDescriptor instanceof DirectoryGroupNode) {
            PsiDirectory[] directories = ((DirectoryGroupNode) selectedDescriptor).getDirectories();
            if (directories.length > 0) {
                return directories[0];
            }
        }
    }
    return super.getData(dataId);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DirectoryGroupNode(com.android.tools.idea.navigator.nodes.DirectoryGroupNode) NodeDescriptor(com.intellij.ide.util.treeView.NodeDescriptor) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) FileGroupNode(com.android.tools.idea.navigator.nodes.FileGroupNode) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile) ArrayList(java.util.ArrayList) List(java.util.List) PackageElement(com.intellij.ide.projectView.impl.nodes.PackageElement) Module(com.intellij.openapi.module.Module) PsiElement(com.intellij.psi.PsiElement)

Example 25 with NodeDescriptor

use of com.intellij.ide.util.treeView.NodeDescriptor in project android by JetBrains.

the class AndroidResComparator method compare.

@Override
public int compare(Object o1, Object o2) {
    if (!(o1 instanceof NodeDescriptor) || !(o2 instanceof NodeDescriptor)) {
        return 0;
    }
    // we only support comparing res file nodes and res group nodes
    if (!(o1 instanceof AndroidResFileNode) && !(o1 instanceof AndroidResGroupNode)) {
        return 0;
    }
    if (!(o2 instanceof AndroidResFileNode) && !(o2 instanceof AndroidResGroupNode)) {
        return 0;
    }
    // if one of them is a group node, then we just have to compare them alphabetically
    if (o1 instanceof AndroidResGroupNode || o2 instanceof AndroidResGroupNode) {
        String n1 = getName(o1);
        String n2 = getName(o2);
        return StringUtil.compare(n1, n2, false);
    }
    AndroidResFileNode r1 = (AndroidResFileNode) o1;
    AndroidResFileNode r2 = (AndroidResFileNode) o2;
    // first check file names
    PsiFile file1 = r1.getValue();
    PsiFile file2 = r2.getValue();
    if (file1 != null && file2 != null) {
        int c = StringUtil.compare(file1.getName(), file2.getName(), false);
        if (c != 0) {
            return c;
        }
    }
    // check folder configurations
    FolderConfiguration config1 = r1.getFolderConfiguration();
    FolderConfiguration config2 = r2.getFolderConfiguration();
    if (config1 != null && config2 != null) {
        int c = config1.compareTo(config2);
        if (c != 0) {
            return c;
        }
    }
    // then check qualifiers
    return StringUtil.compare(r1.getQualifier(), r2.getQualifier(), false);
}
Also used : NodeDescriptor(com.intellij.ide.util.treeView.NodeDescriptor) PsiFile(com.intellij.psi.PsiFile) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration)

Aggregations

NodeDescriptor (com.intellij.ide.util.treeView.NodeDescriptor)25 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)9 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 PsiFile (com.intellij.psi.PsiFile)4 SimpleTextAttributes (com.intellij.ui.SimpleTextAttributes)4 TreePath (javax.swing.tree.TreePath)4 TodoItemNode (com.intellij.ide.todo.nodes.TodoItemNode)3 AbstractTreeNode (com.intellij.ide.util.treeView.AbstractTreeNode)3 DirectoryGroupNode (com.android.tools.idea.navigator.nodes.DirectoryGroupNode)2 PackageElement (com.intellij.ide.projectView.impl.nodes.PackageElement)2 FileElement (com.intellij.openapi.fileChooser.FileElement)2 Module (com.intellij.openapi.module.Module)2 Project (com.intellij.openapi.project.Project)2 ContentEntry (com.intellij.openapi.roots.ContentEntry)2 PsiDirectory (com.intellij.psi.PsiDirectory)2 PsiElement (com.intellij.psi.PsiElement)2 ArrayList (java.util.ArrayList)2 Nullable (org.jetbrains.annotations.Nullable)2 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)1 FileGroupNode (com.android.tools.idea.navigator.nodes.FileGroupNode)1