Search in sources :

Example 1 with GotoFileCellRenderer

use of com.intellij.ide.util.gotoByName.GotoFileCellRenderer in project android by JetBrains.

the class EditMultipleSourcesAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    Project project = e.getData(CommonDataKeys.PROJECT);
    assert project != null;
    final Navigatable[] files = e.getData(CommonDataKeys.NAVIGATABLE_ARRAY);
    assert files != null && files.length > 0;
    if (files.length > 1) {
        DefaultListModel listModel = new DefaultListModel();
        for (int i = 0; i < files.length; ++i) {
            assert files[i] instanceof PsiClassNavigation;
            //noinspection unchecked
            listModel.add(i, ((PsiClassNavigation) files[i]).getPsiFile());
        }
        final JBList list = new JBList(listModel);
        int width = WindowManager.getInstance().getFrame(project).getSize().width;
        list.setCellRenderer(new GotoFileCellRenderer(width));
        JBPopup popup = JBPopupFactory.getInstance().createListPopupBuilder(list).setTitle("Choose Target File").setItemChoosenCallback(new Runnable() {

            @Override
            public void run() {
                Object selectedValue = list.getSelectedValue();
                PsiClassNavigation navigationWrapper = null;
                for (Navigatable file : files) {
                    if (selectedValue == ((PsiClassNavigation) file).getPsiFile()) {
                        navigationWrapper = (PsiClassNavigation) file;
                        break;
                    }
                }
                assert navigationWrapper != null;
                if (navigationWrapper.canNavigate()) {
                    navigationWrapper.navigate(true);
                }
            }
        }).createPopup();
        if (e.getInputEvent().getSource() instanceof ActionButton) {
            popup.showUnderneathOf((ActionButton) e.getInputEvent().getSource());
        } else {
            popup.showInBestPositionFor(e.getDataContext());
        }
    } else {
        assert files[0] instanceof PsiClassNavigation;
        PsiClassNavigation file = (PsiClassNavigation) files[0];
        if (file.canNavigate()) {
            file.navigate(true);
        }
    }
}
Also used : Project(com.intellij.openapi.project.Project) GotoFileCellRenderer(com.intellij.ide.util.gotoByName.GotoFileCellRenderer) ActionButton(com.intellij.openapi.actionSystem.impl.ActionButton) JBList(com.intellij.ui.components.JBList) JBPopup(com.intellij.openapi.ui.popup.JBPopup) Navigatable(com.intellij.pom.Navigatable)

Example 2 with GotoFileCellRenderer

use of com.intellij.ide.util.gotoByName.GotoFileCellRenderer in project intellij-community by JetBrains.

the class MultipleFilesHyperlinkInfo method navigate.

@Override
public void navigate(@NotNull final Project project, @Nullable RelativePoint hyperlinkLocationPoint) {
    List<PsiFile> currentFiles = new ArrayList<>();
    AccessToken accessToken = ReadAction.start();
    try {
        for (VirtualFile file : myVirtualFiles) {
            if (!file.isValid())
                continue;
            PsiFile psiFile = PsiManager.getInstance(project).findFile(file);
            if (psiFile != null) {
                // Sources may be downloaded.
                PsiElement navigationElement = psiFile.getNavigationElement();
                if (navigationElement instanceof PsiFile) {
                    currentFiles.add((PsiFile) navigationElement);
                    continue;
                }
                currentFiles.add(psiFile);
            }
        }
    } finally {
        accessToken.finish();
    }
    if (currentFiles.isEmpty())
        return;
    if (currentFiles.size() == 1) {
        new OpenFileHyperlinkInfo(myProject, currentFiles.get(0).getVirtualFile(), myLineNumber).navigate(project);
    } else {
        final JBList list = new JBList(currentFiles);
        int width = WindowManager.getInstance().getFrame(project).getSize().width;
        list.setCellRenderer(new GotoFileCellRenderer(width));
        JBPopup popup = JBPopupFactory.getInstance().createListPopupBuilder(list).setTitle("Choose Target File").setItemChoosenCallback(() -> {
            VirtualFile file = ((PsiFile) list.getSelectedValue()).getVirtualFile();
            new OpenFileHyperlinkInfo(myProject, file, myLineNumber).navigate(project);
        }).createPopup();
        if (hyperlinkLocationPoint != null) {
            popup.show(hyperlinkLocationPoint);
        } else {
            popup.showInFocusCenter();
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) OpenFileHyperlinkInfo(com.intellij.execution.filters.OpenFileHyperlinkInfo) GotoFileCellRenderer(com.intellij.ide.util.gotoByName.GotoFileCellRenderer) AccessToken(com.intellij.openapi.application.AccessToken) ArrayList(java.util.ArrayList) PsiFile(com.intellij.psi.PsiFile) JBList(com.intellij.ui.components.JBList) JBPopup(com.intellij.openapi.ui.popup.JBPopup) PsiElement(com.intellij.psi.PsiElement) RelativePoint(com.intellij.ui.awt.RelativePoint)

Aggregations

GotoFileCellRenderer (com.intellij.ide.util.gotoByName.GotoFileCellRenderer)2 JBPopup (com.intellij.openapi.ui.popup.JBPopup)2 JBList (com.intellij.ui.components.JBList)2 OpenFileHyperlinkInfo (com.intellij.execution.filters.OpenFileHyperlinkInfo)1 ActionButton (com.intellij.openapi.actionSystem.impl.ActionButton)1 AccessToken (com.intellij.openapi.application.AccessToken)1 Project (com.intellij.openapi.project.Project)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 Navigatable (com.intellij.pom.Navigatable)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 RelativePoint (com.intellij.ui.awt.RelativePoint)1 ArrayList (java.util.ArrayList)1