reused standard PsiElement renderer in 'Goto related' popup

This commit is contained in:
nik
2011-03-31 15:03:33 +04:00
parent ed07bd205d
commit fceeab6b6d
7 changed files with 89 additions and 186 deletions
@@ -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<GotoRelatedItem> createItems(@NotNull Collection<? extends NavigatablePsiElement> elements) {
List<GotoRelatedItem> items = new ArrayList<GotoRelatedItem>(elements.size());
for (NavigatablePsiElement element : elements) {
items.add(new GotoRelatedItem(element));
}
return items;
}
}
@@ -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<PsiGotoRelatedItem> createItems(@NotNull Collection<? extends NavigatablePsiElement> elements) {
return createItems(elements, true);
}
public static List<PsiGotoRelatedItem> createItems(@NotNull Collection<? extends NavigatablePsiElement> elements,
final boolean showIcon) {
List<PsiGotoRelatedItem> items = new ArrayList<PsiGotoRelatedItem>(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;
}
}
@@ -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<? extends GotoRelatedItem> items, final String title) {
final JBList list = new JBList(new CollectionListModel(items));
list.setCellRenderer(new ItemCellRenderer());
public static JBPopup createPopup(final List<? extends GotoRelatedItem> items, final String title) {
PsiElement[] elements = new PsiElement[items.size()];
//todo[nik] move presentation logic to GotoRelatedItem class
final Map<PsiElement, GotoRelatedItem> itemsMap = new HashMap<PsiElement, GotoRelatedItem>();
for (int i = 0; i < items.size(); i++) {
GotoRelatedItem item = items.get(i);
elements[i] = item.getElement();
itemsMap.put(item.getElement(), item);
}
Function<Object, String> toString = new Function<Object, String>() {
@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<PsiElement>() {
@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;
}
}
}
@@ -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<? extends GotoRelatedItem> 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);
}
}
@@ -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<PsiFile> forms = FormClassIndex.findFormsBoundToClass(psiClass);
return ContainerUtil.map(forms, new Function<PsiFile, GotoRelatedItem>() {
@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.<GotoRelatedItem>singletonList(new PsiGotoRelatedItem(aClass));
return Collections.singletonList(new GotoRelatedItem(aClass));
}
}
}
@@ -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();
}
}
@@ -47,7 +47,7 @@ public class HtmlGotoRelatedProvider extends GotoRelatedProvider {
HashSet<PsiFile> resultSet = new HashSet<PsiFile>();
fillRelatedFiles(file, resultSet);
return PsiGotoRelatedItem.createItems(resultSet);
return GotoRelatedItem.createItems(resultSet);
}
private static boolean isAvailable(@NotNull PsiFile psiFile) {