Search in sources :

Example 1 with ConfigFilesTreeBuilder

use of com.intellij.xml.config.ConfigFilesTreeBuilder in project intellij-community by JetBrains.

the class MapExternalResourceDialog method setupSchemasTree.

private void setupSchemasTree(String uri, @NotNull Project project, @Nullable PsiFile file, @Nullable String location) {
    DefaultMutableTreeNode root = new DefaultMutableTreeNode();
    mySchemasTree.setModel(new DefaultTreeModel(root));
    ConfigFileSearcher searcher = new ConfigFileSearcher(file == null ? null : ModuleUtilCore.findModuleForPsiElement(file), project) {

        @Override
        public Set<PsiFile> search(@Nullable Module module, @NotNull Project project) {
            List<IndexedRelevantResource<String, XsdNamespaceBuilder>> resources = XmlNamespaceIndex.getAllResources(module, project, null);
            HashSet<PsiFile> files = new HashSet<>();
            PsiManager psiManager = PsiManager.getInstance(project);
            for (IndexedRelevantResource<String, XsdNamespaceBuilder> resource : resources) {
                VirtualFile file = resource.getFile();
                PsiFile psiFile = psiManager.findFile(file);
                ContainerUtil.addIfNotNull(files, psiFile);
            }
            return files;
        }
    };
    searcher.search();
    new ConfigFilesTreeBuilder(mySchemasTree).buildTree(root, searcher);
    TreeUtil.expandAll(mySchemasTree);
    mySchemasTree.setRootVisible(false);
    mySchemasTree.setShowsRootHandles(true);
    ColoredTreeCellRenderer renderer = new ColoredTreeCellRenderer() {

        @Override
        public void customizeCellRenderer(@NotNull JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
            ConfigFilesTreeBuilder.renderNode(value, expanded, this);
        }
    };
    renderer.setFont(EditorColorsManager.getInstance().getGlobalScheme().getFont(EditorFontType.PLAIN));
    mySchemasTree.setCellRenderer(renderer);
    MouseAdapter mouseAdapter = new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() > 1 && isOKActionEnabled()) {
                doOKAction();
            }
        }
    };
    mySchemasTree.addMouseListener(mouseAdapter);
    mySchemasTree.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {

        @Override
        public void valueChanged(TreeSelectionEvent e) {
            validateInput();
        }
    });
    mySchemasTree.setSelectionRow(0);
    PsiFile schema = null;
    if (file != null) {
        schema = XmlUtil.findNamespaceByLocation(file, uri);
    } else if (location != null) {
        VirtualFile virtualFile = VfsUtilCore.findRelativeFile(location, null);
        if (virtualFile != null) {
            schema = PsiManager.getInstance(project).findFile(virtualFile);
        }
    }
    if (schema != null) {
        DefaultMutableTreeNode node = TreeUtil.findNodeWithObject(root, schema);
        if (node != null) {
            mySchemaFound = true;
            TreeUtil.selectNode(mySchemasTree, node);
        }
        myFileTextField.setText(schema.getVirtualFile().getCanonicalPath());
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) TreeSelectionListener(javax.swing.event.TreeSelectionListener) NotNull(org.jetbrains.annotations.NotNull) IndexedRelevantResource(com.intellij.xml.index.IndexedRelevantResource) ConfigFilesTreeBuilder(com.intellij.xml.config.ConfigFilesTreeBuilder) PsiFile(com.intellij.psi.PsiFile) HashSet(java.util.HashSet) ColoredTreeCellRenderer(com.intellij.ui.ColoredTreeCellRenderer) MouseEvent(java.awt.event.MouseEvent) MouseAdapter(java.awt.event.MouseAdapter) PsiManager(com.intellij.psi.PsiManager) XsdNamespaceBuilder(com.intellij.xml.index.XsdNamespaceBuilder) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) Project(com.intellij.openapi.project.Project) TreeSelectionEvent(javax.swing.event.TreeSelectionEvent) Module(com.intellij.openapi.module.Module) Nullable(org.jetbrains.annotations.Nullable) ConfigFileSearcher(com.intellij.xml.config.ConfigFileSearcher)

Aggregations

Module (com.intellij.openapi.module.Module)1 Project (com.intellij.openapi.project.Project)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiFile (com.intellij.psi.PsiFile)1 PsiManager (com.intellij.psi.PsiManager)1 ColoredTreeCellRenderer (com.intellij.ui.ColoredTreeCellRenderer)1 ConfigFileSearcher (com.intellij.xml.config.ConfigFileSearcher)1 ConfigFilesTreeBuilder (com.intellij.xml.config.ConfigFilesTreeBuilder)1 IndexedRelevantResource (com.intellij.xml.index.IndexedRelevantResource)1 XsdNamespaceBuilder (com.intellij.xml.index.XsdNamespaceBuilder)1 MouseAdapter (java.awt.event.MouseAdapter)1 MouseEvent (java.awt.event.MouseEvent)1 HashSet (java.util.HashSet)1 TreeSelectionEvent (javax.swing.event.TreeSelectionEvent)1 TreeSelectionListener (javax.swing.event.TreeSelectionListener)1 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)1 DefaultTreeModel (javax.swing.tree.DefaultTreeModel)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1