use of com.intellij.codeInsight.hint.ImplementationViewComponent in project intellij-community by JetBrains.
the class ShowImplementationsAction method showImplementations.
protected void showImplementations(@NotNull PsiElement[] impls, @NotNull final Project project, final String text, final Editor editor, final PsiFile file, final PsiElement element, boolean invokedFromEditor, boolean invokedByShortcut) {
if (impls.length == 0)
return;
FeatureUsageTracker.getInstance().triggerFeatureUsed(CODEASSISTS_QUICKDEFINITION_FEATURE);
if (LookupManager.getInstance(project).getActiveLookup() != null) {
FeatureUsageTracker.getInstance().triggerFeatureUsed(CODEASSISTS_QUICKDEFINITION_LOOKUP_FEATURE);
}
int index = 0;
if (invokedFromEditor && file != null && impls.length > 1) {
final VirtualFile virtualFile = file.getVirtualFile();
final PsiFile containingFile = impls[0].getContainingFile();
if (virtualFile != null && containingFile != null && virtualFile.equals(containingFile.getVirtualFile())) {
final PsiFile secondContainingFile = impls[1].getContainingFile();
if (secondContainingFile != containingFile) {
index = 1;
}
}
}
final Ref<UsageView> usageView = new Ref<>();
final String title = CodeInsightBundle.message("implementation.view.title", text);
JBPopup popup = SoftReference.dereference(myPopupRef);
if (popup != null && popup.isVisible() && popup instanceof AbstractPopup) {
final ImplementationViewComponent component = (ImplementationViewComponent) ((AbstractPopup) popup).getComponent();
((AbstractPopup) popup).setCaption(title);
component.update(impls, index);
updateInBackground(editor, element, component, title, (AbstractPopup) popup, usageView);
if (invokedByShortcut) {
((AbstractPopup) popup).focusPreferredComponent();
}
return;
}
final ImplementationViewComponent component = new ImplementationViewComponent(impls, index);
if (component.hasElementsToShow()) {
final PopupUpdateProcessor updateProcessor = new PopupUpdateProcessor(project) {
@Override
public void updatePopup(Object lookupItemObject) {
final PsiElement element = lookupItemObject instanceof PsiElement ? (PsiElement) lookupItemObject : DocumentationManager.getInstance(project).getElementFromLookup(editor, file);
updateElementImplementations(element, editor, project, file);
}
};
popup = JBPopupFactory.getInstance().createComponentPopupBuilder(component, component.getPreferredFocusableComponent()).setProject(project).addListener(updateProcessor).addUserData(updateProcessor).setDimensionServiceKey(project, DocumentationManager.JAVADOC_LOCATION_AND_SIZE, false).setResizable(true).setMovable(true).setRequestFocus(invokedFromEditor && LookupManager.getActiveLookup(editor) == null).setTitle(title).setCouldPin(popup1 -> {
usageView.set(component.showInUsageView());
popup1.cancel();
myTaskRef = null;
return false;
}).setCancelCallback(() -> {
ImplementationsUpdaterTask task = SoftReference.dereference(myTaskRef);
if (task != null) {
task.cancelTask();
}
return Boolean.TRUE;
}).createPopup();
updateInBackground(editor, element, component, title, (AbstractPopup) popup, usageView);
PopupPositionManager.positionPopupInBestPosition(popup, editor, DataManager.getInstance().getDataContext());
component.setHint(popup, title);
myPopupRef = new WeakReference<>(popup);
}
}
use of com.intellij.codeInsight.hint.ImplementationViewComponent in project intellij-community by JetBrains.
the class ImageDuplicateResultsDialog method createCenterPanel.
@Override
protected JComponent createCenterPanel() {
final JPanel panel = new JPanel(new BorderLayout());
DataManager.registerDataProvider(panel, new DataProvider() {
@Override
public Object getData(@NonNls String dataId) {
final TreePath path = myTree.getSelectionPath();
if (path != null) {
Object component = path.getLastPathComponent();
VirtualFile file = null;
if (component instanceof MyFileNode) {
component = ((MyFileNode) component).getParent();
}
if (component instanceof MyDuplicatesNode) {
file = ((MyDuplicatesNode) component).getUserObject().iterator().next();
}
if (CommonDataKeys.VIRTUAL_FILE.is(dataId)) {
return file;
}
if (CommonDataKeys.VIRTUAL_FILE_ARRAY.is(dataId) && file != null) {
return new VirtualFile[] { file };
}
}
return null;
}
});
final JBList list = new JBList(new ResourceModules().getModuleNames());
final NotNullFunction<Object, JComponent> modulesRenderer = dom -> new JLabel(dom instanceof Module ? ((Module) dom).getName() : dom.toString(), PlatformIcons.SOURCE_FOLDERS_ICON, SwingConstants.LEFT);
list.installCellRenderer(modulesRenderer);
final JPanel modulesPanel = ToolbarDecorator.createDecorator(list).setAddAction(new AnActionButtonRunnable() {
@Override
public void run(AnActionButton button) {
final Module[] all = ModuleManager.getInstance(myProject).getModules();
Arrays.sort(all, (o1, o2) -> o1.getName().compareTo(o2.getName()));
final JBList modules = new JBList(all);
modules.installCellRenderer(modulesRenderer);
JBPopupFactory.getInstance().createListPopupBuilder(modules).setTitle("Add Resource Module").setFilteringEnabled(o -> ((Module) o).getName()).setItemChoosenCallback(() -> {
final Object value = modules.getSelectedValue();
if (value instanceof Module && !myResourceModules.contains((Module) value)) {
myResourceModules.add((Module) value);
((DefaultListModel) list.getModel()).addElement(((Module) value).getName());
}
((DefaultTreeModel) myTree.getModel()).reload();
TreeUtil.expandAll(myTree);
}).createPopup().show(button.getPreferredPopupPoint());
}
}).setRemoveAction(new AnActionButtonRunnable() {
@Override
public void run(AnActionButton button) {
final Object[] values = list.getSelectedValues();
for (Object value : values) {
myResourceModules.remove((String) value);
((DefaultListModel) list.getModel()).removeElement(value);
}
((DefaultTreeModel) myTree.getModel()).reload();
TreeUtil.expandAll(myTree);
}
}).disableDownAction().disableUpAction().createPanel();
modulesPanel.setPreferredSize(new Dimension(-1, 60));
final JPanel top = new JPanel(new BorderLayout());
top.add(new JLabel("Image modules:"), BorderLayout.NORTH);
top.add(modulesPanel, BorderLayout.CENTER);
panel.add(top, BorderLayout.NORTH);
panel.add(new JBScrollPane(myTree), BorderLayout.CENTER);
new AnAction() {
@Override
public void actionPerformed(AnActionEvent e) {
VirtualFile file = getFileFromSelection();
if (file != null) {
final PsiFile psiFile = PsiManager.getInstance(myProject).findFile(file);
if (psiFile != null) {
final ImplementationViewComponent viewComponent = new ImplementationViewComponent(new PsiElement[] { psiFile }, 0);
final TreeSelectionListener listener = new TreeSelectionListener() {
@Override
public void valueChanged(TreeSelectionEvent e) {
final VirtualFile selection = getFileFromSelection();
if (selection != null) {
final PsiFile newElement = PsiManager.getInstance(myProject).findFile(selection);
if (newElement != null) {
viewComponent.update(new PsiElement[] { newElement }, 0);
}
}
}
};
myTree.addTreeSelectionListener(listener);
final JBPopup popup = JBPopupFactory.getInstance().createComponentPopupBuilder(viewComponent, viewComponent.getPreferredFocusableComponent()).setProject(myProject).setDimensionServiceKey(myProject, ImageDuplicateResultsDialog.class.getName(), false).setResizable(true).setMovable(true).setRequestFocus(false).setCancelCallback(() -> {
myTree.removeTreeSelectionListener(listener);
return true;
}).setTitle("Image Preview").createPopup();
final Window window = ImageDuplicateResultsDialog.this.getWindow();
popup.show(new RelativePoint(window, new Point(window.getWidth(), 0)));
viewComponent.setHint(popup, "Image Preview");
}
}
}
}.registerCustomShortcutSet(CustomShortcutSet.fromString("ENTER"), panel);
int total = myDuplicates.values().stream().mapToInt(Set::size).sum() - myDuplicates.size();
final JLabel label = new JLabel("<html>Press <b>Enter</b> to preview image<br>Total images found: " + myImages.size() + ". Total duplicates found: " + total + "</html>");
panel.add(label, BorderLayout.SOUTH);
return panel;
}
use of com.intellij.codeInsight.hint.ImplementationViewComponent in project intellij-community by JetBrains.
the class ImplementationsViewTest method testFunctionalInterface.
public void testFunctionalInterface() {
myFixture.configureByText("a.java", "interface AF<caret>oo{\n" + " boolean aaa();\n" + "}\n" + "class AFooImpl {\n" + " {\n" + " AFoo a = () -> {return false;};\n" + " }\n" + "}");
PsiClass psiClass = (PsiClass) TargetElementUtil.findTargetElement(myFixture.getEditor(), TargetElementUtil.getInstance().getAllAccepted());
assert psiClass != null;
final Collection<PsiElement> classes = getClassImplementations(psiClass);
List<PsiElement> all = new ArrayList<>();
all.add(psiClass);
all.addAll(classes);
final ImplementationViewComponent component = new ImplementationViewComponent(all.toArray(new PsiElement[all.size()]), 0);
assertContent(component, new String[] { "a.java (AFoo)", "a.java" });
}
use of com.intellij.codeInsight.hint.ImplementationViewComponent in project intellij-community by JetBrains.
the class ImplementationsViewTest method testInnerClasses.
public void testInnerClasses() {
myFixture.configureByText("a.java", "abstract class AF<caret>oo{\n" + " abstract boolean aaa();\n" + " static class AFoo1 extends AFoo {\n" + " @Override\n" + " boolean aaa() {\n" + " return false;\n" + " }\n" + " }\n" + " static class AFoo3 extends AFoo {\n" + " @Override\n" + " boolean aaa() {\n" + " return false;\n" + " }\n" + " }\n" + " static class AFoo2 extends AFoo {\n" + " @Override\n" + " boolean aaa() {\n" + " return false;\n" + " }\n" + " }\n" + " \n" + "}");
PsiClass psiClass = (PsiClass) TargetElementUtil.findTargetElement(myFixture.getEditor(), TargetElementUtil.getInstance().getAllAccepted());
assert psiClass != null;
final Collection<PsiElement> classes = getClassImplementations(psiClass);
List<PsiElement> all = new ArrayList<>();
all.add(psiClass);
all.addAll(classes);
final ImplementationViewComponent component = new ImplementationViewComponent(all.toArray(new PsiElement[all.size()]), 0);
try {
final String[] visibleFiles = component.getVisibleFiles();
assertTrue(visibleFiles.length > 0);
assertEquals(visibleFiles[0], "a.java (AFoo)");
Arrays.sort(visibleFiles);
Assert.assertArrayEquals(Arrays.toString(visibleFiles), new String[] { "a.java (AFoo)", "a.java (AFoo1 in AFoo)", "a.java (AFoo2 in AFoo)", "a.java (AFoo3 in AFoo)" }, visibleFiles);
} finally {
component.removeNotify();
}
}
use of com.intellij.codeInsight.hint.ImplementationViewComponent in project intellij-community by JetBrains.
the class ImplementationsViewTest method testInterfaceMethodOfFunctionalInterface.
public void testInterfaceMethodOfFunctionalInterface() {
myFixture.configureByText("a.java", "interface AFoo{\n" + " boolean a<caret>aa();\n" + "}\n" + "class AFooImpl {\n" + " {\n" + " AFoo a = () -> {return false;};\n" + " }\n" + "}");
PsiMethod psiMethod = (PsiMethod) TargetElementUtil.findTargetElement(myFixture.getEditor(), TargetElementUtil.getInstance().getAllAccepted());
assert psiMethod != null;
final Collection<PsiElement> methods = getMethodImplementations(psiMethod);
List<PsiElement> all = new ArrayList<>();
all.add(psiMethod);
all.addAll(methods);
final ImplementationViewComponent component = new ImplementationViewComponent(all.toArray(new PsiElement[all.size()]), 0);
assertContent(component, new String[] { "a.java (AFoo)", "a.java" });
}
Aggregations