NavigationGutterIconBuilder
[fedora-idea.git] / xml / dom-openapi / src / com / intellij / codeInsight / navigation / NavigationGutterIconRenderer.java
blobd1c3665eb4b2adaa033537cd9f5591deab64f7de
1 /*
2 * Copyright 2000-2010 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.codeInsight.navigation;
18 import com.intellij.codeInsight.hint.HintUtil;
19 import com.intellij.codeInsight.daemon.GutterIconNavigationHandler;
20 import com.intellij.ide.util.PsiElementListCellRenderer;
21 import com.intellij.openapi.actionSystem.AnAction;
22 import com.intellij.openapi.actionSystem.AnActionEvent;
23 import com.intellij.openapi.editor.markup.GutterIconRenderer;
24 import com.intellij.openapi.ui.popup.JBPopupFactory;
25 import com.intellij.openapi.ui.popup.JBPopup;
26 import com.intellij.openapi.util.NotNullLazyValue;
27 import com.intellij.psi.PsiElement;
28 import com.intellij.psi.SmartPsiElementPointer;
29 import com.intellij.ui.awt.RelativePoint;
30 import com.intellij.util.NullableFunction;
31 import com.intellij.util.PsiNavigateUtil;
32 import com.intellij.util.containers.ContainerUtil;
33 import org.jetbrains.annotations.Nullable;
35 import javax.swing.*;
36 import java.awt.event.MouseEvent;
37 import java.util.List;
39 /**
40 * @author peter
42 public abstract class NavigationGutterIconRenderer extends GutterIconRenderer implements GutterIconNavigationHandler<PsiElement>{
43 private final String myPopupTitle;
44 private final String myEmptyText;
45 private final PsiElementListCellRenderer myCellRenderer;
46 private final NotNullLazyValue<List<SmartPsiElementPointer>> myPointers;
48 protected NavigationGutterIconRenderer(final String popupTitle, final String emptyText, final PsiElementListCellRenderer cellRenderer,
49 final NotNullLazyValue<List<SmartPsiElementPointer>> pointers) {
50 myPopupTitle = popupTitle;
51 myEmptyText = emptyText;
52 myCellRenderer = cellRenderer;
53 myPointers = pointers;
56 public boolean isNavigateAction() {
57 return true;
60 public List<PsiElement> getTargetElements() {
61 return ContainerUtil.mapNotNull(myPointers.getValue(), new NullableFunction<SmartPsiElementPointer, PsiElement>() {
62 public PsiElement fun(final SmartPsiElementPointer smartPsiElementPointer) {
63 return smartPsiElementPointer.getElement();
65 });
68 public boolean equals(final Object o) {
69 if (this == o) return true;
70 if (o == null || getClass() != o.getClass()) return false;
72 final NavigationGutterIconRenderer renderer = (NavigationGutterIconRenderer)o;
74 if (myCellRenderer != null ? !myCellRenderer.equals(renderer.myCellRenderer) : renderer.myCellRenderer != null) return false;
75 if (myEmptyText != null ? !myEmptyText.equals(renderer.myEmptyText) : renderer.myEmptyText != null) return false;
76 if (!myPointers.equals(renderer.myPointers)) return false;
77 if (myPopupTitle != null ? !myPopupTitle.equals(renderer.myPopupTitle) : renderer.myPopupTitle != null) return false;
79 return true;
82 public int hashCode() {
83 int result;
84 result = (myPopupTitle != null ? myPopupTitle.hashCode() : 0);
85 result = 31 * result + (myEmptyText != null ? myEmptyText.hashCode() : 0);
86 result = 31 * result + (myCellRenderer != null ? myCellRenderer.hashCode() : 0);
87 result = 31 * result + myPointers.hashCode();
88 return result;
91 public PsiElementListCellRenderer getCellRenderer() {
92 return myCellRenderer;
95 @Nullable
96 public AnAction getClickAction() {
97 return new AnAction() {
98 public void actionPerformed(AnActionEvent e) {
99 navigate(e == null ? null : (MouseEvent)e.getInputEvent(), null);
104 public void navigate(final MouseEvent event, final PsiElement elt) {
105 final List<PsiElement> list = getTargetElements();
106 if (list.isEmpty()) {
107 if (myEmptyText != null) {
108 final JLabel renderer = HintUtil.createErrorLabel(myEmptyText);
109 final JBPopup popup = JBPopupFactory.getInstance().createComponentPopupBuilder(renderer, renderer).createPopup();
110 if (event != null) {
111 popup.show(new RelativePoint(event));
114 return;
116 if (list.size() == 1) {
117 PsiNavigateUtil.navigate(list.iterator().next());
119 else {
120 final JBPopup popup = NavigationUtil.getPsiElementPopup(list.toArray(new PsiElement[list.size()]), myCellRenderer, myPopupTitle);
121 if (event != null) {
122 popup.show(new RelativePoint(event));