From fceeab6b6d64434423cacb8f3f10997cce3d7a53 Mon Sep 17 00:00:00 2001 From: nik Date: Thu, 31 Mar 2011 15:02:24 +0400 Subject: [PATCH] reused standard PsiElement renderer in 'Goto related' popup --- .../intellij/navigation/GotoRelatedItem.java | 44 ++++++-- .../navigation/PsiGotoRelatedItem.java | 80 -------------- .../ide/actions/GotoRelatedFileAction.java | 102 ++++++++---------- .../RelatedItemLineMarkerGotoAdapter.java | 16 ++- .../binding/FormRelatedFilesProvider.java | 12 +-- .../navigation/DomGotoRelatedItem.java | 19 +--- .../navigation/HtmlGotoRelatedProvider.java | 2 +- 7 files changed, 89 insertions(+), 186 deletions(-) delete mode 100644 platform/lang-api/src/com/intellij/navigation/PsiGotoRelatedItem.java diff --git a/platform/lang-api/src/com/intellij/navigation/GotoRelatedItem.java b/platform/lang-api/src/com/intellij/navigation/GotoRelatedItem.java index be570cd04373..5849ef23b8d1 100644 --- a/platform/lang-api/src/com/intellij/navigation/GotoRelatedItem.java +++ b/platform/lang-api/src/com/intellij/navigation/GotoRelatedItem.java @@ -15,25 +15,51 @@ */ package com.intellij.navigation; -import com.intellij.psi.PsiFile; +import com.intellij.psi.NavigatablePsiElement; +import com.intellij.psi.PsiElement; +import com.intellij.util.PsiNavigateUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import javax.swing.*; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; /** * @author Dmitry Avdeev */ -public abstract class GotoRelatedItem { +public class GotoRelatedItem { + private final PsiElement myElement; - public abstract void navigate(); + public GotoRelatedItem(@NotNull PsiElement element) { + myElement = element; + } + + public void navigate() { + PsiNavigateUtil.navigate(myElement); + } + + @Nullable + public String getCustomName() { + return null; + } + + @Nullable + public Icon getCustomIcon() { + return null; + } @NotNull - public abstract String getText(); + public PsiElement getElement() { + return myElement; + } - @Nullable - public abstract Icon getIcon(); - - @Nullable - public abstract PsiFile getContainingFile(); + public static List createItems(@NotNull Collection elements) { + List items = new ArrayList(elements.size()); + for (NavigatablePsiElement element : elements) { + items.add(new GotoRelatedItem(element)); + } + return items; + } } diff --git a/platform/lang-api/src/com/intellij/navigation/PsiGotoRelatedItem.java b/platform/lang-api/src/com/intellij/navigation/PsiGotoRelatedItem.java deleted file mode 100644 index 63bb1a27ff16..000000000000 --- a/platform/lang-api/src/com/intellij/navigation/PsiGotoRelatedItem.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright 2000-2011 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.intellij.navigation; - -import com.intellij.psi.NavigatablePsiElement; -import com.intellij.psi.PsiFile; -import org.jetbrains.annotations.NotNull; - -import javax.swing.*; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -/** - * @author Dmitry Avdeev - */ -public class PsiGotoRelatedItem extends GotoRelatedItem { - private final NavigatablePsiElement myElement; - private boolean myShowIcon; - - public static List createItems(@NotNull Collection elements) { - return createItems(elements, true); - } - - public static List createItems(@NotNull Collection elements, - final boolean showIcon) { - List items = new ArrayList(elements.size()); - for (NavigatablePsiElement element : elements) { - items.add(new PsiGotoRelatedItem(element, showIcon)); - } - return items; - } - - public PsiGotoRelatedItem(@NotNull NavigatablePsiElement element) { - this(element, true); - } - - public PsiGotoRelatedItem(@NotNull NavigatablePsiElement element, final boolean showIcon) { - myElement = element; - myShowIcon = showIcon; - } - - @Override - public void navigate() { - myElement.navigate(true); - } - - @NotNull - @Override - public String getText() { - return myElement.getName(); - } - - @Override - public Icon getIcon() { - return myShowIcon ? myElement.getIcon(0) : null; - } - - @Override - public PsiFile getContainingFile() { - return myElement instanceof PsiFile ? null : myElement.getContainingFile(); - } - - public NavigatablePsiElement getElement() { - return myElement; - } -} diff --git a/platform/lang-impl/src/com/intellij/ide/actions/GotoRelatedFileAction.java b/platform/lang-impl/src/com/intellij/ide/actions/GotoRelatedFileAction.java index 043263538009..5310f3942359 100644 --- a/platform/lang-impl/src/com/intellij/ide/actions/GotoRelatedFileAction.java +++ b/platform/lang-impl/src/com/intellij/ide/actions/GotoRelatedFileAction.java @@ -15,26 +15,25 @@ */ package com.intellij.ide.actions; +import com.intellij.codeInsight.navigation.NavigationUtil; +import com.intellij.ide.util.DefaultPsiElementCellRenderer; import com.intellij.navigation.GotoRelatedItem; import com.intellij.navigation.GotoRelatedProvider; import com.intellij.openapi.actionSystem.*; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.extensions.Extensions; import com.intellij.openapi.ui.popup.JBPopup; -import com.intellij.openapi.ui.popup.JBPopupFactory; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; -import com.intellij.ui.CollectionListModel; -import com.intellij.ui.components.JBList; -import com.intellij.util.Function; -import com.intellij.util.ui.UIUtil; +import com.intellij.psi.search.PsiElementProcessor; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import javax.swing.*; -import java.awt.*; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * @author Dmitry Avdeev @@ -59,29 +58,46 @@ public class GotoRelatedFileAction extends AnAction { createPopup(items, "Goto Related").showInBestPositionFor(context); } - public static JBPopup createPopup(List items, final String title) { - final JBList list = new JBList(new CollectionListModel(items)); - list.setCellRenderer(new ItemCellRenderer()); + public static JBPopup createPopup(final List items, final String title) { + PsiElement[] elements = new PsiElement[items.size()]; + //todo[nik] move presentation logic to GotoRelatedItem class + final Map itemsMap = new HashMap(); + for (int i = 0; i < items.size(); i++) { + GotoRelatedItem item = items.get(i); + elements[i] = item.getElement(); + itemsMap.put(item.getElement(), item); + } - Function toString = new Function() { - @Override - public String fun(Object o) { - return ((GotoRelatedItem)o).getText(); - } - }; - - return JBPopupFactory.getInstance() - .createListPopupBuilder(list) - .setTitle(title) - .setFilteringEnabled(toString) - .setItemChoosenCallback(new Runnable() { + return NavigationUtil.getPsiElementPopup(elements, new DefaultPsiElementCellRenderer() { @Override - public void run() { - Object value = list.getSelectedValue(); - ((GotoRelatedItem)value).navigate(); + public String getElementText(PsiElement element) { + String customName = itemsMap.get(element).getCustomName(); + return customName != null ? customName : super.getElementText(element); } - }) - .createPopup(); + + @Override + protected Icon getIcon(PsiElement element) { + Icon customIcon = itemsMap.get(element).getCustomIcon(); + return customIcon != null ? customIcon : super.getIcon(element); + } + + @Override + public String getContainerText(PsiElement element, String name) { + PsiFile file = element.getContainingFile(); + return file != null && !file.equals(element) ? "(" + file.getName() + ")" : null; + } + + @Override + protected DefaultListCellRenderer getRightCellRenderer() { + return null; + } + }, title, new PsiElementProcessor() { + @Override + public boolean execute(PsiElement element) { + itemsMap.get(element).navigate(); + return true; + } + }); } @NotNull @@ -106,38 +122,4 @@ public class GotoRelatedFileAction extends AnAction { public void update(AnActionEvent e) { e.getPresentation().setEnabled(LangDataKeys.PSI_FILE.getData(e.getDataContext()) != null); } - - private static class ItemCellRenderer extends JPanel implements ListCellRenderer { - private final JLabel myLeft = new JLabel(); - private final JLabel myRight = new JLabel(); - - private ItemCellRenderer() { - super(new BorderLayout()); - setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2)); - add(myLeft, BorderLayout.WEST); - add(myRight, BorderLayout.EAST); - - JPanel spacer = new JPanel(); - spacer.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 2)); - spacer.setOpaque(false); - add(spacer, BorderLayout.CENTER); - } - - @Override - public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { - GotoRelatedItem item = (GotoRelatedItem)value; - myLeft.setText(item.getText()); - myLeft.setIcon(item.getIcon()); - - PsiFile file = item.getContainingFile(); - myRight.setText(file == null ? null : file.getName()); - myRight.setIcon(file == null ? null : file.getIcon(0)); - - setBackground(UIUtil.getListBackground(isSelected)); - Color foreground = UIUtil.getListForeground(isSelected); - myLeft.setForeground(foreground); - myRight.setForeground(foreground); - return this; - } - } } diff --git a/platform/lang-impl/src/com/intellij/ide/actions/RelatedItemLineMarkerGotoAdapter.java b/platform/lang-impl/src/com/intellij/ide/actions/RelatedItemLineMarkerGotoAdapter.java index 9fa41317d881..a4cefef940c6 100644 --- a/platform/lang-impl/src/com/intellij/ide/actions/RelatedItemLineMarkerGotoAdapter.java +++ b/platform/lang-impl/src/com/intellij/ide/actions/RelatedItemLineMarkerGotoAdapter.java @@ -21,8 +21,6 @@ import com.intellij.codeInsight.daemon.RelatedItemLineMarkerProvider; import com.intellij.codeInsight.daemon.impl.LineMarkersPass; import com.intellij.navigation.GotoRelatedItem; import com.intellij.navigation.GotoRelatedProvider; -import com.intellij.navigation.PsiGotoRelatedItem; -import com.intellij.psi.NavigatablePsiElement; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; import com.intellij.util.containers.ContainerUtil; @@ -69,16 +67,14 @@ public class RelatedItemLineMarkerGotoAdapter extends GotoRelatedProvider { for (RelatedItemLineMarkerInfo marker : markers) { Collection items = marker.createGotoRelatedItems(); for (GotoRelatedItem item : items) { - if (item instanceof PsiGotoRelatedItem) { - NavigatablePsiElement element = ((PsiGotoRelatedItem)item).getElement(); - if (element instanceof PsiFile) { - PsiFile file = (PsiFile)element; - if (addedFiles.contains(file)) { - continue; - } + PsiElement element = item.getElement(); + if (element instanceof PsiFile) { + PsiFile file = (PsiFile)element; + if (addedFiles.contains(file)) { + continue; } - ContainerUtil.addIfNotNull(element.getContainingFile(), addedFiles); } + ContainerUtil.addIfNotNull(element.getContainingFile(), addedFiles); result.add(item); } } diff --git a/plugins/ui-designer/src/com/intellij/uiDesigner/binding/FormRelatedFilesProvider.java b/plugins/ui-designer/src/com/intellij/uiDesigner/binding/FormRelatedFilesProvider.java index 20bfaaee1428..575fde0f2e82 100644 --- a/plugins/ui-designer/src/com/intellij/uiDesigner/binding/FormRelatedFilesProvider.java +++ b/plugins/ui-designer/src/com/intellij/uiDesigner/binding/FormRelatedFilesProvider.java @@ -17,7 +17,6 @@ package com.intellij.uiDesigner.binding; import com.intellij.navigation.GotoRelatedItem; import com.intellij.navigation.GotoRelatedProvider; -import com.intellij.navigation.PsiGotoRelatedItem; import com.intellij.openapi.project.Project; import com.intellij.psi.JavaPsiFacade; import com.intellij.psi.PsiClass; @@ -27,8 +26,6 @@ import com.intellij.psi.search.GlobalSearchScope; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.uiDesigner.GuiFormFileType; import com.intellij.uiDesigner.compiler.Utils; -import com.intellij.util.Function; -import com.intellij.util.containers.ContainerUtil; import org.jetbrains.annotations.NotNull; import java.util.Collections; @@ -45,12 +42,7 @@ public class FormRelatedFilesProvider extends GotoRelatedProvider { PsiClass psiClass = PsiTreeUtil.getParentOfType(context, PsiClass.class, false); if (psiClass != null) { List forms = FormClassIndex.findFormsBoundToClass(psiClass); - return ContainerUtil.map(forms, new Function() { - @Override - public GotoRelatedItem fun(PsiFile psiFile) { - return new PsiGotoRelatedItem(psiFile); - } - }); + return GotoRelatedItem.createItems(forms); } else { PsiFile file = context.getContainingFile(); @@ -61,7 +53,7 @@ public class FormRelatedFilesProvider extends GotoRelatedProvider { Project project = file.getProject(); PsiClass aClass = JavaPsiFacade.getInstance(project).findClass(className, GlobalSearchScope.allScope(project)); if (aClass != null) { - return Collections.singletonList(new PsiGotoRelatedItem(aClass)); + return Collections.singletonList(new GotoRelatedItem(aClass)); } } } diff --git a/xml/dom-openapi/src/com/intellij/codeInsight/navigation/DomGotoRelatedItem.java b/xml/dom-openapi/src/com/intellij/codeInsight/navigation/DomGotoRelatedItem.java index 94233cdd5f27..3a96803ce32b 100644 --- a/xml/dom-openapi/src/com/intellij/codeInsight/navigation/DomGotoRelatedItem.java +++ b/xml/dom-openapi/src/com/intellij/codeInsight/navigation/DomGotoRelatedItem.java @@ -16,8 +16,6 @@ package com.intellij.codeInsight.navigation; import com.intellij.navigation.GotoRelatedItem; -import com.intellij.psi.DelegatePsiTarget; -import com.intellij.psi.PsiFile; import com.intellij.util.xml.DomElement; import org.jetbrains.annotations.NotNull; @@ -27,32 +25,21 @@ import javax.swing.*; * @author Dmitry Avdeev */ public class DomGotoRelatedItem extends GotoRelatedItem { - private final DomElement myElement; public DomGotoRelatedItem(DomElement element) { - + super(element.getXmlElement()); myElement = element; } - @Override - public void navigate() { - new DelegatePsiTarget(myElement.getXmlElement()).navigate(true); - } - @NotNull @Override - public String getText() { + public String getCustomName() { return myElement.getPresentation().getElementName(); } @Override - public Icon getIcon() { + public Icon getCustomIcon() { return myElement.getPresentation().getIcon(); } - - @Override - public PsiFile getContainingFile() { - return myElement.getXmlElement().getContainingFile(); - } } diff --git a/xml/impl/src/com/intellij/navigation/HtmlGotoRelatedProvider.java b/xml/impl/src/com/intellij/navigation/HtmlGotoRelatedProvider.java index aa7ce419f0fb..5f4d850a5d94 100644 --- a/xml/impl/src/com/intellij/navigation/HtmlGotoRelatedProvider.java +++ b/xml/impl/src/com/intellij/navigation/HtmlGotoRelatedProvider.java @@ -47,7 +47,7 @@ public class HtmlGotoRelatedProvider extends GotoRelatedProvider { HashSet resultSet = new HashSet(); fillRelatedFiles(file, resultSet); - return PsiGotoRelatedItem.createItems(resultSet); + return GotoRelatedItem.createItems(resultSet); } private static boolean isAvailable(@NotNull PsiFile psiFile) {