Search in sources :

Example 1 with NodeRenderer

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

the class MavenSelectProjectPopup method attachToButton.

public static void attachToButton(@NotNull final MavenProjectsManager projectsManager, @NotNull final JButton button, @NotNull final Consumer<MavenProject> callback) {
    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            List<MavenProject> projectList = projectsManager.getProjects();
            if (projectList.isEmpty()) {
                JBPopupFactory.getInstance().createMessage("Maven projects not found").showUnderneathOf(button);
                return;
            }
            DefaultMutableTreeNode root = buildTree(projectList);
            final Map<MavenProject, String> projectsNameMap = MavenProjectNamer.generateNameMap(projectList);
            final Tree projectTree = new Tree(root);
            projectTree.setRootVisible(false);
            projectTree.setCellRenderer(new NodeRenderer() {

                @Override
                public void customizeCellRenderer(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
                    if (value instanceof DefaultMutableTreeNode) {
                        MavenProject mavenProject = (MavenProject) ((DefaultMutableTreeNode) value).getUserObject();
                        value = projectsNameMap.get(mavenProject);
                        setIcon(MavenIcons.MavenProject);
                    }
                    super.customizeCellRenderer(tree, value, selected, expanded, leaf, row, hasFocus);
                }
            });
            new TreeSpeedSearch(projectTree, new Convertor<TreePath, String>() {

                @Override
                public String convert(TreePath o) {
                    Object lastPathComponent = o.getLastPathComponent();
                    if (!(lastPathComponent instanceof DefaultMutableTreeNode))
                        return null;
                    Object userObject = ((DefaultMutableTreeNode) lastPathComponent).getUserObject();
                    //noinspection SuspiciousMethodCalls
                    return projectsNameMap.get(userObject);
                }
            });
            final Ref<JBPopup> popupRef = new Ref<>();
            final Runnable clickCallBack = () -> {
                TreePath path = projectTree.getSelectionPath();
                if (path == null)
                    return;
                Object lastPathComponent = path.getLastPathComponent();
                if (!(lastPathComponent instanceof DefaultMutableTreeNode))
                    return;
                Object object = ((DefaultMutableTreeNode) lastPathComponent).getUserObject();
                // may be it's the root
                if (object == null)
                    return;
                callback.consume((MavenProject) object);
                popupRef.get().closeOk(null);
            };
            projectTree.addKeyListener(new KeyAdapter() {

                @Override
                public void keyPressed(KeyEvent e) {
                    if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                        clickCallBack.run();
                        e.consume();
                    }
                }
            });
            JBPopup popup = new PopupChooserBuilder(projectTree).setTitle("Select maven project").setResizable(true).setItemChoosenCallback(clickCallBack).setAutoselectOnMouseMove(true).setCloseOnEnter(false).createPopup();
            popupRef.set(popup);
            popup.showUnderneathOf(button);
        }

        private DefaultMutableTreeNode buildTree(List<MavenProject> projectList) {
            MavenProject[] projects = projectList.toArray(new MavenProject[projectList.size()]);
            Arrays.sort(projects, new MavenProjectNamer.MavenProjectComparator());
            Map<MavenProject, DefaultMutableTreeNode> projectsToNode = new HashMap<>();
            for (MavenProject mavenProject : projects) {
                projectsToNode.put(mavenProject, new DefaultMutableTreeNode(mavenProject));
            }
            DefaultMutableTreeNode root = new DefaultMutableTreeNode();
            for (MavenProject mavenProject : projects) {
                DefaultMutableTreeNode parent;
                MavenProject aggregator = projectsManager.findAggregator(mavenProject);
                if (aggregator != null) {
                    parent = projectsToNode.get(aggregator);
                } else {
                    parent = root;
                }
                parent.add(projectsToNode.get(mavenProject));
            }
            return root;
        }
    });
}
Also used : DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) ActionEvent(java.awt.event.ActionEvent) TreeSpeedSearch(com.intellij.ui.TreeSpeedSearch) KeyAdapter(java.awt.event.KeyAdapter) Convertor(com.intellij.util.containers.Convertor) NodeRenderer(com.intellij.ide.util.treeView.NodeRenderer) KeyEvent(java.awt.event.KeyEvent) Ref(com.intellij.openapi.util.Ref) ActionListener(java.awt.event.ActionListener) MavenProject(org.jetbrains.idea.maven.project.MavenProject) TreePath(javax.swing.tree.TreePath) Tree(com.intellij.ui.treeStructure.Tree) List(java.util.List) PopupChooserBuilder(com.intellij.openapi.ui.popup.PopupChooserBuilder) JBPopup(com.intellij.openapi.ui.popup.JBPopup) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with NodeRenderer

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

the class FileChooserDialogImpl method createTree.

protected JTree createTree() {
    Tree internalTree = createInternalTree();
    myFileSystemTree = new FileSystemTreeImpl(myProject, myChooserDescriptor, internalTree, null, null, null);
    internalTree.setRootVisible(myChooserDescriptor.isTreeRootVisible());
    internalTree.setShowsRootHandles(true);
    Disposer.register(myDisposable, myFileSystemTree);
    myFileSystemTree.addOkAction(this::doOKAction);
    JTree tree = myFileSystemTree.getTree();
    tree.setCellRenderer(new NodeRenderer());
    tree.getSelectionModel().addTreeSelectionListener(new FileTreeSelectionListener());
    tree.addTreeExpansionListener(new FileTreeExpansionListener());
    setOKActionEnabled(false);
    myFileSystemTree.addListener(new FileSystemTree.Listener() {

        public void selectionChanged(final List<VirtualFile> selection) {
            updatePathFromTree(selection, false);
        }
    }, myDisposable);
    new FileDrop(tree, new FileDrop.Target() {

        public FileChooserDescriptor getDescriptor() {
            return myChooserDescriptor;
        }

        public boolean isHiddenShown() {
            return myFileSystemTree.areHiddensShown();
        }

        public void dropFiles(final List<VirtualFile> files) {
            if (!myChooserDescriptor.isChooseMultiple() && files.size() > 0) {
                selectInTree(new VirtualFile[] { files.get(0) }, true, true);
            } else {
                selectInTree(VfsUtilCore.toVirtualFileArray(files), true, true);
            }
        }
    });
    return tree;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) NodeRenderer(com.intellij.ide.util.treeView.NodeRenderer) Tree(com.intellij.ui.treeStructure.Tree)

Example 3 with NodeRenderer

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

the class AbstractTreeClassChooserDialog method createCenterPanel.

@Override
protected JComponent createCenterPanel() {
    final DefaultTreeModel model = new DefaultTreeModel(new DefaultMutableTreeNode());
    myTree = new Tree(model);
    ProjectAbstractTreeStructureBase treeStructure = new AbstractProjectTreeStructure(myProject) {

        @Override
        public boolean isFlattenPackages() {
            return false;
        }

        @Override
        public boolean isShowMembers() {
            return myIsShowMembers;
        }

        @Override
        public boolean isHideEmptyMiddlePackages() {
            return true;
        }

        @Override
        public boolean isAbbreviatePackageNames() {
            return false;
        }

        @Override
        public boolean isShowLibraryContents() {
            return myIsShowLibraryContents;
        }

        @Override
        public boolean isShowModules() {
            return false;
        }
    };
    myBuilder = new ProjectTreeBuilder(myProject, myTree, model, AlphaComparator.INSTANCE, treeStructure);
    myTree.setRootVisible(false);
    myTree.setShowsRootHandles(true);
    myTree.expandRow(0);
    myTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    myTree.setCellRenderer(new NodeRenderer());
    UIUtil.setLineStyleAngled(myTree);
    JScrollPane scrollPane = ScrollPaneFactory.createScrollPane(myTree);
    scrollPane.setPreferredSize(JBUI.size(500, 300));
    myTree.addKeyListener(new KeyAdapter() {

        @Override
        public void keyPressed(KeyEvent e) {
            if (KeyEvent.VK_ENTER == e.getKeyCode()) {
                doOKAction();
            }
        }
    });
    new DoubleClickListener() {

        @Override
        protected boolean onDoubleClick(MouseEvent event) {
            TreePath path = myTree.getPathForLocation(event.getX(), event.getY());
            if (path != null && myTree.isPathSelected(path)) {
                doOKAction();
                return true;
            }
            return false;
        }
    }.installOn(myTree);
    myTree.addTreeSelectionListener(new TreeSelectionListener() {

        @Override
        public void valueChanged(TreeSelectionEvent e) {
            handleSelectionChanged();
        }
    });
    new TreeSpeedSearch(myTree);
    myTabbedPane = new TabbedPaneWrapper(getDisposable());
    final JPanel dummyPanel = new JPanel(new BorderLayout());
    String name = null;
    /*
    if (myInitialClass != null) {
      name = myInitialClass.getName();
    }
*/
    myGotoByNamePanel = new ChooseByNamePanel(myProject, createChooseByNameModel(), name, myScope.isSearchInLibraries(), getContext()) {

        @Override
        protected void showTextFieldPanel() {
        }

        @Override
        protected void close(boolean isOk) {
            super.close(isOk);
            if (isOk) {
                doOKAction();
            } else {
                doCancelAction();
            }
        }

        @NotNull
        @Override
        protected Set<Object> filter(@NotNull Set<Object> elements) {
            return doFilter(elements);
        }

        @Override
        protected void initUI(ChooseByNamePopupComponent.Callback callback, ModalityState modalityState, boolean allowMultipleSelection) {
            super.initUI(callback, modalityState, allowMultipleSelection);
            dummyPanel.add(myGotoByNamePanel.getPanel(), BorderLayout.CENTER);
            IdeFocusManager.getGlobalInstance().doWhenFocusSettlesDown(() -> {
                IdeFocusManager.getGlobalInstance().requestFocus(IdeFocusTraversalPolicy.getPreferredFocusedComponent(myGotoByNamePanel.getPanel()), true);
            });
        }

        @Override
        protected void showList() {
            super.showList();
            if (myInitialClass != null && myList.getModel().getSize() > 0) {
                myList.setSelectedValue(myInitialClass, true);
                myInitialClass = null;
            }
        }

        @Override
        protected void chosenElementMightChange() {
            handleSelectionChanged();
        }
    };
    Disposer.register(myDisposable, myGotoByNamePanel);
    myTabbedPane.addTab(IdeBundle.message("tab.chooser.search.by.name"), dummyPanel);
    myTabbedPane.addTab(IdeBundle.message("tab.chooser.project"), scrollPane);
    myGotoByNamePanel.invoke(new MyCallback(), getModalityState(), false);
    myTabbedPane.addChangeListener(new ChangeListener() {

        @Override
        public void stateChanged(ChangeEvent e) {
            handleSelectionChanged();
        }
    });
    return myTabbedPane.getComponent();
}
Also used : Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) DoubleClickListener(com.intellij.ui.DoubleClickListener) KeyAdapter(java.awt.event.KeyAdapter) TreeSpeedSearch(com.intellij.ui.TreeSpeedSearch) TreeSelectionListener(javax.swing.event.TreeSelectionListener) NotNull(org.jetbrains.annotations.NotNull) AbstractProjectTreeStructure(com.intellij.ide.projectView.impl.AbstractProjectTreeStructure) NodeRenderer(com.intellij.ide.util.treeView.NodeRenderer) KeyEvent(java.awt.event.KeyEvent) ProjectAbstractTreeStructureBase(com.intellij.ide.projectView.impl.ProjectAbstractTreeStructureBase) Tree(com.intellij.ui.treeStructure.Tree) ChangeListener(javax.swing.event.ChangeListener) MouseEvent(java.awt.event.MouseEvent) TabbedPaneWrapper(com.intellij.ui.TabbedPaneWrapper) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) TreePath(javax.swing.tree.TreePath) ChangeEvent(javax.swing.event.ChangeEvent) BaseProjectTreeBuilder(com.intellij.ide.projectView.BaseProjectTreeBuilder) ProjectTreeBuilder(com.intellij.ide.projectView.impl.ProjectTreeBuilder) ModalityState(com.intellij.openapi.application.ModalityState) TreeSelectionEvent(javax.swing.event.TreeSelectionEvent)

Example 4 with NodeRenderer

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

the class NodeHyperlinkSupport method addHyperlinkBehaviorToSupportedNodes.

private void addHyperlinkBehaviorToSupportedNodes() {
    myTree.setCellRenderer(new NodeRenderer() {

        @NotNull
        @Override
        protected SimpleTextAttributes getSimpleTextAttributes(@NotNull PresentationData presentation, Color color, @NotNull Object node) {
            List<PsIssue> issues = Collections.emptyList();
            if (myShowIssues && node instanceof AbstractPsModelNode) {
                AbstractPsModelNode<? extends PsModel> modelNode = (AbstractPsModelNode<? extends PsModel>) node;
                issues = findIssues(modelNode, IssuesByTypeAndTextComparator.INSTANCE);
                presentation.setTooltip(getTooltipText(issues, false));
            }
            if (myHoveredNode != null && myHoveredNode == node) {
                return LINK_ATTRIBUTES;
            }
            SimpleTextAttributes textAttributes = super.getSimpleTextAttributes(presentation, color, node);
            if (!issues.isEmpty()) {
                PsIssue issue = issues.get(0);
                Color waveColor = issue.getSeverity().getColor();
                textAttributes = textAttributes.derive(STYLE_WAVED, null, null, waveColor);
            }
            return textAttributes;
        }

        @Override
        public void customizeCellRenderer(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
            if (myShowIssues) {
                AbstractPsModelNode<? extends PsModel> modelNode = findModelNode(value);
                if (modelNode != null) {
                    List<PsIssue> issues = findIssues(modelNode, null);
                    if (!issues.isEmpty()) {
                        // Force color change of the node.
                        modelNode.getPresentation().clearText();
                    }
                }
            }
            super.customizeCellRenderer(tree, value, selected, expanded, leaf, row, hasFocus);
            Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
            if (userObject instanceof CellAppearanceEx) {
                ((CellAppearanceEx) userObject).customize(this);
            }
        }

        @Nullable
        private AbstractPsModelNode<? extends PsModel> findModelNode(@Nullable Object value) {
            if (value instanceof DefaultMutableTreeNode) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
                Object userObject = node.getUserObject();
                if (userObject instanceof AbstractPsModelNode) {
                    return (AbstractPsModelNode<? extends PsModel>) userObject;
                }
            }
            return null;
        }
    });
    MouseAdapter mouseListener = new MouseAdapter() {

        @Override
        public void mouseMoved(MouseEvent e) {
            Cursor cursor = getDefaultCursor();
            T node = getIfHyperlink(e);
            if (node != null) {
                cursor = getPredefinedCursor(HAND_CURSOR);
            }
            setHoveredNode(node);
            myTree.setCursor(cursor);
        }
    };
    myTree.addMouseMotionListener(mouseListener);
    // Make the cursor change to 'hand' if the mouse pointer is over a supported node and the user presses Ctrl or Cmd.
    myKeyEventDispatcher = e -> {
        T node = null;
        if (e.getID() == KEY_PRESSED) {
            Cursor cursor = getDefaultCursor();
            if (isMetaOrCtrlKeyPressed(e)) {
                node = getNodeUnderMousePointer();
                if (node != null) {
                    cursor = getPredefinedCursor(HAND_CURSOR);
                }
            }
            setHoveredNode(node);
            myTree.setCursor(cursor);
        } else if (e.getID() == KEY_RELEASED) {
            if (isMetaOrCtrlKeyPressed(e)) {
                setHoveredNode(null);
            }
            myTree.setCursor(getDefaultCursor());
        }
        return false;
    };
    KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(myKeyEventDispatcher);
}
Also used : PresentationData(com.intellij.ide.projectView.PresentationData) CellAppearanceEx(com.intellij.openapi.roots.ui.CellAppearanceEx) MouseEvent(java.awt.event.MouseEvent) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) PsModel(com.android.tools.idea.gradle.structure.model.PsModel) MouseAdapter(java.awt.event.MouseAdapter) Cursor(java.awt.Cursor) NotNull(org.jetbrains.annotations.NotNull) NodeRenderer(com.intellij.ide.util.treeView.NodeRenderer) SimpleTextAttributes(com.intellij.ui.SimpleTextAttributes) List(java.util.List) PsIssue(com.android.tools.idea.gradle.structure.model.PsIssue) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with NodeRenderer

use of com.intellij.ide.util.treeView.NodeRenderer in project azure-tools-for-java by Microsoft.

the class AzureSdkTreePanel method initTree.

private Tree initTree() {
    final DefaultMutableTreeNode root = new DefaultMutableTreeNode("Azure SDK Libraries");
    this.model = new DefaultTreeModel(root);
    final SimpleTree tree = new SimpleTree(model);
    tree.putClientProperty(RenderingUtil.ALWAYS_PAINT_SELECTION_AS_FOCUSED, true);
    tree.setCellRenderer(new NodeRenderer());
    tree.setRootVisible(false);
    tree.setShowsRootHandles(true);
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    TreeUtil.installActions(tree);
    RelativeFont.BOLD.install(tree);
    return tree;
}
Also used : NodeRenderer(com.intellij.ide.util.treeView.NodeRenderer) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) SimpleTree(com.intellij.ui.treeStructure.SimpleTree)

Aggregations

NodeRenderer (com.intellij.ide.util.treeView.NodeRenderer)8 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)5 Tree (com.intellij.ui.treeStructure.Tree)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 TreeSpeedSearch (com.intellij.ui.TreeSpeedSearch)3 KeyAdapter (java.awt.event.KeyAdapter)3 KeyEvent (java.awt.event.KeyEvent)3 MouseEvent (java.awt.event.MouseEvent)3 DefaultTreeModel (javax.swing.tree.DefaultTreeModel)3 TreePath (javax.swing.tree.TreePath)3 BaseProjectTreeBuilder (com.intellij.ide.projectView.BaseProjectTreeBuilder)2 AbstractProjectTreeStructure (com.intellij.ide.projectView.impl.AbstractProjectTreeStructure)2 ProjectAbstractTreeStructureBase (com.intellij.ide.projectView.impl.ProjectAbstractTreeStructureBase)2 ProjectTreeBuilder (com.intellij.ide.projectView.impl.ProjectTreeBuilder)2 ModalityState (com.intellij.openapi.application.ModalityState)2 DoubleClickListener (com.intellij.ui.DoubleClickListener)2 TabbedPaneWrapper (com.intellij.ui.TabbedPaneWrapper)2 List (java.util.List)2 ChangeEvent (javax.swing.event.ChangeEvent)2 ChangeListener (javax.swing.event.ChangeListener)2