Search in sources :

Example 1 with Convertor

use of com.intellij.util.containers.Convertor 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 Convertor

use of com.intellij.util.containers.Convertor 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 3 with Convertor

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

the class LayoutTree method configureUiHelper.

@Override
protected void configureUiHelper(TreeUIHelper helper) {
    final Convertor<TreePath, String> convertor = new Convertor<TreePath, String>() {

        @Override
        public String convert(final TreePath path) {
            final SimpleNode node = getNodeFor(path);
            if (node instanceof PackagingElementNode) {
                return ((PackagingElementNode<?>) node).getElementPresentation().getSearchName();
            }
            return "";
        }
    };
    new TreeSpeedSearch(this, convertor, true);
}
Also used : PackagingElementNode(com.intellij.openapi.roots.ui.configuration.artifacts.nodes.PackagingElementNode) TreePath(javax.swing.tree.TreePath) TreeSpeedSearch(com.intellij.ui.TreeSpeedSearch) Convertor(com.intellij.util.containers.Convertor) SimpleNode(com.intellij.ui.treeStructure.SimpleNode)

Example 4 with Convertor

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

the class NewMappings method removeRedundantMappings.

// todo area for optimization
private void removeRedundantMappings() {
    final LocalFileSystem lfs = LocalFileSystem.getInstance();
    final AllVcsesI allVcses = AllVcses.getInstance(myProject);
    for (Iterator<String> iterator = myVcsToPaths.keySet().iterator(); iterator.hasNext(); ) {
        final String vcsName = iterator.next();
        final Collection<VcsDirectoryMapping> mappings = myVcsToPaths.get(vcsName);
        final List<Pair<VirtualFile, VcsDirectoryMapping>> objects = ObjectsConvertor.convert(mappings, new Convertor<VcsDirectoryMapping, Pair<VirtualFile, VcsDirectoryMapping>>() {

            public Pair<VirtualFile, VcsDirectoryMapping> convert(final VcsDirectoryMapping dm) {
                VirtualFile vf = lfs.findFileByPath(dm.getDirectory());
                if (vf == null) {
                    vf = lfs.refreshAndFindFileByPath(dm.getDirectory());
                }
                return vf == null ? null : Pair.create(vf, dm);
            }
        }, ObjectsConvertor.NOT_NULL);
        final List<Pair<VirtualFile, VcsDirectoryMapping>> filteredFiles;
        // todo static
        final Convertor<Pair<VirtualFile, VcsDirectoryMapping>, VirtualFile> fileConvertor = new Convertor<Pair<VirtualFile, VcsDirectoryMapping>, VirtualFile>() {

            public VirtualFile convert(Pair<VirtualFile, VcsDirectoryMapping> o) {
                return o.getFirst();
            }
        };
        if (StringUtil.isEmptyOrSpaces(vcsName)) {
            filteredFiles = AbstractVcs.filterUniqueRootsDefault(objects, fileConvertor);
        } else {
            final AbstractVcs<?> vcs = allVcses.getByName(vcsName);
            if (vcs == null) {
                VcsBalloonProblemNotifier.showOverChangesView(myProject, "VCS plugin not found for mapping to : '" + vcsName + "'", MessageType.ERROR);
                continue;
            }
            filteredFiles = vcs.filterUniqueRoots(objects, fileConvertor);
        }
        final List<VcsDirectoryMapping> filteredMappings = ObjectsConvertor.convert(filteredFiles, new Convertor<Pair<VirtualFile, VcsDirectoryMapping>, VcsDirectoryMapping>() {

            public VcsDirectoryMapping convert(final Pair<VirtualFile, VcsDirectoryMapping> o) {
                return o.getSecond();
            }
        });
        // to calculate what had been removed
        mappings.removeAll(filteredMappings);
        if (filteredMappings.isEmpty()) {
            iterator.remove();
        } else {
            mappings.clear();
            mappings.addAll(filteredMappings);
        }
    }
    sortedMappingsByMap();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Convertor(com.intellij.util.containers.Convertor) LocalFileSystem(com.intellij.openapi.vfs.LocalFileSystem) Pair(com.intellij.openapi.util.Pair)

Example 5 with Convertor

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

the class GitOutgoingChangesProvider method getOutgoingChanges.

public Pair<VcsRevisionNumber, List<CommittedChangeList>> getOutgoingChanges(final VirtualFile vcsRoot, final boolean findRemote) throws VcsException {
    LOG.debug("getOutgoingChanges root: " + vcsRoot.getPath());
    final GitBranchesSearcher searcher = new GitBranchesSearcher(myProject, vcsRoot, findRemote);
    if (searcher.getLocal() == null || searcher.getRemote() == null) {
        return new Pair<>(null, Collections.<CommittedChangeList>emptyList());
    }
    final GitRevisionNumber base = getMergeBase(myProject, vcsRoot, searcher.getLocal(), searcher.getRemote());
    if (base == null) {
        return new Pair<>(null, Collections.<CommittedChangeList>emptyList());
    }
    final List<GitCommittedChangeList> lists = GitUtil.getLocalCommittedChanges(myProject, vcsRoot, new Consumer<GitSimpleHandler>() {

        public void consume(final GitSimpleHandler handler) {
            handler.addParameters(base.asString() + "..HEAD");
        }
    });
    return new Pair<>(base, ObjectsConvertor.convert(lists, new Convertor<GitCommittedChangeList, CommittedChangeList>() {

        @Override
        public CommittedChangeList convert(GitCommittedChangeList o) {
            return o;
        }
    }));
}
Also used : GitRevisionNumber(git4idea.GitRevisionNumber) GitSimpleHandler(git4idea.commands.GitSimpleHandler) GitBranchesSearcher(git4idea.GitBranchesSearcher) Pair(com.intellij.openapi.util.Pair) Convertor(com.intellij.util.containers.Convertor)

Aggregations

Convertor (com.intellij.util.containers.Convertor)8 Pair (com.intellij.openapi.util.Pair)3 TreeSpeedSearch (com.intellij.ui.TreeSpeedSearch)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 Tree (com.intellij.ui.treeStructure.Tree)2 TreePath (javax.swing.tree.TreePath)2 ColumnTreeBuilder (com.android.tools.adtui.common.ColumnTreeBuilder)1 ApkEntry (com.android.tools.idea.apk.viewer.ApkEntry)1 NameRenderer (com.android.tools.idea.apk.viewer.ApkViewPanel.NameRenderer)1 SizeRenderer (com.android.tools.idea.apk.viewer.ApkViewPanel.SizeRenderer)1 AbstractTreeNode (com.intellij.ide.util.treeView.AbstractTreeNode)1 NodeRenderer (com.intellij.ide.util.treeView.NodeRenderer)1 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1 ServiceManager (com.intellij.openapi.components.ServiceManager)1 Logger (com.intellij.openapi.diagnostic.Logger)1 ChangeCounter (com.intellij.openapi.diff.impl.incrementalMerge.ChangeCounter)1 MergeRequestImpl (com.intellij.openapi.diff.impl.mergeTool.MergeRequestImpl)1 DiffDividerPaint (com.intellij.openapi.diff.impl.splitter.DiffDividerPaint)1 EmptyProgressIndicator (com.intellij.openapi.progress.EmptyProgressIndicator)1 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)1